PDA

View Full Version : SERIN2 = ok, HSERIN = no!



flotulopex
- 20th March 2012, 11:04
Hello,

I need some help to understand and make my two codes work.

I'm reading serial data from a GPS module (http://www.gtop-tech.com/ShowAttach?UNID=9d4f9ae5ff7f497de445d29662dda9deb4 8249f4) connected directly (no level shifter / no MAX232) to my PIC18F2420.

As it works fine in a common serial way using SERIN2 command, the same adapted to EUART code won't work. In the HSERIN version, the program will stuck at the HSERIN command.

I've put both code versions herunder.

What am I doing wrong in code HSERIN?



SERIN2 working


'Config Directive settings for MPASM (fuses) for 18F2420
@ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L & _BORV_0_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTBE_3H
@ __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@ __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H & _DEVID1 & _IDLOC0

'Registers
ADCON1 = %00001111 'Turn off all AD's (= "_PBADEN_OFF_3H")
TRISC = %10000000 'GPS module's TX

'-------------------------------------------------------------------------------
' DEFINEs
DEFINE OSC 8

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting PORT.bit (0 or 4)
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 4 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

'-------------------------------------------------------------------------------
INIT:
GPS_D VAR BYTE(80) 'Incomming data bit array
LED0 VAR PORTA.5 'alive LED
GPSfrom VAR PORTC.7 'serial data FROM GPS module
GPSto VAR PORTC.6 'serial data TO GPS module
GPSbps VAR WORD 'GPS module's baudrate
GPSbps = 32852 'Open,True,N,9600bps (TTL level)

'-------------------------------------------------------------------------------
' DOGM LCD display specific settings
PAUSE 1000 'Time to settle Vdd
LCDOUT $FE, $29 'Function Set: 4 bits bus mode
LCDOUT $FE, $1C 'Bias set
LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52/3,3V=55)
LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
LCDOUT $FE, $78 'Contrast (LowByte)

'-------------------------------------------------------------------------------
' Main loop

LCDOUT $FE,2,"SERIN2"

MAIN:

SERIN2 GPSfrom,GPSbps,[WAIT("$GPGGA,"),STR GPS_D\6]

LCDOUT $FE,2,GPS_D(0),GPS_D(1),":",GPS_D(2),GPS_D(3),":",GPS_D(4),GPS_D(5)

TOGGLE LED0

GOTO MAIN:



HSERIN not working


'Config Directive settings for MPASM (fuses) for 18F2420
@ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H ;_OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L & _BORV_0_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTBE_3H
@ __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@ __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H & _DEVID1 & _IDLOC0

'Registers
ADCON1 = %00001111 'Turn off all AD's (= "_PBADEN_OFF_3H")
TRISC = %11000000 'stated by DS(!)
RCSTA = %10010010 'enable serial port, enable continuous receive, automatic clear overrun error
TXSTA = %00100000 'enable transmit, BRGH=0
SPBRG = 51 '9600 Baud @ 8MHz, 0.16%
SPBRGH = 0
BAUDCON = %01001000

'-------------------------------------------------------------------------------
' DEFINEs
DEFINE OSC 8

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting PORT.bit (0 or 4)
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 4 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

'-------------------------------------------------------------------------------
INIT:
GPS_D VAR BYTE(80) 'Incomming data bit array
LED0 VAR PORTA.5 'alive LED

'-------------------------------------------------------------------------------
' DOGM LCD display specific settings
PAUSE 1000 'Time to settle Vdd
LCDOUT $FE, $29 'Function Set: 4 bits bus mode
LCDOUT $FE, $1C 'Bias set
LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52/3,3V=55)
LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
LCDOUT $FE, $78 'Contrast (LowByte)

'-------------------------------------------------------------------------------
' Main loop
LCDOUT $FE,2,"HSERIN"

MAIN:
HSERIN [WAIT("$GPGGA,"),STR GPS_D\6]

LCDOUT $FE,2,GPS_D(0),GPS_D(1),":",GPS_D(2),GPS_D(3),":",GPS_D(4),GPS_D(5)

TOGGLE LED0

GOTO MAIN:

ScaleRobotics
- 20th March 2012, 13:15
EDIT: Your baud looks good.

At first I didn't see your BRG16 =1, but Steves Picmulticalc is still good. I'll see if I can find anything real :)

Steves PicMultiCalc tool works great for this, and give you text results you can just paste in to your code. Just enter OSC speed, baudrate, and press the go button. :)

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 12 ' 9600 Baud @ 8MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically


http://www.picbasic.co.uk/forum/content.php?r=159-New-PIC-Utility.-PICMultiCalc

flotulopex
- 20th March 2012, 13:25
... I used PICMulticalc and pasted its results in my first post's HSERIN code; unfortunately it doesn't work.

Demon
- 20th March 2012, 14:09
Could it be hardware related? Pull-up or current-limitting resistors?

What about the Mode setting (True VS Inverted)?

Robert


EDIT: You use Open,True,N,9600bps for SERIN, that's inverted.

You use BAUDCON = %01001000 for HSERIN, bit 5, 0 = non-inverted Data/Receive Polarity.


There could be other stuff, but that's all I got.

flotulopex
- 20th March 2012, 14:35
Hi Demon,

I tried all that already. Neither a new chip nor data polarity will change anything; HSERIN won't work...while SERIN2 does :-/

Demon
- 20th March 2012, 15:04
BAUDCON bit 3

BRG16: 16-Bit Baud Rate Register Enable bit
1 = 16-bit Baud Rate Generator – SPBRGH and SPBRG
0 = 8-bit Baud Rate Generator – SPBRG only (Compatible mode), SPBRGH value ignored

Don't you want 8-bit?

Robert

flotulopex
- 20th March 2012, 17:00
I tried BAUDCON = %10000000 if this is what you mean. No luck either.

Sherbrook
- 20th March 2012, 17:19
According to datasheet table 18-1 Baud rate formulas

BRGH = 0 baud rate formula Fosc/[64(n+1)]

BRGH = 1 baud rate formula Fosc/[16(n+1)]

You appear to be using the formula for BRGH = 1 while setting BRGH = 0
I think you should try setting BRGH to 1

Phil

Darrel Taylor
- 20th March 2012, 18:18
I think BAUDCON should be ... %00111000 for inverted levels and BRG16.

flotulopex
- 21st March 2012, 22:33
...still not working.

Just to make sure I'm not wrong with the ports: I use PIC18F2420's PORTC.7 as RX. This means, the TX pin from the GPS module is connected to PORTC.7.

Here's the code again:



'Config Directive settings for MPASM (fuses) for 18F2420
@ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H ;_OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L & _BORV_0_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTBE_3H
@ __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@ __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H & _DEVID1 & _IDLOC0

'Registers
ADCON1 = %00001111 'Turn off all AD's (= "_PBADEN_OFF_3H")
TRISC = %11000000 'stated by DS(!)
RCSTA = %10010010 'enable serial port, enable continuous receive, automatic clear overrun error
TXSTA = %00100000 'enable transmit, BRGH=0
SPBRG = 51 '9600 Baud @ 8MHz, 0.16%
SPBRGH = 0
BAUDCON = %00111000

'-------------------------------------------------------------------------------
' DEFINEs
DEFINE OSC 8

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting PORT.bit (0 or 4)
DEFINE LCD_RSREG PORTC 'LCD register select port
DEFINE LCD_RSBIT 4 'LCD register select bit
DEFINE LCD_EREG PORTC 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

'-------------------------------------------------------------------------------
INIT:
GPS_D VAR BYTE(80) 'Incomming data bit array
LED0 VAR PORTA.5 'alive LED

'-------------------------------------------------------------------------------
' DOGM LCD display specific settings
PAUSE 1000 'Time to settle Vdd
LCDOUT $FE, $29 'Function Set: 4 bits bus mode
LCDOUT $FE, $1C 'Bias set
LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52/3,3V=55)
LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
LCDOUT $FE, $78 'Contrast (LowByte)

'-------------------------------------------------------------------------------
' Main loop

LCDOUT $FE,2,"HSERIN"

MAIN:

HSERIN [WAIT("$GPGGA,"),STR GPS_D\6]

LCDOUT $FE,2,GPS_D(0),GPS_D(1),":",GPS_D(2),GPS_D(3),":",GPS_D(4),GPS_D(5)

TOGGLE LED0

GOTO MAIN:

flotulopex
- 22nd March 2012, 08:12
If I remove the WAIT modifier following the HSERIN command, I will receive data and see it.

But, just if I would have a wrong setting (i.e. wrong baudrate or polarity), I get strange characters on the LDC display.

What's the missing/wrong setting....

flotulopex
- 22nd March 2012, 21:16
Finally I made it work by using a MAX232 between the GPS module and the PIC.

My conclusion is that the use of USART (HSERIN/HSEROUT) will set higher and lower voltage thresholds on the PIC's pins as it does when using "simple" serial communication via SERIN/SEROUT commands. Couldn't find anything about that in the DS...or am I blind?

I go back to SERIN2 and its reduced number of components application ;)


The "working" code is:
'Config Directive settings for MPASM (fuses) for 18F2420
@ __CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H ;_OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L & _BORV_0_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_PORTBE_3H
@ __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@ __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H & _DEVID1 & _IDLOC0

'Registers
ADCON1 = %00001111 'Turn off all AD's (= "_PBADEN_OFF_3H")
TRISC = %11000000 'stated by DS(!)
RCSTA = %10010010 'enable serial port, enable continuous receive, automatic clear overrun error
TXSTA = %00000000 'disable transmit, BRGH=0
BAUDCON = %01101000
SPBRG = 51 '9600 Baud @ 8MHz, 0.16%
SPBRGH = 0

'-------------------------------------------------------------------------------
' DEFINEs
DEFINE OSC 8

DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting PORT.bit (0 or 4)
DEFINE LCD_RSREG PORTC 'LCD register select port
DEFINE LCD_RSBIT 4 'LCD register select bit
DEFINE LCD_EREG PORTC 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 2 'Number lines on LCD

'-------------------------------------------------------------------------------
INIT:
GPS_D VAR BYTE(80) 'Incomming data bit array
LED0 VAR PORTA.5 'alive LED

'-------------------------------------------------------------------------------
' DOGM LCD display specific settings
PAUSE 1000 'Time to settle Vdd
LCDOUT $FE, $29 'Function Set: 4 bits bus mode
LCDOUT $FE, $1C 'Bias set
LCDOUT $FE, $52 'Power control + Contrast (HiByte)(for 5V=$52/3,3V=55)
LCDOUT $FE, $69 'Follower control (5V=$69/3,3V=6D)
LCDOUT $FE, $78 'Contrast (LowByte)

'-------------------------------------------------------------------------------
' Main loop

LCDOUT $FE,2,"HSERIN"

MAIN:

HSERIN [WAIT("$GPGGA,"),STR GPS_D\6]

LCDOUT $FE,2,GPS_D(0),GPS_D(1),":",GPS_D(2),GPS_D(3),":",GPS_D(4),GPS_D(5)

TOGGLE LED0

GOTO MAIN:

ScaleRobotics
- 23rd March 2012, 01:19
Your chip is listed as one of the PIC18F chips that can be inverted by setting the BAUDCON RXDTP and TXCKP bits. So if you want, you can go without the max232.

http://www.picbasic.co.uk/forum/showthread.php?t=10361

Demon
- 23rd March 2012, 01:37
That's the first thing I checked in his latest code. He has bit 5 in BAUDCON set to 1; inverted.

I only got more confused with this thread, more than usual anyways. And to make things worse I'm using a hybrid USART without MAX232 in my current project. Good thing for fortunate souls that share code.

Robert

ScaleRobotics
- 23rd March 2012, 02:04
Well I am a bit confused too. It looks like he is inverting it, and using a max232, and has success with that (If I am reading it right). So, if that is the case, don't invert it, and don't use a max232.

Demon
- 23rd March 2012, 13:24
... So, if that is the case, don't invert it, and don't use a max232.


That's what I was going to post, but thought I'd sound even more weird than usual.

I see where my debugging went wrong; I relied on his comments, not his settings.


GPSbps = 32852 'Open,True,N,9600bps (TTL level)

He said TRUE (non-inverted) but then contradicted with N (inverted) in his comment. It can't be both. I was looking for T or N, to compare against the table in PBP manual.

Using PIC Multicalc:
9600, open, inverted, no parity = 49236
9600, open, true, no parity = 32852

The working SERIN was using non-inverted, that would explain why inverting a MAX232 would work.

Does that make sense?

Robert

mackrackit
- 23rd March 2012, 13:38
The "N" in his comment is for No parity.
As in "8,N,1"

Demon
- 23rd March 2012, 14:07
That's possible, but that only explains why I got confused trying to follow his settings.

The fact remains that he was using non-inverted in SERIN, and then trying inverted in HSERIN at the end, no?

He shouldn't have to use a MAX232 with HSERIN if his GPS worked with SERIN non-inverted. Or is he on to something with HSERIN creating higher voltages than SERIN? (I have no idea about that).

Robert


EDIT: Look at SERIN in the PBP manual, that's where I got N as being inverted. His comment didn't mention a stop bit so I didn't catch on as it meaning 8N1.

Archangel
- 24th March 2012, 09:54
I Think HSERIN only speaks TRUE and likes a nice pull up resistor, just to keep it honest. The max232 would invert it, NO?

Ioannis
- 24th March 2012, 17:37
Some PIC have the option to select true or inverted.

Ioannis

flotulopex
- 25th March 2012, 10:04
Based on the code in post #12, I removed the WAIT modifier to be able to see what happens on each try (= display anything data that comes into the PIC): HSERIN[STR GPS_D\6].


...and likes a nice pull up resistor...
I removed the MAX232, added a 10k pull-up on PORTC.7 (PIC's RX pin) and changed BAUDCON to % 01001000 (not inverted). It works now!!!


I re-modified the code to: HSERIN [WAIT("$GPGGA,"),STR GPS_D\6]

I now can see "HH:MM:SS" as it should.

Nevertheless, I have my PIC running for more than an hour now and it often hangs (= time is no incrementing every second anymore) and starts overs maybe 10 or 20 seconds later. Seems that the data signal is just at the limit the PIC can "read" it properly. This does not happen in my SERIN2 mode.

With the oscilloscope, I measure 4,7VDC for the highest data signal level on PORTC.7 while the PIC and the GPS module are powered by the PICKit2 and its 4,95VDC from USB.

I think I will choose to stay on the SERIN2 version since it looks more "reliable" and, as already said, needs less components.

Darrel Taylor
- 25th March 2012, 22:03
Nevertheless, I have my PIC running for more than an hour now and it often hangs (= time is no incrementing every second anymore) and starts overs maybe 10 or 20 seconds later.
That sounds more like a USART buffer overflow.
If it isn't cleared ... it will hang.

Try adding ...

DEFINE HSER_CLROERR 1 ' clear overflow automatically

Archangel
- 26th March 2012, 08:28
Some PIC have the option to select true or inverted.

IoannisDon't know what I saw, I thought he was working with a 16F628 . . . still I did not know this anyway. Thanks to you and Walter.

flotulopex
- 26th March 2012, 19:28
Yup! Adding "DEFINE HSER_CLROERR 1" has solved the "hanging" problem.

As I previously set the RCSTA.1 = 1, I wrongly thought this would handle the automatic overrun. I should have noticed the "Read-Only" flag on this bit in the datasheet.

BTW, what exactly does "DEFINE HSER_CLROERR" 1 do? Where can I find information about what other DEFINE commands do?

NB: my apologise Demon; I missed your advice for a pull-up in an early post :chargrined:

Final working code is 6366

Archangel
- 27th March 2012, 07:23
Yup! Adding "DEFINE HSER_CLROERR 1" has solved the "hanging" problem.

As I previously set the RCSTA.1 = 1, I wrongly thought this would handle the automatic overrun. I should have noticed the "Read-Only" flag on this bit in the datasheet.

BTW, what exactly does "DEFINE HSER_CLROERR" 1 do? Where can I find information about what other DEFINE commands do?

NB: my apologise Demon; I missed your advice for a pull-up in an early post :chargrined:

Final working code is 6366From the book "As the hardware buffer only has 2 byte input buffer, it can easily overflow if charactors are not read from it often enough. When this happens, the USART stops accepting new characters and needs to be reset. This error can be reset by toggling the CREN bit in the RCSTA register. A DEFINE can be used to automatically clear this error. However you will not know that an error has occured and characters may have been lost."

Demon
- 27th March 2012, 17:33
...However you will not know that an error has occured and characters may have been lost.


Ah, and this must be where a checksum comes it to play?

Robert

flotulopex
- 5th April 2012, 07:41
...Where can I find information about what other DEFINE commands do?
Can someone please tell?

HenrikOlsson
- 5th April 2012, 08:11
Believe it or not but they are actually listed in the manual. I know - who would've thought... ;-)
In my 2.60 book they are in Appendix B and in the PBP3 book they are in section 8.7

And, DEFINE is not a command in that it does not execute at runtime. It's a compiler directive that tells the compiler how and what code to generate. This means that you can not use DEFINE to change a setting (like for example the parity for the USART) at runtime.

/Henrik.

flotulopex
- 7th April 2012, 08:41
Thanks ;-)