View previous topic :: View next topic |
Author |
Message |
EmbdFreak
Joined: 28 Jul 2005 Posts: 23
|
Serial Asynchronous comm. using TXREG with interrupts enable |
Posted: Fri Jul 29, 2005 6:39 am |
|
|
Hi all,
Would somebody help in letting me know whats wrong with the following code. Im trying to use the serial hardware registers to send and receive characters a and b one by one, using usart, asychronous mode. I have enabled the interrupts cuz without interrupts the transmission turned out to have overwrite error. Heres the code:
The correct output should be : abababababa.....
*************************************************
#include <16F877a.h>
#include<string.h>
#include<stdlib.h>
#fuses XT,NOWDT
#use delay (clock=4000000)
#byte txsta=0x98
#byte spbrg = 0x99
#byte txreg= 0x19
#byte rcreg= 0x1A
#byte rcsta= 0x18
#bit txen = 0x98.5
#bit trmt = 0x98.1
#bit cren = 0x18.4
#byte trisc= 0x87
#byte intcon= 0x0B
#bit txif = 0x0C.4
#bit rcif = 0x0C.5
#bit txie = 0x8C.4
#bit rcie = 0x8C.5
char ca='a';
char cb='b';
void init_sfr()
{
intcon= 0xC0; // PEIE, GIE of intcon be set for using intrpts in ser. comm.
spbrg = 51; // 4800 bps
txsta = 0x06 ; // 0b 0000 0110 ; tx not enabled
rcsta = 0x88 ; // 0b 1000 1000; rx not enabled
trisc = 0xC0 ; // setting bit7,6 for direction register of USART
txie =1;
}
void main()
{
init_sfr();
cren=1; // serial RX enable
txen =1;
while(1)
{
while(!txif) // TSR empty->trmt =1
{
txreg = ca;
//delay_cycles(1);
}
while(!txif)
{
txreg = cb;
}
}
} |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Jul 29, 2005 7:02 am |
|
|
I guess that you like assembler. Let the compiler to do his job.
Code: |
include <16F877a.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
while(1)
{
putc('a');
putc('b');
}
}
|
Best wishes,
Humberto |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Fri Jul 29, 2005 12:15 pm |
|
|
Or...
Code: | while(1)
{
while(txif); // TSR empty->trmt =1
txreg = ca;
while(txif); // TSR empty->trmt =1
txreg = cb;
} |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Jul 29, 2005 1:03 pm |
|
|
You don't have an interrupt handler! You enabled interrupts! What do you suppose is going to happen when the interrupt occurs? You probably don't and guess what. The micro doesn't know what to do either. That's what the handler is for. You code is going to jump to program location 8 which is basically just going to keep resetting itself. Take a look at the example files for an example of how to implement serial communications. |
|
|
|