Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

rs232 rx tx

Status
Not open for further replies.

butters

Member
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)
    ;
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top