CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Serial Asynchronous comm. using TXREG with interrupts enable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
EmbdFreak



Joined: 28 Jul 2005
Posts: 23

View user's profile Send private message

Serial Asynchronous comm. using TXREG with interrupts enable
PostPosted: Fri Jul 29, 2005 6:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 29, 2005 7:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 29, 2005 12:15 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 29, 2005 1:03 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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