从原理图上可以看到,light sensor与cpu连接的有两个管脚:一个是连接到cpu上普通的gpio引脚,这个是控制light sensor电源的管脚;一个连接到cpu上的adc引脚,这个是采集light sensor电压的管脚。写驱动之前,必须配置到管脚:
1)配置引脚
运行DrvGen.exe(在目录下custom/drv/Drv_Tool),打开文件codegen.dws (custom/drv/misc_drv/SANGFEI36_10A_BB/codegen),点击edit,选择ADC setting选项卡,设置ADC6(ADC Var: ADC_CHR_USB,UEM ADC String:LIGHT SENSOR),依次点击按钮:确定、save、Gen code、exit。
完成后,会自动生成
const unsigned char ADC_CHR_USB = 6;
这是adc的电压采集通道,light sensor采样的时候用到。
2)采集电压并设置背光灯
#define LIGHT_SENSOR_EN 10 /*使能管脚*/
#define LIGHT_SENSOR_DELAYMS 2800 /*定时器时间ms*/
#define LIGHT_SENSOR_SAMPLES 5 /*采集次数*/
void light_sensor_get_level(void)
{
kal_uint8 i;
kal_uint16 sample_temp = 0;
kal_uint32 sample_total = 0;
kal_uint8 lcd_level, key_level;
GPIO_ModeSetup(LIGHT_SENSOR_EN,0);/*设置工作模式为GPIO*/
GPIO_InitIO(1, LIGHT_SENSOR_EN);/*工作方向,0表示输入,1表示输出*/
GPIO_WriteIO(0, LIGHT_SENSOR_EN);/*打开电源*/
for (i =0; i<LIGHT_SENSOR_SAMPLES; i++)
{
sample_temp = ADC_GetData(ADC_CHR_USB);/*采集电压*/
sample_total += sample_temp;
}
GPIO_WriteIO(1, LIGHT_SENSOR_EN);/*关闭电源*/
sample_temp = sample_total/LIGHT_SENSOR_SAMPLES;
/*根据采集到的电压,处理lcd_level和key_level,设置lcd和键盘的背光灯*/
srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, lcd_level);
srv_gpio_set_device_level(GPIO_DEV_LED_KEY, key_level);
StartTimer(SF_LIGHT_SENSOR_TIMER, LIGHT_SENSOR_DELAYMS, light_sensor_timer_hdlr);
}