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 CCS Technical Support

Please optimize my sourcecode about parity bit in soft rs232

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







Please optimize my sourcecode about parity bit in soft rs232
PostPosted: Fri Dec 24, 2004 8:06 am     Reply with quote

This topic is continue from
======================================================================
http://www.ccsinfo.com/forum/viewtopic.php?t=21341
======================================================================
Followed by in software serial is not have parity bit check.
i think and write sourcecode to check about parity bit for my software but it used a lot of time to check data.
please help me optimize my sourcecode. because if parity is error. i will send output low in the guard time
( (1/9600)*2bit = 208 us approximated ). but it use 550 us approximate time it's not work because output.
here is my protocal must be.

1 11111111 1 11
startbit 8bit data parity bit guard time( 2 stop bit)

my think is if i use 9 bit communication and i check number of high in 8 bit data and if it odd i will sent ninth_bit is high
but if it is even, i will sent ninth_bit is low.
if receiver is receive data and check 8 bit data too then jump in loop check ninth_bit. if not correct. receiver will output_low
in pin_b7 in during guard time. if it is correct, noting to do

below is my source code. please optimize my sourcecode
==============================================================================================
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=3579545)

//#use rs232(baud=9600, ERRORS ,parity=E,xmit=PIN_B7,rcv=PIN_B7,bits=8)
#use rs232(baud=9600, parity=N,xmit=PIN_B7,rcv=PIN_B7,bits=9,FORCE_SW,STREAM=scard)

#bit ninth_bit = RS232_ERRORS.7
#bit collision = RS232_ERRORS.6
#bit intf = 11.1
....
........ other sourcecode
.....
......

#int_RB
void checkserial(void)
{
byte data;
int count,evencheck;

if(kbhit())
{
evencheck=0;

data=fgetc(scard);

for(count=0;count<=7;count++)
{
if( bit_test(data,count) ) { evencheck++; }
}

if(ninth_bit)
{
if( (evencheck==0) || (evencheck==2) || (evencheck==4) || (evencheck==8) )
{
// if error output_low on pin_b7 and show " E " on lcd row 2 and col 1

output_low(PIN_B7); delay_us(100); output_high(PIN_B7);
wcharxy(2,1,'E');
}
else
{
wcharxy(2,1,' ');
wcharxy(2,2,'N');
}
}
else
{
if( (evencheck==1) || (evencheck==3) || (evencheck==5) || (evencheck==7) )
{
output_low(PIN_B7); delay_us(100); output_high(PIN_B7);
wcharxy(2,4,'E');
}
else
{
wcharxy(2,1,' ');
wcharxy(2,2,'0');
}
}
hex_to_lcd(1,1,data); // show data on lcd
delay_ms(10);
}
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Dec 24, 2004 10:47 am     Reply with quote

Most time in your routine is taken by accessing the LCD and delay() functions.

you might take a look at the (untested) code below for some ideas. The trick here is that I'm still using the original send #rs232 definition and created a second one with different settings for receiving.
Code:
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=3579545)

// Two streams with the same name, this is possible because one is for
// receive only and the other for transmit only.
#use rs232(STREAM=scard, baud=9600, float_high, xmit=PIN_B0, bits=8, parity=E)
#use rs232(STREAM=scard, baud=9600, float_high, rcv=PIN_B0, bits=9)

#bit ninth_bit = RS232_ERRORS.7

// Calculates the parity value of the given data byte.
// This calculated parity value can be used to compare against a
// received parity bit.
// Even parity means there is an even number of '1's in the character
// data and the parity bit should be even. The number of '1's in the
// character are counted and if the count is even then the parity bit
// must be '0'.
// This routine was copied from the CCS manual.
int find_parity(int data)
{
  int count;
#asm
  movlw 0x8
  movwf count
  movlw 0
loop:
  xorwf data,w
  rrf data,f
  decfsz count,f
  goto loop
  movwf _return_
#endasm
}

void main()
{
  int8 data;
 
  fputc(data, scard);
  data = fgetc(scard);
  if (find_parity(data) == ninth_bit)
  {
    // Odd parity --> error
  }
 
}
a
Guest







PostPosted: Fri Dec 24, 2004 12:24 pm     Reply with quote

thank you again "ckielstra"
i will try your sourcecode in 2 days. because i'm not stay in university. i will back on 26-27/12/47. and i will report result of your sourcecode.
and thank you very much
Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 24, 2004 12:42 pm     Reply with quote

If you want speed, then go to http://www.google.com and
type in: Piclist parity
Then you'll get some links to high speed piclist code.
On Piclist, they have competitions to see who can create
the fastest code for various software topics. So if you
need high-speed (ASM) code, that's the place to go.
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