//************************************************************ // RTCC Check // // Used PIC24F Internal RTCC Module // // LCD unlook busy : not switch direction (write fixed) // // CPU PIC24FJ64GA002 // // Condition: // 8MHz Internal RC oscillator, 4x PLL (8MHzx4= 32MHz) // Fcy=32MHz/2=16MHz, Tcy=62.5ns // // 2011/8/18: N.Ishii //************************************************************* #include #include "My_LCD_Lib.h" // Set Configuration Word 1 _CONFIG1 ( JTAGEN_OFF & // JTAG Port: OFF GCP_OFF & // Code Protect:@OFF GWRP_OFF & // Write ProtectFOFF BKBUG_OFF & // Background Debug: OFF COE_OFF & // Clip On Emulation: OFF ICS_PGx1 & // Select ICD Pin: EMUC/EMUD-> PGC1/PGD1 For Common Use FWDTEN_OFF // WDT: OFF ) // Set Configuration Word 2 _CONFIG2 ( IESO_OFF & // 2 Speed Start Up: OFF FNOSC_FRCPLL & // 8MHz Internal RC oscillator, 4x PLL-> 8MHzx4=32MHz FCKSM_CSDCMD & // Change Clock Control: OFF@Clock Monitor: OFF OSCIOFNC_ON & // OSCO/RA3 function: Used RA3 IOL1WAY_OFF & // RP Register Protection: Unlimited Writes To RP Registers I2C1SEL_PRI & // I2C1 pins Select: Use Primary I2C1 pins POSCMOD_NONE // Oscillator Selection: Primary disabled ) //----------------------------------------------------------------------------------- /// message table char msg1[] = {"20\0"}; // Years 20** char msg2[] = {"SUN\0"}; // Weekdays char msg3[] = {"MON\0"}; char msg4[] = {"TUE\0"}; char msg5[] = {"WED\0"}; char msg6[] = {"THU\0"}; char msg7[] = {"FRI\0"}; char msg8[] = {"SAT\0"}; //---------------------------------------------------------------------------------- /// RTCC Data Buffer unsigned int readTimeDate[4]; //------------------------------------------------------------------------- /// Define LED Port #define LED_500ms LATAbits.LATA3 #define LED_ALM LATBbits.LATB6 //------------------------------------------------------------------------ void const_lcd_disp(void) { // Initialize RTC Screen LCD Disp lcd_inst_wr(0x80); lcd_chr_wr(msg1); // '20' const disp1 lcd_inst_wr(0x84); one_chr_wr(0x2f); // '/' const disp2 lcd_inst_wr(0x87); one_chr_wr(0x2f); // '/' const disp3 lcd_inst_wr(0x87); one_chr_wr(0x2f); // '/' const disp3 lcd_inst_wr(0x8a); one_chr_wr(0x28); // '(' const disp4 lcd_inst_wr(0x8e); one_chr_wr(0x29); // ')' const disp5 lcd_inst_wr(0xc2); one_chr_wr(0x3a); // ':' const disp6 lcd_inst_wr(0xc5); one_chr_wr(0x3a); // ':' const disp6 } //------------------------------------------------------------------------- void RTC_variable_disp(unsigned char Vol, unsigned char loc) { // Vol(BCD): 0x00-0x59( 00-59 Disp) unsigned char ascii[2]; // BCD_to_Ascii ascii[0] = (Vol & 0x0f) | 0x30; ascii[1] = ((Vol >> 4) & 0x0f) | 0x30; // LCD Disp lcd_inst_wr(loc); one_chr_wr(ascii[1]); one_chr_wr(ascii[0]); } void variable_lcd_disp(void) { RTC_variable_disp((char)(readTimeDate[0] & 0x00ff), 0x82); // Years: 10 RTC_variable_disp((char)((readTimeDate[1] >> 8) & 0x00ff), 0x85); // Months: 1-12 RTC_variable_disp((char)(readTimeDate[1] & 0x00ff) , 0x88); // Day: 1-31 lcd_inst_wr(0x8b); if (((readTimeDate[2] >> 8) & 0x00ff) == 0) {lcd_chr_wr(msg2);} // 'SUN' if (((readTimeDate[2] >> 8) & 0x00ff) == 1) {lcd_chr_wr(msg3);} // 'MON' if (((readTimeDate[2] >> 8) & 0x00ff) == 2) {lcd_chr_wr(msg4);} // 'TUE' if (((readTimeDate[2] >> 8) & 0x00ff) == 3) {lcd_chr_wr(msg5);} // 'WED' if (((readTimeDate[2] >> 8) & 0x00ff) == 4) {lcd_chr_wr(msg6);} // 'THU' if (((readTimeDate[2] >> 8) & 0x00ff) == 5) {lcd_chr_wr(msg7);} // 'FRI' if (((readTimeDate[2] >> 8) & 0x00ff) == 6) {lcd_chr_wr(msg8);} // 'SAT' RTC_variable_disp((char)(readTimeDate[2] & 0x00ff), 0xc0); // Hour: 0-24 RTC_variable_disp((char)(readTimeDate[3] >> 8), 0xc3); // Minutes: 0-59 RTC_variable_disp((char)(readTimeDate[3] & 0x00ff), 0xc6); // Seconds: 0-59 } //------------------------------------------------------------------------------------------------------------ /// Function Contorol RTCC /// Unlock Sequence /// Set RCFGCAL RTCWREN Bit void RTCCUnlock(void){ asm volatile("disi #5"); // Disable Interrupts asm volatile("mov #0x55, w7"); // asm volatile("mov w7, _NVMKEY"); // 0x55 -> NVM Reg (NVMKEY File) asm volatile("mov #0xAA, w8"); // asm volatile("mov w8, _NVMKEY"); // 0xAA -> NVM Reg (NVMKEY File) asm volatile("bset _RCFGCAL, #13");// Set the RTCWREN bit (RCFGCAL 13bit) asm volatile("nop"); asm volatile("nop"); } /// Unlock Sequence /// Valid SOSC void SOSCEn(void){ asm("MOV #OSCCON,w1"); // Set OSCCONL Address asm("MOV.b #0x32,w0"); asm("MOV #0x46,w2"); asm("MOV #0x57,w3"); asm("MOV.b w2,[w1]"); // Write unlock key code:0x46 -> OSCCON Low8bit asm("MOV.b w3,[w1]"); // Write unlock key code:0x57 -> OSCCON Low8bit asm("MOV.b w0,[w1]"); // Write 0x32 -> OSCCON Low8bit: Valid Secondary OSC(32kHz) } //------------------------------------------------------------------------------------------------------------ /**************************** * RTCC Interrupt: ALM Disp. *****************************/ void __attribute__ ((interrupt, no_auto_psv)) _RTCCInterrupt(void) { IFS3bits.RTCIF = 0; // Clear Interrupt Flag LED_ALM = !LED_ALM; } /*********************************** * T1 Interrupt: Clock Disp. to LCD ************************************/ void __attribute__ ((interrupt, no_auto_psv)) _T1Interrupt(void) { IFS0bits.T1IF = 0; // Clear Interrupt Flag LED_500ms = !LED_500ms; // Blink LED -> Blink Period= 1Sec(0.5Sec_ON, 0.5Sec_OFF) // Read Real Time RCFGCALbits.RTCPTR = 3; // Set RTCPTR1:RTCPTR0 (RTC Pointer) readTimeDate[0] = RTCVAL; // Year readTimeDate[1] = RTCVAL; // Month Day readTimeDate[2] = RTCVAL; // Week Hour readTimeDate[3] = RTCVAL; // Min Sec // RTCC LCD Disp variable_lcd_disp(); // '2010/xx/xx/xx(xxx) xx:xx:xx } //------------------------------------------------------------------------------------------------------------ /// Function Main int main(void) { // CPU Clock Pre Scalere 1:1 CLKDIV = 0; // Set AD1PCFG: ANx Port is All Digital Pin AD1PCFG = 0xFFFF; // Set Port Direction TRISAbits.TRISA3 = 0; // RA3 is RED_LED output (LED_500ms) TRISBbits.TRISB6 = 0; // RB6 is RED_LED output (LED_ALM) // Inittalize LED OFF: add 110818 LED_ALM = 1; LED_500ms = 1; /* // Inittalize LCD lcd_init(); lcd_inst_wr(DISP_ON_CURSOR_OFF_BLINK_OFF); const_lcd_disp(); // '20xx/ / ...etc */ // Inittalize Timer1:T1= 1/(Fsosc/(PR1+1))= 1/(32768/16383+1)= 1/2 = 0.5Sec (PS=1/1) PR1 = 16383; IPC0bits.T1IP = 5; // Interrupt Priority Level 5 T1CON = 0b1000000000000010; // Inittalize T1 IFS0bits.T1IF = 0; IEC0bits.T1IE = 1; // Enable Interrupt // Valid SOSC SOSCEn(); // Start SOSC // Inittalize RTCC RTCCUnlock(); // Unlock Sequence for RCFGCAL // RCFGCAL = 0xAC00; // Valid RTCC -> Start RTCC (Valid RTCC pin) RCFGCAL = 0xA800; // Valid RTCC -> Start RTCC (Non Valid RTCC pin) // PADCFG1bits.RTSECSEL = 1; // RTCC Outpin is 1Sec Pulse Out -> Not Used // Set Inittal Real Time:2010.12.31 23:59:00 RCFGCALbits.RTCPTR = 3; // Set RTCPTR1:RTCPTR0 (RTC Pointer) RTCVAL = 0x0010; // Year RTCVAL = 0x1231; // Month Day RTCVAL = 0x0523; // Week Hour RTCVAL = 0x5900; // Min Sec RCFGCALbits.RTCWREN = 0; // Lock RTCC // Set Inittal ALM Time:1.1 0:0:15 ALCFGRPTbits.ALRMPTR = 2; // Set ALRMPTR1:ALRMPTR0 (ALRM Pointer) ALRMVAL = 0x0101; // Month Day ALRMVAL = 0x0400; // Week Hour ALRMVAL = 0x0015; // Min Sec ALCFGRPT = 0x8C03; // 1Min mask(Repeat 3 Times/xxMin15Sec) // Set RTCC Interrupt IPC15bits.RTCIP = 5; // Interrupt Priority Level 5 IEC3bits.RTCIE = 1; // Enable Interrupt // Inittalize LCD lcd_init(); lcd_inst_wr(DISP_ON_CURSOR_OFF_BLINK_OFF); const_lcd_disp(); // '20xx/ / ...etc /// Main Loop while(1) { } }