Master to slave 16F767


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Posts
    6

    Default Master to slave 16F767

    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?

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    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.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2008
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    Thanks for your response. I have posted on that forum but no one can help.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Here is an idea.

    In this block:
    Code:
    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.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    There's a potential stack overflow here
    Code:
    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...
    Code:
    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.
    Last edited by mister_e; - 30th May 2008 at 17:24.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    May 2008
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sachymo View Post
    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

Similar Threads

  1. Another I2C Slave Routine Problem
    By DanPBP in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2009, 05:50
  2. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  3. PIC master slave howto
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 18th September 2007, 22:35
  4. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59
  5. Replies: 2
    Last Post: - 10th June 2005, 02:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts