8 电容触摸模块

8.1 引脚介绍

电容模块IC是用BL6133,这个模块是用IIC通信,还有一个中断引脚,如有触摸按下中断引脚会有电平变化。图7-4是BL6133与主控连接图。

 

 

图4.1

8.2 寄存器介绍

图7-5 是介绍本次用到的寄存器功能。

图4.2

8.3 电容触摸驱动程序

 

#define BL6133_ADDR 0x2C // 设备地址 7bit

#define BL6133_Chip_ID 0xE7 // chip ID

#define BL6133_Gesture 0x01 // Gesture Code

#define BL6133_Point_Numb 0x02 // Point Number

#define BL6133_Pos_XH 0x03 // X 高6bit

#define BL6133_Pos_XL 0x04 // X 低8bit

#define BL6133_Pos_YH 0x05 // Y 高4bit

#define BL6133_Pos_YL 0x06 // Y 低8bit

 

#define BL6133_X_OFFSET (0)

#define BL6133_Y_OFFSET (0)

 

/* static functions -----------------------------------------------------*/

/**

* @brief write data to register

* @param reg register address

* @param buf need write data

* @param len need write lenth

* @return true write OK

* @return false write err

*/

static bool _touch_write_reg(uint8_t ic_add , uint8_t reg, uint8_t *buf , uint32_t len)

{

uint32_t i ;

 

// start

IIC_Start();

 

// send addr

IIC_Send_Byte( ic_add << 1 );

if(IIC_Wait_Ack()){

goto ret_fail;

}

 

// send reg

IIC_Send_Byte( reg );

if(IIC_Wait_Ack()){

goto ret_fail;

}

 

// write data

for(i = 0 ; i < len ; i++){

IIC_Send_Byte( buf[i] );

if(IIC_Wait_Ack()){

goto ret_fail;

}

}

 

// stop

IIC_Stop();

return true;

 

ret_fail:

return false;

}

/**

* @brief read data from cst836u register

* @param reg register address

* @param buf read data buff

* @param len need read len

* @return true read OK

* @return false read err

*/

static bool _touch_read_reg(uint8_t ic_add , uint8_t reg, uint8_t *buf , uint32_t len)

{

uint32_t i = 0;

 

// start

IIC_Start();

 

// send addr

IIC_Send_Byte( ic_add << 1 );

if(IIC_Wait_Ack()){

goto ret_fail;

}

// send reg

IIC_Send_Byte( reg );

if(IIC_Wait_Ack()){

goto ret_fail;

}

IIC_Stop();

IIC_Start();

// read

IIC_Send_Byte((ic_add << 1) | 0x01);

if(IIC_Wait_Ack()){

goto ret_fail;

}

 

// read data

for(i = 0 ; i < len-1 ; i++){

buf[i] = IIC_Read_Byte(1);

}

buf[i] = IIC_Read_Byte(0);

 

// stop

IIC_Stop();

return true;

 

ret_fail:

return false;

}

/**

* @brief get one point data from cst836u

* @return true read ok

* @return false read err

*/

static bool bl6133_read_point(void)

{

uint8_t temp[6];

uint16_t x = 0xFFFF, y = 0xFFFF ;

 

if(NULL == tp_dev_bl6133 || NULL == tp_dev_bl6133->read_reg){

return false;

}

//读寄存器 读取6个字节

tp_dev_bl6133->read_reg( BL6133_ADDR , BL6133_Gesture, &temp[0] , 6);

 

x=(uint16_t)((temp[2]&0x3F)<<8)|temp[3];//高6位|低8位

y=(uint16_t)((temp[4]&0x0F)<<8)|temp[5];//高4位|低8位

 

tp_dev_bl6133->point.status = temp[2] >> 6 ;

 

tp_dev_bl6133->point.x = x + BL6133_X_OFFSET;

tp_dev_bl6133->point.y = y + BL6133_Y_OFFSET;

return true;

}

/**

* @brief read chipID

*/

static bool bl6133_read_chipID(void)

{

uint8_t chip_id[2] = {0};

 

if(NULL == tp_dev_bl6133 || NULL == tp_dev_bl6133->read_reg || NULL == tp_dev_bl6133->write_reg){

return false;

}

 

chip_id[0] = ~0x0A;

chip_id[1] = BL6133_Chip_ID;

 

if(!tp_dev_bl6133->write_reg(BL6133_ADDR , BL6133_Chip_ID , &chip_id[0] , 2))

{

return false;

}

 

chip_id[0] = 0x00;

chip_id[1] = 0x00;

if(!tp_dev_bl6133->read_reg( BL6133_ADDR , BL6133_Chip_ID, &chip_id[0] , 1))

{

return false;

}

 

tp_dev_bl6133->chipID = chip_id[1] << 8 | chip_id[0];

return true;

}

/**

* @brief init TP

* @param dev

*/

void bl6133_init(tp_dev_t *dev)

{

if(NULL == dev){

return ;

}

 

dev->read_point = bl6133_read_point;//函数指针指向read point函数

tp_dev_bl6133 = dev;

 

bl6133_read_chipID();//读ID

}

/**

* @brief init touch pin and init cst836u tp ic

*/

void touch_init(void)

{

gpio_init_type gpio_init_struct;

exint_init_type exint_init_struct;

 

crm_periph_clock_enable(TOUCH_RST_GPIO_CRM_CLK, TRUE);

// irq

crm_periph_clock_enable(CRM_SCFG_PERIPH_CLOCK, TRUE);

crm_periph_clock_enable(TOUCH_IRQ_GPIO_CRM_CLK, TRUE);

 

scfg_exint_line_config(TOUCH_IRQ_GPIO, TOUCH_IRQ_PIN);

 

exint_default_para_init(&exint_init_struct);

exint_init_struct.line_enable = TRUE;

exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;

exint_init_struct.line_select = TOUCH_IRQ_EXINT_LINE;

exint_init_struct.line_polarity = EXINT_TRIGGER_FALLING_EDGE;

exint_init(&exint_init_struct);

nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

nvic_irq_enable(TOUCH_IRQ_EXINT_IRQn, 1, 0);

 

/* configure the rst gpio */

gpio_default_para_init(&gpio_init_struct);

gpio_init_struct.gpio_pins = TOUCH_RST_PIN ;

gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;

gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;

gpio_init_struct.gpio_pull = GPIO_PULL_UP;

gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;

gpio_init(TOUCH_RST_GPIO, &gpio_init_struct);

 

// iic io

IIC_Init();

// rst set 1

TOUCH_RST_SET(1);

delay_ms(5);

TOUCH_RST_SET(0);

delay_ms(5);

TOUCH_RST_SET(1);

delay_ms(100);

//tp ic init

tp_dev.write_reg = _touch_write_reg;

tp_dev.read_reg = _touch_read_reg;

bl6133_init(&tp_dev);

}

//中断读取坐标值

void TOUCH_IRQ_EXINT_IRQHandler(void)

{

if(exint_flag_get(TOUCH_IRQ_EXINT_LINE) != RESET)

{

if(NULL != tp_dev.read_point){

tp_dev.read_point();

}

 

exint_flag_clear(TOUCH_IRQ_EXINT_LINE);

}

}

下一页
上一页
    • 联系电话

    • 0755—83453881
    • 服务时间

    • 周一至周五 9:00-18:00
    • 高通GT-HMI交流群