Hello.
I have a small code for GPS, which works correctly, however, I've noticed the following:
IF GPS is physically disconnected from the input, or I disable the "serin 2" line, main loop crashes after the first execution and MCU restarts from the beginning. This happens also on other physical boards, so it is not issue of this particular MCU. Other power supplies, other ways of output, like LCD screen instead of serout were tried, as well as hardware serial inputs and outputs - no changes. I also tried same code on 16F1939, 18F45K22 - same result.
Code:
;----[16F886 Hardware Configuration]--------------------------------------------
#CONFIG
cfg1 = _INTRC_OSC_NOCLKOUT ; INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
cfg1&= _WDT_ON ; WDT enabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_OFF ; RE3/MCLR pin function is digital input, MCLR internally tied to VDD
cfg1&= _CP_OFF ; Program memory code protection is disabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOR_OFF ; BOR disabled
cfg1&= _IESO_ON ; Internal/External Switchover mode is enabled
cfg1&= _FCMEN_ON ; Fail-Safe Clock Monitor is enabled
cfg1&= _LVP_OFF ; RB3 pin has digital I/O, HV on MCLR must be used for programming
cfg1&= _DEBUG_OFF ; In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
__CONFIG _CONFIG1, cfg1
cfg2 = _BOR40V ; Brown-out Reset set to 4.0V
cfg2&= _WRT_OFF ; Write protection off
__CONFIG _CONFIG2, cfg2
#ENDCONFIG
TRISA=%00000001 'SET A TO OUTPUT 1=input
TRISC=%00001000 'set C3 for gps
TRISB=%00011000 'set PortB to output
ANSELH=%00000000 ' ADC OFF B
ANSEL=%000000000 'configure PortA as digital except first 2
ADCON1=%10000000 'adc justify
OSCCON=%01110101 'SET FREQUENCY TO 8MHZ
WPUB=%00000000 'turn off Pullups
CM1CON0=0 'DISABLE COMPARATORS
CM2CON0=0 'SAME HERE
'CCP1CON=%01000000 ' configure pwm
'PSTRCON=%00010110 'disable C pwm
DEFINE OSC 8
DEFINE ADC_BITS 12
DEFINE ADC_CLOCK 5
DEFINE ADC_SAMPLEUS 5
gps_data VAR BYTE[80] ' Buffer to store GPS data
gps_char VAR BYTE ' Character received from GPS
time_data VAR BYTE[6] ' Array to store the extracted time
i VAR BYTE ' Loop variable
start_found VAR BIT ' Flag to indicate start of time data
' Define the serial pins
GPS_RX var PORTC.7 ' RX pin for GPS (Adjust according to your setup)
GPS_TX var PORTC.5 ' TX pin for GPS (Adjust according to your setup)
' Initialize variables
start_found = 0
i = 0
serout2 portc.5,84, ["Start ok", 10, 13]
toggle portc.3 'test led
pause 100
mainx:
' Wait for data from GPS
serin2 GPS_RX,85,[gps_char] 'If I remove this line or disconnect the GPS, MCU will crash!
' Store data in buffer
gps_data[i] = gps_char
i = i + 1
' Check if we have a complete line of data
IF gps_char = 10 THEN
' Look for the $GPRMC sentence
IF (gps_data[1] = "G") AND (gps_data[2] = "P") AND (gps_data[3] = "R") AND (gps_data[4] = "M") AND (gps_data[5] = "C") THEN
' Extract time from the sentence
start_found = 1
FOR i = 7 TO 12
time_data[i-7] = gps_data[i]
NEXT i
ENDIF
' Reset buffer
i = 0
ENDIF
' Display time data
IF start_found = 1 THEN
serout2 portc.5,84, ["Time: ", time_data[0], time_data[1], ":", time_data[2], time_data[3], ":", time_data[4], time_data[5], 13, 10]
start_found = 0
ENDIF
GOTO mainx
high portc.3 'check if code comes to here
stop
Any ideas?
Bookmarks