/*! * Reset SmaRTClock counter. */ void RTC_Reset(void) { RTC_Write(CAPTURE0, 0x00); RTC_Write(CAPTURE1, 0x00); RTC_Write(CAPTURE2, 0x00); RTC_Write(CAPTURE3, 0x00); RTC_Write(RTC0CN, 0x92); }
//----------------------------------------------------------------------------- // RTC_Init () //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function will initialize the RTC. // //----------------------------------------------------------------------------- void RTC_Init (void) { SFRPAGE = LEGACY_PAGE; // Unlocking the interface is not necessary for the 'F960. // configure using low-power LFO (lowest power option) RTC_Write (RTC0XCN, 0x08); // LFO mode RTC_Write (RTC0XCF, 0x06); // load capacitance to 12 pF RTC_Write (RTC0CN, 0x80); // Enable smaRTClock }
//----------------------------------------------------------------------------- // RTC_CaptureTimer () //----------------------------------------------------------------------------- // // Return Value : U32 capture time // Parameters : none // // This function will read the 32-bit capture value. // //----------------------------------------------------------------------------- U32 RTC_CaptureTimer (void) { UU32 timer; U8 control; SFRPAGE = LEGACY_PAGE; control = RTC_Read(RTC0CN); if((control&0x80)==0) return 0xFFFF; // error RTC must be enabled RTC_Write(RTC0CN, (control|0x05)); // capture using F960 fast mode // wait for capture bit to go to zero while(((RTC_Read(RTC0CN))&0x01)==0x01); // read using auto-increment RTC0ADR = (0x80 | CAPTURE0); // read CAPTURE0 timer.U8[b0]= RTC0DAT; timer.U8[b1]= RTC0DAT; timer.U8[b2]= RTC0DAT; timer.U8[b3]= RTC0DAT; return timer.U32; }
//============================================================================= // Timer Set and Capture Functions using new F960 fast mode. //----------------------------------------------------------------------------- // RTC_SetTimer () //----------------------------------------------------------------------------- // // Parameters : U32 Timer set time // Return Value : U8 returns 0xFF for error. // // This function will write to the Capture registers and set the timer. // //----------------------------------------------------------------------------- U8 RTC_SetTimer(U32 time) { UU32 value; U8 control; value.U32 = time; SFRPAGE = LEGACY_PAGE; control = RTC_Read(RTC0CN); if((control&0x80)==0) return 0xFF; // error RTC must be enabled // write using auto-increment RTC0ADR = 0x00; RTC0DAT = value.U8[b0]; RTC0DAT = value.U8[b1]; RTC0DAT = value.U8[b2]; RTC0DAT = value.U8[b3]; RTC_Write(RTC0CN, (control|0x06)); // set using F960 fast mode // wait for set bit to go to zero while(((RTC_Read(RTC0CN))&0x02)==0x02); return 0; }
//----------------------------------------------------------------------------- // RTC_SetTimeout () //----------------------------------------------------------------------------- // // Return Value : none // Parameters : U32 Timeout // // This function will set an incremental alarm based on the current time. // //----------------------------------------------------------------------------- void RTC_Timeout (U32 timeout) { U32 capture; // RTC_Write (RTC0CN, 0xD0); // clear alarm RTC_StartCapture (); while(RTC_CapturePending()); capture = RTC_ReadCapture (); RTC_WriteAlarm(capture + timeout); RTC_Write (RTC0CN, 0xD8); // enable RTC alarm }
void timeset(uint8_t *value, uint8_t min_num, uint8_t max_num, uint8_t Reg, uint8_t column, uint8_t row) { sw_RA0.flags = 0; sw_RA1.flags = 0; while (!sw_RA0.flag.press) { if (cnt_t1 % 16 >= 8) { LCD_SetCursor(column, row); // 表示位置を設定する LCD_Puts(" "); } else { *value = RTC_Read(Reg); display(); LCD_SetCursor(column, row); // 表示位置を設定する LCD_Putc(*value / 16 + '0'); LCD_Putc(*value % 16 + '0'); } if (sw_RA1.flag.press) { sw_RA1.flag.press = 0; uint8_t t = *value % 16 + (*value / 16)*10; if (t >= max_num) { t = min_num; } else { t++; } *value = t % 10 + (t / 10)* 16; RTC_Write(Reg, *value); } if (sw_RA1.flag.long_holding_1) { sw_RA1.flag.long_holding_1 = 0; uint8_t t = *value % 16 + (*value / 16)*10; if (t + 9 >= max_num) { t = t % 10; } else { t += 10; } *value = t % 10 + (t / 10)* 16; RTC_Write(Reg, *value); } } sw_RA0.flags = 0; }
//----------------------------------------------------------------------------- // RTC_Init () //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // //----------------------------------------------------------------------------- void RTC_Init (void) { SEGMENT_VARIABLE(Is_RTC_Ready, static U8, SEG_XDATA) = FALSE; if (!Is_RTC_Ready) { RTC0KEY = 0xA5; // unlock the RTC interface RTC0KEY = 0xF1; RTC_Write (RTC0XCN, 0x60); // Configure the smaRTClock // oscillator for crystal mode // Bias Doubling Enabled // AGC Disabled RTC_Write (RTC0XCF, CAP_AUTO_STEP|LOAD_CAP_VALUE); // load capacitance value from rtc.h RTC_Write (RTC0CN, 0x80); // enable RTC rtcDelay(150); // wait for clock ready while ((RTC_Read (RTC0XCN) & 0x10)== 0x00); rtcDelay(150); // wait 1 ms // wait for cap ready while ((RTC_Read (RTC0XCF) & 0x40)== 0x00); RTC_Write (RTC0XCN, 0x40); // disable bias doubling //RTC_Write (RTC0CN, 0xC0); // Enable Missing clock detector RTC_ClearCapture (); // clear CAPTUREn registers RTC_ClearAlarm (); // clear ALARMn registers RTC_Write (RTC0CN, 0xC2); // transfer capture to clock RTC_Write (RTC0CN, 0xD0); // enable RTC run Is_RTC_Ready = TRUE; } }
void RTC_SetTimeout (U32 timeout) { RTC_WriteAlarm(RTC_ReadCapture() + timeout); RTC_Write (RTC0CN, 0xD8); // enable TRTC alarm }
void RTC_SetAlarm(U32 time) { RTC_WriteAlarm (time); RTC_Write (RTC0CN, 0xD8); // enable RTC alarm }
//----------------------------------------------------------------------------- // RTC_StartCapture () //----------------------------------------------------------------------------- // // Return Value : none // Parameters : // //----------------------------------------------------------------------------- void RTC_StartCapture (void) { RTC_Write (RTC0CN, 0xD1); // start capture, }
void main(void) { OSCCON = 0b11110000; // 内部クロックは8MHzとする OPTION_REG = 0b00000000; // デジタルI/Oに内部プルアップ抵抗を使用する ANSELA = 0b00000000; // AN0-AN4は使用しない全てデジタルI/Oとする ANSELB = 0b00000000; // AN5-AN11は使用しない全てデジタルI/Oとする TRISA = 0b00000011; // ピン(RA)は全て出力に割当てる(RA5は入力のみとなる) TRISB = 0b00010110; // ピン(RB)はRB4(SCL1)/RB1(SDA1)のみ入力 WPUB = 0b00010010; // RB1/4は内部プルアップ抵抗を指定する PORTA = 0b00000000; // RA出力ピンの初期化(全てLOWにする) PORTB = 0b00000000; // RB出力ピンの初期化(全てLOWにする) APFCON0bits.RXDTSEL = 1; APFCON1bits.TXCKSEL = 1; T1CON = 0x21; //Fosc/4, ps:1/4 TMR1H = 0x00; TMR1L = 0x00; PIE1bits.TMR1IE = 1; I2C_init(); LCD_init(); UART_init(PIC16F1827); INTCONbits.PEIE = 1; INTCONbits.GIE = 1; uint8_t buf[70]; ringbuf_init(&tx_buf, buf, sizeof (buf)); RTC_Write(0x07, 0x00); while (1) { display(); if (sw_RA1.flag.press) { sw_RA1.flag.press=0; tx_send('2'); tx_send('0'); tx_send(((YY & 0xF0) >> 4) + '0'); tx_send((YY & 0x0F) + '0'); tx_send('/'); tx_send(((MM & 0xF0) >> 4) + '0'); tx_send((MM & 0x0F) + '0'); tx_send('/'); tx_send(((DD & 0xF0) >> 4) + '0'); tx_send((DD & 0x0F) + '0'); tx_send('('); tx_send((EE & 0x0F) + '0'); tx_send(')'); tx_send(((hh & 0xF0) >> 4) + '0'); tx_send((hh & 0x0F) + '0'); tx_send(':'); tx_send(((mm & 0xF0) >> 4) + '0'); tx_send((mm & 0x0F) + '0'); tx_send('-'); tx_send(((ss & 0xF0) >> 4) + '0'); tx_send((ss & 0x0F) + '0'); tx_send('.'); tx_send('\n'); tx_sends("Real Time Clock\n"); } if (sw_RA0.flag.long_holding_1) { sw_RA0.flag.long_holding_1 = 0; timeset(&YY, 0, 99, 0x06, 0, 0); timeset(&MM, 1, 12, 0x05, 3, 0); timeset(&DD, 1, month_length(YY, MM), 0x04, 6, 0); timeset(&hh, 0, 23, 0x02, 0, 1); timeset(&mm, 0, 59, 0x01, 3, 1); timeset(&ss, 0, 59, 0x00, 6, 1); } }