SHARE
TWEET

Untitled

a guest Jun 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. volatile int NopWriteVal;
  56.  
  57. unsigned getSPBRG(unsigned baudrate, char brg16, char brgh) {
  58.     unsigned mult;
  59.     if (brg16 == 0 && brgh == 0) mult = 64;
  60.     else if (brg16 == 1 && brgh == 1) mult = 4;
  61.     else mult = 16;
  62.    
  63.     // baudrate = fOsc / (mult*(spbrg + 1))
  64.     // baudrate * (spbrg + 1) = fOsc / mult
  65.     // spbrg + 1 = (fOsc / mult) / baudrate
  66.     // spbrg = (fOsc / mult) / baudrate - 1
  67.     return (_XTAL_FREQ / mult) / baudrate - 1;
  68. }
  69.  
  70. char getch(){
  71.     if (OERR) {
  72.         CREN = 0;
  73.         NopWriteVal = 0;
  74.         CREN = 1;
  75.     }
  76.     while(!RCIF);
  77.     return RCREG;
  78. }
  79.  
  80. void printSerial(char* txt, char ch) {
  81.     while(*txt) {
  82.         TXREG = *txt;
  83.         txt++;
  84.         while (!TXIF);
  85.     }
  86.     TXREG = ch;
  87.     while (!TXIF);
  88.     TXREG = '\n';
  89.     while (!TXIF);
  90. }
  91.  
  92. void GPStime(){
  93.     char x = 0;
  94.     x = getch(); if(x != '$') return;
  95.     printSerial("Got $", ' ');
  96.     x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting", ' '); return; }
  97.     x = getch(); printSerial("Got: ", x); if(x != 'P') { printSerial("Quitting", ' '); return; }
  98.     x = getch(); printSerial("Got: ", x); if(x != 'G') { printSerial("Quitting", ' '); return; }
  99.     x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting", ' '); return; }
  100.     x = getch(); printSerial("Got: ", x); if(x != 'L') { printSerial("Quitting", ' '); return; }
  101.  
  102.     {
  103.         char ch;
  104.         LATA0 = 0;
  105.         do {
  106.             ch = getch();
  107.             printSerial("Got: ", ch);
  108.         } while (ch != 'E' && ch != 'W');
  109.         char comma = getch(); // read a comma
  110.         printSerial("Got: ", comma);
  111.         GPSh1 = getch() - '0';
  112.         printSerial("Got: ", GPSh1 + '0');
  113.         GPSh2 = getch() - '0';
  114.         printSerial("Got: ", GPSh2 + '0');
  115.         GPSm1 = getch() - '0';
  116.         printSerial("Got: ", GPSm1 + '0');
  117.         GPSm2 = getch() - '0';
  118.         printSerial("Got: ", GPSm2 + '0');
  119.         GPSs1 = getch() - '0';
  120.         printSerial("Got: ", GPSs1 + '0');
  121.         GPSs2 = getch() - '0';
  122.         printSerial("Got: ", GPSs2 + '0');
  123.     }
  124.    
  125. }
  126.  
  127. void main(void)
  128. {
  129.     TRISA = 0b00000000;
  130.     TRISB = 0b00000000;
  131.    
  132.     // LATA = 1; __delay_ms(1000);
  133.  
  134.     PORTC = 0;
  135.     TRISC = 0b10000000; // all outputs, except RX on RC7
  136.     ANSELC = 0b00000000; // disable analog
  137.    
  138.     RC6PPS = 0x10;
  139.    
  140.     //init USART
  141.     // SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
  142.     RCSTA = 0b00110000; // TX9D=0 -->8-bit; SPEN=1, CREN=1, addr-det disabled.
  143.     // CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
  144.     TXSTA = 0b00100100; // TX9D=0 -->8-bit; BRGH=1; TXEN=1 --> enable, SYNC=0 --> async
  145.     //ABDOVF RCIDL NOP SCKP BRG16 NOP WUE ABDEN
  146.     BAUDCON = 0b00001000; // SCKP=0 -> not inverted, BRG16=1, WUE=0, ABDEN=0
  147.     SPEN = 1;
  148.    
  149.     // LATA = 2; __delay_ms(1000);
  150.  
  151.     unsigned brg = getSPBRG(9600, 1, 1);
  152.     SPBRGL = brg & 0xFF;
  153.     SPBRGH = brg >> 8;
  154.    
  155.     // LATA = 3; __delay_ms(1000);
  156.  
  157.     while(1){
  158.         GPStime();
  159.         TXREG = GPSh1 + '0';
  160.         while (!TXIF);
  161.         TXREG = GPSh2 + '0';
  162.         while (!TXIF);
  163.         TXREG = GPSm1 + '0';
  164.         while (!TXIF);
  165.         TXREG = GPSm2 + '0';
  166.         while (!TXIF);
  167.         TXREG = GPSs1 + '0';
  168.         while (!TXIF);
  169.         TXREG = GPSs2 + '0';
  170.         while (!TXIF);
  171.         TXREG = '\n';
  172.         while (!TXIF);
  173.     }
  174.    
  175.     LATA = 7;
  176.     return;
  177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top