PDA

View Full Version : GPS Project: PIC18F26K22 or PIC18F46K22



astanapane
- 24th June 2018, 10:07
Dear all:D, i open a separate thread from this one http://www.picbasic.co.uk/forum/showthread.php?t=19080&page=2 as far as i made a mistake continuing in there. I need to apologize for that.

I have opened it under PBP3, because a friend has a PBP3 in his laptop, i still have an older version but i'm planning to purchase the PBP3 soon.

If an admin would like to move all the post from #66 and then, please do so in here. thanks a lot.

Now as long as the PIC16F88, is limited to what i need to do for the project i have placed an order for the following PICs.

1. PIC18F26K22
2. PIC18F46K22

I know that it will be difficult to understand the fuses but i would like to read and learn if you also help me. As i have told in the past im not a programmer so the code i have done up to now with your help it may reached my limits. I keep reading on the internet useful info and datasheets.

So lets start from the beginning:

I need to write a code that is getting the NMEA information from the GPS and display them at an LCD. For the LCD i prefer in the beginning to have the uLCD 1'44 from 4D Systems as fas as it doesnt need any driver. At the end of the project i would like to use the .inc files in order to use a Nokia 3310 or any other cheaper display.

I have in mind to start from the scratch, and with your advice based on what i need to do, to write the code in an appropriate format. Please be patience as i need time to understand things. And i need to understand what am i doing on the code. It will be useful for a newbie like me in the future to read the thread and really understand. Because for you all, that are experienced and you are in a good level of programming, you do not have either time to explain what you are doing and why you are doing at a difficult parts of code.

So now i will start the project from the very basic, like why do we pick this particular PIC, why we select these fuses.....

I place myself under a newbie and for the rest like me, i will start with a very basic info and code. We will continue that in order to reach a good level and to complete an average project.

At the end it will be like a training with examples but real life examples with explanations and detailed info for each step.

If there is anyone would like to help here please do so.

astanapane
- 24th June 2018, 10:32
So simply i start, with what the first target is, for the code.

1. We need to get NMEA information from any GPS module that Outputs: NMEA 0183, 9600 baud default, and display them on:

a. A display that accept directly commands without any driver. (i have for testing the uLCD 1'44 from 4D systems with SGC ios for serial interface)
b. A display that accept commands but we need to include the driver files. (a Nokia 3310 for example)

Those files i believe are already completed by the experienced users in the forum.
So they could help us and explain to us how to use them if they have the time and patience.

The PIC that i will use for all way to the end, even for the very basic code in the beginning is the PIC18F26K22.

astanapane
- 24th June 2018, 11:06
First Question is: How do we select the PIC, and why did we select this particular one (PIC18F26K22 (https://www.microchip.com/wwwproducts/en/PIC18F26K22)) for a number of examples in this project.

The Datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/40001412G.pdf) is very important to have it as a reference all the time.

I will answer as a newbie and hopefully the experienced programmers and users will help to correct me or add useful additional information.

In most of the new basic projects, we need to add components in our circuit that require more ports to be in use, specific features, High CPU SPEED and SPACE in RAM....... For example, in this final project, we will use a variety of components like:

1. Display
2. GPS Module, (GSM 2G-SIM module, this will be added at the end)
3. SD card

Each of the components required to have a dedicated port on the PIC and a specific protocol to communicate with.

For example we need to identifyfor each component, how it works and what communication protocol needs to be connected with the PIC!!!

A Display and the SD that has the SPI or I2C option to communicate , needs to be connected with a PIC that features these peripherals.

astanapane
- 1st July 2018, 12:52
Second Question is: How to do we set the configuration fuses for the PIC, especially for the PIC18F26K22?

Right now the first step is to Flash an LED at port RC3 ( pin14 ) of the PIC18F26K22.

The following code is written in order to Flash the LED on the specific port.

For me at the moment is quit difficult to explain the configuration fuses for all the setups, mostly important to the ones setting the frequency at high external speed HS of 64Mhz.

From the manual it says that we need to enable the PLL bit and also specify that we are going to use the external OSC.

I might need the help of the ones are most expirienced to help me out regarding the setting for 64 MHZ with an external crystal 16Mhz.


@ ERRORLEVEL -306 ; this command prevents the compiler to give you a notice of *
; crossing page boundary - make sure bits are set *
'************************************************* ********************************

#CONFIG ; The PBP default configuration for the PIC18F26K22 is:
; CONFIG FOSC = RCIO6 ; External RC oscillator
CONFIG FOSC = HSHP ; HS oscillator (high power > 16 MHz)
; CONFIG FOSC = INTIO67 ; Internal oscillator block
; CONFIG PLLCFG = ON ; Oscillator used directly
CONFIG PRICLKEN = ON ; Primary clock can be disabled by software
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
CONFIG IESO = OFF ; Oscillator Switchover mode disabled
CONFIG PWRTEN = OFF ; Power up timer disabled
CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
; CONFIG BORV = 190 ; VBOR set to 1.90 V nominal

;*--------------------------------------------------------------------------------------------------------;|
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect ;|
CONFIG WDTPS = 32768 ; 1:32768 ---> HERE enable the watchdog timer with a 1:32768 postscale ;|
;*--------------------------------------------------------------------------------------------------------;|

; CONFIG CCP2MX = PORTC1 ; CCP2 input/output is multiplexed with RC1
; CONFIG PBADEN = OFF ; PORTB<5:0> pins are configured as digital I/O on Reset
; CONFIG CCP3MX = PORTB5 ; P3A/CCP3 input/output is multiplexed with RB5
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
; CONFIG T3CMX = PORTC0 ; T3CKI is on RC0
; CONFIG P2BMX = PORTB5 ; P2B is on RB5
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
; CONFIG STVREN = ON ; Stack full/underflow will cause Reset
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Disabled

;*----------------------------------------------------------------------------------|
;*---------------Available configuration settings for PIC18F26K22-------------------|
;*----------------------------------------------------------------------------------|
;
; Oscillator Selection bits
; CONFIG FOSC = RC ;111X External RC oscillator, CLKOUT function on RA6
; CONFIG FOSC = ECLPIO6 ;EC oscillator (low power, &lt;500 kHz)
; CONFIG FOSC = ECLP ;EC oscillator, CLKOUT function on OSC2 (low power, &lt;500 kHz)
; CONFIG FOSC = ECMPIO6 ;EC oscillator (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = ECMP ;EC oscillator, CLKOUT function on OSC2 (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = INTIO7 ;Internal oscillator block, CLKOUT function on OSC2
; CONFIG FOSC = INTIO67 ;Internal oscillator block
; CONFIG FOSC = RCIO6 ;External RC oscillator
; CONFIG FOSC = RC ;External RC oscillator, CLKOUT function on OSC2
; CONFIG FOSC = ECHPIO6 ;EC oscillator (high power, &gt;16 MHz)
; CONFIG FOSC = ECHP ;EC oscillator, CLKOUT function on OSC2 (high power, &gt;16 MHz)
; CONFIG FOSC = HSMP ;HS oscillator (medium power 4-16 MHz)
; CONFIG FOSC = HSHP ;HS oscillator (high power &gt; 16 MHz)
; CONFIG FOSC = XT ;XT oscillator
; CONFIG FOSC = LP ;LP oscillator
;
;*------------------------------------------------------------------------------------*|
;* *|
;*---------------------------------4x PLL ENABLE--------------------------------------*|
;* *|
;*------------------------------------------------------------------------------------*|
;* *|
;* 4X PLL Enable *|
;* CONFIG PLLCFG = OFF ;Oscillator used directly *|
CONFIG PLLCFG = ON ;Oscillator multiplied by 4 *|
;* *|
;*------------------------------------------------------------------------------------*|
;
; Primary clock enable bit
; CONFIG PRICLKEN = OFF ;Primary clock can be disabled by software
; CONFIG PRICLKEN = ON ;Primary clock enabled
;
; Fail-Safe Clock Monitor Enable bit
; CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
; CONFIG FCMEN = ON ;Fail-Safe Clock Monitor enabled
;
; Internal/External Oscillator Switchover bit
; CONFIG IESO = OFF ;Oscillator Switchover mode disabled
; CONFIG IESO = ON ;Oscillator Switchover mode enabled

;*--------------------------------------------------------------------------------------------------------|
;* | -------------------------- | |
;* ----------------------------- | [PROTECTED OPTION FUSES] | ------------------------------------ |
;* | -------------------------- | |
;*--------------------------------------------------------------------------------------------------------|

CONFIG CP0 = OFF ; Block 0 (000800-003FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot Block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

;*---------------------------------------------------------------------------------------------------------|
;*---------------------------------------------------------------------------------------------------------|

#ENDCONFIG

define OSC 64

INCLUDE "modedefs.bas"
INCLUDE"ALLDIGITAL.pbp"

OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL

LED var PORTC.3


main:
high LED
pause 10
low LED
pause 1000
goto main

Ioannis
- 1st July 2018, 18:04
pause 10 is too little time.

Maybe you need to make this 1000.

Then check with a timer that it actually pauses every 1 sec confirming that osc is setup correctly.

Recently I had similar matter setting up system oscillator again with 4xPLL and was running slow. Richard got it and we are OK now.

Ioannis

astanapane
- 2nd July 2018, 08:27
pause 10 is too little time.

Maybe you need to make this 1000.

Then check with a timer that it actually pauses every 1 sec confirming that osc is setup correctly.

Recently I had similar matter setting up system oscillator again with 4xPLL and was running slow. Richard got it and we are OK now.

Ioannis

Ioanni,

thanks a lot for the help. I'll do it tonight and try to increase the time.

To be honest i made many changing to the pause commands like : 10ms , 100ms, 1000ms, 2000ms. All seem to work fine.

It would be a good practice to see how could we identify that the PIC clock now is running at 64Mhz?

Is there any way to measure this with an Oscilloscope? I believe that this is a question have been asked many times.

If someone could help me to identify the cpu clock speed of the PIC with the new setting of external 16Mhz crystal and 4xPLL, i can do the testing, and share the information step by step in here. If there is already posted an example like this i would like to have a look at it.

thanks a lot. :)

richard
- 2nd July 2018, 10:32
Is there any way to measure this with an Oscilloscope?

measure the led on/off time with the Oscilloscope if the osc is correct then the on/off times will be as per your code


I would leave CONFIG PWRTEN = ON


AND ADD THIS
OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL
while ! osccon2.7 :WEND

to make sure the pll has stabilised before you run any other code , if timing is critical

astanapane
- 2nd July 2018, 11:05
measure the led on/off time with the Oscilloscope if the osc is correct then the on/off times will be as per your code


I would leave CONFIG PWRTEN = ON


AND ADD THIS
OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL
while ! osccon2.7 :WEND

to make sure the pll has stabilised before you run any other code , if timing is critical

Hi Richard,

Thanks, i will do that. So that means that the 8th Bit of the OSCCON2 which is the .7 must be presented?

richard
- 2nd July 2018, 11:39
I seldom use crystals [you lose two pins for marginal increase in accuracy ] but I believe you can set pll mode in the config section and is all you need

CONFIG PLLCFG = ON
;CONFIG PLLCFG = OFF // pll can only be engaged with software selection for int osc


and then all this is not valid , it applies to internal osc only

OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL


this may or may not be necessary with xtal osc , I have found bootloaders won't sync up unless this is done using int osc with pll.
the PLLRDY bit is asserted when pll is stable [although I can find no mention in data sheet]
while ! osccon2.7 :WEND

astanapane
- 2nd July 2018, 12:11
Hi Richard,

i got confused. Really im reading the datasheet and there is nowhere clear setting for using external crystal.

I came up with the following


OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL

Because i was thinking that in order to specify the OSCCON, and the multiplier PLL we need to add those lines even if it is for internal or external source. I didnt think that those where valid only for internal OSC.

So what should i add as OSCCON value, as i use and external crystal 16Mhz? How do i specify the OSCCON value for 64Mhz?

thanks once again. Apologize for all my questions.

richard
- 2nd July 2018, 12:15
So what should i add as OSCCON value, as i use and external crystal 16Mhz? How do i specify the OSCCON value for 64Mhz?
nothing , leave it alone

OSCTUNE.6 = 1 ; Enable 4x PLL becomes non functional if CONFIG PLLCFG = ON

astanapane
- 2nd July 2018, 12:29
nothing , leave it alone

OSCTUNE.6 = 1 ; Enable 4x PLL becomes non functional if CONFIG PLLCFG = ON

Thanks i'll do so. Tonight i will post result in addition to some pictures from Oscilloscope. Lets see.

astanapane
- 2nd July 2018, 20:33
I did the following code test in order to check communication with an uLCD 144 and the LED as before at the same port.

I would love to use the .inc files for the Nokia 3310 or 5110 LCD but really i dont know which files are the ones match.

Anyway i think the code works as it is and the configuration settings are ok.


'************************************************* ********************************
@ ERRORLEVEL -306 ; this command prevents the compiler to give you a notice of *
; crossing page boundary - make sure bits are set *
'************************************************* ********************************

#CONFIG ; The PBP configuration for the PIC18F26K22 is:
; CONFIG FOSC = RCIO6 ; External RC oscillator
CONFIG FOSC = HSHP ; HS oscillator (high power &gt; 16 MHz)


;*---------------------------------4x PLL ENABLE--------------------------------------*|
;* 4X PLL Enable *|
;* CONFIG PLLCFG = OFF ;Oscillator used directly *|
CONFIG PLLCFG = ON ;Oscillator multiplied by 4 *|
;*------------------------------------------------------------------------------------*|
;
; Primary clock enable bit
; CONFIG PRICLKEN = OFF ;Primary clock can be disabled by software
CONFIG PRICLKEN = ON ;Primary clock enabled
;
; Fail-Safe Clock Monitor Enable bit
CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
; CONFIG FCMEN = ON ;Fail-Safe Clock Monitor enabled
;
; Internal/External Oscillator Switchover bit
CONFIG IESO = OFF ;Oscillator Switchover mode disabled
; CONFIG IESO = ON ;Oscillator Switchover mode enabled

CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)

;*--------------------------------------------------------------------------------------------------------;|
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect ;|
CONFIG WDTPS = 32768 ; 1:32768 ---> HERE enable the watchdog timer with a 1:32768 postscale;|
;*--------------------------------------------------------------------------------------------------------;|

CONFIG PWRTEN = ON
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Disabled

;*----------------------------------------------------------------------------------|
;*---------------Available configuration settings for PIC18F26K22-------------------|
;*----------------------------------------------------------------------------------|
;
; Oscillator Selection bits
; CONFIG FOSC = RC ;111X External RC oscillator, CLKOUT function on RA6
; CONFIG FOSC = ECLPIO6 ;EC oscillator (low power, &lt;500 kHz)
; CONFIG FOSC = ECLP ;EC oscillator, CLKOUT function on OSC2 (low power, &lt;500 kHz)
; CONFIG FOSC = ECMPIO6 ;EC oscillator (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = ECMP ;EC oscillator, CLKOUT function on OSC2 (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = INTIO7 ;Internal oscillator block, CLKOUT function on OSC2
; CONFIG FOSC = INTIO67 ;Internal oscillator block
; CONFIG FOSC = RCIO6 ;External RC oscillator
; CONFIG FOSC = RC ;External RC oscillator, CLKOUT function on OSC2
; CONFIG FOSC = ECHPIO6 ;EC oscillator (high power, &gt;16 MHz)
; CONFIG FOSC = ECHP ;EC oscillator, CLKOUT function on OSC2 (high power, &gt;16 MHz)
; CONFIG FOSC = HSMP ;HS oscillator (medium power 4-16 MHz)
; CONFIG FOSC = HSHP ;HS oscillator (high power &gt; 16 MHz)
; CONFIG FOSC = XT ;XT oscillator
; CONFIG FOSC = LP ;LP oscillator

;*--------------------------------------------------------------------------------------------------------|
;* | -------------------------- | |
;* ----------------------------- | [PROTECTED OPTION FUSES] | ------------------------------------ |
;* | -------------------------- | |
;*--------------------------------------------------------------------------------------------------------|

CONFIG CP0 = OFF ; Block 0 (000800-003FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot Block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

#ENDCONFIG

;*---------------------------------------------------------------------------------------------------------|
;*---------------------------------------------------------------------------------------------------------|

define OSC 64

INCLUDE "modedefs.bas"
INCLUDE"ALLDIGITAL.pbp"

OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL

while ! osccon2.7 :WEND ; to make sure the pll has stabilised before you run any other code

TRISA = %00000000 'use TRISA to specify which pin is (1 = input) and which (0 = output) (ALL pins are output)
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

' --------------- TRIS A-B-C ---------------
'------------------------------------------------------------------------|
' PORTA.7 PORTA.6 PORTA.5 PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 0 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTC.7 PORTC.6 PORTC.5 PORTC.4 PORTC.3 PORTC.2 PORTC.1 PORTC.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|

PORTA = 0 'make low all ports at A range
PORTB = 0 'make low all ports at B range
PORTC = 0 'make low all ports at C range
PORTE = 0 'make low all ports at E range


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'*-----------------------------------------------------------------------------|

Timeout CON 1000
Counter var byte
ser_in_data var byte
LED var PORTC.3


' -------------------------------------------------------------------------/
' [ LCD Initialization ] /
'------------------------------------------------------------------------/
' FIRST TIME BOOTUP: We give plenty of time for tbe LCD '
pause 2000
HSEROUT2[$55] ' uOLED Initialize this is the 'U' character of autoband rate to LCD
Hserin2 timeout,main,[wait(6)]
hserout2[$56,$01] ' this is the Version info --> '01 10 17 28 28 -->> "(("
Hserin2 timeout,main,[wait(1),wait(16),wait(23),wait(40),wait(40)]
HSEROUT2 [$45] ' clear the LCD
Hserin2 timeout,main,[wait(6)]
pause 500

hserout2[$73,$00,$03,$11,$ff,$ff," Leonardo Bilalis",$00]
Hserin2 timeout,main,[wait(6)]
hserout2[$73,$02,$06,$10,$ff," Copyright 2018",$00]
Hserin2 timeout,main,[wait(6)]
pause 500

Hserout2[$4F,01]
Hserin2 timeout,main,[wait(6)]

HSEROUT2 [$45] ' clear the LCD
Hserin2 timeout,main,[wait(6)]

Start:
for counter = 1 to 100
HSEROUT2 [$73,$03,$02,$11,$ff,$ff,"Counter:", #counter,$00]
pause 10
next counter
;gosub main ---> i did commented this as from Ioannis's advice.

main:
high LED
pause 1000
low LED
pause 1000
goto main

Ioannis
- 2nd July 2018, 20:41
This is a bad programing.

You use a gosub to main but there is no return from the main.

Delete the line gosub main as there is no point.

Be very careful with such commands, as it is common to novice programmers this error. Your program will crash with no obvious reason.

Ioannis

astanapane
- 2nd July 2018, 20:59
This is a bad programing.

You use a gosub to main but there is no return from the main.

Delete the line gosub main as there is no point.

Be very careful with such commands, as it is common to novice programmers this error. Your program will crash with no obvious reason.

Ioannis

Oppps , i didnt notice.

Thanks Ioanni. As you see i have also configured the First EUART. There will be placed either the GPS module or the SD card.

P.S. didnt have a time tonight to do the test and post pictures from the oscilloscope. Will do that tomorrow.

richard
- 3rd July 2018, 00:01
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.
setting the tris incorrectly will stop the eusarts from working


from data sheet


Setting the TXEN bit of the TXSTAx register enables thetransmitter circuitry of the EUSART. Clearing the SYNC
bit of the TXSTAx register configures the EUSART for
asynchronous operation. Setting the SPEN bit of the
RCSTAx register enables the EUSART and
automatically configures the TXx/CKx I/O pin as an
output. If the TXx/CKx pin is shared with an analog
peripheral the analog I/O function must be disabled by
clearing the corresponding ANSEL bit.

astanapane
- 3rd July 2018, 06:47
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.
setting the tris incorrectly will stop the eusarts from working


from data sheet


Setting the TXEN bit of the TXSTAx register enables thetransmitter circuitry of the EUSART. Clearing the SYNC
bit of the TXSTAx register configures the EUSART for
asynchronous operation. Setting the SPEN bit of the
RCSTAx register enables the EUSART and
automatically configures the TXx/CKx I/O pin as an
output. If the TXx/CKx pin is shared with an analog
peripheral the analog I/O function must be disabled by
clearing the corresponding ANSEL bit.



To be honest i didnt really understand the statement. I understand that it is not necessary to set a TRIS register at TX bit as automatically the TX configured as an output.....


TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

as we have already stated and configured the EUSART with the following:


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'*-----------------------------------------------------------------------------|


How Do we configure the rest of the TRIS bits?


TRISB = %1X000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %1X000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

richard
- 3rd July 2018, 08:28
How Do we configure the rest of the TRIS bits?

when you read


Setting the SPEN bit of the
RCSTAx register enables the EUSART and
automatically configures the TXx/CKx I/O pin as an
output.


in the eusart section it means its unnecessary to clear the tris bit for the tx pin, keep in mind that on some chips clearing the tx pin tris bit
will stop the eusart from working . I prefer to form good habits ,although the 18f26k22 does not seem to object and functions either way .

astanapane
- 3rd July 2018, 10:12
Richard, bardon me. Still im confused from your answer.

I understand the part of setting up the EUSART, automatically we make the TX port as an output.

My question is, what do we do with the rest of the TRISB and TRISC bits apart from the RX/TX.


TRISA = %00000000 'use TRISA to specify which pin is (1 = input) and which (0 = output) (ALL pins are output)
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

' --------------- TRIS A-B-C ---------------
'------------------------------------------------------------------------|
' PORTA.7 PORTA.6 PORTA.5 PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 0 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTC.7 PORTC.6 PORTC.5 PORTC.4 PORTC.3 PORTC.2 PORTC.1 PORTC.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|


I feel like i'm missing something. Sorry for my questions, but i would like to be sure i understand every part of the code. And when i configure the ports as an input or outputs, or Digital/Analogue, i need to know exactly what am i doing.

In our case as we want to configure the EUSART, as you stated from the manual -datasheet, we have automatically the TX pin as an output. But dont we continue to specify the other pins with the TRIS command?

richard
- 3rd July 2018, 10:25
My question is, what do we do with the rest of the TRISB and TRISC bits apart from the RX/TX.
that would depend entirely on what you want do with each pin , if and only if you need it to be an output clear the corresponding tris bit

astanapane
- 3rd July 2018, 11:03
.
that would depend entirely on what you want do with each pin , if and only if you need it to be an output clear the corresponding tris bit

Thanks a lot for your answer. I guess we cant leave unassigned the bits. Normally as far as i have learned from what i have read in this forum, it is better to configure the bits of each port and give an Input or Output direction in addition to high or low stage.

richard
- 3rd July 2018, 11:27
I guess we cant leave unassigned the bits

there is no such state , pins are either inputs or outputs or analog inputs , outputs can only be high or low
if you have no reason to use a pin as an output then leave as an input , if you have no reason to use a pin as a digital input you can leave it analog
the pic chip won't care. the pic chip does not care if outputs are high or low its only what they connect to that cares , it up to you to initialise the pins
in use to the correct type/state and direction to match the requirement of what they connect to and then make sure they stay that way.

astanapane
- 3rd July 2018, 12:59
there is no such state , pins are either inputs or outputs or analog inputs , outputs can only be high or low
if you have no reason to use a pin as an output then leave as an input , if you have no reason to use a pin as a digital input you can leave it analog
the pic chip won't care. the pic chip does not care if outputs are high or low its only what they connect to that cares , it up to you to initialise the pins
in use to the correct type/state and direction to match the requirement of what they connect to and then make sure they stay that way.

now it is clear. Really appreciate your help.

Thanks a lot Richard.

Ioannis
- 3rd July 2018, 13:46
I have my doubts on leaving inputs floating.

Usually if I have any unused ports, I turn them as outputs and clear them.

If they are inputs, maybe enable the pull ups if any or tie them to ground.

A floating pin can cause troubles in certain cases.

Ioannis

richard
- 3rd July 2018, 15:33
if your program is adversely affected by an unused pin then your code is incomplete and needs more debugging

Ioannis
- 3rd July 2018, 15:48
No the program itself but on the total current consumption as the CMOS do not like to be float.

Ioannis

Dave
- 3rd July 2018, 17:48
Especially when in the presence of stray RF. They make rather nice antenna's.

astanapane
- 3rd July 2018, 17:54
Especially when in the presence of stray RF. They make rather nice antenna's.

Ok then what is the most professional way in most of the cases to configure the pins?

Should we make all the non used pins as an outputs - low stage?

richard
- 3rd July 2018, 23:43
its my understanding that pins left as inputs and tied or pulled down to vss will minimise current consumption.
when i'm breadboarding a design I let them float and have never had an issue , if you accidently plug something
into a input pin at least smoke is not released.

Ioannis
- 4th July 2018, 06:52
Depends. If you care about minimum current as is the case in battery operated devices, then yes. Do set them as inputs with proper pull up enabled inside the PIC or a large size output.

I found that makes no difference, at least for the chips I used, to set them as outputs logic low, and use no pull up or down. Pull up/down can also contribute to current consumption.

I attach the page 2-4 of the Tips and Tricks, file DS01146B regarding I/O that has more info on this.

Ioannis

8732

astanapane
- 4th July 2018, 07:29
I believe that any input pin must be pulled either up or down depends on the use. If there is a switch normally most of the people, pull up a resistor at the input pin, and the switch goes to ground.

Now for the rest of the pins personally if the pins are output and digital, i leave them floating, but i use the command:


PORTx = low

Just read Ioannis previous post. It is really useful info. Thanks a lot.

Ioannis
- 4th July 2018, 08:41
PortX=0
TrisX=xxxx

Thats all.
Ioannis

astanapane
- 4th July 2018, 10:29
Ok lets move forward to the project.

I have received the PIC18F26K22, and connected the ulcd and the Gps module at the EUSARTs.

I tried to configure each EUSART with a different baudrate but didnt work. Anyway i did the baudrate the same for both of the ports and uLCd and GPS worked fine.


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator




'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


'*-----------------------------------------------------------------------------|
'*-----------------------------------------------------------------------------|


Then on the main program, i have placed the code to get the information from the GPS and display them to uLcd.

Some coding is still not good, but at the moment i need to solve the problem that is mentioned at the end of this message. So please ignore any minor issues at them moment.


'--------------------------------------------------------------------------------|
' |
' ---------------------------- [ MAIN ] ----------------------------------- |
' |
'--------------------------------------------------------------------------------|

main:

'-------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GNRMC ] /
' [$GNRMC,090045.000,A,3823.6645,N,02353.3600,E,0.02, 195.80,170518,,,A*62] /
' /
'--------------------------------------------------------------------------/
;serin2 gps_tx,84,timeout,lostcable,[wait("$GNRMC"),_ ;we wait for $GNRMC
HSERIN timeout,lostcable,[wait("$GPRMC"),_ ;we wait for $GPRMC
wait(","),dec2 hh,dec2 mm,dec2 ss,wait("."),dec3 sss,_ ;we wait for 090045.000 which is the time when we got the info
wait(","),fix,_ ;we wait for A
wait(","),dec2 degrees,dec2 minutes,wait("."),dec4 minutesd,_ ;we wait for 3823.6645
wait(","),dir,_ ;we wait for N/S ;wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dir2,_ ;we wait for E/W
wait(","),wait(","),_
wait(","),dec2 day,dec2 month,dec2 year,_ ;we wait for 170518
wait(","),SKIP 3]

'------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GNGGA ] /
' [$GNGGA,140405.000,3823.6010,N,02353.3054,E,1,9,0.8 8,0.8,M,35.9,M,,*40 /
' /
'-------------------------------------------------------------------------/
'serin2 gps_tx,84,timeout,lostcable,[wait("$GNGGA"),_ ;we wait for GNGGA
HSERIN timeout,lostcable,[wait("$GPGGA"),_ ;we wait for GPGGA
wait(","),skip 35,_
wait(","),modefix,_ ;we wait for modefix 1,2 or 3 check gps manual for NMEA
wait(","),dec2 satno,_
wait(","),skip 19]


'------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GPGSA ] /
' [$GPGSA,A,3,18,08,10,11,14,27,22,32,01,,,,2.62,1.11 ,2.37*03] /
' /
'-------------------------------------------------------------------------/

HSERIN timeout,lostcable,[wait("$GPGSA"),_
wait(","),wait(","),mode2d3d,_
wait(","),SKIP 50]

'------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GPVTG ] /
' [$GPVTG,215.60,T,,M,0.44,N,0.82,K,A*37] /
' /
'-------------------------------------------------------------------------/

'serin2 gps_tx,84,timeout,lostcable,[wait("GNVTG"),_
HSERIN timeout,lostcable,[wait("GPVTG"),wait("N"),_
wait(","),TS[0],TS[1],TS[2],TS[3],TS[4],_
wait(","),SKIP 3]

for i = 1 to 3
if TS[i]="." then decimal = i 'TOTAL SPEED. find which character is the decimal point
next i

select case decimal 'if the gps shows 1.45 it may be 145
'for the conversion ASCII to number we can use the -48
case 1 'decimal position is x.xx
SPEED = (10*(TS[0]-48))+(TS[2]-48)
case 2 'decimal position is xx.xx
SPEED = (100*(TS[0]-48))+(10*(TS[1]-48))+(TS[3]-48)
case 3 'decimal position is xxx.xx
SPEED = (1000*(TS[0]-48))+(100*(TS[1]-48))+(10*(TS[2]-48))
end select

gosub OVERSPEED:

'---------------------------------------------------------------------------------/
' Here if the GPS module havent been fixed to any satellite will jump to notfix /
'-------------------------------------------------------------------------------/

if fix = "V" then notfix
;pause 100

'-----------------------------------------------------------------------------/
' [ Return from notfix ] /
' [ We clear any character on the display ] /
'--------------------------------------------------------------------------/

'serout2 lcd,6,[$45] 'Clear the ulcd 1'44
'pause 100

'-----------------------------------------------------------------------------/
' Here we start the calendar conversion /
'---------------------------------------------------------------------------/

arraywrite ndays,13,NDAY,[0,31,28,31,30,31,30,31,31,30,31,30,31]
if ndays = 2 then
if ((year//4 = 0) and (year//400 != 0)) then 'check the leap year
ndays[2] = 29 'then February has 29 days
else
ndays[2] = 28 'else has 28 days
endif
endif
hh = hh + 3 'the Hour from GPS is UTC so for our country in Greece we add +3
if hh>23 then 'if the hh+3 hour is greater than 23 (23:00) then
day = day + 1 'we check the day added is true
hh = hh//24 'but the hour not exceed the 24 so go to 00:00
if (day > ndays[month]) then
month = month + 1
day = 1
if (month > 12) then
year = year + 1
month = 1
endif
endif
endif

'--------------------------------------------------------------------------/
' here is where the code dispays the stored info from the NMEA sentenses /
'------------------------------------------------------------------------/


HSEROUT2 [$73,$03,$00,$00,$FF,$FF," Protocol:","NMEA",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$01,$02,$01,$07,$E0," Date:",dec2 day,"/",dec2 month,"/",dec2 year,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$00,$04,$00,$FF,$E0," TIME: ",dec2 hh," :",dec2 mm," :",dec2 ss,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$00,$07,$00,$FF,$FF," Sats",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$01,$09,$11,$6D,$BF,dec2 satno,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$0E,$07,$00,$FF,$FF," Fixed",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$0B,$09,$11,$6D,$BF," ",fix," ",mode2d3d,"D",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$05,$06,$11,$FF,$E0," SPEED",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$06,$05,$12,$6D,$BF,TS[0],TS[1],TS[2],TS[3],$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$06,$06,$12,$FF,$E0,"Km/h",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$00,$0C,$00,$ff,$ff," Lat : ",dec2 degrees,"*",dec2 minutes,"'",dec4 minutesd,"@"," ",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$BE,$0C,$10,$f8,$00,dir,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$00,$0E,$00,$ff,$ff," Lon :",dec3 degrees2,"*",dec2 minutes2,"'",dec4 minutesd2,"@"," ",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$BE,$0E,$10,$f8,$00,dir2,$00]
Hserin2 timeout,lostcable,[wait(6)]

goto main


As you see in the main code there is a line that checking if there is no FIX, so will jump to a label notfix:


'---------------------------------------------------------------------------------/
' Here if the GPS module havent been fixed to any satellite will jump to notfix /
'-------------------------------------------------------------------------------/

if fix = "V" then notfix

See next the notfix label:

As i do not use any interrupt at the moment, any instruction is waiting to be completed until the next one will continue.

So at this code, when i POWERUP the circuit, until the GPS gets the signal, jumps at the notfix.


notfix: ' Still looking for fixed signal

hh = hh + 3
hh = hh//24
HSEROUT2 [$73,$00,$03,$11,$6D,$BF," Waiting for GPS",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$00,$07,$00,$07,$ff,rep "." \dots,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$07,$09,$10,$6D,$BF,"SATs :",dec2 satno,$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$04,$07,$12,$F8,$00,"NOT FIXED",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$06,$0D,$11,$6D,$BF,fix," ",mode2d3d,"D",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2 [$73,$03,$06,$10,$ff,$E0," Time:",dec2 hh," :",dec2 mm," :",dec2 ss,$00]
Hserin2 timeout,lostcable,[wait(6)]
pause 3000

HSEROUT2 [$45] ' clears the LCD
Hserin2 timeout,lostcable,[wait(6)]


goto main 'returns to main

QUESTION:
Now, i need to clear at the end of the notfix the display, because if there is a FIX from the GPS, will go back to main, and i need to see the new data and not a mixed from notfix and main. Is there any way to clear the LCD before i go back to main without place the clear lcd command inside the main or the notfix label? ? Could you please help me and give me some advice?



Still i havent design any circuit as i develop the test board some things are changing.

I post pictures of the test board, and i would try to explain what i have done.

astanapane
- 5th July 2018, 09:24
A Test with an not very good ATTEN oscilloscope.

main:
high LED
pause 10
low LED
pause 100
goto main

With an Auto-trigger i get 9.11 Hz

If i make the following change

main:
high LED
pause 10
low LED
pause 50
goto main

i get a frequency of: 16.72 Hz.

As far as i have discussed with Ioannis, it should be ok, and the setting for 64Mhz is activated up and running.

Ioannis
- 5th July 2018, 09:47
Obviously your picture shows the first piece of code. You Oscilloscope is set for 50ms/div, so your period seems to measure about 110ms.

Your program is making high for 10ms and another 100ms low which makes for a 110ms plus any PIC delays and inaccuracies, very much close to 120ms.

Good to go!

Ioannis

astanapane
- 11th July 2018, 06:56
Dear all,

i have a question it maybe answered in lots of different threads.
http://www.picbasic.co.uk/forum/showthread.php?t=10389

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

QUESTION: Can i have different baudrates on the 2 EUARTs? How could i change baudrate during start up in one of the EUART port?

Have in mind that i use both of the EUARTs. One for the Display and one for the GPS.

The GPS has a firmware with the following configuration:

Baudrate: 9600 default
Refresh rate: 1hz

On the code i have configured the EUART for 38400, because i set the GPS externally with serial terminal at 38400. But GPS keeps the configs only if a backup battery is presented.

So i came up with this problem.

I need to send the following command first to SET the BAURATE at 38400, but with the EUART at 9600, because at that stage the GPS communicate only at 9600.


HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration.

Then GPS as i tested i connected successfully, at 38400, but the following command cannot be sent before i change on the fly the EUART at 38400.


HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz

From the links above, i checked the way you are setting the new baudrate, and tried to do it as well but didnt really worked.

HenrikOlsson
- 11th July 2018, 07:47
Yes, you can have different baudrate(s) on different UARTs and yes you can change it at any time you want. But, as the posts in the other thread says, to change it you can't use PBPs DEFINE HSER_BAUD because that's a compile time directive and not a runtime command.

If you want to set or change the baudrate at runtime you need to set or change the registers manually. BAUDCON, SPBRG, SPBRGH most of the time. And remember that the register names are different for the second UART - BAUDCON2, SPBRG2 etc.

Without seeing your code we can't say why what you did didn't work.

astanapane
- 11th July 2018, 08:18
Yes, you can have different baudrate(s) on different UARTs and yes you can change it at any time you want. But, as the posts in the other thread says, to change it you can't use PBPs DEFINE HSER_BAUD because that's a compile time directive and not a runtime command.

If you want to set or change the baudrate at runtime you need to set or change the registers manually. BAUDCON, SPBRG, SPBRGH most of the time. And remember that the register names are different for the second UART - BAUDCON2, SPBRG2 etc.

Without seeing your code we can't say why what you did didn't work.

Hi Henrik,

i did have the DEFINEs on the code like this.


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator






'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

'DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
'DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
'DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
'DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
'SPBRGH2 = 1
'BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 130 ' 9600 Baud @ 64MHz, -0,02%
SPBRGH2 = 6
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


'------------------------------------------------------------------------------|
'-----------sent a sentence to gps, this is not working at the moment----------|
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.

' $PMTK011,MTKGPS*08
' $PMTK010,001*2E
' $PMTK010,002*2D

pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 500
RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $24 ' Enable transmit, BRGH = 1
SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz

On the first stage that i send the following command, to change the baudrate for the GPS, it works because both PIC and GPS are at the same rate.


B]HSEROUT2[/B] ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration

then i guess with the following code that i change the rate on the PIC's port.


RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $24 ' Enable transmit, BRGH = 1
SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%

then send the command which seems that it doesnt work.


HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz

richard
- 11th July 2018, 08:34
if you are using the 16 bit baudrate generator
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


then the baud rate is set using SPBRGHx and SPBRGx



setting
SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08% is only half the job

astanapane
- 11th July 2018, 08:44
if you are using the 16 bit baudrate generator
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


then the baud rate is set using SPBRGHx and SPBRGx



setting
SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08% is only half the job

I will check the code later today based on your given directions. thanks a lot.

I might need to check and wait for the GPS responds and PIC port response. I will try to set a code, and if i need again your help i will keep you posted.

astanapane
- 11th July 2018, 13:08
Dear all,

i did the following....


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $24 ' Enable transmit, BRGH = 1
'CLROERR = 1 ' Clear overflow automatically
SPBRG = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $24 ' Enable transmit, BRGH = 1
'CLROERR2 1 ' Clear overflow automatically
SPBRG2 = 130 ' 9600 Baud @ 64MHz, -0,02%
SPBRGH2 = 6
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


'------------------------------------------------------------------------------|
'-----------sent a sentence to gps, this is not working at the moment----------|
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.

' $PMTK011,MTKGPS*08
' $PMTK010,001*2E
' $PMTK010,002*2D

pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
Pause 500

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

It works the part of a baudrate change, when i send the command to the gps


HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

but i think the following it goes to GPS but for some reason is not set.



HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

tumbleweed
- 11th July 2018, 15:04
When you switch the baudrate you have to set both SPBRG2 and SPBRGH2.

Try:

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500

astanapane
- 11th July 2018, 15:39
When you switch the baudrate you have to set both SPBRG2 and SPBRGH2.

Try:

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500


It worked!!! I will post later on what i have done up to now. Maybe i need to set up a small indication when those commands are set. So i know that program is working up to that stage.

This is the code for changing the baudrate to GPS and the hz rate at 10Hz at the NMEA data.


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $24 ' Enable transmit, BRGH = 1
'CLROERR = 1 ' Clear overflow automatically
SPBRG = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $24 ' Enable transmit, BRGH = 1
'CLROERR2 1 ' Clear overflow automatically
SPBRG2 = 130 ' 9600 Baud @ 64MHz, -0,02%
SPBRGH2 = 6
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator


'------------------------------------------------------------------------------|
'-----------sent a sentence to gps, this is not working at the moment----------|
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.

' $PMTK011,MTKGPS*08
' $PMTK010,001*2E
' $PMTK010,002*2D

pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

astanapane
- 12th July 2018, 18:24
Dear all,

i need you help.

After removing all the DEFINEs and use only the following


'RCSTA2 = $90 ' Enable serial port & continuous receive
'TXSTA2 = $24 ' Enable transmit, BRGH = 1
'DEFINE HSER_CLROERR2 1 ' Clear overflow automatically
'SPBRG2 = 130 ' 9600 Baud @ 64MHz, -0,02%
'SPBRGH2 = 6
'BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

and for changing the baudrate and the frequency for the Second Serial,


pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500

;HSEROUT2 ["$PMTK314,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0*29",13,10]
HSEROUT2 ["$PMTK314,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28",13,10]
pause 100

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

I found out that on SERIAL TERMINAL on the computer of course i see the data coming from the GPS but unfortunately when i connect an LCD to PIC using the first Serial, the data cannot be displayed.

It seems that when i use


HSERIN2[wait("$GPRMC")

the data cannot be read from the GPS to the RX of the PIC.

I tried to check if there is a problem with the LCD, but i can send text to the LCD.

I tried to check and use only the DEFINEs with 9600 because the default for the GPS is 9600 and it worked.

so when i do not use the defines, it looks like that RX port on the PIC doesnt work.

Could you please help?

Ioannis
- 12th July 2018, 20:51
I don't know about other members of the forum, but I got confused.

Best throw in both codes that work and don't work.

Do they compile OK?

Are you sure about your connections?

Ioannis

astanapane
- 12th July 2018, 21:42
Ioanni,

thanks for your reply.

The problem is when i use the code to change on the fly as of the guys on the forum advice, without using the "DEFINE" word.

So using the registers like in the following code:


'RCSTA2 = $90 ' Enable serial port & continuous receive
'TXSTA2 = $24 ' Enable transmit, BRGH = 1
'DEFINE HSER_CLROERR2 1 ' Clear overflow automatically
'SPBRG2 = 130 ' 9600 Baud @ 64MHz, -0,02%
'SPBRGH2 = 6
'BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

and later in order to change the baudrate i use the following:


pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500

;HSEROUT2 ["$PMTK314,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0*29",13,10]
HSEROUT2 ["$PMTK314,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28",13,10]
pause 100

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

The GPS gets the command and set correctly the new configuration.

So up to here we are fine. I checked this by using a Serial Terminal and i see that both Baudrate and frequency have changed.

That means that TX is working well, for the first command to the GPS


HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

and changing on the fly the Baudrate the following command to GPS is working fine as well with the NEW baudrate.


HSEROUT2 ["$PMTK314,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28",13,10]
pause 100

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100[/CODE]

Now what is the problem.

I used a uLCD to send the VTG and RCM data to the display as i have done in the past using both SEROUT and HSEROUT2 command (for the HSEROUT2 i used the following code with the "DEFINE" and worked well)


'DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
'DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
'DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
'DEFINE HSER2_SPBRG 130 ' 9600 Baud @ 64MHz, -0,02%
'SPBRGH2 = 6
'BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

Now using the registers without the "DEFINEs" the RX on the PIC seems that it doesnt work, something like it is closed or cannot read data from GPS.

Forgot to mention that 2 hours now i have tried most of the different settings for the


BAUDCON2 = %xxxxxxxx

without any luck.

astanapane
- 12th July 2018, 22:00
Compile is ok, the same circuit as when the code is working with the "DEFINE" code.

Something is wrong with the new code that i change the baudrate on the fly.

there is something going really wrong. Maybe i need to give more PAUSE time?

I try that as well.

tumbleweed
- 12th July 2018, 23:00
Are you aware of what's been commented out in the code you're showing?

Post the entire code as it is.

Ioannis
- 12th July 2018, 23:49
Page 281 of the K22 PIC has all the settings of the BRGH, BRG16, SPBRGx in relation to the system Clock.

Also, depending whether you have RS232 driver or not you have to set or reset the BAUDCONx bits 4 and 5.

If you will use on both Rx and Tx pins a driver (like the MAX232) then these bits should be 0. If you connect directly to another TTL serial port then these bits should be 1.

Ioannis

astanapane
- 13th July 2018, 07:47
Are you aware of what's been commented out in the code you're showing?

Post the entire code as it is.

Thanks for the reply. Yes i will post later today the code. But i understand the logic using MISTERE's calculator for EUART and it worked fine. But As the GPS at default is working at 9600, i need to speed it up to 38400. So at this point baudarate change on the fly needs a different settings.

I have configured the code in order to change the baudrate as mentioned on my previous posts, and from the SERIAL TERMINAL, on the computer i see that TX from PIC to GPS is working fine.

But the RX of the PIC looks like is not receiving or is not reading the info.

In any case i will post the entire code later.

Thanks once again.

astanapane
- 13th July 2018, 13:29
Here is the code that is not working.


'************************************************* ********************************
@ ERRORLEVEL -306 ; this command prevents the compiler to give you a notice of *
; crossing page boundary - make sure bits are set *
'************************************************* ********************************

#CONFIG ; The PBP configuration for the PIC18F26K22 is:
; CONFIG FOSC = RCIO6 ; External RC oscillator
CONFIG FOSC = HSHP ; HS oscillator (high power &gt; 16 MHz)


;*---------------------------------4x PLL ENABLE--------------------------------------*|
;* 4X PLL Enable *|
;* CONFIG PLLCFG = OFF ;Oscillator used directly *|
CONFIG PLLCFG = ON ;Oscillator multiplied by 4 *|
;*------------------------------------------------------------------------------------*|
;
; Primary clock enable bit
; CONFIG PRICLKEN = OFF ;Primary clock can be disabled by software
CONFIG PRICLKEN = ON ;Primary clock enabled
;
; Fail-Safe Clock Monitor Enable bit
CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
; CONFIG FCMEN = ON ;Fail-Safe Clock Monitor enabled
;
; Internal/External Oscillator Switchover bit
CONFIG IESO = OFF ;Oscillator Switchover mode disabled
; CONFIG IESO = ON ;Oscillator Switchover mode enabled

CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)

;*--------------------------------------------------------------------------------------------------------;|
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect ;|
CONFIG WDTPS = 32768 ; 1:32768 ---> HERE enable the watchdog timer with a 1:32768 postscale;|
;*--------------------------------------------------------------------------------------------------------;|

CONFIG PWRTEN = ON
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Disabled

;*----------------------------------------------------------------------------------|
;*---------------Available configuration settings for PIC18F26K22-------------------|
;*----------------------------------------------------------------------------------|
;
; Oscillator Selection bits
; CONFIG FOSC = RC ;111X External RC oscillator, CLKOUT function on RA6
; CONFIG FOSC = ECLPIO6 ;EC oscillator (low power, &lt;500 kHz)
; CONFIG FOSC = ECLP ;EC oscillator, CLKOUT function on OSC2 (low power, &lt;500 kHz)
; CONFIG FOSC = ECMPIO6 ;EC oscillator (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = ECMP ;EC oscillator, CLKOUT function on OSC2 (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = INTIO7 ;Internal oscillator block, CLKOUT function on OSC2
; CONFIG FOSC = INTIO67 ;Internal oscillator block
; CONFIG FOSC = RCIO6 ;External RC oscillator
; CONFIG FOSC = RC ;External RC oscillator, CLKOUT function on OSC2
; CONFIG FOSC = ECHPIO6 ;EC oscillator (high power, &gt;16 MHz)
; CONFIG FOSC = ECHP ;EC oscillator, CLKOUT function on OSC2 (high power, &gt;16 MHz)
; CONFIG FOSC = HSMP ;HS oscillator (medium power 4-16 MHz)
; CONFIG FOSC = HSHP ;HS oscillator (high power &gt; 16 MHz)
; CONFIG FOSC = XT ;XT oscillator
; CONFIG FOSC = LP ;LP oscillator

;*--------------------------------------------------------------------------------------------------------|
;* | -------------------------- | |
;* ----------------------------- | [PROTECTED OPTION FUSES] | ------------------------------------ |
;* | -------------------------- | |
;*--------------------------------------------------------------------------------------------------------|

CONFIG CP0 = OFF ; Block 0 (000800-003FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot Block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

#ENDCONFIG

;*---------------------------------------------------------------------------------------------------------|
;*---------------------------------------------------------------------------------------------------------|

define OSC 64

INCLUDE "modedefs.bas"
INCLUDE "ALLDIGITAL.pbp" ; Make all ports Digital
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts

OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL

while ! osccon2.7 :WEND ; to make sure the pll has stabilised before you run any other code
'*-----------------------------------------------------------------------------|

'------------------------------------------------------------------------------|
' INITIALIZE RAM |
'------------------------------------------------------------------------------|
initialize:
CLEAR
'-------------------------------------------------------------------------------------------------------------------------------------------------------------

TRISA = %00000000 'use TRISA to specify which pin is (1 = input) and which (0 = output) (ALL pins are output)
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %10000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

'------------------------------------------------------------------------|
'--------------------------- TRIS A B C ---------------------------------|
'------------------------------------------------------------------------|
' PORTA.7 PORTA.6 PORTA.5 PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 0 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTC.7 PORTC.6 PORTC.5 PORTC.4 PORTC.3 PORTC.2 PORTC.1 PORTC.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|

'------------------------------------------------------------------------|
'----------------------- At start all PORTS LOW -------------------------|
'------------------------------------------------------------------------|
PORTA = 0 'make low all ports at A range |
PORTB = 0 'make low all ports at B range |
PORTC = 0 'make low all ports at C range |
PORTE = 0 'make low all ports at E range |
'------------------------------------------------------------------------|

'------------------------------------------------------------------------|
'-------------------------- COMPARATORS OFF -----------------------------|
'------------------------------------------------------------------------|
CM1CON0.7 = 0 'Disable comparator1 |
CM2CON0.7 = 0 'Disable comparator2 |
'------------------------------------------------------------------------|

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

'DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
'DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
'DEFINE HSER_CLROERR 1 ' Clear overflow automatically
'DEFINE HSER_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
'SPBRGH = 1
'BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $24 ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR = 1 ' Clear overflow automatically
SPBRG = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH = 1
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | --------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

'DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
'DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
'DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
'DEFINE HSER2_SPBRG 130 ' 9600 Baud @ 64MHz, -0,02%
'SPBRGH2 = 6
'BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $24 ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR2 1 ' Clear overflow automatically
SPBRG2 = 130 ' 9600 Baud @ 64MHz, -0,02%
SPBRGH2 = 6
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator
'BAUDCON2.5 = 1

'------------------------------------------------------------------------------|
'-----------sent a sentence to gps, this is not working at the moment----------|
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.

' $PMTK011,MTKGPS*08
' $PMTK010,001*2E
' $PMTK010,002*2D
Pause 800
HSEROUT2 ["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
pause 800

SPBRG2 = 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
Pause 500

;HSEROUT2 ["$PMTK314,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0*29",13,10]
HSEROUT2 ["$PMTK314,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28",13,10]
pause 100

HSEROUT2 ["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

'------------------------------------------------------------------------------|

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _ToggleLED, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
T0CON = %10010110 ; T0 = 16-bit, Prescaler 8
@ INT_ENABLE TMR0_INT ; enable Timer 0 interrupts

'*-----------------------------------------------------------------------------|
timeout con 2000
LED VAR PORTC.4
PWRLED var PORTC.3 ' indicates that PIC is working on port C.4
TOGPS var PORTB.5 ' this is the pin serout to GPS rx port

hh var byte 'hours
mm var byte 'minutes
ss var byte 'seconds
sss var word 'milisecs
degrees VAR BYTE 'latitude/longitude degrees
minutes VAR BYTE 'latitude/Longitude minutes
minutesD VAR word 'latitude/LONGITUDE DECIMAL MINUTES
dir VAR BYTE ' direction (latitude: 0 = N, 1 = S / longitude: 0 = E, 1 = W)
degrees2 VAR BYTE 'latitude/longitude degrees
minutes2 VAR BYTE 'latitude/Longitude minutes
minutesD2 VAR word 'latitude/LONGITUDE DECIMAL MINUTES
dir2 var byte 'direction (latitude: 0 = N, 1 = S / longitude: 0 = E, 1 = W)
SatNo VAR BYTE 'number of satellites connected
day var byte 'day
month var byte 'month
year var byte 'year
SPEED var word 'speed with conversion ASCII to dec -48
Km var byte 'this is a letter K --> KM/H om the GPVTG sentence
TS var byte[4] 'TOTAL SPEED. we create an array to combine each possible character for speed
decimal var byte
i var word 'we use this for Total speed point
FIX var byte 'fix sat V/A
Modefix var byte 'mode fix 1 or 2 or 3 depens

RED var PORTA.3
GREEN var PORTA.2
BLUE VAR PORTA.1
' -----------------------------------------------------------------------------|
' [ LCD Initialization ] |
'------------------------------------------------------------------------------|
' FIRST TIME BOOTUP: We give plenty of time for tbe LCD '

pause 2000
HSEROUT[$55] ' uOLED Initialize this is the 'U' character of autoband rate to LCD
Hserin [wait(6)]
hserout[$56,$01] ' this is the Version info --> '01 10 17 28 28 -->> "(("
Hserin [wait(1),wait(16),wait(23),wait(40),wait(40)]
HSEROUT [$45] ' clear the LCD
Hserin [wait(6)]
pause 500

hserout[$73,$00,$03,$11,$ff,$ff," Leonardo Bilalis",$00]
Hserin [wait(6)]
hserout[$73,$02,$06,$10,$ff," Copyright 2018",$00]
Hserin [wait(6)]
pause 3000

Hserout [$4F,01] 'Set transparent / opaque TEXT ($00 for transparent or $01 for opaque)
Hserin [wait(6)]

HSEROUT [$45] ' clear the LCD
Hserin [wait(6)]

'-------------------------------------------------------------------------------------

high led

Main:

;serin2 gps_tx,84,timeout,lostcable,[wait("$GNRMC"),_ ;we wait for $GNRMC
HSERIN2[wait("$GPRMC"),_ ;we wait for $GPRMC
wait(","),dec2 hh,dec2 mm,dec2 ss,wait("."),dec3 sss,_ ;we wait for 090045.000 which is the time when we got the info
wait(","),fix,_ ;we wait for A
wait(","),dec2 degrees,dec2 minutes,wait("."),dec4 minutesd,_ ;we wait for 3823.6645
wait(","),dir,_ ;we wait for N/S ;wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dir2,_ ;we wait for E/W
wait(","),wait(","),_
wait(","),dec2 day,dec2 month,dec2 year,_ ;we wait for 170518
wait(","),SKIP 3]

if fix = "V" then gps

'serin2 gps_tx,84,timeout,lostcable,[wait("GNVTG"),_
HSERIN2[wait("GPVTG"),wait("N"),_
wait(","),TS[0],TS[1],TS[2],TS[3],TS[4],_
wait(","),SKIP 3]

for i = 1 to 3
if TS[i]="." then decimal = i 'TOTAL SPEED. find which character is the decimal point
next i

select case decimal 'if the gps shows 1.45 it may be 145
'for the conversion ASCII to number we can use the -48
case 1 'decimal position is x.xx
SPEED = (10*(TS[0]-48))+(TS[2]-48)
case 2 'decimal position is xx.xx
SPEED = (100*(TS[0]-48))+(10*(TS[1]-48))+(TS[3]-48)
case 3 'decimal position is xxx.xx
SPEED = (1000*(TS[0]-48))+(100*(TS[1]-48))+(10*(TS[2]-48))
end select

gosub OVERSPEED

HSEROUT [$73,$05,$06,$11,$FF,$E0," SPEED",$00]
Hserin [wait(6)]
HSEROUT [$73,$06,$05,$12,$6D,$BF,TS[0],TS[1],TS[2],TS[3],$00]
Hserin [wait(6)]
HSEROUT [$73,$06,$06,$12,$FF,$E0,"Km/h",$00]
Hserin [wait(6)]

goto main

'-----------------------------------------------------------------------------/
' [ SUB ROUTINES ] /
'---------------------------------------------------------------------------/

OVERSPEED:

if SPEED > 1000 then ' 100Km/h
high BLUE
low GREEN
low RED
else
LOW BLUE

if SPEED > 500 then ' 50Km/h
high GREEN
low RED
else
LOW GREEN

if SPEED > 100 then ' 10Km/h
high RED
else
LOW RED

endif
endif
endif
return

GPS:
HSEROUT [$45] ' clears the LCD
Hserin [wait(6)]

HSEROUT [$73,$00,$03,$11,$6D,$BF," Waiting for GPS",$00]
Hserin [wait(6)]
HSEROUT [$73,$04,$07,$12,$F8,$00,"NOT FIXED",$00]
Hserin [wait(6)]
pause 3000

HSEROUT [$45] ' clears the LCD
Hserin [wait(6)]

goto main 'returns to notfix
'---[INT - interrupt handler]---------------------------------------------------

ToggleLED:
TOGGLE pwrled
@ INT_RETURN

tumbleweed
- 13th July 2018, 15:45
Try changing this:

DEFINE HSER_CLROERR2 1 ' Clear overflow automatically


to this:

DEFINE HSER2_CLROERR 1 ' Clear overflow automatically


You'll definitely be getting overrun errors, so if they're not cleared properly it'll stop receiving.

I'd also try some a LOT simpler to start with. I'd be amazed if those HSERIN2 commands in your main loop work reliably... one thing out of place and it'll likely take a long time to sync up again (if ever).

astanapane
- 13th July 2018, 17:23
Hi tumbleweed,

that was the typo mistake really. Now it works. thanks a lot.

I was stuck for a day! Now i can move forward to the next target. :)

Really appreciate your help.

astanapane
- 14th July 2018, 22:53
Dear all,

as for the last step, changing the baudrate of the GPS on the fly, the same time as the PIC chip it self on PORT EUART 2, i've notice that not all the time the configuration work.

Sometimes the GPS doesn't get the command. I tried to see if i could get an ACK but couldn't see anything coming on RX.

Base on the manufacturer datasheet there should be a reply message for every command we send. But really didn't see any reply.

Is there anything i could do, to help the GPS to receive always the command and not "reject" it? Should i give more time at start up?

I have already place the following commands on the code in order to check if the GPS has successfully powered up.


' -----------------------------------------------------------------------------|
' [ GPS Initialization ] |
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.
HSERIN2 [wait("PMTK011,MTKGPS*08")]
HSERIN2 [wait("PMTK010,001*2E")]
HSERIN2 [wait("PMTK010,002*2D")]

richard
- 15th July 2018, 03:45
I'd be amazed if those HSERIN2 commands in your main loop work reliably

I agree with tumbleweeds thought, increasing baud rate to 38k cuts the time available to process that streamed data to one quarter of what was available at 9600 baud . its asking for trouble .
what possible advantage are you seeking with the higher baud rate ?
there is great merit in sending data to the serial display as fast as possible but I have difficulty seeing any benefit in getting nmea data at higher speeds.

now that you have a chip with adequate memory and horsepower why not use a buffer to store and process the sentences?

astanapane
- 15th July 2018, 07:36
I agree with tumbleweeds thought, increasing baud rate to 38k cuts the time available to process that streamed data to one quarter of what was available at 9600 baud . its asking for trouble .
what possible advantage are you seeking with the higher baud rate ?
there is great merit in sending data to the serial display as fast as possible but I have difficulty seeing any benefit in getting nmea data at higher speeds.

now that you have a chip with adequate memory and horsepower why not use a buffer to store and process the sentences?

Hi Richard, to be honest i dont know the way to store and process the sentences , but i will search for it and come back with some question. First i will try to understand the concept.

Now at the moment i need to speed up the serial communication because i need to use to fastest refresh rate of NMEA data from GPS at 10Hz.

As from the manufacturer and datasheet of the GPS, if you need to go at 5Hz or 10Hz then you need at least to speed up the GPS communication rate at 38400.

The SERIN command inside the main look, do not have any issue. Only at the beginning of the initialization and changing the baudrate and refresh rate.

Apart from that everything else works fine at that rate. At the end of the project which may not be able to complete, i will not use the GPS at 10Hz.

Now i do some fancy test to see how fast the Speed on the Display can be refreshed. As you better understand than me, in order to achieve that, the GPS data must be at a very high rate and the serial communication as well. Actually is a simple speedometer.

Thanks once again all of you for your time and effort to help me.

tumbleweed
- 15th July 2018, 10:38
Do you control power to the GPS?

The sequence of what to do depends on who powers up first. You probably shouldn't assume that the mcu is powered up and initialized before the GPS. It would be good to know how long the GPS takes to powerup and start sending those initial strings.

The sequence might look something like:

- wait several seconds at powerup for the GPS to initialize
leave the uart set for 9600 baud
You'll likely miss any initial serial transmissions that the GPS sends you, but that's ok.

- send a string to the GPS that you know the response for, and wait for that response with a timeout.
The simpler the better... a single response that's easy to parse would work a lot better than a sequence of responses.
loop doing this until you have sync with the GPS.

- now you can switch to 38400

astanapane
- 15th July 2018, 11:37
Do you control power to the GPS?

The sequence of what to do depends on who powers up first. You probably shouldn't assume that the mcu is powered up and initialized before the GPS. It would be good to know how long the GPS takes to powerup and start sending those initial strings.

The sequence might look something like:

- wait several seconds at powerup for the GPS to initialize
leave the uart set for 9600 baud
You'll likely miss any initial serial transmissions that the GPS sends you, but that's ok.

- send a string to the GPS that you know the response for, and wait for that response with a timeout.
The simpler the better... a single response that's easy to parse would work a lot better than a sequence of responses.
loop doing this until you have sync with the GPS.

- now you can switch to 38400

Hi tumbleweed,

i think i could control the GPS module via Enable pin.

The GPS based on the datasheet, powerup and start sending commands after the following PMTKs.


' -----------------------------------------------------------------------------|
' [ GPS Initialization ] |
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.
HSERIN2 [wait("PMTK011,MTKGPS*08")]
HSERIN2 [wait("PMTK010,001*2E")]
HSERIN2 [wait("PMTK010,002*2D")]

I guess that by using the above code, i wait for each of this commands, and the program only continues if those PMKT sentences from GPS has been received and read. Is that right?

May i use a timeout in there?

tumbleweed
- 15th July 2018, 13:30
i think i could control the GPS module via Enable pin.
Does the Enable act like a reset? Do you have a link to the datasheet?

Maybe you could keep the GPS disabled until the main uC powers up and is ready...

astanapane
- 15th July 2018, 17:10
Hmm i need to check that. For the datasheet the gps module is from the MKT with MK3339 chip in it.

Datasheet is on the following Page on the Links mention PA6H
https://www.adafruit.com/product/790

astanapane
- 15th July 2018, 18:13
Enable pin has only the MKT3333 chip, which i also have but i wont use at the moment the MK3333 chip.

http://www.alphamicrowireless.com/media/513113/globaltop-gms-g9-datasheet-v0e.pdf

tumbleweed
- 15th July 2018, 21:33
Both of those have an NRESET input which is a low-active signal that will reset the module.
Connect that to one of the pic's IO pins (as long as you're powering the pic at 3.3V).

At startup, init the uart to 9600.
Then set NRESET low, delay 1msec, and set it high. They don't have any timing specs for the NRESET input, but that should do it.

After you get the initial sequence from the GPS then you can switch it to 38400.

astanapane
- 15th July 2018, 22:01
Both of those have an NRESET input which is a low-active signal that will reset the module.
Connect that to one of the pic's IO pins (as long as you're powering the pic at 3.3V).

At startup, init the uart to 9600.
Then set NRESET low, delay 1msec, and set it high. They don't have any timing specs for the NRESET input, but that should do it.

After you get the initial sequence from the GPS then you can switch it to 38400.

Thanks a lot, i'll do that tomorrow and i will post the result. :)

astanapane
- 16th July 2018, 10:46
I was checking the manual but at the same time the pcb board, which is an adafruit ultimate GPS. They do have an ENABLE pin but this is not coming from the GPS module but from the mic5225 3.3V legulator.

So i guess that via EN pin on the regulator we can hold the pin to GND or LOW for a 1ms and will do the job. I think it will work. Thanks a lot once again.

I would also like to thank you all, because i learn everyday more things step by step.

Apologize for my limited knowlegde, i know that i cannot help you, and it is difficult for you to understand my questions in order to help me as well.

Appreciate your patience.....

tumbleweed
- 16th July 2018, 14:11
The EN to the regulator should work as well, but since that will shut off power to the GPS you should make sure to set any IO pins that are outputs to the GPS to tri-state (ie inputs), or drive them low while you have the power off.

For the UART TX output that means disabling the uart to get control of the IO pins.

Also, you may need to pull EN low for longer than 1ms. When the regulator shuts off it'll probably take a while for the GPS 3.3V to drop.

astanapane
- 16th July 2018, 19:04
What i did, is to have all ports low, until the LCD is powered up. Then i set one of the PIC's port to high to enable the GPS's regulator.

That's all and it works up to now fine. Should i leave the PIC's pin to high? Or should i add a transistor and use it as a switch to control the EN pin of the regulator? So setting the PIC's pin to high, close the transistor EC to GND, and the EN pin is set to 0. Then release the PIC pin to LOW, and the same time open the EC from the transistor, and enable the GPS.

But with this method i will have one more component in my final circuit and couple of resistors at B and E.

EDITED:....................

I did make the portb.2 as an input after i powered up the GPS. It seems that works the same.


'------------------------------------------------------------------------------|
' [ GPS HOLD ON RESET for a bit ] |
'------------------------------------------------------------------------------|
high portb.2 ' we set high the ENABLE pin at GPS |
' -----------------------------------------------------------------------------|
' [ GPS Initialization ] |
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.
HSERIN2 [wait("PMTK011,MTKGPS*08")]
HSERIN2 [wait("PMTK010,001*2E")]
HSERIN2 [wait("PMTK010,002*2D")]

HSEROUT [$73,$01,$03,$00,$ff,$ff,"GPS is ENABLED",$00]
Hserin [wait(6)]
pause 1500
HSEROUT [$45] ' clear the LCD
Hserin [wait(6)]

'------------------------------------------------------------------------------|
input portb.2 'release the pic's pin and the ENABLE pin of the GPS's regulator|
'------------------------------------------------------------------------------|

tumbleweed
- 16th July 2018, 20:55
Sorry, I should have explained better.

The PIC IO pin is fine for driving the regulator ENable input... you shouldn't need any other circuitry. Set PORTB.2 as an output low to power off the GPS, and then set it high to turn on the GPS. Leave it as an output high, otherwise the regulator may shut itself off.

However, when you have the GPS power off you must make sure that NONE of the PIC output pins that connect to the GPS are high, otherwise the GPS could get partially powered from those logic lines that are sourcing 3.3V.

The UART TX output pin idles high once the UART is enabled (RCSTA2.SPEN, bit 7 = 1), which I think happens during initialization. With SPEN = 0 the uart TX and RX pins are under control of the ports.

astanapane
- 16th July 2018, 21:35
Sorry, I should have explained better.

The PIC IO pin is fine for driving the regulator ENable input... you shouldn't need any other circuitry. Set PORTB.2 as an output low to power off the GPS, and then set it high to turn on the GPS. Leave it as an output high, otherwise the regulator may shut itself off.

However, when you have the GPS power off you must make sure that NONE of the PIC output pins that connect to the GPS are high, otherwise the GPS could get partially powered from those logic lines that are sourcing 3.3V.

The UART TX output pin idles high once the UART is enabled (RCSTA2.SPEN, bit 7 = 1), which I think happens during initialization. With SPEN = 0 the uart TX and RX pins are under control of the ports.

Now it is clear. I did it as you advised. Thank a lot.

astanapane
- 25th July 2018, 07:32
i havent post for some days now, because i have changed the Display from 1'44 to 3.2 and the old serial interface code from 1'44 to SPE as 4D Systems calls it.

I have changed also the GPS and the firmware which is configured at 115200 bps. Still the code is very simple.


'************************************************* *********************
'* Name : 18F26K22.BAS *
'* Author : [Leonardo Bilalis] *
'* Notice : Copyright (c) 2018 [Leonardo Bilalis] *
'* : All Rights Reserved *
'* Date : 18/Jul/2018 *
'* Version : 1.0 *
'* Notes : This is a GPS Tracker based on the following components *
'* : PIC18F26K22 or PIC18F46K22 @ 64Mhz *
'* : MKT 3333 or 3339 GPS MODULE *
'* : 4D Systems ULCD Goldelox or Picasso Graphic Processor *
'************************************************* *********************

'************************************************* *********************
'* AXN5.1.1_8524_3333_1152.1151100.1 *
'************************************************* *********************

' --------------------[ GPS Variables for NMEA sentenses ]---------------------/
' /
' $GPRMC,090045.000,A,3823.6645,N,02353.3600,E,0.02, 195.80,170518,,,A*62 /
' $GNGGA,140405.000,3823.6010,N,02353.3054,E,1,9,0.8 8,0.8,M,35.9,M,,*40 /
' $GPGSA,A,3,18,08,10,11,14,27,22,32,01,,,,2.62,1.11 ,2.37*03 /
' $GPVTG,215.60,T,,M,0.44,N,0.82,K,A*37 /
' /
'-----------------------------------------------------------------------/

' When the GPS Module is POWERED ON, then the following sentenses are received.

' $PMTK011,MTKGPS*08
' $PMTK010,001*2E
' $PMTK010,002*2D


'************************************************* ********************************
@ ERRORLEVEL -306 ; this command prevents the compiler to give you a notice of *
; crossing page boundary - make sure bits are set *
'************************************************* ********************************

#CONFIG ; The PBP configuration for the PIC18F26K22 is:
; CONFIG FOSC = RCIO6 ; External RC oscillator
CONFIG FOSC = HSHP ; HS oscillator (high power &gt; 16 MHz)


;*---------------------------------4x PLL ENABLE--------------------------------------*|
;* 4X PLL Enable *|
;* CONFIG PLLCFG = OFF ;Oscillator used directly *|
CONFIG PLLCFG = ON ;Oscillator multiplied by 4 *|
;*------------------------------------------------------------------------------------*|
;
; Primary clock enable bit
; CONFIG PRICLKEN = OFF ;Primary clock can be disabled by software
CONFIG PRICLKEN = ON ;Primary clock enabled
;
; Fail-Safe Clock Monitor Enable bit
CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
; CONFIG FCMEN = ON ;Fail-Safe Clock Monitor enabled
;
; Internal/External Oscillator Switchover bit
CONFIG IESO = OFF ;Oscillator Switchover mode disabled
; CONFIG IESO = ON ;Oscillator Switchover mode enabled

CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)

;*--------------------------------------------------------------------------------------------------------;|
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect ;|
CONFIG WDTPS = 32768 ; 1:32768 ---> HERE enable the watchdog timer with a 1:32768 postscale;|
;*--------------------------------------------------------------------------------------------------------;|

CONFIG PWRTEN = ON
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Disabled

;*----------------------------------------------------------------------------------|
;*---------------Available configuration settings for PIC18F26K22-------------------|
;*----------------------------------------------------------------------------------|
;
; Oscillator Selection bits
; CONFIG FOSC = RC ;111X External RC oscillator, CLKOUT function on RA6
; CONFIG FOSC = ECLPIO6 ;EC oscillator (low power, &lt;500 kHz)
; CONFIG FOSC = ECLP ;EC oscillator, CLKOUT function on OSC2 (low power, &lt;500 kHz)
; CONFIG FOSC = ECMPIO6 ;EC oscillator (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = ECMP ;EC oscillator, CLKOUT function on OSC2 (medium power, 500 kHz-16 MHz)
; CONFIG FOSC = INTIO7 ;Internal oscillator block, CLKOUT function on OSC2
; CONFIG FOSC = INTIO67 ;Internal oscillator block
; CONFIG FOSC = RCIO6 ;External RC oscillator
; CONFIG FOSC = RC ;External RC oscillator, CLKOUT function on OSC2
; CONFIG FOSC = ECHPIO6 ;EC oscillator (high power, &gt;16 MHz)
; CONFIG FOSC = ECHP ;EC oscillator, CLKOUT function on OSC2 (high power, &gt;16 MHz)
; CONFIG FOSC = HSMP ;HS oscillator (medium power 4-16 MHz)
; CONFIG FOSC = HSHP ;HS oscillator (high power &gt; 16 MHz)
; CONFIG FOSC = XT ;XT oscillator
; CONFIG FOSC = LP ;LP oscillator

;*--------------------------------------------------------------------------------------------------------|
;* | -------------------------- | |
;* ----------------------------- | [PROTECTED OPTION FUSES] | ------------------------------------ |
;* | -------------------------- | |
;*--------------------------------------------------------------------------------------------------------|

CONFIG CP0 = OFF ; Block 0 (000800-003FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot Block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

#ENDCONFIG

;*---------------------------------------------------------------------------------------------------------|
;*---------------------------------------------------------------------------------------------------------|

define OSC 64

INCLUDE "modedefs.bas"
INCLUDE"ALLDIGITAL.pbp"

OSCCON = %01110000 ; 64Mhz
OSCTUNE.6 = 1 ; Enable 4x PLL

while ! osccon2.7 :WEND ; to make sure the pll has stabilised before you run any other code


'------------------------------------------------------------------------------|
' INITIALIZE RAM |
'------------------------------------------------------------------------------|
initialize:
CLEAR

'-------------------------------------------------------------------------------------------------------------------------------------------------------------

TRISA = %00000000 'use TRISA to specify which pin is (1 = input) and which (0 = output) (ALL pins are output)
TRISB = %10000000 'use TRISB to specify which pin is (1 = input) and which (0 = output) (RB7 PORTB.7 is the RX input pin of the EUART, the rest are output)
TRISC = %00000000 'use TRISC to specify which pin is (1 = input) and which (0 = output) (RC7 PORTC.7 is the RX input pin of the EUART, the rest are output)
'in PORTC the RC3 attached is an LED.

'------------------------------------------------------------------------|
'--------------------------- TRIS A B C ---------------------------------|
'------------------------------------------------------------------------|
' PORTA.7 PORTA.6 PORTA.5 PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 0 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|
'------------------------------------------------------------------------|
' PORTC.7 PORTC.6 PORTC.5 PORTC.4 PORTC.3 PORTC.2 PORTC.1 PORTC.0 |
' bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 |
' 1 0 0 0 0 0 0 0 |
'------------------------------------------------------------------------|

'------------------------------------------------------------------------|
'----------------------- At start all PORTS LOW -------------------------|
'------------------------------------------------------------------------|
PORTA = 0 'make low all ports at A range |
PORTB = 0 'make low all ports at B range |
PORTC = 0 'make low all ports at C range |
PORTE = 0 'make low all ports at E range |
'------------------------------------------------------------------------|

'------------------------------------------------------------------------|
'-------------------------- COMPARATORS OFF -----------------------------|
'------------------------------------------------------------------------|
CM1CON0.7 = 0 'Disable comparator1 |
CM2CON0.7 = 0 'Disable comparator2 |
'------------------------------------------------------------------------|

'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 1 Configuration | ---------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|


'RCSTA = $90 ' Enable serial port & continuous receive
'TXSTA = $24 ' Enable transmit, BRGH = 1
'DEFINE HSER_CLROERR 1 ' Clear overflow automatically
'SPBRG = 130 ' 9600 Baud @ 64MHz, -0,02%
'SPBRGH = 6
'BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
'BAUDCON.5 = 1
'BAUDCON = %10001000

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 138 ' 115200 Baud @ 64MHz, -0,08%
SPBRGH = 0
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator


'*-----------------------------------------------------------------------------|
'* | --------------------- | |
'*----------------------- | EUART 2 Configuration | ---------------------------|
'* | --------------------- | |
'*-----------------------------------------------------------------------------|

DEFINE HSER2_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER2_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER2_CLROERR 1 ' Clear overflow automatically
DEFINE HSER2_SPBRG 160 ' 38400 Baud @ 64MHz, -0,08%
SPBRGH2 = 1
BAUDCON2.3 = 1 ' Enable 16 bit baudrate generator

'*-----------------------------------------------------------------------------|
'*-----------------------------------------------------------------------------|

PWRLED var PORTA.0 ' indicates that PIC is working on port C.4



'------------------------------------------------------------------------------|
' FONTS FROM THE uSD |
'------------------------------------------------------------------------------|
'file_LoadImageControl[0009 "FONTST~2.d01" "FONTST~2.g01" 0001] 0,033 (ACK 5148 0x141C)
'file_LoadImageControl[0009 "FONTST~2.d02" "FONTST~2.g02" 0001] 0,038 (ACK 4845 0x12ED)
'file_LoadImageControl[0009 "FONTST~2.d03" "FONTST~2.g03" 0001] 0,037 (ACK 4542 0x11BE)
'file_LoadImageControl[0009 "FONTST~2.d04" "FONTST~2.g04" 0001] 0,039 (ACK 4239 0x108F)
'file_LoadImageControl[0009 "FONTST~2.d05" "FONTST~2.g05" 0001] 0,042 (ACK 3936 0x0F60)
'file_LoadImageControl[0009 "FONTST~2.d06" "FONTST~2.g06" 0001] 0,038 (ACK 3633 0x0E31)
'file_LoadImageControl[0009 "FONTST~2.d07" "FONTST~2.g07" 0001] 0,040 (ACK 3330 0x0D02)
'file_LoadImageControl[0009 "FONTST~2.d08" "FONTST~2.g08" 0001] 0,045 (ACK 3027 0x0BD3)

' -----------------------------------------------------------------------------|
' [ LCD Initialization ] |
'------------------------------------------------------------------------------|
' FIRST TIME BOOTUP: We give plenty of time for tbe LCD '

pause 3000
high pwrled

HSerout2[$FF,$9E,$00,$01] 'SPE serial graphic interface -SET THE LANDSCAPE R
Hserin2 [wait(6)]
HSEROUT2 [$FF,$03] ' MOUNT FILE, this is very imporntat command in here.
Hserin2 [wait(6)]
'HSEROUT2[$00,$09,"FONTST~2.d07","FONTST~2.g07",$00,$01] ' SPE serial graphic interface - CALL FONT from SD
'Hserin2 [wait(6)]

HSEROUT2 [$FF,$00,$00,$46] ' SOUND VOLUME to 70
Hserin2 [wait(6)]
Pause 100

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$07,$7F] ' SPE serial graphic interface - SET COLOR (CYAN)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$01,$00,$02] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
pause 100
hserout2[$00,$18,"Leonardo Bilalis",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$03,$00,$03] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
pause 100
hserout2[$00,$18,"Copyright 2018",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$05,$00,$05] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (YELLOW)
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
pause 100
hserout2[$00,$18,"GPS SYSTEM",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$07,$00,$03] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$01] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$01] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$07,$E0] ' SPE serial graphic interface - SET COLOR (LIME)
Hserin2 [wait(6)]
hserout2[$00,$18,"G",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"P",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"S",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18," M",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"K",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"T",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18," 3",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"3",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"3",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"9",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18," a",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"n",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"d",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18," P",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"I",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"C",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"1",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"8",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"F",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"2",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"6",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"K",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"2",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
HSEROUT2 [$00,$0B,"SOUND.WAV",$00]
'HSEROUT2 [$00,$0B,"LASER2.WAV",$00]
Hserin2 [wait(6)]
hserout2[$00,$18,"2",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]
pause 3000
HSEROUT2 [$FF,$CD] ' clear the LCD
Hserin2 [wait(6)]


'------------------------------------------------------------------------------|
' [ GPS HOLD ON RESET for a bit ] |
'------------------------------------------------------------------------------|
high portc.3 ' we set high the ENABLE pin at GPS |

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$07,$E0] ' SPE serial graphic interface - SET COLOR (LIME)
Hserin2 [wait(6)]
'HSEROUT2[$FF,$DE,$00,$01] ' SPE serial graphic interface - SETO BOLD
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$01,$00,$00] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
hserout2[$00,$18,"Powering up the GPS",$00] 'SPE serial graphic interface - PUT STRING
Hserin2 [wait(6)]

' -----------------------------------------------------------------------------|
' [ GPS Initialization ] |
'------------------------------------------------------------------------------|
' When the GPS Module is POWERED ON, then the following sentenses are received.
HSERIN [wait("PMTK011,MTKGPS*08")]
HSERIN [wait("PMTK010,001*2E")]
HSERIN [wait("PMTK010,002*2D")]
HSEROUT2[$FF,$E9,$00,$03,$00,$03] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"GPS is ENABLED",$00]
Hserin2 [wait(6)]
pause 1500
'------------------------------------------------------------------------------|
' Changing Baudrate and Frequency |
'------------------------------------------------------------------------------|
HSEROUT ["$PMTK314,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28",13,10]
'HSEROUT["$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0*29",13,10] ' ZDA ONLY
pause 100

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$01] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$01] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$FF] ' SPE serial graphic interface - SET COLOR (WHITE)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$0C,$00,$02] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
hserout2[$00,$18,"NMEA:",$00]
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$0F,$00,$01] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$07,$7F] ' SPE serial graphic interface - SET COLOR (CYAN)
Hserin2 [wait(6)]
hserout2[$00,$18,"RMC+VTG",$00]
Hserin2 [wait(6)]


'HSEROUT["$PMTK251,38400*27",13,10] ' 38400 boundrate, check MTK manual configuration
'pause 100

'HSEROUT2[$FF,$E9,$00,$0C,$00,$0F] ' SPE serial graphic interface - SET Y,X position
'Hserin2 [wait(6)]
'HSEROUT2[$FF,$E7,$FF,$FF] ' SPE serial graphic interface - SET COLOR (WHITE)
'Hserin2 [wait(6)]
'hserout2[$00,$18,"Baudrate:",$00]
'Hserin2 [wait(6)]
'HSEROUT2[$FF,$E9,$00,$0F,$00,$0F] ' SPE serial graphic interface - SET Y,X position
'Hserin2 [wait(6)]
'HSEROUT2[$FF,$E7,$07,$7F] ' SPE serial graphic interface - SET COLOR (CYAN)
'Hserin2 [wait(6)]
'hserout2[$00,$18,"38400 bps",$00]
'Hserin2 [wait(6)]

'SPBRG = 160 ' 38400 Baud @ 64MHz, -0,08%
'SPBRG2 = 21 ' 57600 Baud @ 64MHz, -0,08%
'SPBRG2 = 138 ' 115200 Baud @ 64MHz, -0,08%
'SPBRGH = 1

HSEROUt["$PMTK220,100*2F",13,10] ' 100(millisecond)=0.1(sec)-->1/0.1= 10Hz
pause 100

HSEROUT2[$FF,$E9,$00,$0C,$00,$1B] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$FF] ' SPE serial graphic interface - SET COLOR (WHITE)
Hserin2 [wait(6)]
hserout2 [$00,$18,"Refresh Rate:",$00]
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$0F,$00,$20] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$07,$7F] ' SPE serial graphic interface - SET COLOR (CYAN)
Hserin2 [wait(6)]
hserout2 [$00,$18,"10Hz",$00]
Hserin2 [wait(6)]


pause 3000

HSEROUT2 [$FF,$CD] ' clears the LCD
Hserin2 [wait(6)]
'*-----------------------------------------------------------------------------|
timeout con 2000
hh var byte 'hours
mm var byte 'minutes
ss var byte 'seconds
sss var word 'milisecs
degrees VAR BYTE 'latitude/longitude degrees
minutes VAR BYTE 'latitude/Longitude minutes
minutesD VAR word 'latitude/LONGITUDE DECIMAL MINUTES
dir VAR BYTE 'direction (latitude: 0 = N, 1 = S / longitude: 0 = E, 1 = W)
degrees2 VAR BYTE 'latitude/longitude degrees
minutes2 VAR BYTE 'latitude/Longitude minutes
minutesD2 VAR word 'latitude/LONGITUDE DECIMAL MINUTES
dir2 var byte 'direction (latitude: 0 = N, 1 = S / longitude: 0 = E, 1 = W)
SatNo VAR BYTE 'number of satellites connected
day var byte 'day
month var byte 'month
year var byte 'year
SPEED var word 'speed with conversion ASCII to dec -48
Km var byte 'this is a letter K --> KM/H om the GPVTG sentence
TS var byte[4] 'TOTAL SPEED. we create an array to combine each possible character for speed
decimal var byte
i var word 'we use this for Total speed point
FIX var byte 'fix sat V/A
Modefix var byte 'mode fix 1 or 2 or 3 depens
Day_added var byte
Numofdays var byte 'these are the days of each months
LeapYear var byte 'this is a year leap
ndays var byte[13]'create an array with 13 byte location for Months values
mode2d3d var byte

RED var PORTA.3
GREEN var PORTA.2
BLUE VAR PORTA.1


'--------------------------------------------------------------------------------|
' |
' ---------------------------- [ MAIN ] ----------------------------------- |
' |
'--------------------------------------------------------------------------------|

Main:

'-------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GNRMC ] /
' [$GNRMC,090045.000,A,3823.6645,N,02353.3600,E,0.02, 195.80,170518,,,A*62] /
' /
'--------------------------------------------------------------------------/
;serin2 gps_tx,84,timeout,lostcable,[wait("$GNRMC"),_ ;we wait for $GNRMC
HSERIN timeout,lostcable,[wait("$GNRMC"),_ ;we wait for $GPRMC
wait(","),dec2 hh,dec2 mm,dec2 ss,wait("."),dec3 sss,_ ;we wait for 090045.000 which is the time when we got the info
wait(","),fix,_ ;we wait for A
wait(","),dec2 degrees,dec2 minutes,wait("."),dec4 minutesd,_ ;we wait for 3823.6645
wait(","),dir,_ ;we wait for N/S ;wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dec3 degrees2,dec2 minutes2,wait("."),dec4 minutesd2,_;we wait for 02353.3600
wait(","),dir2,_ ;we wait for E/W
wait(","),wait(","),_
wait(","),dec2 day,dec2 month,dec2 year,_ ;we wait for 170518
wait(","),SKIP 3]

'------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GNGGA ] /
' [$GNGGA,140405.000,3823.6010,N,02353.3054,E,1,9,0.8 8,0.8,M,35.9,M,,*40 /
' /
'-------------------------------------------------------------------------/
'serin2 gps_tx,84,timeout,lostcable,[wait("$GNGGA"),_ ;we wait for GNGGA
HSERIN timeout,lostcable,[wait("$GNGGA"),_ ;we wait for GPGGA
wait(","),skip 41,_
wait(","),dec2 satno,_
wait(","),skip 19]

'wait(","),modefix,_ ;we wait for modefix 1,2 or 3 check gps manual for NMEA
'------------------------------------------------------------------------------/
' /
' [ Example NMEA Sentense of GPGSA ] /
' [$GPGSA,A,3,18,08,10,11,14,27,22,32,01,,,,2.62,1.11 ,2.37*03] /
' /
'-------------------------------------------------------------------------/

HSERIN timeout,lostcable,[wait("$GPGSA"),wait(","),wait(","),mode2d3d,wait(","),SKIP 50]

if fix = "V" then gps

'serin2 gps_tx,84,timeout,lostcable,[wait("GNVTG"),_
HSERIN timeout,lostcable,[wait("GNVTG"),wait("N"),_
wait(","),TS[0],TS[1],TS[2],TS[3],TS[4],_
wait(","),SKIP 3]

for i = 1 to 3
if TS[i]="." then decimal = i 'TOTAL SPEED. find which character is the decimal point at ulcd fort is the ":"
next i

select case decimal 'if the gps shows 1.45 it may be 145
'for the conversion ASCII to number we can use the -48
case 1 'decimal position is x.xx
SPEED = (10*(TS[0]-48))+(TS[2]-48)
case 2 'decimal position is xx.xx
SPEED = (100*(TS[0]-48))+(10*(TS[1]-48))+(TS[3]-48)
case 3 'decimal position is xxx.xx
SPEED = (1000*(TS[0]-48))+(100*(TS[1]-48))+(10*(TS[2]-48))
end select

gosub OVERSPEED
'-----------------------------------------------------------------------------/
' Here we start the calendar conversion /
'---------------------------------------------------------------------------/

arraywrite ndays,13,NDAY,[0,31,28,31,30,31,30,31,31,30,31,30,31]
if ndays = 2 then
if ((year//4 = 0) and (year//400 != 0)) then 'check the leap year
ndays[2] = 29 'then February has 29 days
else
ndays[2] = 28 'else has 28 days
endif
endif
hh = hh + 3 'the Hour from GPS is UTC so for our country in Greece we add +3
if hh>23 then 'if the hh+3 hour is greater than 23 (23:00) then
day = day + 1 'we check the day added is true
hh = hh//24 'but the hour not exceed the 24 so go to 00:00
if (day > ndays[month]) then
month = month + 1
day = 1
if (month > 12) then
year = year + 1
month = 1
endif
endif
endif

'--------------------------------------------------------------------------/
' here is where the code dispays the stored info from the NMEA sentenses /
'------------------------------------------------------------------------/

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$FF] ' SPE serial graphic interface - SET COLOR (WHITE)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$02,$00,$04] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"GROUND SPEED",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E9,$00,$06,$00,$08] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"Km/h",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E7,$07,$E0] ' SPE serial graphic interface - SET COLOR (LIME)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$05] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$06] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$01,$00,$02] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,TS[0],TS[1],TS[2],TS[3],$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (Yellow)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$00,$00,$00] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,dec2 hh,":",dec2 mm,":",dec2 ss,$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (Yellow)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$00,$00,$0C] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,dec2 day,"/",dec2 month,"/",dec2 year,$00]
Hserin2 [wait(6)]


HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$04,$00,$00] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"Sats",$00]
Hserin2 timeout,lostcable,[wait(6)]

HSEROUT2[$FF,$E7,$07,$FF] ' SPE serial graphic interface - SET COLOR (AQUA)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$05,$00,$01] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,dec2 satno,$00]
Hserin2 timeout,lostcable,[wait(6)]

HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$04,$00,$11] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"Fix",$00]
Hserin2 timeout,lostcable,[wait(6)]

HSEROUT2[$FF,$E7,$07,$FF] ' SPE serial graphic interface - SET COLOR (AQUA)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$05,$00,$011] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,fix,mode2d3d,"D",$00]
Hserin2 timeout,lostcable,[wait(6)]

HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$08,$00,$01] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$00,$18,"Lat:",$00]
Hserin2 timeout,lostcable,[wait(6)]
HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (Yellow)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$08,$00,$06] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$00,$18,dec2 degrees,"°",dec2 minutes,".",dec4 minutesd,"'"," ",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E7,$07,$E0] ' SPE serial graphic interface - SET COLOR (LIME)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$08,$00,$11] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$00,$18,dir,$00]
Hserin2 [wait(6)]


HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$09,$00,$01] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$00,$18,"Lon:",$00]
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (Yellow)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$09,$00,$05] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$00,$18,dec3 degrees2,"°",dec2 minutes2,".",dec4 minutesd2,"'"," ",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E7,$07,$FF] ' SPE serial graphic interface - SET COLOR (CYAN)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$09,$00,$11] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$00,$18,dir2,$00]
Hserin2 [wait(6)]


goto main

'-----------------------------------------------------------------------------/
' [ SUB ROUTINES ] /
'---------------------------------------------------------------------------/

OVERSPEED:

if SPEED > 1000 then ' 100Km/h
high BLUE
low GREEN
low RED
else
LOW BLUE

if SPEED > 500 then ' 50Km/h
high GREEN
low RED
else
LOW GREEN

if SPEED > 100 then ' 10Km/h
high RED
else
LOW RED

endif
endif
endif
return

GPS:
HSEROUT2 [$FF,$CD] ' clear the LCD
Hserin2 [wait(6)]

hh = hh + 3
hh = hh//24

HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$FF] ' SPE serial graphic interface - SET COLOR (WHITE)
Hserin2 [wait(6)]
HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E9,$00,$01,$00,$05] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"PLEASE WAIT",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E9,$00,$04,$00,$09] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"GPS",$00]
Hserin2 [wait(6)]

HSEROUT2[$FF,$E9,$00,$06,$00,$06] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"NOT FIXED",$00]
Hserin2 [wait(6)]
pause 3000

HSEROUT2 [$FF,$CD] ' clear the LCD
Hserin2 [wait(6)]

goto main 'returns to main

'-----------------------------------------------------------------------------/
' [ Connection lost Please check the GPS ] /
'---------------------------------------------------------------------------/

lostcable:

HSEROUT2 [$FF,$CD] ' clear the LCD
Hserin2 [wait(6)]


HSEROUT2[$FF,$E5,$00,$02] ' SPE serial graphic interface - SET FONT
Hserin2 [wait(6)]

HSEROUT2[$FF,$E4,$00,$02] 'SPE serial graphic interface - SET WIDTH of the text
Hserin2 [wait(6)]
HSEROUT2[$FF,$E3,$00,$02] ' SPE serial graphic interface - SET HEIGHT of the text
Hserin2 [wait(6)]

HSEROUT2[$FF,$E9,$00,$02,$00,$02] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$F8,$00] ' SPE serial graphic interface - SET COLOR (RED)
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"Connection lost",$00]
Hserin2 timeout,lostcable,[wait(6)]

HSEROUT2[$FF,$E9,$00,$04,$00,$01] ' SPE serial graphic interface - SET Y,X position
Hserin2 [wait(6)]
HSEROUT2[$FF,$E7,$FF,$E0] ' SPE serial graphic interface - SET COLOR (YELLOW)
Hserin2 [wait(6)]
HSEROUT2 [$00,$18,"Please check cable",$00]
Hserin2 timeout,lostcable,[wait(6)]

pause 2000

HSEROUT2 [$FF,$CD] ' clear the LCD
Hserin2 [wait(6)]

goto main

'-----------------------------------------------------------------------------/
' [ Calendar days in Months ] /
'---------------------------------------------------------------------------/
NDAY:
ndays[0] = 0
ndays[1] = 31 'January
ndays[2] = 28 'February
ndays[3] = 31 'March
ndays[4] = 30 'April
ndays[5] = 31 'May
ndays[6] = 30 'June
ndays[7] = 31 'July
ndays[8] = 31 'August
ndays[9] = 30 'September
ndays[10] = 31 'October
ndays[11] = 30 'November
ndays[12] = 31 'December