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

12F675 RS232 Help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
apl01



Joined: 18 Dec 2007
Posts: 18

View user's profile Send private message

12F675 RS232 Help
PostPosted: Wed Dec 19, 2007 4:32 pm     Reply with quote

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

View user's profile Send private message

Re: 12F675 RS232 Help
PostPosted: Wed Dec 19, 2007 5:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 5:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 5:18 pm     Reply with quote

GETC() not GETC(A) i sorry i cant type
ckielstra



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

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 7:19 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 7:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 20, 2007 4:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 20, 2007 4:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 20, 2007 4:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 20, 2007 5:27 pm     Reply with quote

You could get a USB to serial adapter cable. Plug the USB end
into your Laptop, and plug the RS232 end into your board. (with MAX232
added to your board).
Example:
http://www.siig.com/ViewProduct.aspx?pn=JU-CB1S12-S3
apl01



Joined: 18 Dec 2007
Posts: 18

View user's profile Send private message

PostPosted: Thu Dec 20, 2007 5:35 pm     Reply with quote

Ok great thanks ill give it a shot.
apl01



Joined: 18 Dec 2007
Posts: 18

View user's profile Send private message

PostPosted: Sat Dec 22, 2007 3:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Dec 22, 2007 3:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Dec 22, 2007 9:03 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Dec 23, 2007 7:00 pm     Reply with quote

Just to make sure: how is the connection from PIC to your PC? Have you added a required voltage level converter like the MAX232?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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