Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Main.c
- * Author:
- *
- * Created on June 13, 2019, 2:22 PM
- */
- #define _XTAL_FREQ 32000000
- // PIC16F18875 Configuration Bit Settings
- // 'C' source line config statements
- // CONFIG1
- #pragma config FEXTOSC = OFF // External Oscillator mode selection bits (EC above 8MHz; PFM set to high power)
- #pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
- #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
- #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
- #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
- // CONFIG2
- #pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
- #pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
- #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
- #pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
- #pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
- #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
- // CONFIG3
- #pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
- #pragma config WDTE = OFF // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
- #pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits (window always open (100%); software control; keyed access not required)
- #pragma config WDTCCS = SC // WDT input clock selector (Software Control)
- // CONFIG4
- #pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
- #pragma config SCANE = available // Scanner Enable bit (Scanner module is available for use)
- #pragma config LVP = OFF // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
- // CONFIG5
- #pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
- #pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)
- #include <xc.h>
- int GPSh1;
- int GPSh2;
- int GPSm1;
- int GPSm2;
- int GPSs1;
- int GPSs2;
- volatile int NopWriteVal;
- unsigned getSPBRG(unsigned baudrate, char brg16, char brgh) {
- unsigned mult;
- if (brg16 == 0 && brgh == 0) mult = 64;
- else if (brg16 == 1 && brgh == 1) mult = 4;
- else mult = 16;
- // baudrate = fOsc / (mult*(spbrg + 1))
- // baudrate * (spbrg + 1) = fOsc / mult
- // spbrg + 1 = (fOsc / mult) / baudrate
- // spbrg = (fOsc / mult) / baudrate - 1
- return (_XTAL_FREQ / mult) / baudrate - 1;
- }
- char getch(){
- if (OERR) {
- CREN = 0;
- NopWriteVal = 0;
- CREN = 1;
- }
- while(!RCIF);
- return RCREG;
- }
- void printSerial(char* txt, char ch) {
- while(*txt) {
- TXREG = *txt;
- txt++;
- while (!TXIF);
- }
- TXREG = ch;
- while (!TXIF);
- TXREG = '\n';
- while (!TXIF);
- }
- void GPStime(){
- char x = 0;
- x = getch(); if(x != '$') return;
- printSerial("Got $", ' ');
- x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting", ' '); return; }
- x = getch(); printSerial("Got: ", x); if(x != 'P') { printSerial("Quitting", ' '); return; }
- x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting", ' '); return; }
- x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting", ' '); return; }
- x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting", ' '); return; }
- {
- char ch;
- LATA0 = 0;
- do {
- ch = getch();
- printSerial("Got: ", ch);
- } while (ch != 'E' && ch != 'W');
- char comma = getch(); // read a comma
- printSerial("Got: ", comma);
- GPSh1 = getch() - '0';
- printSerial("Got: ", GPSh1 + '0');
- GPSh2 = getch() - '0';
- printSerial("Got: ", GPSh2 + '0');
- GPSm1 = getch() - '0';
- printSerial("Got: ", GPSm1 + '0');
- GPSm2 = getch() - '0';
- printSerial("Got: ", GPSm2 + '0');
- GPSs1 = getch() - '0';
- printSerial("Got: ", GPSs1 + '0');
- GPSs2 = getch() - '0';
- printSerial("Got: ", GPSs2 + '0');
- }
- }
- void main(void)
- {
- TRISA = 0b00000000;
- TRISB = 0b00000000;
- // LATA = 1; __delay_ms(1000);
- PORTC = 0;
- TRISC = 0b10000000; // all outputs, except RX on RC7
- ANSELC = 0b00000000; // disable analog
- RC6PPS = 0x10;
- //init USART
- // SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
- RCSTA = 0b00110000; // TX9D=0 -->8-bit; SPEN=1, CREN=1, addr-det disabled.
- // CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
- TXSTA = 0b00100100; // TX9D=0 -->8-bit; BRGH=1; TXEN=1 --> enable, SYNC=0 --> async
- //ABDOVF RCIDL NOP SCKP BRG16 NOP WUE ABDEN
- BAUDCON = 0b00001000; // SCKP=0 -> not inverted, BRG16=1, WUE=0, ABDEN=0
- SPEN = 1;
- // LATA = 2; __delay_ms(1000);
- unsigned brg = getSPBRG(9600, 1, 1);
- SPBRGL = brg & 0xFF;
- SPBRGH = brg >> 8;
- // LATA = 3; __delay_ms(1000);
- while(1){
- GPStime();
- TXREG = GPSh1 + '0';
- while (!TXIF);
- TXREG = GPSh2 + '0';
- while (!TXIF);
- TXREG = GPSm1 + '0';
- while (!TXIF);
- TXREG = GPSm2 + '0';
- while (!TXIF);
- TXREG = GPSs1 + '0';
- while (!TXIF);
- TXREG = GPSs2 + '0';
- while (!TXIF);
- TXREG = '\n';
- while (!TXIF);
- }
- LATA = 7;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement