//************************************************************ // I2C_RTC Check(CPU: dsPIC30F4013) // // Soft I2C // Used RTC8564 // // LCD unlook busy : not switch direction (write fixed) // // CPU dsPIC30F4013 // 7.37MHz Internal RC oscillator, 16x PLL enabled // Fcy=7.37MHzx16/4=29.48MHz, Tcy=33.92ns // // 2010/9/07: N.Ishii //************************************************************* #include // Divice configuration _FWDT(WDT_OFF); _FGS(CODE_PROT_OFF); _FOSC(CSW_FSCM_OFF & FRC_PLL16); //OSC, PLL Setting _FBORPOR(PBOR_OFF & PWRT_64 & MCLR_EN); // message table for RTC 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"}; // LCD Define: R/W_pin= GND (Fixed Write) #define LCD_DATA_WR_MODE LATFbits.LATF1 = 1 // RS_pin = 1 #define LCD_INST_WR_MODE LATFbits.LATF1 = 0 // RS_pin = 0 #define LCD_ENABLE_ON LATFbits.LATF0 = 1 #define LCD_ENABLE_OFF LATFbits.LATF0 = 0 #define LCD_DATA_BIT4 LATBbits.LATB9 #define LCD_DATA_BIT5 LATBbits.LATB10 #define LCD_DATA_BIT6 LATBbits.LATB11 #define LCD_DATA_BIT7 LATBbits.LATB12 //--------------------------------------------------------------------------------------------- // LCD Instraction Code #define FOUR_BIT_FONT5x7dot 0x28 // + TWO_LINE(1/16Duty), SEG1_50_SEG51_100, COM1_COM16 #define DISP_ON_CURSOR_ON_BLINK_OFF 0x0e #define DISP_CLEAR_CURSOR_HOME 0x01 #define RAM_WR_AFTER_PLUS_1 0x06 // Entory mode set #define DDRAM_START_ADDRESS_SET 0x80 #define DISP_ON_CURSOR_OFF_BLINK_OFF 0x0c //--------------------------------------------------------------------------------------------- // I2C define #define RTC8564_SLVADRS_WR 0xa2 #define RTC8564_SLVADRS_RD 0xa3 #define SDA_OUT LATFbits.LATF2 #define SCL_OUT LATFbits.LATF3 #define SDA_IN PORTFbits.RF2 //--------------------------------------------------------------------------------------------- // For I2C // initialize RTC8564 Side Reg Write Data unsigned char rtc_write_data_tbl[] = { 0x20, // [0]:Control1REG: Time Stop 0x00, // [1]:Control2REG: ALM INT.TIMMER INT Desable 0x00, // [2]:SecondsREG: Set 0 Set 0x00, // [3]:MinutesREG: Set 0 Min 0x00, // [4]:HourREG: Set 0 Hour 0x15, // [5]:DayREG 0x02, // [6]:WeekdaysREG: TUE 0x06, // [7]:MonthsREG 0x10, // [8]:YearsREG 0x80, // [9]:Minute_AlamREG: Not Used ALM Interrupt 0x80, // [10]:Hour_AlamREG 0x80, // [11]:Day_AlamREG 0x80, // [12]:Weekday_AlamREG 0x83, // [13]:CLKOUT_frequencyREG: Set 1Hz Output ON 0x00, // [14]:Timer_contorolREG: Not Used Timer Interrupt 0x01 // [15]:Timer_downcount Set Vule }; unsigned char rec_data[16]; //------------------------------------------------------------------------- // Delay Subrutin void Waitx1ms(int x) // Td = 1mS * x { int i,j; for(i = 0 ; i < x ; ++i) { for(j = 0 ; j < 5000 ; ++j) asm("clrwdt"); //Td=1mS } } void Waitx1us(int x) // Td = 1uS * x { int i,j; for(i = 0 ; i < x ; ++i) { for(j = 0 ; j < 5 ; ++j) asm("clrwdt"); //Td=1uS } } //-------------------------------------------------------------------- void one_chr_wr(char chr_code) { LCD_DATA_WR_MODE; //RS_pin = 1: select data Reg // A Higher 4bit data out LCD_DATA_BIT7 = (unsigned int)((chr_code & 0x80)>>7); LCD_DATA_BIT6 = (unsigned int)((chr_code & 0x40)>>6); LCD_DATA_BIT5 = (unsigned int)((chr_code & 0x20)>>5); LCD_DATA_BIT4 = (unsigned int)((chr_code & 0x10)>>4); Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; // A Lower 4bit data out LCD_DATA_BIT7 = (unsigned int)((chr_code & 0x08)>>3); LCD_DATA_BIT6 = (unsigned int)((chr_code & 0x04)>>2); LCD_DATA_BIT5 = (unsigned int)((chr_code & 0x02)>>1); LCD_DATA_BIT4 = (unsigned int)(chr_code & 0x01); Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; Waitx1us(50); } void lcd_chr_wr(char *buffer) { while(*buffer != '\0') { one_chr_wr(*buffer); /* calling another function */ /* to write each char to the lcd module */ buffer++; } } void lcd_inst_wr(char inst_code) { LCD_INST_WR_MODE; //RS_pin = 0: select Instruction Reg // A Higher 4bit data out LCD_DATA_BIT7 = (unsigned int)((inst_code & 0x80)>>7); LCD_DATA_BIT6 = (unsigned int)((inst_code & 0x40)>>6); LCD_DATA_BIT5 = (unsigned int)((inst_code & 0x20)>>5); LCD_DATA_BIT4 = (unsigned int)((inst_code & 0x10)>>4); Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; // A Lower 4bit data out LCD_DATA_BIT7 = (unsigned int)((inst_code & 0x08)>>3); LCD_DATA_BIT6 = (unsigned int)((inst_code & 0x04)>>2); LCD_DATA_BIT5 = (unsigned int)((inst_code & 0x02)>>1); LCD_DATA_BIT4 = (unsigned int)(inst_code & 0x01); Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; if ((inst_code & 0x03) != 0) Waitx1ms(20); // inst_code is Clear(0x01) or Cursor Home(0x02) Then Wait 20mS else Waitx1us(50); // other code Then Wait 50uS } void lcd_init(void) { // Allow a delay(minimum of 15ms) Waitx1ms(20); //------ 1st step: port_output regster set -------------- // Initialize the data port/control pins to zero LCD_DATA_BIT7 = 0; LCD_DATA_BIT6 = 0; LCD_DATA_BIT5 = 0; LCD_DATA_BIT4 = 0; LCD_INST_WR_MODE; //RS_pin = 0 LCD_ENABLE_OFF; //E_pin = 0 //-------------------------------------------------------- // dsPIC30F4013 //------- 2nd step: port_mode regster set --------------- // Configure the data pins as output TRISBbits.TRISB9 =0; // RB9 is LCD DB4 OUTPUT TRISBbits.TRISB10 =0; // RB10 is LCD DB5 OUTPUT TRISBbits.TRISB11 =0; // RB11 is LCD DB6 OUTPUT TRISBbits.TRISB12 =0; // RB12 is LCD DB7 OUTPUT // Make all control pins as outputs TRISFbits.TRISF0 =0; // RF0 is LCD Enable OUTPUT TRISFbits.TRISF1 =0; // RF1 is LCD RS OUTPUT //------------------------------------------------------- // Initialize stage 1: Set stage 1 only 8-bit Interface // Set Upper 4 Bit Data on RB9 - RB12 LCD_DATA_BIT7 = 0; LCD_DATA_BIT6 = 0; LCD_DATA_BIT5 = 1; LCD_DATA_BIT4 = 1; Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; Waitx1ms(5); // Initialize stage 2: Set stage 2 only 8-bit Interface LCD_DATA_BIT7 = 0; LCD_DATA_BIT6 = 0; LCD_DATA_BIT5 = 1; LCD_DATA_BIT4 = 1; Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; Waitx1ms(1); // Initialize stage 3: Set stage 3 only 8-bit Interface LCD_DATA_BIT7 = 0; LCD_DATA_BIT6 = 0; LCD_DATA_BIT5 = 1; LCD_DATA_BIT4 = 1; Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; Waitx1ms(1); // Initialize stage 4: 4-bit Interface LCD_DATA_BIT7 = 0; LCD_DATA_BIT6 = 0; LCD_DATA_BIT5 = 1; LCD_DATA_BIT4 = 0; Waitx1us(1); LCD_ENABLE_ON; Waitx1us(1); LCD_ENABLE_OFF; Waitx1ms(1); //----- Fixed LCD Setting------------ lcd_inst_wr(FOUR_BIT_FONT5x7dot); // Function Set lcd_inst_wr(DISP_ON_CURSOR_ON_BLINK_OFF); lcd_inst_wr(DISP_CLEAR_CURSOR_HOME); // Set DDRAM Address = 0 lcd_inst_wr(RAM_WR_AFTER_PLUS_1); // Entry mode Set } 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(rec_data[8], 0x82); // Years: 10 RTC_variable_disp(rec_data[7] & 0x1f, 0x85); // Months: 1-12 (Noise mask: AND 1Fh Add 0613) RTC_variable_disp(rec_data[5] & 0x3f, 0x88); // Day: 1-31 lcd_inst_wr(0x8b); if ((rec_data[6] & 0x07) == 0) {lcd_chr_wr(msg2);} // 'SUN' if ((rec_data[6] & 0x07) == 1) {lcd_chr_wr(msg3);} // 'MON' if ((rec_data[6] & 0x07) == 2) {lcd_chr_wr(msg4);} // 'TUE' if ((rec_data[6] & 0x07) == 3) {lcd_chr_wr(msg5);} // 'WED' if ((rec_data[6] & 0x07) == 4) {lcd_chr_wr(msg6);} // 'THU' if ((rec_data[6] & 0x07) == 5) {lcd_chr_wr(msg7);} // 'FRI' if ((rec_data[6] & 0x07) == 6) {lcd_chr_wr(msg8);} // 'SAT' RTC_variable_disp(rec_data[4] & 0x3f, 0xc0); // Hour: 0-24 RTC_variable_disp(rec_data[3] & 0x7f, 0xc3); // Minutes: 0-59 RTC_variable_disp(rec_data[2] & 0x7f, 0xc6); // Seconds: 0-59 } //------------------------------------------------------------------------------ // Soft I2C Sub routine void i2c_write(unsigned char send_data) { unsigned char i; for (i = 0; i <= 7; ++i) { if (( send_data & 0x80) == 0) SDA_OUT = 0; else SDA_OUT = 1; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SCL_OUT = 0; send_data <<= 1; } } unsigned char i2c_read(void) { unsigned char i; unsigned char read_data; TRISFbits.TRISF2=1; // RF2 is SDA Input for (i = 0; i <= 7; ++i) { read_data <<= 1; read_data |= SDA_IN; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SCL_OUT = 0; } return(read_data); } void receive_ack(void) { TRISFbits.TRISF2=1; // RF2 is SDA Hi-Z(input) Waitx1us(5); SCL_OUT = 1; while(SDA_IN != 0); // Wait ACK Waitx1us(5); SCL_OUT = 0; TRISFbits.TRISF2=0; // RF2 is SDA output } //------------------------------------------------------------------------------------ void i2c_write_sr(unsigned char start_adrs, unsigned char end_adrs) { unsigned char i; // START Bus Event TRISFbits.TRISF3=0; // RF3 is SCL output TRISFbits.TRISF2=0; // RF2 is SDA output SCL_OUT = 1; SDA_OUT = 1; Waitx1us(5); SDA_OUT = 0; Waitx1us(5); SCL_OUT = 0; // Send Slave Address + Writebit i2c_write(RTC8564_SLVADRS_WR); // Receive ACK From RTC8564 receive_ack(); // Send Write Top Address i2c_write(start_adrs); // Receive ACK From RTC8564 receive_ack(); // Send RTC_REG Write Data (Write Address Auto Incriment) for (i = start_adrs; i != end_adrs + 1; ++i) { i2c_write(rtc_write_data_tbl[i]); // Receive ACK From RTC8564 receive_ack(); } // Stop Bus Event SDA_OUT = 0; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SDA_OUT = 1; TRISFbits.TRISF3=1; // RF3 is SCL Hi-Z TRISFbits.TRISF2=1; // RF2 is SDA Hi-Z Waitx1us(200); // Delay for 200uS (needs > 1.3uS next start) } void i2c_read_sr(unsigned char start_adrs, unsigned char end_adrs) { unsigned char i; // START Bus Event TRISFbits.TRISF3=0; // RF3 is SCL output TRISFbits.TRISF2=0; // RF2 is SDA output SCL_OUT = 1; SDA_OUT = 1; Waitx1us(5); SDA_OUT = 0; Waitx1us(5); SCL_OUT = 0; // Send Slave Address + Writebit i2c_write(RTC8564_SLVADRS_WR); // Receive ACK From RTC8564 receive_ack(); // Send Read Start Address i2c_write(start_adrs); // Receive ACK From RTC8564 receive_ack(); // RESTART Bus Event SDA_OUT = 1; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SDA_OUT = 0; Waitx1us(5); SCL_OUT = 0; // Send Slave Address + Readbit i2c_write(RTC8564_SLVADRS_RD); // Receive ACK From RTC8564 receive_ack(); // Receive RTC_REG Read Data (Read Address Auto Incriment) for (i = start_adrs; i != end_adrs + 1; ++i) { rec_data[i] = i2c_read(); // Send ACK From PIC if (i != end_adrs) { TRISFbits.TRISF2=0; // RF2 is SDA output SDA_OUT = 0; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SCL_OUT = 0; } } // Send NACK From PIC: End of Rx TRISFbits.TRISF2=0; // RF2 is SDA output SDA_OUT = 1; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SCL_OUT = 0; // Stop Bus Event SDA_OUT = 0; Waitx1us(5); SCL_OUT = 1; Waitx1us(5); SDA_OUT = 1; TRISFbits.TRISF3=1; // RF3 is SCL Hi-Z TRISFbits.TRISF2=1; // RF2 is SDA Hi-Z Waitx1us(200); // Delay for 200uS (needs > 1.3uS next start) } //--------------------------------------------------------------------------------------------- // INT0 External Interrupt T=1Sec void __attribute__((__interrupt__ , no_auto_psv)) _INT0Interrupt(void) { PORTDbits.RD0 = 0; // Red LED ON i2c_read_sr(2,8); // Read SecondsREG - YearsREG Add 0614 variable_lcd_disp(); // '2010/xx/xx/xx(xxx) xx:xx:xx //Reset INT0 interrupt flag IFS0bits.INT0IF = 0; } //------------------------------------------------------------------------------------------ // Main routine int main(void) { // Set ADPCFG: RB1 Port is All Digital Pin ADPCFG = 0xFFFF; TRISDbits.TRISD0=0; // RD0 is LED output used debug PORTDbits.RD0 = 1; // Red LED OFF // Initialize I2C Port TRISFbits.TRISF3=1; // RF3 is SCL Hi-Z TRISFbits.TRISF2=1; // RF2 is SDA Hi-Z // Initialize LCD lcd_init(); lcd_inst_wr(DISP_ON_CURSOR_OFF_BLINK_OFF); // INT0 pin to interupt on Rise Edge Trig INTCON2bits.INT0EP=0; Waitx1ms(1000); // Wait 1Sec // Initialize Slave RTC8564 i2c_write_sr(0,15); // Write RTC_Reg 16bytes: adrs = 0 to 15 -> Time Stop // CLKOUT = 1Hz const_lcd_disp(); // '20xx/ / ...etc // RTC Time Start-> RTC_CLOCKOUT ON rtc_write_data_tbl[0] = 0x00; i2c_write_sr(0,0); // Enable Interrupt IEC0bits.INT0IE = 1; // Enable INT0 Interrupt Service Routine DISICNT = 0x0000; // Enable Interrupt while(1) { PORTDbits.RD0 = 1; // Red LED OFF } }