|
|
View previous topic :: View next topic |
Author |
Message |
jahan
Joined: 04 Apr 2005 Posts: 63
|
simple BSII code convert ... |
Posted: Sat May 14, 2005 4:19 pm |
|
|
I have this simple code in BSII:
-------------------------------------
synch CON 254
relay VAR Byte
stat VAR Byte
start:
FOR relay = 1 TO 12
stat = 1
SEROUT 0,16780,[synch,relay,stat]
PAUSE 100
stat = 0
SEROUT 0,16780,[synch,relay,stat]
NEXT
GOTO start
--------------------------------------------
can somebody help me convert it to CCS? (C) |
|
|
Guest
|
|
Posted: Sat May 14, 2005 9:31 pm |
|
|
Assuming a crystal speed of 4mhz using a PIC16C57 (just like the BS2 does).
And considering that my BS2 dialect is a little rusty here goes...
Code: |
#include <16c57.H>
#use delay (clock=4000000) //declare the oscillator speed
#use rs232(baud=9600, xmit=PIN_B0, rcv=PIN_B0) //preprocessor rs232
set_tris_b(0b11111110); //set pin RB0 for output, all others as input
#define synch 254
int8 relay,stat;
void main(void){
start:;
for(relay=1;relay<=12;relay++){
stat=1;
printf("%u%u%u",synch,relay,stat);
delay_ms(100);
stat=0;
printf("%u%u%u",synch,relay,stat);
}
goto start;
}
|
|
|
|
Guest
|
|
Posted: Sat May 14, 2005 9:35 pm |
|
|
oops!
You wanted a baud rate of 2400. Change the rs232 preprocessor statement to the following...
Code: |
#use rs232(baud=2400, xmit=PIN_B0, rcv=PIN_B0) //preprocessor rs232
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat May 14, 2005 10:19 pm |
|
|
Use a while instead of a goto. Also note that set_tris instructions need to go inside a function.
Code: |
void main(void){
set_tris_b(0b11111110); //set pin RB0 for output, all others as input
while(1)
{
for(relay=1;relay<=12;relay++){
stat=1;
printf("%u%u%u",synch,relay,stat);
delay_ms(100);
stat=0;
printf("%u%u%u",synch,relay,stat);
}
}
}
|
|
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
Thank you all |
Posted: Sun May 15, 2005 1:17 am |
|
|
it is working beautifully
I've posted another question regarding BSII and SERIN.
would you please take a look and give me suggestions.
Thankx in advance...
here is my other posting:
Hi again,
any suggestion for the following BS2 code converted to CCS (C)?
SERIN PIN1,N2400,[254],R,S
it is reading on RA1 at speed 2400.
waiting for 254 to come in and then read the next 2 values as R and S.
I cannot make this work in C.
Thankx |
|
|
Guest
|
|
Posted: Sun May 15, 2005 10:29 pm |
|
|
To the best of my recollection your serin statement is syntax for the BS1, not the BS2. But lets give it a try as a complementary device to the original transmitting C code.
But first, be warned that (just like Basic Stamp code line) this code will not timeout if the character [254] isn't received. In other words, it will wait forever........
Code: |
#include <16c57.H>
#use delay (clock=4000000) //declare the oscillator speed
#use rs232(baud=2400, xmit=PIN_B0, rcv=PIN_A1) //preprocessor rs232
#define synch 254
int8 R,S; //declare global variables
void SERIN(void){
int8 start_char=0, char_count=0; //declare & init local variables
do{
if(kbhit()){ //don't execute until a start bit occurs
if(start_char==254){ //don't fill vars until a 254 char is received
char_count++;
if(char_count==1) R=getc();
if(char_count==2) S=getc();
}else
start_char=getc();
}
} while(char_count<2);
}
void main(void){
set_tris_b(0b11111110); //set pin RB0 for output, all others as input
set_tris_a(0b11111); //set pin RA1:RA4 for input
do{
SERIN();
// if we get back to this point, 'R' and 'S' are now received char values
}while(1); //repeat the SERIN call...
}
|
|
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
Missing getc()??? |
Posted: Sun May 15, 2005 10:44 pm |
|
|
Hi,
thank you for your input.
I have a quick question.
do you need any getc() after the following codes?
do{
if(kbhit()){ //don't execute until a start bit occurs
// here
Thankx |
|
|
Guest
|
Re: Missing getc()??? |
Posted: Sun May 15, 2005 10:57 pm |
|
|
jahan wrote: | Hi,
thank you for your input.
I have a quick question.
do you need any getc() after the following codes?
do{
if(kbhit()){ //don't execute until a start bit occurs
// here
Thankx |
Not immediately, first you'll need to qualify if you've already received the start byte or not (wait until you do), and once you do, then posture to receive the data bytes. |
|
|
|
|
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
|