Ht1381 clock chip driver
# Include "Global. H "// clock chip ht1381 pin definitions # define ht1381_cs_gpio_port gpiod # define serial gpio_pin_3 # define ht1381cshigh () (mirror-> ODR | = (u8) (mirror )) # define ht1381cslow () (ht1381_cs_gpio_port-> ODR & = (u8 )(~) # Define ht1381_data_gpio_port gpiod # define ht1381_data_gpio_pin gpio_pin_2 # define ht1381datahigh () (ht1381_data_gpio_port-> ODR | = (u8) (bytes) # define ht1381datalow () (ht1381_data_gpio_port-> ODR & = (u8 )(~) # Define ht1381dataout () gpio_init (ht1381_data_gpio_port, timeout, timeout); # define ht1381datain () gpio_init (ht1381_data_gpio_port, timeout, timeout ); # define ht1381datastatus (ht1381_data_gpio_port-> IDR & gt) # define ht1381_clk_gpio_port gpiod # define gpio_pin_0 # define ht1381clkhigh () (H T1381_clk_gpio_port-> ODR | = (u8) (ht1381_clk_gpio_pin) # define ht1381clklow () (ht1381_clk_gpio_port-> ODR & = (u8 )(~ Ht1381_clk_gpio_pin) extern void int_ht1381 (void); ictimertab_tictimertab; authorization; void delay_us (u8 data) {for (u8 I = 0; I <data; I ++) {Nop ();}} /*************************************** **************************** write 1byte data to ht1381 ****** **************************************** * *******************/void send_ht1380_1byte (unsigned char tdata) {unsigned char I; ht1381dataout (); for (I = 0; I <8; I ++) {If (tdata & 0x01 )! = 0) {ht1381datahigh () ;}else {ht1381datalow ();} tdata >>=1; delay_us (8); ht1381clkhigh (); delay_us (5); ht1381clklow (); delay_us (5 );}} /*************************************** **************************** read 1byte data from ht1381 ****** **************************************** * *******************/unsigned char read_ht1380_1byte (void) {unsigned char I, tdata; ht1381datain (); for (I = 0; I <8; I ++) {ht1381clkh Igh (); delay_us (8); tdata >>= 1; if (! Ht1381datastatus) {tdata & = 0x7f;} else {tdata | = 0x80;} ht1381clklow (); delay_us (5);} ht1381dataout (); Return tdata ;} /*************************************** * ***************************** Name: write Data to ht1381 * Description: Write the address first, then write the command/Data * Call: send_ht1380_1byte * input: saddr: ht1381 address, sdata: ************************************ * *****************************/void write_ht1381addr_1byte (unsigned char saddr, UN Signed Char sdata) {ht1381cslow (); delay_us (5); ht1381clklow (); delay_us (5); ht1381cshweigh (); send_ht1380_1byte (saddr); // write address, command send_ht1380_1byte (sdata); // write 1byte data ht1381clkhigh (); delay_us (5); ht1381cslow ();} /*************************************** * ***************************** Name: read data from an address of ht1381 * Description: Write the address first, then write the command/Data * Call: send_ht1380_1byte, read_ht1380_1byte * input: saddr: ht1381 address * return value: sdata: read data ****** **************************************** * *******************/Unsigned char read_ht1381addr_1byte (unsigned char saddr) {unsigned char tdata; ht1381cslow (); delay_us (5); ht1381clklow (); delay_us (5); ht1381cshigh (); send_ht1380_1byte (saddr); // write address, command tdata = read_ht1380_1byte (); // write 1byte data ht1381clkhigh (); delay_us (5); ht1381cslow (); Return tdata ;} /*************************************** ************* * *************** Name: Read the current ht1381 time * Call: read_ht1381addr_1byte * input: Save the current time address + current time format: second minute hour day month week Year * 7 byte (BCD code) 1B 1B 1B 1B 1B 1B ********************************* * ********************************/void read_ht1381_nowtimer (void) {unsigned char I; unsigned char saddr; unsigned char * P; saddr = 0x81; P = (unsigned char *) & ictimertabbuf; for (I = 0; I <7; I ++) {* P = read_ht1381addr_1byte (saddr); P ++; Saddr + = 2 ;}} /*************************************** * ***************************** Name: initialize ht1381 * Call: write_ht1381addr_1byte * input: initial time address + initial time format: second minute hour day month week Year * 7 byte (BCD code) 1B 1B 1B 1B 1B 1B ********************************* * *********************************/void int_ht1381 (void) {unsigned char I; unsigned char saddr; unsigned char * P; saddr = 0x80; P = (unsigned char *) & ictimertabbuf; write _ Ht1381addr_1byte (0x8e, 0x00); // control command, WP = 0, write operation? For (I = 0; I <7; I ++) {write_ht1381addr_1byte (saddr, * P); // second, minute, hour, day, month, week, year P ++; saddr + = 2;} write_ht1381addr_1byte (0x8e, 0x80); // control command, WP = 1, write protection ?} /*************************************** ******* **************************************** * *******************/Void int_timer (void) {// whether the clock chip is powered on. read_ht1381_nowtimer (); If (ictimertabbuf. timersec & 0x80 )! = 0) {read_ht1381_nowtimer (); If (ictimertabbuf. timersec & 0x80 )! = 0) {ictimertabbuf. timersec = 0x20; ictimertabbuf. timermin = 0x57; ictimertabbuf. timerhour = 0x18; ictimertabbuf. timerday = 0x11; ictimertabbuf. timermonth = 0x07; ictimertabbuf. timerweek = 0x04; ictimertabbuf. timeryear = 0x13; int_ht1381 (); // initialize ht1381} ictimertab. timersec = (ictimertabbuf. timersec/16) * 10 + (ictimertabbuf. timersec % 16); ictimertab. timermin = (ictimertabbuf. timermin/16) * 10 + (ictimertabbuf. timermin % 16); ictimertab. timerhour = (ictimertabbuf. timerhour/16) * 10 + (ictimertabbuf. timerhour % 16); ictimertab. timerday = (ictimertabbuf. timerday/16) * 10 + (ictimertabbuf. timerday % 16); ictimertab. timermonth = (ictimertabbuf. timermonth/16) * 10 + (ictimertabbuf. timermonth % 16); ictimertab. timerweek = (ictimertabbuf. timerweek/16) * 10 + (ictimertabbuf. timerweek % 16); ictimertab. timeryear = (ictimertabbuf. timeryear/16) * 10 + (ictimertabbuf. timeryear % 16 );}