Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 13th October 2009, 04:24 AM   #1
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)
    ;
}
butters is offline  
Old 13th October 2009, 09:08 AM   #2
Default

Do you have a question or are you sharing your code for others to use?
__________________
MoonTaj.com - Lead developer
All around tech enthusiast.
jimlovell777 is offline  
Old 13th October 2009, 09:22 AM   #3
Default

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

Mike.
Pommie is offline  
Reply

Tags
rs232

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Rs232 mos General Electronics Chat 3 1st October 2009 11:13 PM
PC to RS232 HiJack Micro Controllers 7 29th March 2009 11:41 AM
rs232 to rs232 data logger with 1meg buffer -help needed ccaammaaccaazzii Electronic Projects Design/Ideas/Reviews 11 20th October 2004 10:43 PM
rs232 jijita Micro Controllers 10 3rd August 2004 04:29 AM
RS232 Spasm Micro Controllers 7 17th January 2004 04:22 AM



All times are GMT. The time now is 07:32 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker