PDA

View Full Version : Serial Communications initialising PIC USART



Bevo
- 6th September 2005, 15:54
G'Day all,
I'm looking for working C++ code to setup the USART in a 16f877 PIC in async to send and receive data from a PC via RS232 (using Matlab). At the moment I'm having no luck in sending or receiving anything, the circuit setup is fine as I've had it checked. I just want to send an echo from the computer to make sure that the communication link is fine before moving. The USART setup and an extract from the code is shown below...any help or even a nudge in the right direction would be appreciated:

void initUSART (void)
{SPBRG=0x19; //set baud rate to 2400
RCSTA=0x90;
TXSTA=0x24;}

void interruptFlag(void)
{ GIE=0; //disable global interrupts
if(RCIF)
{
flag=1;
TXREG=RCREG;
flag=0;}
}
void main(void)
{
init_portB(); //the port setup is in my original code
initUSART();

while(1)
{
{if(flag>0)
flag=0;
PORTB=RCREG;
}}}

mister_e
- 6th September 2005, 17:32
be sure you'd set TRISC and you have the right SPBRG for the right OSC. And you interrupt handling looks weird... and... and.. and.. sorry ;)

BTW, unfortunately you didn't choose the right forum. Melabs PICBasic here.