serial communication problem with PIC18F25K22


Results 1 to 4 of 4

Threaded View

  1. #3
    Join Date
    Sep 2005
    Location
    France
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: serial communication problem with PIC18F25K22

    HI Bruce,

    OK, I will try to explain more.

    here are simplified shematic and code

    Code:
    '****************************************************************
    '*  Name    : net-master-test.pbp                               *
    '****************************************************************
    
    
    Include "modedefs.bas"
    
    DEFINE OSC 48
            
    '-----------------------------------------------------------------------
    ' configuration USART2 (communication avec FAV)
    RCSTA2 = $90 ' Enable serial port & continuous receive
    TXSTA2 = $20 ' Enable transmit, BRGH = 0
    SPBRG2 = 77  ' 9600 Baud @ 48MHz, 0,16%
    
    ' configuration USART1 (communication RS232 externe)
    RCSTA1 = $90 ' Enable serial port & continuous receive
    TXSTA1 = $20 ' Enable transmit, BRGH = 0
    SPBRG1 = 77  ' 9600 Baud @ 48MHz, 0,16%
    
    ANSELA = 0   ' analog off
    ANSELB = 0   ' analog off
    ANSELC = 0   ' analog off
    
    TRISA = %00000100
    TRISB = %10001000
    TRISC = %10100100
    
    PORTA = 0
    PORTB = 0
    PORTC = 0
    
    led      var PORTA.5
    GP2      VAR BYTE            ' GP variable 
    BytesIn2 VAR BYTE[80]        ' Up to 80 bytes 
    ByteCnt2 VAR BYTE            ' Index pointer 
    EOM      CON 10              ' EOM = "End Of Msg" terminating character 
    OERR2    VAR RCSTA2.1        ' Alias USART over-run bit 
    CREN2    VAR RCSTA2.4        ' Alias USART continuous receive enable bit
    RCIF2    VAR PIR3.5   ' Alias USART received character interrupt flag bit
    RCIF1    VAR PIR1.5 
    GP1      VAR BYTE            ' GP variable 
    BytesIn1 VAR BYTE[80]        ' Up to 80 bytes 
    ByteCnt1 VAR BYTE            ' Index pointer 
    OERR1    VAR RCSTA1.1        ' Alias USART over-run bit 
    CREN1    VAR RCSTA1.4        ' Alias USART continuous receive enable bit
    
    
    depart:        
    clear
    high led
    portc.4 = 1    ' serial to netatt
    PAUSE 1000
    
    ' ask for position
    position:
    portc.4 = 1
    serout2 PORTC.4,32,["?0",10]
    toggle led
    pause 500
    goto position
    Code:
    '****************************************************************
    '*  Name    : netatt-test.pbp                                   *
    '****************************************************************
    
    
    Include "modedefs.bas"
    define OSC 48
    
    ' - Memorisation des données EEPROM à la programmation
    		' à partir de l'addresse 1
            EEPROM  1,[24,41,36,21,29,80]    
            '       1, H, Y, T, E, M, _
            ' à partir de l'adresse 16
            '          17,18,19,20
            EEPROM 16,[87,65,89,88]
            '      16, W ,A ,Y, X  
    '-----------------------------------------------------------------------
    DEFINE ADC_BITS  8     ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 7     ' ADC clock source  (Frc)
    DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
    
    
    ' configuration USART2 portb.6 (communication to net-master) 
    RCSTA2 = $90 ' Enable serial port & continuous receive
    TXSTA2 = $20 ' Enable transmit, BRGH = 0
    SPBRG2 = 38  ' 19200 Baud @ 48MHz, 0,16%
    
    
    ' configuration USART1 (external communication with EM1206 TIBBO)
    RCSTA1 = $90 ' Enable serial port & continuous receive
    TXSTA1 = $20 ' Enable transmit, BRGH = 0
    SPBRG1 = 77 ' 9600 Baud @ 48MHz, 0,16%
    
    ANSELA = 0
    ANSELB = 0
    ANSELC = 16     'analog on portc.4
    
    
    TRISA = %10001111
    TRISB = %10000001
    TRISC = %10010000
    
    PORTA = 0
    PORTB = 0
    PORTC = 0
    
    led      var PORTA.5
    x        var byte
    voie     var byte
    nv1      var byte
    nv2      var byte
    nv3      var byte
    nv4      var byte
    what     var byte
    way      var byte
    vmax     var WORD
    att      var word
    GP2      VAR BYTE            ' GP variable 
    BytesIn2 VAR BYTE[10]        ' Up to 10 bytes 
    ByteCnt2 VAR BYTE            ' Index pointer 
    EOM      CON 10              ' EOM = "End Of Msg" terminating character 
    OERR2    VAR RCSTA2.1        ' Alias USART over-run bit 
    CREN2    VAR RCSTA2.4        ' Alias USART continuous receive enable bit
    RCIF2    VAR PIR3.5   ' Alias USART received character interrupt flag bit
    RCIF1    VAR PIR1.5 
    GP1      VAR BYTE            ' GP variable 
    BytesIn1 VAR BYTE[80]        ' Up to 80 bytes 
    ByteCnt1 VAR BYTE            ' Index pointer 
    OERR1    VAR RCSTA1.1        ' Alias USART over-run bit 
    CREN1    VAR RCSTA1.4        ' Alias USART continuous receive enable bit
    
    pause 10
    clear
    
    
    '********************************
    ' What is my way ? (0-3)
    '********************************
    
    ADCIn 16,way
    
    IF way < 30 then 
    way = 48               'way 0 for 470R
    goto mainloop
    endif
    
    IF way < 60 then 
    way = 49               'way 1 for 2K2
    goto mainloop
    endif
    
    IF way < 90 then 
    way = 50               'way 2 for 3K9
    goto mainloop
    endif
    
    way = 51               'way 3 for 10K
    
    
    '*******************************************
    mainloop:
        IF RCIF2 then      'reception from master PIC ?
        gosub RX2       
        endif
        
        goto mainloop
    
    '********* reception with USART2 (from netMaster PIC) ****
    RX2:
        IF RCIF2 THEN       ' If RCIF2=1 there's a new character in RCREG 
        BytesIn2[ByteCnt2]=RCREG2 ' Yes. Then store character in array 
        IF BytesIn2[ByteCnt2]=10 THEN OutMsg2 ' LF (0A) 
        ByteCnt2=ByteCnt2+1 ' Increment element index pointer 
        ENDIF 
        IF !OERR2 THEN RX2 ' If NOT over-run, then re-cycle 
        CREN2 = 0           ' Yes. Over-run condition is present, so clear it 
        CREN2 = 1           ' Re-enable & clear over-run condition 
        HSEROUT2 ["Over-run reset",13,10]
        return 
    
    OutMsg2: 
    HSEROUT2 [13,10,DEC ByteCnt2," received bytes",13,10]  
    HSEROUT2 [STR BytesIn2\ByteCnt2,13,10] ' Show each character 
    hserout2 ["time:",dec x,10]
    
        x = x + 1
    
    IF BytesIn2[0] = 63 and Bytesin2[1] = way THEN         '?way
        gosub sendata                                          
        gosub reset2
        goto mainloop
    endif
    
    hserout2 ["?"]
    gosub reset2
    goto mainloop
    
    
    reset2:              
    FOR GP2=0 TO ByteCnt2 ' Clear array bytes 0 to ByteCnt 
    BytesIn2[GP2]=0   
    NEXT GP2   
    ByteCnt2=0           ' Reset index pointer back to first element 
    WHILE RCIF2          ' Trash any left over characters in RCREG buffer 
    GP2=RCREG2           ' after EOM is received 
    WEND 
    RETURN 
        
    sendata:
        toggle led
        read 16,nv1      'first letter
        read 17,nv2      'second
        read 18,nv3      '....
        read 19,nv4      '....
        
        if att < 10 then 
        hserout2 ["00",dec att,nv1,nv2,nv3,nv4,10]
        return
        endif
        
        if att < 100 then 
        hserout2 ["0",dec att,nv1,nv2,nv3,nv4,10]
        return
        endif
        
        hserout2 [dec att,nv1,nv2,nv3,nv4,10]  'att value + name
        return

    First PIC (U5) is sending every 500mSec : serout2 PORTC.4,32,["?0",10]
    to my second PIC (U1). That's working fine

    After receiving this, my second PIC must answer:
    -How many bytes it received
    -showing each character
    -How many times it received something
    -A string (always the same in this case)

    Please see below what I see
    Code:
    
    			'taken with PICKIT serial analyser
    
    2 received bytes	'beginning working fine (problem is below)
    ?0
    time:0
    625WAYX
    
    2 received bytes
    ?0
    time:1
    625WAYX
    
    2 received bytes
    ?0
    time:2
    625WAYX
    
    2 received bytes
    ?0
    time:3
    625WAYX
    
    2 received bytes
    ?0
    time:4
    625WAYX
    
    2 received bytes
    ?0
    time:5
    625WAYX
    
    2 received bytes
    ?0
    time:6
    625WAYX
    
    2 received bytes
    ?0
    time:7
    625WAYX
    
    2 received bytes
    ?0
    time:8
    625WAYX
    
    2 received bytes
    ?0
    time:9
    625WAYX
    
    2 received bytes
    ?0
    time:10
    625WAYX
    
    2 received bytes
    ?0
    time:11
    625WAYX
    
    2 received bytes
    ?0
    time:12
    625WAYX
    
    2 received bytes
    ?0
    time:13
    625WAYX
    
    2 received bytes
    ?0
    time:14
    625WAYX
    
    2 received bytes
    ?0
    time:15
    625WAYX
    
    2 received bytes
    ?0
    time:16
    625WAYX
    
    2 received bytes
    ?0
    time:17
    625WAYX
    
    2 received bytes
    ?0
    time:18
    625WAYX
    
    2 received bytes
    ?0
    time:19
    625WAYX
    
    2 received bytes
    ?0
    time:20
    625WAYX
    
    2 received bytes
    ?0
    time:21
    625WAYX
    
    2 received bytes
    ?0
    time:22
    625WAYX
    
    2 received bytes
    ?0
    time:23
    625WAYX
    
    2 received bytes
    ?0
    time:24
    625WAYX
    
    2 received bytes
    ?0
    time:25
    625WAYX
    
    2 received bytes
    ?0
    time:2?			' Time must be 26 but it sends 2? and didn't send 625WAYX
    2 received bytes	' Restarting ? what's wrong ?
    ?0			
    time:0			' It seems that my counter time is restarting... Why ?
    625WAYX
    
    2 received bytes	' After this problem, Now it's working again...
    ?0			' other problem below (time = 28)
    time:1
    625WAYX
    
    2 received bytes
    ?0
    time:2
    625WAYX
    
    2 received bytes
    ?0
    time:3
    625WAYX
    
    2 received bytes
    ?0
    time:4
    625WAYX
    
    2 received bytes
    ?0
    time:5
    625WAYX
    
    2 received bytes
    ?0
    time:6
    625WAYX
    
    2 received bytes
    ?0
    time:7
    625WAYX
    
    2 received bytes
    ?0
    time:8
    625WAYX
    
    2 received bytes
    ?0
    time:9
    625WAYX
    
    2 received bytes
    ?0
    time:10
    625WAYX
    
    2 received bytes
    ?0
    time:11
    625WAYX
    
    2 received bytes
    ?0
    time:12
    625WAYX
    
    2 received bytes
    ?0
    time:13
    625WAYX
    
    2 received bytes
    ?0
    time:14
    625WAYX
    
    2 received bytes
    ?0
    time:15
    625WAYX
    
    2 received bytes
    ?0
    time:16
    625WAYX
    
    2 received bytes
    ?0
    time:17
    625WAYX
    
    2 received bytes
    ?0
    time:18
    625WAYX
    
    2 received bytes
    ?0
    time:19
    625WAYX
    
    2 received bytes
    ?0
    time:20
    625WAYX
    
    2 received bytes
    ?0
    time:21
    625WAYX
    
    2 received bytes
    ?0
    time:22
    625WAYX
    
    2 received bytes
    ?0
    time:23
    625WAYX
    
    2 received bytes
    ?0
    time:24
    625WAYX
    
    2 received bytes
    ?0
    time:25
    625WAYX
    
    2 received bytes
    ?0
    time:26
    625WAYX
    
    2 received bytes
    ?0
    time:27
    5WAYX			'not the good answer !!!
    
    2 received bytes
    ?0
    time:8			'Why my counter = 8 ? It must be 28 ?
    WAYX			'not the good answer !!!
    
     received bytes		'Now there is no answer
    ?
    time:
    			'no more data the pic seems sleeping ?
    Any idea ? I'm working hard on it..
    Attached Images Attached Images

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