+ Reply to Thread
Results 1 to 3 of 3

Thread: rs232 rx tx

  1. #1
    butters Newbie
    Join Date
    May 2009
    Posts
    53

    Default rs232 rx tx

    Code:
    #include <p18f4520.h>
    #include <usart.h>
    #include <delays.h>
    #include <portb.h>
    
    unsigned char c;
    unsigned char c1;
    unsigned char c2;
    unsigned char c3;
    unsigned char c4;
    unsigned char checksum;
    unsigned char m;
    
      unsigned char i;
      unsigned char b;
    
    void rx_handler (void);//declare the ISR function.HIGH
    
    
    
    
    
    #define BUF_SIZE 25
    
    /*
     * Step #1  The data is allocated into its own section.
     */
    #pragma idata bigdata
    char data[8][BUF_SIZE+1] = {
      { "You Typed: 0\n\r" },
      { "You Typed: 1\n\r" },
      { "You Typed: 2\n\r" },
      { "You Typed: 3\n\r" },
      { "You Typed: 4\n\r" },
      { "You Typed: 5\n\r" },
      { "Invalid key (0-5 only)\n\r" }
    };
    
    
    #pragma idata
    
    
    //location of the ISR.(HIGH)
    #pragma code rx_interrupt = 0x8
    void rx_int (void)
    {
      _asm goto rx_handler _endasm
    }
    #pragma code  //return to default code section
    
    
    
    #pragma interrupt rx_handler
    void rx_handler (void)
    {
    
    
    
    while(!DataRdyUSART());
        c  = getcUSART();//start byte
    
    while(!DataRdyUSART());
        c1  = getcUSART();//add
    
    while(!DataRdyUSART());
        c2  = getcUSART();//cmd
    
    while(!DataRdyUSART());
        c3  = getcUSART();//data
    
    while(!DataRdyUSART());
        c4  = getcUSART();//chksum
    
    
    checksum = (c1 + c2 + c3 + c4);
    
    
    if(checksum==0xFF)
    {
    
    //OK
    
    
    (0xFF - (c2 + 0x06 + c3)) == m;
    
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0xCE);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(c3);
    
    while (BusyUSART());
    //value sent 
    	putcUSART(0x06);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(c2);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(m);
    
            //got a valid package so send acknowledge
            //and use the data
        
    }
    
    
    else{
    
    //NOT OK
    
    unsigned char m;
    m = (0xFF - (c3+ 0x21 + c2));
    
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0xCE);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(c3);
    
    while (BusyUSART());
    //value sent 
    	putcUSART(0x21);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(c2);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(m);
    
    
    
    
    }
    
        //Clear the interrupt flag 
        PIR1bits.RCIF = 0;
    }
    
    
    
    
    void main (void)
    {
    ADCON1 = 0x0F;
    TRISCbits.TRISC5 = 0;
    
    //LED
    TRISB = 0;
    
    
    
      OpenUSART (USART_TX_INT_OFF &
                 USART_RX_INT_ON &
                 USART_ASYNCH_MODE &
                 USART_EIGHT_BIT &
                 USART_CONT_RX &
                 USART_BRGH_HIGH, 25);
    
    
    
    
    
    
    
    
    
    PORTBbits.RB3 = 1;
    
    PORTCbits.RC5 = 1;
    Delay10KTCYx(50);
    
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0x31); 
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0x32);
    
    while (BusyUSART());
    //value sent 
    	putcUSART(0x33);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0x34);
    
    while (BusyUSART());
    	//value sent
    	putcUSART(0x35);
    
    
    
    Delay10KTCYx(50);
    
    PORTBbits.RB3 = 0;
    PORTCbits.RC5 = 0;
    
    
    
    
    /* Enable interrupt priority */
      RCONbits.IPEN = 1;
    
      /* Make receive interrupt high priority */
      IPR1bits.RCIP = 1;
    
      /* Enable all high priority interrupts */
      INTCONbits.GIEH = 1;
    
      /* Loop forever */
     while (1)
        ;
    }
    


  2. #2
    jimlovell777 Newbie
    Join Date
    Mar 2009
    Posts
    133

    Default

    Do you have a question or are you sharing your code for others to use?

  3. #3
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,806

    Default

    I'm guessing that the question is the same as in this thread.

    Mike.

+ Reply to Thread

Similar Threads

  1. Rs232
    By mos in forum General Electronics Chat
    Replies: 3
    Latest: 1st October 2009, 10:13 PM
  2. PC to RS232
    By HiJack in forum Micro Controllers
    Replies: 7
    Latest: 29th March 2009, 10:41 AM
  3. rs232 to rs232 data logger with 1meg buffer -help needed
    By ccaammaaccaazzii in forum Electronic Projects Design/Ideas/Reviews
    Replies: 11
    Latest: 20th October 2004, 09:43 PM
  4. rs232
    By jijita in forum Micro Controllers
    Replies: 10
    Latest: 3rd August 2004, 03:29 AM
  5. RS232
    By Spasm in forum Micro Controllers
    Replies: 7
    Latest: 17th January 2004, 03:22 AM

Tags for this Thread