PIC - PIC Comm


Closed Thread
Results 1 to 9 of 9

Thread: PIC - PIC Comm

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    When a PIC have a built-in USART use it.. PIC16F877A have it, switch to HSERIN

    SEROUT on one side
    HSERIN on the other side

    Begin with it, then add some sugar around...
    I assume then I use PIN 26, labled RX on the PIC?

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


    Did you find this post helpful? Yes | No

    Default

    yes indeed, and you may need to add some DEFINEs at the top of your code as well.

    Well, by default @4MHz it's already set @2400 baud, so... humm... you may not need any Define at all... it's just good programming
    Steve

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

  3. #3
    Join Date
    Feb 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Cool, theres deff communication because I've got the LCD to display the value of the Dat variable, only problem is, when I press 1 on the keypad I get 10 on the display, and for 2 I get 40 and then it changes to 60 when I press 2 again, so something strange is going on... any ideas?


    Keypad:
    Code:
    INCLUDE "modedefs.bas"          
                 
    ' Declare Variables...
    Col1      VAR PORTA.0
    Col2      VAR PORTA.1
    Col3      VAR PORTA.2
    
    Row1      VAR PORTB.4
    Row2      VAR PORTB.5
    Row3      VAR PORTB.6
    Row4      VAR PORTB.7
    
    LED       var PORTB.1
    SERTX     var PORTB.2
    SERoK     var PORTB.3
    
    Key       var byte
    
    ' Setup Ports
    
    input row1 : input row2 : input row3 : input row4
    output col1 : output col2 : output col3 : output SERTX
    
    ' Setup Var
    
    key  = 20
    
    ' Program Start
    Boot:
        high led
        pause 500
        low led
        pause 100
        high led
        pause 100
        low led
        pause 2000
        high serok
        goto main
    
    Main:
        gosub ScanKeys
        if key <> 20 then gosub transmit
        goto main
    
    ScanKeys:    
        high col1
        pause 50
            if row1 = 1 then Key = 1
            if row2 = 1 then Key = 4
            if row3 = 1 then Key = 7
            if row4 = 1 then Key = 10                    
        low col1
                
        high col2
        pause 50
            if row1 = 1 then Key = 2
            if row2 = 1 then Key = 5
            if row3 = 1 then Key = 8
            if row4 = 1 then Key = 0     
        low col2
            
        high col3
        pause 50
            if row1 = 1 then Key = 3
            if row2 = 1 then Key = 6
            if row3 = 1 then Key = 9
            if row4 = 1 then Key = 11     
        low col3
                    
        return
        
    Transmit:
        gosub flash 
        serout sertx,T2400,[key]
        pause 50
        key = 20
        low led
        return
        
    Flash:
        high led
        pause 50
        low led
        pause 50
        high led
        pause 50
        return
        
    end
    Robot:
    Code:
    INCLUDE "modedefs.bas"
    
    ' Define LCD registers and bits
    Define    LCD_DREG     PORTA
    Define    LCD_DBIT     0
    Define    LCD_RSREG    PORTA
    Define    LCD_RSBIT    4
    Define    LCD_EREG     PORTA
    Define    LCD_EBIT     5
    DEFINE    LCD_LINES    4
    
    ' Define HSER Registers
    ' Set receive register to receiver enabled
    DEFINE HSER_RCSTA 90h 
    ' Set baud rate
    DEFINE HSER_BAUD 2400 
    
    'Define Aliases
    RHF       var          PORTB.0
    RHB       VAR          PORTB.1
    LHF       VAR          PORTB.2
    LHB       VAR          PORTB.3
    LineCom   var          PORTB.4
    LEDS      var          PORTB.5
    SERoK     var          PORTB.7
    SERRX     var          PORTB.6
    
    'Define Vars
    Dat       var          byte
    
    ADCON1 = 7
    
    'Begin program
    Boot:
        dat = 20
        Pause 500       ' Wait for LCD to startup
        gosub display
        low leds
        repeat
            Lcdout $FE, $C0, "!! Insert Ctrl !!"
            Lcdout $FE, $94, "Waiting..."
            Lcdout $FE, $D4, "Dat = ",#Dat
            pause 100
        until serok = 1
        goto modeselect       
        
    ModeSelect:
        Lcdout $FE, $C0, "!! Select Mode !!"
        Lcdout $FE, $94, "Waiting..."
        Lcdout $FE, $D4, "Dat = ",#Dat
        gosub commrx
        if dat <> 20 then
        goto discon
        else
        goto modeselect
        endif
    Discon:
        Lcdout $FE, $94, "Pls Discon..."
        Lcdout $FE, $D4, "Dat = ",#Dat
        if serok = 0 then GoMode
        goto discon
        
    CommRX:
        HSERIN [Dat]
        return
    
    GoMode:
        if dat <> 20 then
        select case dat
            case 20
                goto boot
            case 2
                goto linefollow
        end select
        endif
    goto boot
          
    Display:   
       Lcdout $fe, 1   ' Clear LCD screen
       Lcdout "PIC-Bot... v1.0"
       Lcdout $FE, $94, "Pls Wait..."
       Pause  1000
       return
       
    'Check for controler
    ChkCtrl:
    if serok = 1 then modeselect
    return
            
    'Line Follow loop    
    LineFollow:
        Lcdout $FE, $C0, "~~ LiNe FoLlOw ~~"
        Lcdout $FE, $94, "...Scan Line..."
        low RHF
        low LHF
        PWM LHF,60,18
        pause 100
        if linecom = 0 then gosub moveleft
        low RHF
        low LHF
        PWM RHF,60,18
        pause 100
        if linecom = 0 then gosub moveright
        gosub chkctrl
        goto linefollow
        
    MoveLeft:
        Lcdout $FE, $94, "Correct Left"
        repeat
            low RHF
            low LHF
            PWM RHF,60,20
            PWM LHb,60,20
            pause 100
        until linecom = 1
        pause 50
        low RHF
        low LHF
        PWM RHF,60,18
        return
        
    MoveRight:
        Lcdout $FE, $94, "Correct Right"
        repeat
            low RHF
            low LHF
            PWM LHF,60,20
            PWM rHb,60,20
            pause 100
        until linecom = 1
        pause 50
        low RHF
        low LHF
        PWM LHF,60,18
        return
        
    end

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


    Did you find this post helpful? Yes | No

    Default

    There's probably some timing issue... no I haven't got to far in your code. But your first one had this preamble stuff which I suggest you to add in your current one...

    serout sertx,T2400,["plahplahplah",55,key]

    HSERIN [Wait(55), key]
    Steve

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

Similar Threads

  1. Pic to pic interrupt ans serial comm
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th May 2008, 22:43
  2. High Speed Serial Comm PC - PIC and PIC - PIC
    By manumenzella in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 16th January 2007, 21:55
  3. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  4. Comm example: Pic to Pic
    By Chadhammer in forum Code Examples
    Replies: 4
    Last Post: - 14th November 2006, 23:26
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

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