//********************************************************************** /*   「GPS時計(自動時刻同期&低消費電力)」 */ //********************************************************************** #define SW1 PORTA.F4 #define SW2 PORTA.F5 #define SW3 PORTB.F0 #define SW4 PORTB.F1 #define GT720F PORTA.F6 #define CR 0x0D #define LF 0x0A #define FF 0x0C #define LED PORTB.F3 #define ON 1 #define OFF 0 //********************************************************************** static unsigned long clock; void interrupt() { if (PIR1.CCP1IF == 1) { PIR1.CCP1IF = 0; // clock++; } } //********************************************************************** static unsigned short flg, len; static unsigned short hd[8], utc[12], latitude[12], longitude[12]; static unsigned short quality[4], satellites[4]; void gps_recv() { static unsigned short rd; // flg = 0; len = 0; // while (flg != 8) { if (Usart_Data_Ready() == 0) continue; rd = Usart_Read(); if (rd == LF) { len = 0; continue; } // switch (flg) { case 0: hd[len] = rd; len++; if (len == 7) { len = 0; if (strncmp(hd, "$GPGGA,", 7) == 0) { flg = 1; } } break; case 1: if (rd == ',') { utc[len] = 0x00; len = 0; flg = 2; } else { utc[len] = rd; len++; } break; case 2: if (rd == ',') { latitude[len] = 0x00; len = 0; flg = 3; } else { latitude[len] = rd; len++; } break; case 3: if (rd == ',') { len = 0; flg = 4; } break; case 4: if (rd == ',') { longitude[len] = 0x00; len = 0; flg = 5; } else { longitude[len] = rd; len++; } break; case 5: if (rd == ',') { len = 0; flg = 6; } break; case 6: if (rd == ',') { quality[len] = 0x00; len = 0; flg = 7; } else { quality[len] = rd; len++; } break; case 7: if (rd == ',') { satellites[len] = 0x00; len = 0; flg = 8; } else { satellites[len] = rd; len++; } break; } } } //********************************************************************** static char buf[16], tmp[8]; static unsigned short hh, mm, ss, ms; unsigned long gps_get_clock() { static unsigned long t; // while (1) { gps_recv(); Lcd_Custom_Out(2, 1, utc); Lcd_Custom_Out(2, 13, quality); Lcd_Custom_Out(2, 15, satellites); if ((quality[0] != '0') || (SW2 == 0)) break; } // hh = ((utc[0] - '0') * 10) + (utc[1] - '0'); mm = ((utc[2] - '0') * 10) + (utc[3] - '0'); ss = ((utc[4] - '0') * 10) + (utc[5] - '0'); ms = ((utc[7] - '0') * 100) + ((utc[8] - '0') * 10) + (utc[8] - '0'); t = ((long)hh * 36000) + ((long)mm * 600) + ((long)ss * 10) + ((long)ms / 100); return (t + (9 * 36000)); //UTC -> JST } //********************************************************************** void Usart_Write_Str(unsigned short *pData) { while (*pData != 0x00) { Usart_Write(*pData); pData++; Delay_ms(10); } } //********************************************************************** void ByteToStr2(unsigned short number, char *output) { ByteToStr(number, output); output[0] = (output[1] == ' ') ? '0' : output[1]; output[1] = output[2]; output[2] = 0x00; } //********************************************************************** void main() { // // OSCCON = 0b01110000; CMCON = 0b00000111; ANSEL = 0b00000000; TRISA = 0b10110000; TRISB = 0b00000111; // LED = OFF; GT720F = 1; // TIMER1の設定 PIE1.TMR1IE = 0; PIR1.TMR1IF = 0; T1CON.T1CKPS0 = 1; T1CON.T1CKPS1 = 1; T1CON.TMR1ON = 0; TMR1L = 0; TMR1H = 0; // CCPの設定 PIE1.CCP1IE = 1; PIR1.CCP1IF = 0; CCP1CON.CCP1M3 = 1; CCP1CON.CCP1M2 = 0; CCP1CON.CCP1M1 = 1; CCP1CON.CCP1M0 = 1; CCPR1L = 0xA8; // 0.1sec...(1÷8000000)*4*8*25000 CCPR1H = 0x61; // // Lcd_Custom_Config(&PORTA, 0, 1, 2, 3, &PORTB, 4, 6, 7); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Out(1, 1, "GpsClock v2"); Delay_ms(500); Lcd_Custom_Cmd(LCD_CLEAR); Lcd_Custom_Chr(1, 3, ':'); Lcd_Custom_Chr(1, 6, ':'); Lcd_Custom_Chr(1, 9, '.'); // Usart_Init(9600); // clock = 0; // 割り込みを許可する。 INTCON.PEIE = 1; INTCON.GIE = 1; // T1CON.TMR1ON = 1; // while (1) { //clock変数から、時、分、秒、ミリ秒を求める。 hh = clock / 36000; mm = (clock - (36000 * (long)hh)) / 600; ss = (clock - (36000 * (long)hh) - (600 * (long)mm)) / 10; ms = (clock - (36000 * (long)hh) - (600 * (long)mm)) - (10 * (long)ss); //時を表示する。 ByteToStr2(hh, buf); Lcd_Custom_Out(1, 1, buf); //分を表示する。 ByteToStr2(mm, buf); Lcd_Custom_Out(1, 4, buf); //秒を表示する。 ByteToStr2(ss, buf); Lcd_Custom_Out(1, 7, buf); //ミリ秒を表示する。 ByteToStr(ms, buf); Lcd_Custom_Out(1, 10, &buf[2]); //GPSと時刻同期する。(手動) if (SW1 == 0) { GT720F = 0; clock = gps_get_clock(); GT720F = 1; } //GPSと時刻同期する。(自動:毎時0分0秒) if ((SW3 == 0) && (mm == 0) && (ss == 0)) { GT720F = 0; clock = gps_get_clock(); GT720F = 1; } //RS232Cに時刻データを送信する。 if (SW4 == 0) { while (SW4 == 0) { Delay_ms(100); } LED = ON; Usart_Write_Str("S"); ByteToStr2(hh, buf); Usart_Write_Str(buf); Usart_Write_Str(":"); ByteToStr2(mm, buf); Usart_Write_Str(buf); Usart_Write_Str(":"); ByteToStr2(ss, buf); Usart_Write_Str(buf); Usart_Write_Str("E"); LED = OFF; } } } //**********************************************************************