PDA

View Full Version : Comm example: Pic to Pic



Chadhammer
- 23rd June 2006, 02:10
I have been playing around with a pair of Pic 16f870 chips. I have a project that I'm working on with some goals in mind. I thought I'd post this as an example t other newbies like myself and hopefully get some critique/feedback how to make my coding better for the learning experience from you good people here. I just a begginner with Pics really . Have too many other projects going on most of the time.

My project: 2 Pics each with a 2x16 LCD and 2 buttons communicate with each other using Hserin and Hserout. When either chip sends a comm the other chip responds for confirmation. It's a starting point.

My goal is to put my message strings in EEPROM, increase the number of message strings and access them with a nested menu system that uses 1 or 2 buttons to send messages back and forth.

Pic1
<div id="post_message_11692"><div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">Code:</div>
<pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:250px; overflow:auto">
'_______________Setup PIC1__________________________

DEFINE HSER_CLROERR 1

Init:
TRISB = %00000111 'Setup Port B
PORTB = %00000000 'Clear port B pins
Mess Var Byte 'serial incoming message variable
Chkswitch VAR BYTE 'Button input variable

Lcd:
PAUSE 1000 'Pause for LCD intialization
Lcdout $fe, 1, "B1 sends hello"
Lcdout $fe, $c0, "B2 sends goodbye"
pause 300
Start:
pir1.5 = 0 'clear interrupt flags
If PIR1.5 = 1 then Comroute 'interupt for serial comm
Chkswitch=PORTB & %00000011 'setup pins b0 and b1 for button inputs
Select case Chkswitch
case 1
Goto Mess1
case 2
goto Mess2
end select
Goto Start

Comroute:
Hserin [Mess] 'incoming message routine
Select case Mess
case %00000001
Goto Mess5
case %00000010
goto Mess6
case %00000100
goto mess3
case %00000101
goto mess4
end select
goto comroute
Return

'------------Subroutines----------------------------
Mess1:
Lcdout $fe, 1, "Hello message"
LCDOUT $fe, $c0, "sent to Pic2"
Pause 1000
HSEROUT [%00000001]
goto start
return
Mess2:
Lcdout $fe, 1, "Goodbye message"
Lcdout $fe, $c0, "sent to Pic2"
PAUSE 1000
Hserout [%00000010]
goto start
return
Mess3:
LCDOUT $fe, 1, "Pic2 recieved"
LCDOUT $fe, $c0, "hello message"
Pause 2000
goto lcd
return
Mess4:
LCDOUT $fe, 1, "Pic2 recieved"
LCDOUT $fe, $c0, "goodbye message"
Pause 2000
goto lcd
return
Mess5:
LCDOUT $fe, 1, "Pic2 said"
LCDOUT $fe, $c0, "H E L L O !"
pause 1000
LCDout $fe, 1
HSEROUT [%00000100]
return
Mess6:
LCDOUT $fe, 1, "Pic2 said"
LCDOUT $fe, $c0, "G O O D B Y E !"
pause 1000
LCDout $fe, 1
HSEROUT [%00000101]
return
End
</div></pre>
</div>


Pic2
<div id="post_message_11692"><div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">Code:</div>
<pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:250px; overflow:auto">
'_______________Setup PIC2__________________________

DEFINE HSER_CLROERR 1

Init:
TRISB = %00000111 'Setup Port B
PORTB = %00000000 'Clear port B pins
Mess Var Byte
Chkswitch VAR BYTE

LCD:
PAUSE 1000 'Pause for the LCD to wake
LCDOUT $fe, 1 'Clear the LCD display
Lcdout $fe, 1, "B1 sends hello" ' Clear LCD screen
Lcdout $fe, $c0, "B2 sends goodbye"
Pause 300

Start:
pir1.5 = 0 'clearing interrupt flags
If PIR1.5 = 1 then Comroute 'Interupt for serial comm
Chkswitch=PORTB & %00000011 'sets port B0-B1 as inputs
Select case Chkswitch 'button B1 & B2 input routine
case 1
Goto Mess1
case 2
goto Mess2
end select
Goto Start

Comroute:
Hserin [Mess] 'Incoming to Variable "Mess"
Select case Mess
case %00000001
Goto Mess5
case %00000010
goto Mess6
case %00000100
goto mess3
case %00000101
goto mess4
end select
goto comroute
Return

'______________Message routines___________________
Mess1:
Lcdout $fe, 1, "Hello message"
LCDOUT $fe, $c0, "sent to Pic1"
Pause 1000
HSEROUT [%00000001]
goto start
return
Mess2:
Lcdout $fe, 1, "Goodbye message"
Lcdout $fe, $c0, "sent to Pic1"
PAUSE 1000
Hserout [%00000010]
goto start
return
Mess3:
LCDOUT $fe, 1, "Pic1 recieved"
LCDOUT $fe, $c0, "hello message"
Pause 2000
goto lcd
return
Mess4:
LCDOUT $fe, 1, "Pic1 recieved"
LCDOUT $fe, $c0, "goodbye message"
Pause 2000
goto lcd
return
Mess5:
LCDOUT $fe, 1, "Pic1 said"
LCDOUT $fe, $c0, "H E L L O !"
pause 1000
LCDout $fe, 1
HSEROUT [%00000100]
return
Mess6:
LCDOUT $fe, 1, "Pic1 said"
LCDOUT $fe, $c0, "G O O D B Y E !"
pause 1000
LCDout $fe, 1
HSEROUT [%00000101]
return
End
</div></pre>
</div>

I have it bread boarded and working. Any help or ideas to improve my code would be appreciated greatly. I was also wondering If I could use the serial HX/TX with IR instead of wires possibly? I'm using one pair of a cat5 cable now.

jblackann
- 13th November 2006, 19:49
Have you ever received any advice on this? Have you made any improvements to your code? The only recommendations that I would suggest is using some GOSUBs to call things that you use frequently (example same messages, etc.). I am looking to do a similar project but have yet to get started. Did you use an external crystal or resonator? I am just wondering what would be the best method. I plan on experimenting with SEROUT/SERIN. Was also considering HSERIN/HSEROUT. Best of luck on your project if you are still working on it.

anu_kan2
- 14th November 2006, 11:04
hi,
wat was the compiler which u used. iam doing my project in PIC to pic communication.can u guide me up. iam using micro C compiler and i want to write in embedded C .plz reply. i have only one day of deadline.

skimask
- 14th November 2006, 23:18
>>hi all,
Iam doing a project using PIC 16F877A.I like to make PIC to PIC communicate by sending a simple message through SPI and display the message in an LCD or seven segment .I tried a lot doing this.but i cudnt find the correct output.i doing it micro C compiler using embedded C programming.plz do help me i have only one day left for my dead line.it would be more grateful if i get the code and the hardware connection for the same. plzzzzz reply soon.

anu<<

>>hi,
wat was the compiler which u used. iam doing my project in PIC to pic communication.can u guide me up. iam using micro C compiler and i want to write in embedded C .plz reply. i have only one day of deadline.<<

anu_kan...

You had one day before the deadline back on the 8th on the other post.

What's the deal? Is it the end of the semester and you haven't done any of your homework and you're looking for somebody to bail you out or what?

And another thing...even though most of us don't really care one way or the other, this forum is mainly for MELabs PicBasic and PicBasicPro, not embedded C, or any other C. So, needless to say, you'll get more answers in the form of PicBasic.

Is the search function not working again? (it's always worked just fine for me).
Is www.google.com down again? (never been down for more than a few hours last time I checked)

skimask
- 14th November 2006, 23:26
I have been playing around with a pair of Pic 16f870 chips. I have a project that I'm working on with some goals in mind. I thought I'd post this as an example t other newbies like myself and hopefully get some critique/feedback how to make my coding better for the learning experience from you good people here. I just a begginner with Pics really . Have too many other projects going on most of the time.

My project: 2 Pics each with a 2x16 LCD and 2 buttons communicate with each other using Hserin and Hserout. When either chip sends a comm the other chip responds for confirmation. It's a starting point.

My goal is to put my message strings in EEPROM, increase the number of message strings and access them with a nested menu system that uses 1 or 2 buttons to send messages back and forth.

I have it bread boarded and working. Any help or ideas to improve my code would be appreciated greatly. I was also wondering If I could use the serial HX/TX with IR instead of wires possibly? I'm using one pair of a cat5 cable now.


Serial TX/RX direct from the chip instead of serin and serout? Sure, why not. Good call using the cat5 cable, at least it has some shielding going for it. Most 'nubs' would use straight hookup wire and wonder why high baud rates don't work.

If you want to try out some simple IR, check out some of the stuff at www.rentron.com, and do a bit of research on the Sony IR protocol for a method of encoding/decoding your bits and bytes. It works well for me for short range, albiet it's a bit slow, but it works.
While you're at Rentron's site, you may as well check out the cheaper RF modules and figure out a scheme for bidirectional comm's between 2 PIC's (i.e. you'll have to figure out some way for the individual PICs to ignore itself while it's transmitting to the other PIC, such as turning off it's own receiver or something along those lines).

JDG

P.S. You're code looks good enough. If you try to streamline it and make it real pretty looking, it might get hard for you to read later on. At least that's my opinion.