|
|
View previous topic :: View next topic |
Author |
Message |
funmix
Joined: 27 Mar 2007 Posts: 33
|
USART address detect |
Posted: Wed Apr 25, 2007 10:33 am |
|
|
Any comment for the code? I am going to implement 9 bit addressable mode USART.
Code: |
#case
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#define FREQ_OSC 20000000
#define BAUD_RATE 19200
#use delay(clock = FREQ_OSC)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7,ENABLE=PIN_C5,bits=8,parity=N)
#define RS485_ENABLE PIN_C5
#byte SPBRG = 0xFAF
#byte PIR1 = 0xF9E
#byte RCSTA = 0xFAB
#byte TXREG = 0xFAD
#byte RCREG = 0xFAE
#byte TXSTA = 0xFAC
#bit TXIF = PIR1.4
#bit RCIF = PIR1.5
#bit CSRC = TXSTA.7
#bit TX9 = TXSTA.6
#bit TXEN = TXSTA.5
#bit SYNC = TXSTA.4
#bit BRGH = TXSTA.2
#bit TRMT = TXSTA.1
#bit TX9D = TXSTA.0
#bit SPEN = RCSTA.7
#bit RX9 = RCSTA.6
#bit SREN = RCSTA.5
#bit CREN = RCSTA.4
#bit ADDEN = RCSTA.3
#bit FERR = RCSTA.2
#bit OERR = RCSTA.1
#bit RX9D = RCSTA.0
int8 g_packet[40];
#define BUFFER_LEN 32
char recchar = 0;
char txvchar = 0;
#int_rda
void rda_isr()
{
recchar = RCREG;
if(OERR)
{
CREN = 0;
#asm
nop
#endasm
CREN = 1;
}
}
void my_send(char cmd)
{
while(!TXIF);
TXREG = cmd;
}
char my_receive(void)
{
int temp;
int retval;
while(!RCIF);
temp = RCSTA;
retval = RCREG;
if(bit_test(temp,1))
{
CREN = 0;
CREN = 1;
}
return(retval);
}
void init_uart(void)
{
SPBRG = (FREQ_OSC/(BAUD_RATE*16))-1;
TXSTA = 0x66;//0x26
RCSTA = 0xD8;//0x90
}
void SendPacket(void)
{
int16 hdr;
char cs = 0;
char i;
output_high(RS485_ENABLE);
TX9 = 1;
TX9D = 1;
hdr = 0x100 +g_packet[0];
putc(hdr);
TX9 = 0;
TX9D = 0;
for(i=1;i<BUFFER_LEN;i ++)
{
cs+=g_packet[i];
putc(g_packet[i]);
}
putc(cs);
output_low(RS485_ENABLE);
}
void main()
{
char cmd;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
init_uart();
printf(my_send,"Sending out\n\r");
SendPacket()
/* while(1)
{
cmd = my_receive();
my_send(cmd);
}*/
}
|
|
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Thu Apr 26, 2007 2:05 am |
|
|
I have modifed the code..
below is the #int_rda routine, please comment
Code: |
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#define FREQ_OSC 20000000
#define BAUD_RATE 19200
#use delay(clock = FREQ_OSC)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7,ENABLE=PIN_C5,bits=8)
#int_rda
void serial_isr()
{
int t;
char temp;
ADDEN = 1;
temp = RCREG;
if(ninth_bit)
{
if(temp == Node_Address)
{
ADDEN = 0;
receive_data()
}
}
}
|
|
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Thu Apr 26, 2007 6:50 am |
|
|
I think you neeed to start over, you are missing a few things and you have stuff you don't need.
You need a line as shown below for the compiler to setup the uart correctly.
Code: |
#use delay( clock = 20,000,000 )
|
If you are using 9 bits you neet to set the uart correctly.
Code: |
#use rs232 baud=19200,xmit=PIN_C6,rcv=PIN_C7,ENABLE=PIN_C5,bits=9)
|
You are duplicating things that are built in to the compiler.
This is taken care of by the #use rs232 line
Code: |
void init_uart(void)
{
SPBRG = (FREQ_OSC/(BAUD_RATE*16))-1;
TXSTA = 0x66;//0x26
RCSTA = 0xD8;//0x90
}
|
look at the CCS serial examples. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|