Helpful ReplyHot!Initial startup latency on the UART - parsing - display

Page: 12 > Showing page 1 of 2
Author
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
2017/09/13 16:21:35 (permalink)
0

Initial startup latency on the UART - parsing - display


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <p18lf26k22.h>
#include "spi_pic18f.h"
#include "time_delay.h"
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "font3.h"
#include "font1.h"

char c, temp; //temp dump
volatile unsigned int ping, isrcall, index, reading, new_rx;
int ready, gpgga, gprmc, mode; //gps associated vars
char buffer1[20];

struct gpsStruct {//GPRMC
int timeZone;
bool flagRead; // flag used by the parser, when a valid sentence has begun
bool flagDataReady; // valid GPS fix and data available, user can call reader functions
char words[20][15]; // hold parsed words for one given NMEA sentence
char szChecksum[15]; // hold the received checksum for one given NMEA sentence
// will be set to true for characters between $ and * only
bool flagComputedCks; // used to compute checksum and indicate valid checksum interval (between $ and * in a given sentence)
int checksum; // numeric checksum, computed for a given sentence
bool flagReceivedCks; // after getting * we start cuttings the received checksum
int index_received_checksum;// used to parse received checksum
// word cutting variables
int wordIdx; // the current word in a sentence
int prevIdx; // last character index where we did a cut
int nowIdx ; // current character index
// globals to store parser results
bool positionFixIndicator; //GPGGA
bool dataValid; //GPRMC
float longitude; // GPRMC and GPGGA
float latitude; // GPRMC and GPGGA
unsigned char UTCHour, UTCMin, UTCSec, // GPRMC and GPGGA
UTCDay, UTCMonth, UTCYear; // GPRMC
int satellitesUsed;// GPGGA
float dilution; //GPGGA
float altitude; // GPGGA
float speed; // GPRMC
float bearing; // GPRMC
};
struct gpsStruct gps;
/*
* Interrupt Service
*/
void interrupt ISR() {
if (PIR1bits.RCIF){ // see if interrupt caused by incoming data
isrcall = 0x01;
temp = RCREG1; // read the incoming data
ready = 1;
fuseDataGPS(temp);
}
}
void serial_init()
{
TXSTA1bits.BRGH1=1; // select low speed Baud Rate 
TXSTA1bits.TX9=0; // select 8 data bits
TXSTA1bits.TXEN = 1; // enable transmit
TXSTA1bits.SYNC = 0;

RCSTA1bits.SPEN=1; // serial port is enabled
RCSTA1bits.RX9=0; // select 8 data bits
RCSTA1bits.CREN=1; // receive enabled
PIR1bits.RCIF=0; // make sure receive interrupt flag is clear
BAUDCONbits.BRG16 = 1;
SPBRGH1 = 6;
SPBRG = 130;
__delay_ms(50); // give time for voltage levels on board to settle
}
void parseDataGPS(void){
// int received_cks = 16*digit2dec(gps.tmp_szChecksum[0]) + charToInt(gps.tmp_szChecksum[1]);
int received_cks = 16*charToI(gps.szChecksum[0]) + charToI(gps.szChecksum[1]);
//uart1.Send("seq: [cc:%X][words:%d][rc:%s:%d]\r\n", m_nChecksum,m_nWordIdx, tmp_szChecksum, received_cks);
// check checksum, and return if invalid!
if (gps.checksum != received_cks) {
//m_bFlagDataReady = false;
return;
}
/* $GPGGA
* $GPGGA,hhmmss.ss,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
* WORDS:
* 0 = $GPGGA - Gloabl Positioning System Fixed Data
* 1 = UTC of Position
* 2 = Latitude
* 3 = N or S
* 4 = Longitude
* 5 = E or W
* 6 = GPS quality indicator (0=invalid; 1=GPS fix; 2=Diff. GPS fix)
* 7 = Number of satellites in use [not those in view]
* 8 = Horizontal dilution of position
* 9 = Antenna altitude above/below mean sea level (geoid)
* 10 = Meters (Antenna height unit)
* 11 = Geoidal separation (Diff. between WGS-84 earth ellipsoid and mean sea level.
* -geoid is below WGS-84 ellipsoid)
* 12 = Meters (Units of geoidal separation)
* 13 = Age in seconds since last update from diff. reference station
* 14 = Diff. reference station ID#
* 15 = Checksum
*/
if (strcmp(gps.words[0], "$GPGGA") == 0) {
// Check GPS Fix: 0=no fix, 1=GPS fix, 2=Dif. GPS fix
if (gps.words[6][0] == '0') {
gps.positionFixIndicator = false;
// clear data
// gps.res_fLatitude = 0;
// gps.res_fLongitude = 0;
gps.flagDataReady = false;
return;
}

gps.positionFixIndicator = true;
// parse time
gps.UTCHour = charToI(gps.words[1][0]) * 10 + charToI(gps.words[1][1]);
gps.UTCMin = charToI(gps.words[1][2]) * 10 + charToI(gps.words[1][3]);
gps.UTCSec = charToI(gps.words[1][4]) * 10 + charToI(gps.words[1][5]);


// parse latitude and longitude in NMEA format
gps.latitude = atof(gps.words[2]);
gps.longitude = atof(gps.words[4]);
// get decimal format
if (gps.words[3][0] == 'S') gps.latitude *= -1.0;
if (gps.words[5][0] == 'W') gps.longitude *= -1.0;
float degrees = trunc(gps.latitude / 100.0f);
float minutes = gps.latitude - (degrees * 100.0f);
gps.latitude = degrees + minutes / 60.0f;
degrees = trunc(gps.longitude / 100.0f);
minutes = gps.longitude - (degrees * 100.0f);
gps.longitude = degrees + minutes / 60.0f;

// parse number of satellites
gps.satellitesUsed = (int)atof(gps.words[7]);

// parse horizontal dilution of precision
gps.dilution = atof(gps.words[8]);
// parse altitude
gps.altitude = atof(gps.words[9]);
// data ready
gps.flagDataReady = true;
}//$GPGGA
/* $GPRMC
* note: a siRF chipset will not support magnetic headers.
* $GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a*hh
* ex: $GPRMC,230558.501,A,4543.8901,N,02112.7219,E,1.50,181.47,230213,,,A*66,
*
* WORDS:
* 0 = $GPRMC - Recommended Minimum Specific GNSS Data
* 1 = UTC of position fix
* 2 = Data status (V=navigation receiver warning)
* 3 = Latitude of fix
* 4 = N or S
* 5 = Longitude of fix
* 6 = E or W
* 7 = Speed over ground in knots
* 8 = Track made good in degrees True, Bearing This indicates the direction that the device is currently moving in,
* from 0 to 360, measured in ?azimuth?.
* 9 = UT date
* 10 = Magnetic variation degrees (Easterly var. subtracts from true course)
* 11 = E or W
* 12 = Checksum
*/
if (strcmp(gps.words[0], "$GPRMC") == 0) {
// Check data status: A-ok, V-invalid
if (gps.words[2][0] == 'V') {
gps.dataValid = false;
// clear data
// gps.res_fLatitude = 0;
// gps.res_fLongitude = 0;
gps.flagDataReady = false;
return;
}
gps.dataValid = true;
gps.speed = atof(gps.words[7]);
gps.speed *= 1.15078; // convert to mph
// parse bearing
gps.bearing = atof(gps.words[8]);
// parse UTC date
gps.UTCDay = charToI(gps.words[9][0]) * 10 + charToI(gps.words[9][1]);
gps.UTCMonth = charToI(gps.words[9][2]) * 10 + charToI(gps.words[9][3]);
gps.UTCYear = charToI(gps.words[9][4]) * 10 + charToI(gps.words[9][5]);
// data ready
gps.flagDataReady = true;
}//$GPRMC
return;
}//parseDataGPS
fuseDataGPS(c){
if (c == '$') {
gps.flagRead = true;
// init parser vars
gps.flagComputedCks = false;
gps.checksum = 0;
// after getting * we start cuttings the received m_nChecksum
gps.flagReceivedCks = false;
gps.index_received_checksum = 0;
// word cutting variables
gps.wordIdx = 0; gps.prevIdx = 0; gps.nowIdx = 0;
}
if (gps.flagRead) {
// check ending
if (c == '\r' || c== '\n') {
// catch last ending item too
gps.words[gps.wordIdx][gps.nowIdx - gps.prevIdx] = 0;
gps.wordIdx++;
// cut received m_nChecksum
gps.szChecksum[gps.index_received_checksum] = 0;
// sentence complete, read done
gps.flagRead = false;
// parse
parseDataGPS();
} else {
// computed m_nChecksum logic: count all chars between $ and * exclusively
if (gps.flagComputedCks && c == '*') gps.flagComputedCks = false;
if (gps.flagComputedCks) gps.checksum ^= c;
if (c == '$') gps.flagComputedCks = true;
// received m_nChecksum
if (gps.flagReceivedCks) {
gps.szChecksum[gps.index_received_checksum] = c;
gps.index_received_checksum++;
}
if (c == '*') gps.flagReceivedCks = true;
// build a word
gps.words[gps.wordIdx][gps.nowIdx - gps.prevIdx] = c;
if (c == ',') {
gps.words[gps.wordIdx][gps.nowIdx - gps.prevIdx] = 0;
gps.wordIdx++;
gps.prevIdx = gps.nowIdx;
}
else gps.nowIdx++;
}
}
return;
}//fuseDataGPS

void main(void){
int i;
STS_LED = 1;
OSCCON = 0b01110000;
OSCTUNEbits.PLLEN = 1; // turn on the PLL 64 MHz
ANSELA = 0; ANSELB = 0; ANSELC = 0; //ANSELD = 0; ANSELE = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTE = 0; //PORTD = 0;
TRISA = 0; TRISB = 0; TRISC = 0; TRISE = 0; //TRISD = 0;
INTCONbits.GIE = 0; //no interruptions please
TRISCbits.TRISC6 = 1; // input
TRISCbits.TRISC7 = 1; // input
ping = 0;
new_rx = 0;
isrcall = 0;

SPI1_Close();
SPI1_Init();
TFT_Init(); 
STS_LED = 0;
serial_init();
Print --("Serial Port Initialised");
//RCONbits.IPEN = 0;
Print -- ("Searching for Satellites");
STS_LED = 1;
PIE1bits.RC1IE = 1; //Enable RX Interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupt
INTCONbits.GIE = 1; // enable global interrupt
ready = 0;
isrcall = 0;
ping = 0;
while (1)
{
if (ready){

STS_LED = 0;
sprintf(buffer1, "%f ", gps.latitude);
sprintf(buffer1, "%f ", gps.longitude);
sprintf(buffer1, "%02d ", gps.UTCHour);
sprintf(buffer1, "%02d ", gps.UTCMin);
sprintf(buffer1, "%02d ", gps.UTCSec); 
}
}
 
 

}
int charToI(c){ return (c - 48);}
double trunc(double d){ return (d>0) ? floor(d) : ceil(d) ; }

#1
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/13 16:35:07 (permalink)
+2 (2)
What is the question?
 
Note: Comes from http://www.microchip.com/forums/m1014447.aspx
It really would have been better to continue in the existing topic...
 
#2
Aussie Susan
Super Member
  • Total Posts : 2892
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/13 19:26:34 (permalink) ☄ Helpfulby Dartmoor 2017/09/14 08:04:54
+2 (2)
...and that code still has a lot of the issues pointed out to you in the other thread.
Susan
post edited by Aussie Susan - 2017/09/13 19:29:00
#3
CinziaG
rome burns :D
  • Total Posts : 3135
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/14 01:23:52 (permalink)
+1 (1)
wink
#4
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/14 16:29:30 (permalink)
0
Hi, 
After several hours of work on reading code, scanning the forum for more tips and clues. I am not getting the expected result. 
Note: In the main() function I use a print statement to print the value of temp. 
What I mean is when I run the code for the first time, I get just a single character say G and then it hands. If I tickle it by toggling the MCLR checkbox on PICKIT2, I may get another character. If I'm lucky the characters then starting flowing through rapidly (correct state)
 

void interrupt ISR() {
if (PIR1bits.RCIF && PIE1bits.RCIE){

// if (PIR1bits.RCIF){ // see if interrupt caused by incoming data
//PIE1bits.RCIE = 0;
temp = RCREG1; // read the incoming data
//PIE1bits.RCIE = 1; // ### Set Receive interrupt enable bit
}
}

 
I have used this 

void serial_init()
{
RCSTA1bits.SPEN = 1; // Enable USART1
RCSTA1bits.RX9 = 0; // 8 bits RX data
RCSTA1bits.CREN = 1; // Enable USART1 RX
RCSTA1bits.FERR = 0; // No framing error on USART1
RCSTA1bits.OERR = 0; // No OverRun error on USART1
RCSTA1bits.ADDEN = 0; // disables addr det

TXSTA1bits.TX9 = 0; // 8 bits TX data
TXSTA1bits.TXEN = 1; // Enable USART1 TX
TXSTA1bits.SYNC = 0; // USART1 Async Mode
TXSTA1bits.SENDB = 0; // USART1 send break character bit
TXSTA1bits.BRGH1 = 1; // USART1 High Speed

BAUDCON1bits.BRG16 = 1; // 16-bit Baud Rate Generator
//BAUDCON1bits.WUE = 1; // EUSART will continue to sample the Rx pin
BAUDCON1bits.ABDEN = 0; // 0 = Baud rate measurement is disabled or completed

/////TXSTA1bits.BRGH1=1; // select low speed Baud Rate
/////TXSTA1bits.TX9=0; // select 8 data bits
//////TXSTA1bits.TXEN = 1; // enable transmit
//////TXSTA1bits.SYNC = 0;

////RCSTA1bits.SPEN=1; // serial port is enabled
////RCSTA1bits.RX9=0; // select 8 data bits
////RCSTA1bits.CREN=1; // receive enabled
////PIR1bits.RCIF=0; // make sure receive interrupt flag is clear
////BAUDCONbits.BRG16 = 1;
SPBRGH1 = 6;
SPBRG = 130;
__delay_ms(100); // give time for voltage levels on board to settle
}

 
and settings are as follows:

OSCCON = 0b01110000;
OSCTUNEbits.PLLEN = 1; // turn on the PLL 64 MHz
ANSELA = 0; ANSELB = 0; ANSELC = 0; //ANSELD = 0; ANSELE = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTE = 0; //PORTD = 0;
TRISA = 0; TRISB = 0; TRISC = 0; TRISE = 0; //TRISD = 0;
INTCONbits.GIE = 0; //no interruptions please
TRISCbits.TRISC6 = 1; // input
TRISCbits.TRISC7 = 1; // input
....
PIE1bits.TXIE = 0; // ### Clear Transit interrupt enable bit
PIE1bits.RCIE = 1; // ### Set Receive interrupt enable bit
INTCONbits.PEIE = 1; // ### Enable Peripheral Interrupt
INTCONbits.GIE = 1; // ### Enable Global interrupt*/

 
What am I still missing?
 
/D
#5
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/14 16:58:19 (permalink)
+1 (1)
This doesn't explain the described problem, but you're still setting the baudrate incorrectly.
SPBRGH1 = 6;
SPBRG = 130;

As discussed here: http://www.microchip.com/forums/FindPost/1014464
Writing to SPBRGH1 writes to just that 8 bit register.
Writing to SPBRG  writes to both registers, so it's clearing SPBRGH1.
 
Either use:
SPBRGH1 = 6;
 
SPBRGL1 = 130;

to write them individually, or
SPBRG=1666;

to write them in one C line. (The assembly output still does it in two 8 bit writes, but that's invisible to you).
 
 
 
#6
Aussie Susan
Super Member
  • Total Posts : 2892
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/14 19:32:50 (permalink)
+2 (2)
Please(!) write a simple program that reads in a character from the UART and not much else (you will need to se the config registers and the oscillator). Don't use interrupts - keep it very simple.
Get that to work and build up from there.
Susan
#7
CinziaG
rome burns :D
  • Total Posts : 3135
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 02:19:08 (permalink)
+1 (1)
After this one, I still again and more enforce the "handle errors in receiving" issue Smile
#8
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:25:52 (permalink)
0
 
I'm trying to understand what handle errors in receiving"means, I already have 

RCSTA1bits.FERR = 0; // No framing error on USART1 
RCSTA1bits.OERR = 0; // No OverRun error on USART1

#9
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:29:06 (permalink)
0
Aussie Susan
Please(!) write a simple program that reads in a character from the UART and not much else (you will need to se the config registers and the oscillator). Don't use interrupts - keep it very simple.
Get that to work and build up from there.
Susan


My new code is as follows:
I'm getting 3 chars or so and then it gives up and prints zeros.

char UART_Data_Ready()
{
return RCIF;
STS_LED=0;
}
char UART_Read()
{
while(!RCIF);
temp = RCREG;
}
void UART_Read_Text()
{
UART_Read();
fuseDataGPS(temp);
}

 
 
#10
CinziaG
rome burns :D
  • Total Posts : 3135
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:41:12 (permalink)
0
If you get exaclty 3 chars, then you're defintiely running into Overrun error! (2 byte FIFO + 1)
 
Which means you're not reading the RX register fast enough (you should show/check all of your code).
 
As for error handling, it will avoid blocking the USART but you'll still be in troubles. A proper error checking must check for the error flags upon every read at RCREG, and take actions when needed.
#11
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:41:16 (permalink)
+3 (3)
Dartmoor
 
I'm trying to understand what handle errors in receiving"means, I already have 
RCSTA1bits.FERR = 0; // No framing error on USART1 
RCSTA1bits.OERR = 0; // No OverRun error on USART1

Examine your PIC's datasheet, figure: "REGISTER 16-2: RCSTAX: RECEIVE STATUS AND CONTROL REGISTER"
See above "FERR" and "OERR" is shows "R-0" ?
That means the flag is "read-only" (and that is is zero after a power-on reset).
So, it's impossible to clear the flag just by writing to it.
 
There's no need to clear FERR, it just indicates if the last received character had a correct stop bit. It updates each time you receive a character. It can be useful to test it to detect if you are getting framing errors, which often indicates an incorrect baud rate.
You do need to clear OERR though. It halts USART receive while it is set.
Lower down on the same page in the datasheet, the descriptoin of that bit says:

OERR: Overrun Error bit
1 = Overrun error (can be cleared by clearing bit CREN)
0 = No overrun error

This is described in more detail under "16.5.1.8 Receive Overrun Error"
 
#12
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:44:08 (permalink)
+2 (2)
n.b. in this routine:
char UART_Data_Ready()
{
return RCIF;
STS_LED=0;
}

it will never get to the STS_LED=0; line. "return" causes an immediate return.
 
 
#13
Gort2015
Klaatu Barada Nikto
  • Total Posts : 1335
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:46:01 (permalink)
-1 (1)
char UART_Read()
{
while(!RCIF);
temp = RCREG;
}

PIR1bits.RCIF
Once that flag is set, it will always be set unless you clear it.


post edited by Gort2015 - 2017/09/15 14:48:16

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#14
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/15 14:48:38 (permalink)
+1 (1)
Gort2015
char UART_Read()
{
while(!RCIF);
temp = RCREG;
}

PIR1bits.RCIF

Once that flag is set, it will always be set unless you clear it.

The only way to clear it is by reading RCREG, which that code is doing.
 
#15
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 15:00:25 (permalink)
0
Sorry everyone, I seem to be failing in understanding what's required. I tried ISR and removed that, I've tried the code above but it hangs, Error checking is beyond me at present, I've just gone numb after days and days of reading posts across the microchip forum 
#16
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/15 15:05:51 (permalink)
+2 (2)
Please show your entire program.
It looks like you still have too much else going on to focus on the real issue.
 
#17
CinziaG
rome burns :D
  • Total Posts : 3135
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 15:29:19 (permalink) ☄ Helpfulby Dartmoor 2017/09/16 13:01:34
+2 (2)
My code for polled receive:

  if(! /*PIR1bits.RCIF*/ DataRdyUSART())
        return 0xffff;

    if(RCSTAbits.FERR) {
 }
    if(RCSTAbits.OERR) {
        RCSTAbits.CREN=0;
        Delay10TCY();
        RCSTAbits.CREN=1;
        }

  temp=RCSTA;                                //salvare RCSTA PRIMA di leggere RCREG (v. datasheet) e
  ByteRec=RCREG /*ReadUSART()*/;                            //ricevo solo 1 BYTE per volta!

 
 
#18
Dartmoor
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2017/08/24 06:06:17
  • Location: London, United Kingdom
  • Status: offline
Re: Initial startup latency on the UART - parsing - display 2017/09/15 15:39:02 (permalink)
+1 (1)
qhb
Please show your entire program.
It looks like you still have too much else going on to focus on the real issue.
 


Thanks qhb. Posting the code. To be honest I 've been doing this for the past 30 days or more every evening after work till 12 midnight and then every weekend. Of course I've learned a lot in xc8, fonts, tft, NGR cals etc. but the uart still eludes me, I've tried the /plib/eusart library and that had an error. 

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
//#include <p18lf26k22.h>
#include "bit_settings.h"
#include "spi_pic18f.h"
#include "ili9341.h"
#include "bitmap.h"
#include "time_delay.h"
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "font3.h"
#include "font1.h"
char c, temp; //temp dump
volatile unsigned int index, reading, new_rx;
int ready, gpgga, gprmc, mode; //gps associated vars
char buffer1[20];

struct gpsStruct {//GPRMC
int timeZone;
bool flagRead; // flag used by the parser, when a valid sentence has begun
bool flagDataReady; // valid GPS fix and data available, user can call reader functions
char words[20][15]; // hold parsed words for one given NMEA sentence
char szChecksum[15]; // hold the received checksum for one given NMEA sentence
// will be set to true for characters between $ and * only
bool flagComputedCks; // used to compute checksum and indicate valid checksum interval (between $ and * in a given sentence)
int checksum; // numeric checksum, computed for a given sentence
bool flagReceivedCks; // after getting * we start cuttings the received checksum
int index_received_checksum;// used to parse received checksum
// word cutting variables
int wordIdx; // the current word in a sentence
int prevIdx; // last character index where we did a cut
int nowIdx ; // current character index
// globals to store parser results
bool positionFixIndicator; //GPGGA
bool dataValid; //GPRMC
float longitude; // GPRMC and GPGGA
float latitude; // GPRMC and GPGGA
unsigned char UTCHour, UTCMin, UTCSec, // GPRMC and GPGGA
UTCDay, UTCMonth, UTCYear; // GPRMC
int satellitesUsed;// GPGGA
float dilution; //GPGGA
float altitude; // GPGGA
float speed; // GPRMC
float bearing; // GPRMC
};
struct gpsStruct gps;
/*
* Interrupt Service
*/

void serial_init()
{
RCSTA1bits.SPEN = 1; // Enable USART1
RCSTA1bits.RX9 = 0; // 8 bits RX data
RCSTA1bits.CREN = 1; // Enable USART1 RX
RCSTA1bits.FERR = 0; // No framing error on USART1
RCSTA1bits.OERR = 0; // No OverRun error on USART1
RCSTA1bits.ADDEN = 0; // disables addr det

TXSTA1bits.TX9 = 0; // 8 bits TX data
TXSTA1bits.TXEN = 1; // Enable USART1 TX
TXSTA1bits.SYNC = 0; // USART1 Async Mode
TXSTA1bits.SENDB = 0; // USART1 send break character bit
TXSTA1bits.BRGH1 = 1; // USART1 High Speed

BAUDCON1bits.BRG16 = 1; // 16-bit Baud Rate Generator
//BAUDCON1bits.WUE = 1; // EUSART will continue to sample the Rx pin
BAUDCON1bits.ABDEN = 0; // 0 = Baud rate measurement is disabled or completed

/////TXSTA1bits.BRGH1=1; // select low speed Baud Rate
/////TXSTA1bits.TX9=0; // select 8 data bits
//////TXSTA1bits.TXEN = 1; // enable transmit
//////TXSTA1bits.SYNC = 0;

////RCSTA1bits.SPEN=1; // serial port is enabled
////RCSTA1bits.RX9=0; // select 8 data bits
////RCSTA1bits.CREN=1; // receive enabled
////PIR1bits.RCIF=0; // make sure receive interrupt flag is clear
////BAUDCONbits.BRG16 = 1;
SPBRGH1 = 6;
SPBRG = 130;
__delay_ms(100); // give time for voltage levels on board to settle
}

//void interrupt ISR() {
// if (PIR1bits.RCIF && PIE1bits.RCIE){
//
// // if (PIR1bits.RCIF){ // see if interrupt caused by incoming data
// //PIE1bits.RCIE = 0;
// c = RCREG1; // read the incoming data
// //PIE1bits.RCIE = 1; // ### Set Receive interrupt enable bit
// fuseDataGPS(c);
//}
//}
char UART_Data_Ready(){
return RCIF;
}
char UART_Read(){
while(!RCIF);
RCSTA1bits.CREN=0;
return RCREG;
}

void main(void){
int i;
STS_LED = 1;
OSCCON = 0b01110000;
OSCTUNEbits.PLLEN = 1; // turn on the PLL 64 MHz
ANSELA = 0; ANSELB = 0; ANSELC = 0; //ANSELD = 0; ANSELE = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTE = 0; //PORTD = 0;
TRISA = 0; TRISB = 0; TRISC = 0; TRISE = 0; //TRISD = 0;
INTCONbits.GIE = 0; //no interruptions please
TRISCbits.TRISC6 = 1; // input
TRISCbits.TRISC7 = 1; // input
new_rx = 0;
__delay_ms(100);
SPI1_Close();
SPI1_Init();
TFT_Init();
STS_LED = 0;

serial_init();
//RCONbits.IPEN = 0;

STS_LED = 1;
// PIE1bits.TXIE = 0; // ### Clear Transit interrupt enable bit
// PIE1bits.RCIE = 1; // ### Set Receive interrupt enable bit
// INTCONbits.PEIE = 1; // ### Enable Peripheral Interrupt
// INTCONbits.GIE = 1; // ### Enable Global interrupt*/
ready = 0;

do {

while(!(UART_Data_Ready())); // wait for receive character
temp = UART_Read(); // read value sent


}while(1);
}
int charToI(c){ return (c - 48);}
double trunc(double d){ return (d>0) ? floor(d) : ceil(d) ; }
 

#19
qhb
Superb Member
  • Total Posts : 5785
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Initial startup latency on the UART - parsing - display 2017/09/15 15:52:27 (permalink)
+3 (3)
There's a lot of fluff in there that you should have removed for a simple test.
How do you know if it's working? The main routine just reads characters received then does nothing with them.
 
For that matter, it won't work. You are clearing CREN as soon as you receive a character.
I guess you are just skimming the datasheet instead of reading what the bits actually do. That will shut down the receiver. It told you to turn it off, then back on again to clear the error. Only do it when there IS an error, otherwise you will miss characters if you disable it while the next character is being received.
 
post edited by qhb - 2017/09/15 15:59:34
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2017 APG vNext Commercial Version 4.5