SHARE
TWEET

Untitled

a guest Jun 25th, 2019 61 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. unsigned getSPBRG(unsigned, char, char);
  49. void GPStime();
  50. char getch();
  51. void display();
  52. void displayNumber(int);
  53.  
  54. int GPSh1;
  55. int GPSh2;
  56. int GPSm1;
  57. int GPSm2;
  58. int GPSs1;
  59. int GPSs2;
  60.  
  61. int h1;
  62. int h2;
  63. int m1;
  64. int m2;
  65. int s1;
  66. int s2;
  67.  
  68. void main(void)
  69. {    
  70.     TRISA = 0b00000000;     //Bits A0-A3 for multiplexing
  71.     TRISB = 0b00000000;     //Bits B0-B5 & A4 For number display (7 segments)
  72.     TRISD = 0b11111111;     //Inputs for clock GMT offset
  73.     TRISE = 0b111;          //inputs for clock settings
  74.    
  75.     //A register still has A5-A7 free                                           (3 bits)
  76.     //B register is all used (except for B6/B7 but those are for debugging)     (0 bits)
  77.     //C register C0-C5 is free C6/C7 is used for RX/TX                          (6 bits)
  78.     //D register is free                                                        (8 bits)
  79.     //E register is free                                                        (3 bits)
  80.    
  81.     PORTC = 0;
  82.     TRISC = 0b10000000; // all outputs, except RX on RC7
  83.     ANSELC = 0b00000000; // disable analog
  84.     ANSELE = 0b000;
  85.     ANSELD = 0b00000000;
  86.    
  87.     PORTA = 0x00;
  88.     PORTB = 0x00;
  89.     //PORTC = 0x00;
  90.     PORTD = 0x00;
  91.     PORTE = 0b000;
  92.    
  93.     RC6PPS = 0x10;
  94.    
  95.     //init USART
  96.     // SPEN  RX9   SREN  CREN  ADDEN FERR OERR   RX9D
  97.     RCSTA = 0b00110000;        //    TX9D=0 -->8-bit; SPEN=1, CREN=1, addr-det disabled.
  98.     // CSRC  TX9   TXEN  SYNC  SENDB BRGH  TRMT  TX9D
  99.     TXSTA = 0b00100100;        //    TX9D=0 -->8-bit; BRGH=1; TXEN=1 --> enable, SYNC=0 --> async
  100.     //ABDOVF RCIDL NOP   SCKP  BRG16 NOP   WUE   ABDEN
  101.     BAUDCON = 0b00001000;    //    SCKP=0 -> not inverted, BRG16=1, WUE=0, ABDEN=0
  102.     SPEN = 1;
  103.    
  104.     unsigned brg = getSPBRG(9600, 1, 1);
  105.     SPBRGL = brg & 0xFF;
  106.     SPBRGH = brg >> 8;
  107.  
  108.     //TXREG = 'X';
  109.    
  110.         //__delay_ms(500);
  111.         //GPStime();
  112.         //__delay_ms(500);
  113.         h1 = GPSh1;
  114.         h2 = GPSh2;
  115.         m1 = GPSm1;
  116.         m2 = GPSm2;
  117.         s1 = GPSs1;
  118.         s2 = GPSs2;
  119.    
  120.     while(1){
  121.         //while (!RCIF);
  122.         //char recv = RCREG;
  123.         //while (!TXIF);
  124.         //TXREG = recv; Debugging code for arduino serial
  125.        
  126.         LATA0 = 1;
  127.         __delay_ms(1000);
  128.         LATA0 = 0;
  129.         __delay_ms(1000);
  130.         LATA0 = 1;
  131.         __delay_ms(1000);
  132.        
  133.         GPStime();
  134.        
  135.         LATA0 = 0;
  136.         __delay_ms(1000);
  137.        
  138.     }
  139.     return;
  140. }
  141.  
  142. unsigned getSPBRG(unsigned baudrate, char brg16, char brgh) {
  143.     unsigned mult;
  144.     if (brg16 == 0 && brgh == 0) mult = 64;
  145.     else if (brg16 == 1 && brgh == 1) mult = 4;
  146.     else mult = 16;
  147.    
  148.     // baudrate = fOsc / (mult*(spbrg + 1))
  149.     // baudrate * (spbrg + 1) = fOsc / mult
  150.     // spbrg + 1 = (fOsc / mult) / baudrate
  151.     // spbrg = (fOsc / mult) / baudrate - 1
  152.     return (_XTAL_FREQ / mult) / baudrate - 1;
  153. }
  154.  
  155. void GPStime(){
  156.    
  157.     if(getch() == '$' && getch() == 'G' && getch() == 'P' && getch() == 'G' && getch() == 'G' && getch() == 'A'){
  158.         char ch;
  159.         LATA0 = 0;
  160.         //do {
  161.         //    ch = getch();
  162.         //} while (ch != 'E' && ch != 'W');
  163.         getch(); // read a comma
  164.         GPSh1 = getch() - '0';
  165.         GPSh2 = getch() - '0';
  166.         GPSm1 = getch() - '0';
  167.         GPSm2 = getch() - '0';
  168.         GPSs1 = getch() - '0';
  169.         GPSs2 = getch() - '0';
  170.     }
  171.    
  172. }
  173.  
  174. char getch(){
  175.     while(!RCIF);
  176.     return RCREG;
  177. }
  178.  
  179. void display(){
  180.         //8888888888888888888888888888888888
  181.         displayNumber(h1);                   //First Display
  182.         LATA0 = 0;
  183.         __delay_ms(1);
  184.         LATA0 = 1;
  185.         //8888888888888888888888888888888888
  186.         displayNumber(h2);                   //Second Display
  187.         LATA1 = 0;
  188.         __delay_ms(1);
  189.         LATA1 = 1;
  190.         //8888888888888888888888888888888888
  191.         displayNumber(m1);
  192.         LATA2 = 0;
  193.         __delay_ms(1);
  194.         LATA2 = 1;
  195.         //8888888888888888888888888888888888
  196.         displayNumber(m2);
  197.         LATA3 = 0;
  198.         __delay_ms(1);
  199.         LATA3 = 1;
  200.         //8888888888888888888888888888888888
  201. }
  202.  
  203. void displayNumber(int display){
  204.    
  205.     if(display == 1){
  206.         LATB0 = 1;
  207.         LATB1 = 1;
  208.         LATB2 = 1;
  209.         LATB3 = 0;
  210.         LATB4 = 0;
  211.         LATB5 = 1;
  212.         LATA4 = 1;
  213.     }else if(display == 2){
  214.         LATB0 = 1;
  215.         LATB1 = 0;
  216.         LATB2 = 0;
  217.         LATB3 = 1;
  218.         LATB4 = 0;
  219.         LATB5 = 0;
  220.         LATA4 = 0;
  221.     }else if(display == 3){
  222.         LATB0 = 1;
  223.         LATB1 = 1;
  224.         LATB2 = 0;
  225.         LATB3 = 0;
  226.         LATB4 = 0;
  227.         LATB5 = 0;
  228.         LATB4 = 0;
  229.     }else if(display == 4){
  230.         LATB0 = 0;
  231.         LATB1 = 1;
  232.         LATB2 = 1;
  233.         LATB3 = 0;
  234.         LATB4 = 0;
  235.         LATB5 = 1;
  236.         LATA4 = 0;
  237.     }else if(display == 5){
  238.         LATB0 = 0;
  239.         LATB1 = 1;
  240.         LATB2 = 0;
  241.         LATB3 = 0;
  242.         LATB4 = 1;
  243.         LATB5 = 0;
  244.         LATA4 = 0;
  245.     }else if(display == 6){
  246.         LATB0 = 0;
  247.         LATB1 = 0;
  248.         LATB2 = 0;
  249.         LATB3 = 0;
  250.         LATB4 = 1;
  251.         LATB5 = 0;
  252.         LATA4 = 0;
  253.     }else if(display == 7){
  254.         LATB0 = 1;
  255.         LATB1 = 1;
  256.         LATB2 = 1;
  257.         LATB3 = 0;
  258.         LATB4 = 0;
  259.         LATB5 = 0;
  260.         LATA4 = 1;
  261.     }else if(display == 8){
  262.         LATB0 = 0;
  263.         LATB1 = 0;
  264.         LATB2 = 0;
  265.         LATB3 = 0;
  266.         LATB4 = 0;
  267.         LATB5 = 0;
  268.         LATA4 = 0;
  269.     }else if(display == 9){
  270.         LATB0 = 0;
  271.         LATB1 = 1;
  272.         LATB2 = 0;
  273.         LATB3 = 0;
  274.         LATB4 = 0;
  275.         LATB5 = 0;
  276.         LATA4 = 0;
  277.     }else if(display == 0){
  278.         LATB0 = 0;
  279.         LATB1 = 0;
  280.         LATB2 = 0;
  281.         LATB3 = 0;
  282.         LATB4 = 0;
  283.         LATB5 = 0;
  284.         LATA4 = 1;
  285.     }
  286. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top