UART/CODE problem Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi C,
That image shows a 5/Sec rate with a transmission period of ~83mSec, which assuming a GPS message length of 80 characters, indicates a 9600 Baud rate.
Hi E,
The GPS sends a sentence 72 BYTES long, but only 45 BYTES are needed, and I think at 45 the PIC stops reading and starts PARSING.
C
 

ericgibbs

Joined Jan 29, 2010
21,442
Hi C,
Simply put, as the clip states,
The internal MCU peripherals, when performing their internal hardware functions, can override any Pin setting that you have made in your software.

Why are you using the TX pin as an SPI acknowledgment track between the 2x PICs, ?


E
 

ericgibbs

Joined Jan 29, 2010
21,442
The GPS sends a sentence 72 BYTES long, but only 45 BYTES are needed, and I think at 45 the PIC stops reading and starts PARSING.
Hi,
So how are you clearing and resetting the UART RXD buffers and over run errors?

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi C,
Simply put, as the clip states,
The internal MCU peripherals, when performing their internal hardware functions, can override any Pin setting that you have made in your software.

Why are you using the TX pin as an SPI acknowledgment track between the 2x PICs, ?


E
Hi E,
That's my mistake, it should read the TX pin is used as an SS/CS pin for the 4431 SLAVE.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi,
So how are you clearing and resetting the UART RXD buffers and over run errors?

E
Hi E,
Here is the INTERRUPT CODE. It's full of suggestions, made earlier, with its predecessor commented out until proved (I doubt you'll like it):)
C
Code:
On High Interrupt
Save System

Dim temp As Byte  'For crearing Over run errors ¦

If PIE1.RCIE = 1 Then
    ''overrun error
    'If RCSTA.OERR = 1 Then
        'RCSTA.CREN = 0  'disable UART
        'RXIRQchar = RCREG  '1    'clear UART RX registers
        'RXIRQchar = RCREG  '2
        'RCSTA.CREN = 1  'reenable UART
        'Gosub IRQinitBuf  're-init buffer, discard bad sentence
    'Goto RXIRQdone  'done, wait for next character
    'Endif  'OERR
'+++++++++++++++++++++++++++++++++++++++++++++
'If(OERR)
'{
'    do
'    {
'        temp = RCREG;
'        temp = RCREG;
'        temp = RCREG;
'CREN = 0;
'        CREN = 1;
'    } While(OERR);
'}

    If RCSTA.OERR = 1 Then
        'temp = RCREG  'TEMP = DUMP ¦
        'temp = RCREG
        'temp = RCREG
        Gosub IRQinitBuf  'init buffer, index and flags¦
        RCSTA.CREN = 1
      
        While RCSTA.OERR
        Wend
    Endif


'+++++++++++++++++++++++++++++++++++++++++++++

''framing error
'If RCSTA.FERR = 1 Then
    'RXIRQchar = RCREG  'Read char to clear RCIF and FERR
    'Gosub IRQinitBuf  'Re-init buffer, discard bad sentence
    'Goto RXIRQdone  'wait for next
'Endif  'FERR

'+++++++++++++++++++++++++++++++++++++++
'If(FERR)
'{
'    temp = RCREG;
'    TXEN = 0;
'    TXEN = 1;
'}
    If RCSTA.FERR = 1 Then
        temp = RCREG
        TXSTA.TXEN = 0
        TXSTA.TXEN = 1
    Endif
'+++++++++++++++++++++++++++++++++++++++

'No UART errors, process character
If PIR1.RCIF = 1 Then
    RXIRQchar = RCREG  'read the received char, clears RCIF

    'Look for $, start/restart filling buf when found
    If RXIRQchar = "$" Then  'Start (re)filling buf on any $
        Gosub IRQinitBuf  'init buffer, index and flags
        gps(rxpsn) = "$"  'RXIRQchar  'store $ $ ADDED instead of RXIRQchar, because no $ was being stored
        buf_fill = 1  'start storing the sentence
        Goto RXIRQdone  'done with this character
    Endif  'char was $

    'no UART errors and character was not $
    'If $ was found previously, process character looking for W and no buffer overflow.
    'If haven't found $ yet, just ignore character.

        If buf_fill = 1 Then  'if filling buffer, see if there is room in buf
            If rxpsn >= (rxbufsize - 1) Then  'last char was at end of buf - buffer overflow so..
                Gosub IRQinitBuf  'restart buffer, discard sentence
                RXerr = 1  'let main know that the buffer overflowed and is restarting
                Goto RXIRQdone  'done, resume looking for next $
            Endif  'buffer overflow

            rxpsn = rxpsn + 1  'else, there's room in buf so bump index and store character, might be W
            's2m(rxpsn) = RXIRQchar
            gps(rxpsn) = RXIRQchar  '<<<<<<<<<<<¦
            If RXIRQchar = "W" Then  'if end of sentence..
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi C,
When you use HSEROPEN, compile as usual, then look at the ASM file that the compiler created.
It shows you what the HSEROPEN created.
E
Example:


; 4: Hseropen 9600
; exact baud rate achieved = 9603.84153; bit period = 104.125µs; baud rate error = 0.04%
BSF TRISC,6
BSF TRISC,7
MOVLW 0x40
MOVWF SPBRG
MOVLW 0x03
MOVWF SPBRGH
CLRF BAUDCON
BSF BAUDCON,BRG16
MOVLW 0x24
MOVWF TXSTA
MOVLW 0x90
MOVWF RCSTA
; 5:


Actual Basic test Code. I used,
'DEBUG 2
Hseropen 9600

Loop:
nop
Goto Loop
Hi E,
When I tried this, I noticed that there is no CONFIG [ Register: CONFIG1H (300001h) ]
How does the CONFIG set-up affect the OSC, XTL or BAUD rate etc ?
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi C,
I can see that ISR will cause you misery.
Who wrote that Code?

E
Hi E,
It has been written over the years, and it has been pointed out to me that an ISR should be as short as possible.
I think if I get the nerve to change it, I'll try 'looking for the $ (Sentence start)' then jump to the MAIN LOOP for the routine, if that's possible.
C
 

Ian Rogers

Joined Dec 12, 2012
1,136
Lets go back a tad...
You mentioned that the pic18f46xx was working tickey boo with the pic18f4431 up until recently.

What was the turning point. If Valds compiler really does achieve 0.04% then it should work perfectly.
I'm starting to smell something else.. Reason!! because I have run a 20Mhz for ages at 9600 baud.
I think ( just for a test ) try your current system at 2400 baud and setting GPS to 9600. If what seems to be happening is the baud is being quadrupled then it should work a lot better
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi,
Ok,
So Add that CONFIG to that Basic program I posted, compile it and repost the ASM code for the HSEROPEN 9600

E
Hi E,
Here's the ASM after CONFIGS:
It seems the BAUD rate is good at 9600 [ no need for the £10 - 7.2728mHz XTLs that have just arrived ]
C
Code:
; Compiled with: PIC18 Simulator IDE v4.42
; Microcontroller model: PIC18F4431
; Clock frequency: 32.0MHz
;
    ORG 0x000000
    BSF RCON,IPEN
    GOTO L0003
    ORG 0x000008
    RETFIE
    ORG 0x000018
    RETFIE
; User code start
L0003:
; 1: Define CONFIG1L = 0x00
;       The value of 'CONFIG1L' is 0
; 2: Define CONFIG1H = 0x06  '8mHz x4 =32
;       The value of 'CONFIG1H' is 6
; 3: Define CONFIG2L = 0x0c
;       The value of 'CONFIG2L' is 12
; 4: Define CONFIG2H = 0x20
;       The value of 'CONFIG2H' is 32
; 5: Define CONFIG3L = 0x04
;       The value of 'CONFIG3L' is 4
; 6: Define CONFIG3H = 0x80
;       The value of 'CONFIG3H' is 128
; 7: Define CONFIG4L = 0x80  'Set for HVP
;       The value of 'CONFIG4L' is 128
; 8: Define CONFIG4H = 0x00
;       The value of 'CONFIG4H' is 0
; 9: Define CONFIG5L = 0x0f
;       The value of 'CONFIG5L' is 15
; 10: Define CONFIG5H = 0xc0
;       The value of 'CONFIG5H' is 192
; 11: Define CONFIG6L = 0x0f
;       The value of 'CONFIG6L' is 15
; 12: Define CONFIG6H = 0xe0
;       The value of 'CONFIG6H' is 224
; 13: Define CONFIG7L = 0x0f
;       The value of 'CONFIG7L' is 15
; 14: Define CONFIG7H = 0x40
;       The value of 'CONFIG7H' is 64
; 15:
; 16: 'OSH config
; 17: Define CLOCK_FREQUENCY = 32
;       The value of 'CLOCK_FREQUENCY' is 32
; 18: Define SINGLE_DECIMAL_PLACES = 2
;       The value of 'SINGLE_DECIMAL_PLACES' is 2
; 19: Define STRING_MAX_LENGTH = 20
;       The value of 'STRING_MAX_LENGTH' is 20
; 20: Define SEROUT_DELAYUS = 1000
;       The value of 'SEROUT_DELAYUS' is 1000
; 21:
; 22:
; 23:
; 24: 'actual basic test code.i used,
; 25: 'DEBUG 2
; 26: Hseropen 9600
; exact baud rate achieved = 9603.84153; bit period = 104.125µs; baud rate error = 0.04%
    BSF TRISC,6
    BSF TRISC,7
    MOVLW 0x40
    MOVWF SPBRG
    MOVLW 0x03
    MOVWF SPBRGH
    CLRF BAUDCON
    BSF BAUDCON,BRG16
    MOVLW 0x24
    MOVWF TXSTA
    MOVLW 0x90
    MOVWF RCSTA
; 27:
; 28: Loop:
L0001:
; 29: nop
; 30: Goto Loop
    BRA L0001
; Library code
L0004    BRA L0004
L0002:
    RETURN
; End of user code
L0005    BRA L0005
;
;
;
;
;
;
; Configuration settings
    ORG 0x300000
    DW 0x0600
    DW 0x200C
    DW 0x8004
    DW 0x0080
    DW 0xC00F
    DW 0xE00F
    DW 0x400F
; End of listing
    END
 

ericgibbs

Joined Jan 29, 2010
21,442
Hi,
That is not the basic program and ASM result that I asked you to post

It was this with the CONFIG1H settings in the test program
; 4: Hseropen 9600
; exact baud rate achieved = 9603.84153; bit period = 104.125µs; baud rate error = 0.04%
BSF TRISC,6
BSF TRISC,7
MOVLW 0x40
MOVWF SPBRG
MOVLW 0x03
MOVWF SPBRGH
CLRF BAUDCON
BSF BAUDCON,BRG16
MOVLW 0x24
MOVWF TXSTA
MOVLW 0x90
MOVWF RCSTA
; 5:
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Lets go back a tad...
You mentioned that the pic18f46xx was working tickey boo with the pic18f4431 up until recently.

What was the turning point. If Valds compiler really does achieve 0.04% then it should work perfectly.
I'm starting to smell something else.. Reason!! because I have run a 20Mhz for ages at 9600 baud.
I think ( just for a test ) try your current system at 2400 baud and setting GPS to 9600. If what seems to be happening is the baud is being quadrupled then it should work a lot better
Hi I,
I've set the GPS to 9600.

Do you want me to use this? Hseropen 2400 [ does a basic config of TX and RX including BAUDCON, SPEN, CREN, TXEN etc. ]
Note: it sets the TX as well, and as mentioned previously the TX pin is SS/CS for the SLAVE.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,830
Hi I,
I tried the:
'Hseropen 9600 'does a basic config of TX and RX including BAUDCON, SPEN, CREN, TXEN etc.
Note what I said in #58.
This fired once, and the GPS is gobbledegook.

Then I tried my mates method:
'setup USART for 9600 baud receive
RCSTA = %10010000
TXSTA.BRGH = 1
BAUDCON.BRG16 = 1
SPBRG = 207
PIR1.RCIF = 0
PIE1.RCIE = 1
PIE1.CCP1IE = 1
INTCON.PEIE = 1
INTCON.GIE = 1

This kept firing, so good! but I these settings are for 9600?
If you know the settings for 2400, I can try that.

C
 
Top