View difference between Paste ID: fb1JXYq4 and E1cHjMbY
SHOW: | | - or go back to the newest paste.
1
/*
2
 * File: Main.c
3
 * Author:
4
 *
5
 * Created on June 13, 2019, 2:22 PM
6
 */
7
 
8
#define _XTAL_FREQ 32000000
9
 
10
// PIC16F18875 Configuration Bit Settings
11
 
12
// 'C' source line config statements
13
 
14
// CONFIG1
15
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (EC above 8MHz; PFM set to high power)
16
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
17
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
18
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
19
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
20
 
21
// CONFIG2
22
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
23
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
24
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
25
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
26
#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)
27
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
28
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
29
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
30
 
31
// CONFIG3
32
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
33
#pragma config WDTE = OFF // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
34
#pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits (window always open (100%); software control; keyed access not required)
35
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
36
 
37
// CONFIG4
38
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
39
#pragma config SCANE = available // Scanner Enable bit (Scanner module is available for use)
40
#pragma config LVP = OFF // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
41
 
42
// CONFIG5
43
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
44
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)
45
 
46
#include <xc.h>
47
 
48
int GPSh1;
49
int GPSh2;
50
int GPSm1;
51
int GPSm2;
52
int GPSs1;
53
int GPSs2;
54
55
unsigned getSPBRG(unsigned baudrate, char brg16, char brgh) {
56
    unsigned mult;
57
    if (brg16 == 0 && brgh == 0) mult = 64;
58
    else if (brg16 == 1 && brgh == 1) mult = 4;
59
    else mult = 16;
60
   
61
    // baudrate = fOsc / (mult*(spbrg + 1))
62
    // baudrate * (spbrg + 1) = fOsc / mult
63
    // spbrg + 1 = (fOsc / mult) / baudrate
64
    // spbrg = (fOsc / mult) / baudrate - 1
65
    return (_XTAL_FREQ / mult) / baudrate - 1;
66
}
67
68
char getch(){
69
    if (OERR) {
70
        CREN = 0;
71
        Nop();
72
        CREN = 1;
73
    }
74
    while(!RCIF);
75
    return RCREG;
76
}
77
78
void printSerial(char* txt, char ch) {
79
    while(*txt) {
80
        TXREG = *txt;
81
        txt++;
82
        while (!TXIF);
83
    }
84
    TXREG = ch;
85
    while (!TXIF);
86
    TXREG = '\n';
87
    while (!TXIF);
88
}
89
90
void GPStime(){
91
    char x = 0;
92
    x = getch(); if(x != '$') return;
93
    printSerial("Got $", ' ');
94
    x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting"); return; }
95
    x = getch(); printSerial("Got: ", x); if(x != 'P') { printSerial("Quitting"); return; }
96
    x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting"); return; }
97
    x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting"); return; }
98
    x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting"); return; }
99
100
    {
101
        char ch;
102
        LATA0 = 0;
103
        do {
104
            ch = getch();
105
            printSerial("Got: ", ch);
106
        } while (ch != 'E' && ch != 'W');
107
        char comma = getch(); // read a comma
108
        printSerial("Got: ", comma);
109
        GPSh1 = getch() - '0';
110
        printSerial("Got: ", GPSh1 + '0');
111
        GPSh2 = getch() - '0';
112
        printSerial("Got: ", GPSh2 + '0');
113
        GPSm1 = getch() - '0';
114
        printSerial("Got: ", GPSm1 + '0');
115
        GPSm2 = getch() - '0';
116
        printSerial("Got: ", GPSm2 + '0');
117
        GPSs1 = getch() - '0';
118
        printSerial("Got: ", GPSs1 + '0');
119
        GPSs2 = getch() - '0';
120
        printSerial("Got: ", GPSs2 + '0');
121
    }
122
   
123
}
124
125
void main(void)
126
{
127
    TRISA = 0b00000000;
128
    TRISB = 0b00000000;
129
   
130
    // LATA = 1; __delay_ms(1000);
131
 
132
    PORTC = 0;
133
    TRISC = 0b10000000; // all outputs, except RX on RC7
134
    ANSELC = 0b00000000; // disable analog
135
   
136
    RC6PPS = 0x10;
137
   
138
    //init USART
139
    // SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
140
    RCSTA = 0b00110000; // TX9D=0 -->8-bit; SPEN=1, CREN=1, addr-det disabled.
141
    // CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
142
    TXSTA = 0b00100100; // TX9D=0 -->8-bit; BRGH=1; TXEN=1 --> enable, SYNC=0 --> async
143
    //ABDOVF RCIDL NOP SCKP BRG16 NOP WUE ABDEN
144
    BAUDCON = 0b00001000; // SCKP=0 -> not inverted, BRG16=1, WUE=0, ABDEN=0
145
    SPEN = 1;
146
   
147
    // LATA = 2; __delay_ms(1000);
148
 
149
    unsigned brg = getSPBRG(9600, 1, 1);
150
    SPBRGL = brg & 0xFF;
151
    SPBRGH = brg >> 8;
152
   
153
    // LATA = 3; __delay_ms(1000);
154
 
155
    while(1){
156
        GPStime();
157
        TXREG = GPSh1 + '0';
158
        while (!TXIF);
159
        TXREG = GPSh2 + '0';
160
        while (!TXIF);
161
        TXREG = GPSm1 + '0';
162
        while (!TXIF);
163
        TXREG = GPSm2 + '0';
164
        while (!TXIF);
165
        TXREG = GPSs1 + '0';
166
        while (!TXIF);
167
        TXREG = GPSs2 + '0';
168
        while (!TXIF);
169
        TXREG = '\n';
170
        while (!TXIF);
171
    }
172
   
173
    LATA = 7;
174
    return;
175
}