|
|
View previous topic :: View next topic |
Author |
Message |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
rs232 at baud 2 to 5 |
Posted: Fri Jun 10, 2011 1:23 am |
|
|
Hi,
Wondering if anyone has any code for a software uart running at a baud rate of 2 to 5. It seems the CCS in-built functions only work at 6 and above! I only need transmit. Or is there a work around before I write my own :-( |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Jun 10, 2011 4:21 am |
|
|
I'm surprised the CCS functions allow rates this low. At 2 baud, you are talking 5 seconds to send a single character.
Best way probably, something like (depending on your master clock):
Code: |
#include <16F877A.h>
#device *=16
#device adc=16
#FUSES NOWDT, HS, PUT, NOLVP
#use delay(clock=20000000)
#define TXBUFFSIZE (32)
char RSTXbuffer[TXBUFFSIZE]; //Make large enough for your longest message.....
int8 buffer_in=0,buffer_out=0;
//Buffer handling tests and code
int8 btemp;
#define isempty(buff,in,out,size) (in==out)
#define hasdata(buff,in,out,size) (!(in==out))
#define isfull(buff,in,out,size) (((in+1)&(size-1))==out)
#define tobuff(buff,in,out,size,chr) { buff[in]=chr;\
in=((in+1) & (size-1));\
if (in==out) out=((out+1) & (size-1));\
}
#define frombuff(buff,in,out,size) (btemp=buff[out],\
out=(out+1) & (size-1), \
btemp)
#define clrbuff(buff,in,out,size) {in=0;\
out=0;}
#define TICKS_PER_BAUD (124) //Change as required
#define TXPIN (PIN_A0) //Change as required
void bputc(char chr) {
tobuff(RSTXbuffer,buffer_in,buffer_out,TXBUFFSIZE,chr);
}
//Tick routine - remember if the TICKS_PER_BAUD, goes over 255, to change ctr
//to an int16
#INT_TIMER2
void tick(void) {
static int8 ctr;
static int8 working;
static int8 bitno;
static int1 sending=FALSE;
if (sending) {
//Code to actually send byte
//Wait for the counter to get to zero
if (--ctr == 0) {
//Now send the next bit
bitno++;
if (bitno<8) {
output_bit(TXPIN,working & 1);
working/=2;
ctr=TICKS_PER_BAUD;
}
else {
//Stop bit or finished
if (bitno==8) {
output_high(TXPIN);
ctr=TICKS_PER_BAUD;
}
else {
sending=FALSE; //Finished byte
}
}
}
}
else {
if (hasdata(RSTXbuff,buffer_in,buffer_out,TXBUFFSIZE)) {
//Here not s3ending, and a byte is in the output buffer
working=frombuff(RSTXbuffer,buffer_in,buffer_out,TXBUFFSIZE); //Get character
sending=TRUE;
bitno=0;
ctr=TICKS_PER_BAUD;
output_low(TXPIN);
}
}
}
void main(void) {
setup_timer_2(T2_DIV_BY_16,251,10); //806 us overflow, 8.0 ms interrupt
clrbuff(RSTXbuff,buffer_in,buffer_out,TXBUFFSIZE);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
//Gives 124 calls per bit
//Then to send:
printf(bputc,"Test message\n");
do {
} while (TRUE);
}
|
No guarantees, but it then allows your main code to be doing other things while sending data at this ultra-slow rate....
Best Wishes |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Fri Jun 10, 2011 7:31 am |
|
|
Thanks. That looks a great foundation. Yes slow rate, slow transmission process! I wish I could tell you more!
Keep well,
Will |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Jun 10, 2011 8:32 am |
|
|
Very low rates, are common, but not quite this low!.
However depending on the exact setup, given just how long it'll take to send a message, and hence the difficulty of asking for a repeat if something goes wrong, I'd suggest you look at hamming codes. Adding 50% to the transmitted data length, would allow detection of two bit errors, and correction of any single bit error.
Best Wishes |
|
|
|
|
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
|