serial communication

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Aug 6, 2012.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Code ( (Unknown Language)):
    1. #include <P18F458.h>
    2.  
    3.  
    4. unsigned char RX(void);
    5. void TX(unsigned char x);
    6. void init_comms(void);
    7.  
    8.  
    9. #define BAUD 9600      
    10. #define FOSC 4000000
    11. #define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
    12.  
    13. /* configuration words for pic */
    14. /* fuses of pic */
    15.  
    16. #pragma config PWRT  =  OFF
    17. #pragma config BOR   =  OFF
    18. #pragma config DEBUG =  OFF
    19. #pragma config OSC   =   HS       // 4MHz Crystal, (HS oscillator)
    20. #pragma config WDT   =   OFF   // watch dog timer off
    21. #pragma config LVP   =   OFF   // Low voltage program off
    22.  
    23.        
    24. void main(void)
    25.   {
    26.       unsigned char x;
    27.       TRISD=0;
    28.       PORTD=255;
    29.      
    30. init_comms();
    31.    
    32. while(1)
    33. {
    34.     x=RX();
    35.     if(x=='A')
    36.     PORTD=0;
    37.     else if(x=='B')
    38.     PORTD=10;
    39.    
    40. TX(x);
    41. }
    42.   }
    43.  
    44.  
    45. void init_comms(void)
    46. {
    47. TRISCbits.TRISC6=0;
    48. TRISCbits.TRISC7=1;
    49. TXSTAbits.BRGH=1;     // high speed uart
    50. SPBRG = DIVIDER;
    51. RCSTAbits.CREN=1;     // continous recieve enable bit (asynchronous mode)
    52. RCSTAbits.SPEN=1;     // serial port enable
    53. TXSTAbits.TXEN=1;
    54. }
    55.  
    56. void TX(unsigned char x) //send data to serial port
    57. {
    58. while(PIR1bits.TXIF==0); // wait here till transmit complete
    59. TXREG=x;
    60. }
    61.  
    62. unsigned char RX(void) {
    63.     /* retrieve one byte */
    64.     while(PIR1bits.RCIF==0);    /* set when register is not empty */
    65.     return RCREG;  
    66. }
    67.  
    68.  
    69.  
    70.  

    this is the code for serial communication.in proteous 7.8 when i write A nd B in virtual terminal then port D has '0',&'10' is transfered to PortD.
    i want that when i write A or B then after pressing enter then data should be transfer to PORTD.
    please help me!!!
     
  2. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    "Pressing Enter" causes two characters to be transmitted: 13 and 10. So if you're want to detect the "Enter" you'll need to detect that character sequence.
     
  3. osx-addict

    Member

    Feb 9, 2012
    122
    9
    I believe that's true if you're using Windoze -- but if using any unix box it should only see LF's (10's) whenever the enter key is pressed..
     
  4. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    how to transmit it...............
     
Loading...