Touch the buttons-simulate an I2C-driven ts12 Chip

Source: Internet
Author: User
Tags dio

Ts12 chip driver

Use the analog I2C driver ts12 to complete the touch button detection function:

# Include "ipv8s. H "# include" Global. H "# include" drv_key.h "uint8_t keybyte; /*************************************** **************************************** // function: i2c_int // Description: simulate I2C and ds1307 port initialization // Param: // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static void i2c_int (void) {iic_sda_output; iic_scl_output ;}/ **************************************** ***************************************/ /Function: delay_5us // Description: the latency of the latency function is about 16 us // Param: // return: FCPU 16 MHz // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static void delay_5us (void) {uint8_t I; for (I = 5; I> 0; I --);} /*************************************** ****************** * ******************** // Function: i2c_start // description: i2C starts to transmit signals when the SCL is high, SDA changes from high to low // Param: // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static void i2c_start (void) {// SDA 1-> 0 while SCL high sda_high; scl_high; delay_5us (); sda_low; delay_5us (); scl_low ;} /*************************************** ****************** * ******************** // Function: i2c_stop // description: i2C stop transmission signal when the SCL is high, SDA changes from low to high // Param: // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static void i2c_stop (void) {// SDA 0-> 1 while SCL high sda_low; scl_high; delay_5us (); sda_high; delay_5us ();} /*************************************** ***************************** * ********* // Function: i2c_sendack // Description: The host sends a response signal to the slave host // Param: Response Signal 1: SDA high (no response) 0: SDA low (with a response) // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static void i2c_sendack (uint8_t ACK) {If (ACK = 0) {sda_low;} else {sda_high;} scl_high; delay_5us (); scl_low; delay_5us ();} /*************************************** * ***************************************/ /Function: i2c_sendbyte // Description: simulate I2C communication to send 8-Bit Data // Param: The sent 8 is the data value // return: return response signal 0 indicates there is a response 1 indicates no response // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static uint8_t i2c_sendbyte (uint8_t sendbyte) {static uint8_t I, revackts; sda_low; for (I = 0; I <8; I ++) {scl_low; If (sendbyte & 0x80) // wri Te data {sda_high;} else {sda_low;} delay_5us (); sendbyte <= 1; scl_high; delay_5us ();} scl_low; sda_high; lower (); lower; scl_high; ASM ("NOP"); ASM ("NOP"); revackts = (uint8_t) read_sda; delay_5us (); scl_low; iic_sda_output; delay_5us (); Return revackts ;} /*************************************** **************************************** // function: i2c_recvbyte // Description: simulate I2C communication The email reads 8-bit data from the slave machine // Param: // return: The returned 8 data values are: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/static uint8_t i2c_recvbyte () {uint8_t I; uint8_t recvdata = 0; sda_high; // latch the data port befor reading iic_sda_input; for (I = 0; I <8; I ++) {recvdata <= 1; scl_high; ASM ("NOP"); ASM ("NOP"); If (read_sda) {recvdata | = 0x01;} else {Recv Data & = 0xfe;} delay_5us (); scl_low; delay_5us ();} iic_sda_output; return recvdata ;} /*************************************** **************************************** // function: ts12_writebyte // Description: simulate I2C communication and write 1 byte of data to the specified address // Param: writeaddr: data to be written writedata; write address // return: 1: successfully written 0: write error // Author: huangzhigang 2014-0410 ************************************ *********************************** * ******/Uint8_t ts12_writebyte (uint8_t writeaddr, uint8_t writedata) {i2c_start (); If (0xf0) // device ADDR + write (Operation) {return 0;} If (i2c_sendbyte (writeaddr) {return 0;} If (i2c_sendbyte (writedata) {return 0;} i2c_stop (); return 1 ;} /*************************************** **************************************** // function: ts12_readbyte // Description: reads 1 word from the specified address through analog I2C communication. Section Data // Param: readaddr: Read address // return: revdata: read 8-Bit Data // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/uint8_t ts12_readbyte (uint8_t readaddr) {uint8_t revdata; i2c_start (); i2c_sendbyte (0xf0); // device ADDR + write (operation) values (readaddr); i2c_start (); i2c_sendbyte (0xf1 ); // device ADDR + Read (operation) revdata = i2c_rec Vbyte (); i2c_sendack (1); i2c_stop (); Return revdata;} void ts12_initagain () {i2c_start (); random (0xf0); i2c_sendbyte (0x02 ); values (0xaa); // sensitivity1 i2c_sendbyte (0xaa); i2c_sendbyte (0xaa); values (0xaa); values (0xaa); values (0xaa); i2c_sendbyte (0x33 ); // ctrl1 i2c_stop (); i2c_start (); i2c_sendbyte (0xf0); i2c_sendbyte (0x0a); i2c_sendbyte (0x00); // ref_rst1 i2c_sendbyte (0x00); // ref_rst2 i2c_sendbyte (0x00); // ch_hold1 i2c_sendbyte (0x00); // ch_hold2 i2c_sendbyte (0x00 ); // cal_hold1 i2c_sendbyte (0x00); // cal_hold2 i2c_stop ();} /*************************************** **************************************** // function: ts12_init // Description: simulate I2C communication and write 8 bytes of data from 0x02 ~ 0x0f // Param: pwritedata: pointer pointing to the address of the array to be written // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/void ts12_init () {i2c_int (); i2c_start (); i2c_sendbyte (0xf0); // device ADDR + write (operation) values (0x09); i2c_sendbyte (0x0f); i2c_stop (); ts12_initagain (); i2c_start (); i2c_sendbyte (0xf0); // device ADDR + write (operation) I2C _ Sendbyte (0x09); i2c_sendbyte (0x07); i2c_stop ();} /*************************************** **************************************** // function: ts12_readkey // Description: reads 8 bytes of data from 0x10 ~ 0x12 // Param: preaddata: pointer to the array that stores the data // return: // Author: huangzhigang 2014-0410 ************************************ **************************************** * **/void ts12_readkey () {static uint8_t keyvalue1; static uint8_t keyvalue2; static uint8_t keyvalue3; i2c_start (); values (0xf0); // device ADDR + write (operation) i2c_sendbyte (0x10 ); // i2c_stop (); // you can add i2c_start (); i2c_sendbyte (0xf1); // device ADDR + Read (Operation) keyvalue1 = i2c_recvbyte (); i2c_sendack (0); // Dio low level indicates ack keybyte = key_event_null_click; // If (keyvalue1> 6) & 0x03)> = keystandard) // I-sense {keybyte + = key_event_smart_click;} If (keyvalue1> 4) & 0x03)> = keystandard) // crisper {keybyte + = key_event_criper_click;} If (keyvalue1> 2) & 0x03)> = keystandard) // mode {keybyte + = key_event_mode_click ;} if (keyvalue1) & 0x03> = keystandard) // down {keybyte + = key_event_down_click;} keyvalue2 = i2c_recvbyte (); i2c_sendack (0 ); // Dio low-level expression ack if (keyvalue2> 6) & 0x03)> = keystandard) // auto defrost {keybyte + = key_event_ad_click ;} if (keyvalue2> 4) & 0x03)> = keystandard) // up {keybyte + = key_event_ice_click;} If (keyvalue2> 2) & 0x03)> = keystandard) // fast ice {keybyte + = key_event_up_click;} If (keyvalue2 & 0x03)> = keystandard) // energy saving {keybyte + = key_event_save_click;} keyvalue3 = i2c_recvbyte (); i2c_sendack (1); If (keyvalue3 & 0x03)> = keystandard) // Manual defrost {keybyte + = key_event_md_click;} i2c_stop ();}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.