PDA

View Full Version : Master to slave 16F767



sachymo
- 30th May 2008, 14:06
Hi
I am trying to communicate between two 16F767 pics. The master PIC operates as an RGB mood lamp and sends a serial signal out to tell where in the program the master PIC is at, once the slave PIC is connected to the master PIC, the slave is supposed to receive the serial data and then continue at the same place wher the matsre is in terms of the RGB moodlamp effect. The code that follows has a glitch. If I take out the serial comms part of the code, the hardware PWM of the program works 100%, if I take out the hardware PWM part of the code, the serial part of the program works 100%, however, when they are put together as shown in the SLAVE's code below, the program does not work 100%. The RGB's leds light up randomly for a minute or so before the actual serial signal is recognised and then the program jumps to the corrrect place.

Device 16F767


CMCON = 7 ' Comparators Off
ADCON0 = 0 ' A/D Modules Off
ADCON1 = 7 ' All Digital Pins


Dim value As Byte

Dim red As Word 'Channel #1
Dim green As Word '#2
Dim blue As Word '#3

TRISC = %10000000
TRISA = 0
TRISB = 0 'CCP3 output



Dim redid As Byte
Dim orangeid As Byte
Dim yellowid As Byte
Dim greenid As Byte
Dim indigoid As Byte
Dim blueid As Byte
Dim violetid As Byte
Dim whiteid As Byte
Dim reddid As Byte

redid = 1
orangeid = 2
yellowid = 3
greenid = 4
indigoid = 5
blueid = 6
violetid = 7
whiteid = 8
reddid = 9

Declare RSIN_PIN PORTC.7
Declare RSIN_MODE 1
Declare RSIN_TIMEOUT 1000

Declare RSOUT_PIN PORTC.6
Declare RSOUT_MODE 1
Declare SERIAL_BAUD 2400
Declare RSOUT_PACE 100

DelayMS 500


blue = 0
green = 0
red = 0
value = 0


loop:
RSIn Wait("SLAVE"), value
If value = 0 Then
GoTo loop
Else
GoSub charin ' Get a character from serial input, if any
EndIf

charin:

If value = 1 Then
GoTo start1
ElseIf value = 2 Then
GoTo orange1
ElseIf value = 3 Then
GoTo yellow1
ElseIf value = 4 Then
GoTo greenn1

ElseIf value = 5 Then

GoTo indigo1
ElseIf value = 6 Then

GoTo bluee1
ElseIf value = 7 Then

GoTo violet1
ElseIf value = 8 Then

GoTo white1
ElseIf value = 9 Then

GoTo redd1
Else GoTo loop
EndIf

start1:
red = red + 1
GoSub LED_PWM
If red > 1000 Then
red = 1000
Else GoTo start1
DelayMS 1
EndIf

orange1:
green = green + 1
GoSub LED_PWM
If green > 1000 Then
green = 1000
red = 1000
Else GoTo orange1
DelayMS 1
End If

yellow1:
red = red - 1
DelayUS 13
GoSub LED_PWM
If red < 250 Then
red = 250
green = 1000
Else GoTo yellow1
DelayMS 1
EndIf

greenn1:
red = red - 1
DelayMS 4
GoSub LED_PWM
If red < 1 Then
red = 0
green = 1000
Else GoTo greenn1
DelayMS 1
EndIf

indigo1:
blue = blue + 1
GoSub LED_PWM
If blue > 1000 Then
blue = 1000
green = 1000
red = 0
Else GoTo indigo1
DelayMS 1
EndIf

bluee1:
green = green - 1
GoSub LED_PWM
If green < 1 Then
green = 0
blue = 1000
red = 0
Else GoTo bluee1
DelayMS 1
EndIf

violet1:
red = red + 1
GoSub LED_PWM
If red > 1000 Then
red = 1000
blue = 1000
green = 0
Else GoTo violet1
DelayMS 1
EndIf

white1:
green = green + 1
GoSub LED_PWM
If green > 1000 Then
green = 1000
red = 1000
blue = 1000
Else GoTo white1
DelayMS 1
EndIf

redd1:
green = green - 1
blue = blue - 1
GoSub LED_PWM
If green < 1 Then
red = 1000
blue = 0
green = 0
Else GoTo redd1
DelayMS 1
EndIf

GoTo orange1


'subroutine to utilise hardware PWM
LED_PWM:

CCP1CON = %00001100 'Mode select = PWM
CCP2CON = %00001100 'Mode select = PWM
CCP3CON = %00001100 'Mode select = PWM

PR2 = $FF

' Set TMR2 up for 1:16 prescale & turn it on
T2CON = %00001110 ' TMR2 ON 1:16 prescale

CCP1CON.4 = red.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = red.1 ' a 10-bit word
CCPR1L = red >> 2

CCP2CON.4 = blue.0
CCP2CON.5 = blue.1
CCPR2L = blue >> 2

CCP3CON.4 = green.0
CCP3CON.5 = green.1
CCPR3L = green >> 2

DelayMS 50
Return

End

Any ideas?

mackrackit
- 30th May 2008, 14:52
A fast read through your logic looks OK. But I may be missing something because I do not use proton. Is that the language? Try the other forum and see if they can help.

sachymo
- 30th May 2008, 16:02
Thanks for your response. I have posted on that forum but no one can help.

mackrackit
- 30th May 2008, 16:55
Here is an idea.

In this block:


loop:
RSIn Wait("SLAVE"), value
If value = 0 Then
GoTo loop
Else
GoSub charin ' Get a character from serial input, if any
EndIf

Turn the hardware PWM off. Turn it on after the correct serial command is received.
Or make all of the LED pins LOW in the above.

mister_e
- 30th May 2008, 17:03
There's a potential stack overflow here



loop:
RSIn Wait("SLAVE"), value
If value = 0 Then
GoTo loop
Else
GoSub charin ' Get a character from serial input, if any
EndIf

charin:

There's no return in your code to handle it, the only one you have do the PWM, so if the value <>0, yes it will perform the Charin procedure... but you never POP the stack, after 2-3 time, yes indeed, your program may behave in a weird way. Try this...



loop:
RSIn Wait("SLAVE"), value
If value = 0 Then GoTo loop

charin:
Also, you want to make sure that your Serial dataline is noise free and idle at the right level if both device are disconnected.

sachymo
- 3rd June 2008, 19:51
Thanks for the tips, I originally had similar code under the routine "loop" which also failed to work, but I'll try again and let you know.

Cheers

skimask
- 3rd June 2008, 20:22
Thanks for the tips, I originally had similar code under the routine "loop" which also failed to work, but I'll try again and let you know.
Cheers

http://www.picbasic.org/forum

Good luck getting any of the follow lines/commands to compile under PBP...

Device 16F767
Dim whatever As something
Declare anything_at_all
RSIn Wait("For it to compile in PBP"), value
X = 'until you get to the right forum'
DelayMS X