|
|
View previous topic :: View next topic |
Author |
Message |
apl01
Joined: 18 Dec 2007 Posts: 18
|
12F675 RS232 Help |
Posted: Wed Dec 19, 2007 4:32 pm |
|
|
Hi,
I am trying to connect a master and around 50 slaves using 12F675/12F510s with bidirectional communication. I2C looked like the perfect choice but since these chips are incapable RS232 looks like my next best option. I am very new to comms so I am trying to get basic programs to work like having two push buttons on the master one button switches on a led in the slave and the other button the other led. For some reason when i press either button both leds turn on.
Master code:
#include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
A=0x10;
B=0x20;
Void main(){
While(1){
If(input(Button1))
PUTC(A); //if Button 1 is on send 0x10 to the slave
If(input(Button2))
PUTC(B); // if Button 2 is on send 0x20 to the slave
}
}
Slave code:
#include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
Void main(){
While(1){
If(PUTC(A)=0x10)
Output_high(LED1); //0x10 is recieved turn LED1 on
If(PUTC(A)=0x20)
Output_high(LED2); //0x20 is recieved turn LED2 on
} |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: 12F675 RS232 Help |
Posted: Wed Dec 19, 2007 5:07 pm |
|
|
apl01 wrote: |
Code: |
While(1){
If(PUTC(A)=0x10)
Output_high(LED1); //0x10 is recieved turn LED1 on
If(PUTC(A)=0x20)
Output_high(LED2); //0x20 is recieved turn LED2 on
|
|
One problem is the you are using a single "=" in the "if" statement, where you should be using two ("=="). The other thing is, shouldn't your slave device be using something named "get" instead of "put"?
Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Wed Dec 19, 2007 5:17 pm |
|
|
Sorry, the correct code is:
#include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
A=0x10;
B=0x20;
Void main(){
While(1){
If(input(Button1))
PUTC(A); //if Button 1 is on send 0x10 to the slave
If(input(Button2))
PUTC(B); // if Button 2 is on send 0x20 to the slave
}
}
Slave code:
#include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
Void main(){
While(1){
If(GETC(A)=0x10)
Output_high(LED1); //0x10 is recieved turn LED1 on
If(GETC(A)=0x20)
Output_high(LED2); //0x20 is recieved turn LED2 on
}
I have tried using equal to (==) but still no luck. |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Wed Dec 19, 2007 5:18 pm |
|
|
GETC() not GETC(A) i sorry i cant type |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Dec 19, 2007 7:19 pm |
|
|
When posting source code, please use the 'code' buttons to preserve the layout of your code. This makes for much easier reading.
Quote: | GETC() not GETC(A) i sorry i cant type | A terminating '}' character is missing in the slave as well. This could indicate the code you post here is not the same code you are testing with. How can we be sure there are no errors in the other parts you don't show us? Please post the exact code you are testing with (copy/paste from your editor).
Quote: | I have tried using equal to (==) but still no luck. | With the single '=' your code is wrong, you must use the '==' if you want to compare values. If it is not working then there are probably more errors to be solved.
Note that getc() will wait until a character is received before continuing. In your current code this means that you will have to press Button2 two times before LED2 will be activated. If you want the slave to react on every button press you could change the slave to: Code: | void main()
{
int8 RxChar;
while(1)
{
RxChar = getc();
if (RxChar==0x10)
output_high(LED1); //0x10 is received turn LED1 on
if (RxChar==0x20)
output_high(LED2); //0x20 is received turn LED2 on
}
} |
|
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Wed Dec 19, 2007 7:34 pm |
|
|
Yes i know sorry but my code is not accessible at the moment. Ok what you say about getc() makes sense i will give that a try thanks. |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Thu Dec 20, 2007 4:16 pm |
|
|
I tried the code you suggested below but with still no luck, when using RxChar = 0x10 switching either pushbutton will switch on both LEDs when using RxChar == 0x10 Both LEDs flicker when either button is pressed. Strange.
Slave code:
#include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
void main()
{
int8 RxChar;
while(1)
{
RxChar = getc();
if (RxChar==0x10)
output_high(LED1); //0x10 is received turn LED1 on
if (RxChar==0x20)
output_high(LED2); //0x20 is received turn LED2 on
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 20, 2007 4:43 pm |
|
|
You can split these boards into two separate projects and trouble-shoot
them one at a time, by using a PC.
1. Add a MAX232-type chip to each board. Connect the MAX232 to the
PIC Tx and Rx pins, and to the PC's serial port.
2. Change the commands to visible ASCII characters, such as '1' and '2'.
(Note the single quotes). Edit your code to use these commands.
3. Connect the transmitter PIC board to the PIC. Press your push-
button switches and watch if the proper commands are sent to the PC.
You should see 1 or 2 displayed on the PC's terminal window (HyperTerm).
4. Disconnect the transmitter board. Set it aside. Now connect the
receiver board to the PC. Type commands into the Hyperterminal
window. (ie., type 1 or 2). Watch if the LEDs on the receiver board
turn on/off properly.
5. When both boards work correctly with the PC, you can connect them
together. You can leave the MAX232 chips in place, or you could
remove them at this time, and use a direct connection. Now test
both boards together. If the wiring is correct, they should work. |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Thu Dec 20, 2007 4:49 pm |
|
|
Yes thats a very good idea thanks but i dont have rs232 on my laptop is there a different chip i could use or convertor etc? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Thu Dec 20, 2007 5:35 pm |
|
|
Ok great thanks ill give it a shot. |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Sat Dec 22, 2007 3:06 am |
|
|
I tried connecting up the PIC to the computers rs232 port and have been having difficulties getting it to work.
First off the circuit diagram from the max232 is slightly different to that of the CCS compiler manual pg 276 (caps are back to front) i have gone with the max232 schematic. I have used an old rs232 cable where i have established (i think) Red is Rx, Green is Tx and Black is GND. I have connected this through a rs232 to usb converter.
In the CCS serial port monitor i have 1 stop bit, the software flow control is Both with no hardware flow control items selected.I am not to sure if i need to use DTR and DTS so i have un-selected them. I am trying to send a hex value of 10 from the serial port monitor to the PIC and for the PIC to respond by turning a LED on the code is:
Code: | #include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=57600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
void main()
{
while(1){
if(getc()==0x10)
output_high(LED);
}
}
|
I have no ideas why this won't work? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 22, 2007 3:41 am |
|
|
1. Use a lower baud rate, such as 9600 on both the PIC and the PC.
2. Don't use any flow control, hardware or software.
3. Make sure the connections between the MAX232 and the PC are
correct.
4. Change the command character to one that can easily be typed
from a terminal. Instead of 0x10, change it to '1' (0x31).
Note the single quotes. Those are necessary. Then you can
just type the 1 key and it will send the character. Example:
Code: |
if(RxChar == '1')
{
// Turn on LED.
}
|
5. Put in a line of code at the start of main() to initialize the value
of the LED pin to the "off" state.
6. Post your compiler version. |
|
|
apl01
Joined: 18 Dec 2007 Posts: 18
|
|
Posted: Sat Dec 22, 2007 9:03 pm |
|
|
My compiler version is 4.057
I still cant send data to turn on the LED but i can recieve data from the PIC to the PC with the program below but garbage appears on the termianl rather than Hello World?
Quote: | #include <12F675.h>
#device adc=8
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
void main()
{
printf("Hello World\n\r");
while(1);
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Dec 23, 2007 7:00 pm |
|
|
Just to make sure: how is the connection from PIC to your PC? Have you added a required voltage level converter like the MAX232? |
|
|
|
|
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
|