【1】(添加/删减)定时器
static u32 my_led_timer1 = 0;
u16 usr_timer_add(void *priv, void (*func)(void *priv), u32 msec, u8 priority);
void my_led_on(void){
gpio_direction_output(My_LED_GPIO1, 1);
}
my_led_timer1 = usr_timer_add(NULL, my_led_on, flashtime, 0);
usr_timer_del(my_led_timer1);
【2】ADC电压检测
u32 my_adc_get_voltage(u32 ch);
void adc_init();
void adc_test();
int adc_init_flag = 0;
static u32 my_GetADC_voltage_timer = 0;
my_GetADC_voltage_timer = usr_timer_add(NULL, adc_test, 1*000, 0);
adc_add_sample_ch(AD_CH_PA3);
adc_set_sample_freq(AD_CH_PA3, VBAT_SAMPLE_FREQ);
adc_sample(AD_CH_PA3);
void adc_test()
{
printf("Func:%s() VBAT:value->%d voltage->%d mv\n", __func__,adc_get_value(AD_CH_VBAT), adc_get_voltage(AD_CH_VBAT) * 4);
printf("Func:%s() PA3:value->%d voltage->%d mv\n", __func__,adc_get_value(AD_CH_PA3), adc_get_voltage(AD_CH_PA3));
}
#define AD_CH_PA3 (0x1)
【3】利用定时器实现gpio高低电平交替
#define PB4 IO_PORTA_04
gpio_direction_output(PB4, 1);
static u32 my_led_timer1 = 0;
static u32 my_led_timer2 = 0;
u16 usr_timer_add(void *priv, void (*func)(void *priv), u32 msec, u8 priority);
void my_led_on(void){
gpio_direction_output(PB4, 1);
}
void my_led_off(void){
gpio_direction_output(PB4, 0);
}
void my_led_lowflash(void){
int flashtime = 250;
usr_timer_del(my_led_timer1);
usr_timer_del(my_led_timer2);
my_led_timer1 = usr_timer_add(NULL, my_led_on, flashtime, 0);
my_led_timer2 = usr_timer_add(NULL, my_led_off, flashtime*2, 0);
}
void my_led_fastflash(void){
int flashtime = 100;
usr_timer_del(my_led_timer1);
usr_timer_del(my_led_timer2);
my_led_timer1 = usr_timer_add(NULL, my_led_on, flashtime, 0);
my_led_timer2 = usr_timer_add(NULL, my_led_off, flashtime*2, 0);
}
void my_led_LongOn(void){
usr_timer_del(my_led_timer1);
usr_timer_del(my_led_timer2);
my_led_off();
my_led_on();
}
【4】IIC-VCNL36826S
#define TCFG_SW_I2C0_CLK_PORT IO_PORTA_05
#define TCFG_SW_I2C0_DAT_PORT IO_PORTA_06
#define TCFG_SW_I2C0_DELAY_CNT 50
#define TCFG_HW_I2C0_PORTS 'B'
#define TCFG_HW_I2C0_CLK 100000
#if 1
#define INT_IO IO_PORTA_03
#define MY_READ_ADDR 0xC1
#define MY_WRITE_ADDR 0xC0
#define VCNL36826S_SLAVE_ADD 0xC0
#define ST_CONF 0x00
#define PS_CONF1 0x03
#define PS_CONF3 0x04
#define PS_THDL 0x05
#define PS_THDH 0x06
#define PS_CANC 0x07
#define PS_DATA 0xF8
#define INT_FLAG 0xF9
#define VCNL36826s_FLAG 0xF9
#define LEVEL_LOW_THDH 0x00
#define LEVEL_LOW_THDL 0xC8
#define LEVEL_HIGH_THDH 0x01
#define LEVEL_HIGH_THDL 0x90
#define LEVEL3_THDH 200
#define LEVEL3_THDL 150
#endif
void my_I2C_init(u8 iic)
{
int i = 0;
u8 write_data[2];
my_iic_hdl = iic;
soft_iic_init(iic);
#if 1
write_data[0]= 0x03;
write_data[1]= 0x02;
my_write_ndata(iic,ST_CONF,&write_data[0],1);
write_data[0]= 0x83;
write_data[1]= 0x02;
my_write_ndata(iic,ST_CONF,&write_data[0],2);
write_data[0]= 0xC4;
write_data[1]= 0x5C;
my_write_ndata(iic,PS_CONF1,&write_data[0],2);
write_data[0]= 0x08;
write_data[1]= 0xE3;
my_write_ndata(iic,PS_CONF3,&write_data[0],2);
write_data[0]= LEVEL_LOW_THDL;
write_data[1]= LEVEL_LOW_THDH;
my_write_ndata(iic,PS_THDL,&write_data[0],2);
write_data[0]= LEVEL_HIGH_THDL;
write_data[1]= LEVEL_HIGH_THDH;
my_write_ndata(iic,PS_THDH,&write_data[0],2);
write_data[0]=0xff;
write_data[1]=0xff;
my_read_ndata(iic,0xFA,&write_data[0],2);
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("write_data[0]:0x%x\n",write_data[0]);
printf("write_data[1]:0x%x\n",write_data[1]);
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
write_data[0]=0xff;
write_data[1]=0xff;
my_read_ndata(iic,PS_THDL,&write_data[0],2);
write_data[0]=0xff;
write_data[1]=0xff;
my_read_ndata(iic,PS_THDH,&write_data[0],2);
my_read_ndata(iic,0xF8,&write_data[0],2);
printf("PS_DATA:\n");
printf("write_data[0]:0x%x\n",write_data[0]);
printf("write_data[1]:0x%x\n",write_data[1]);
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
#endif
my_I2C_init(MY_WRITE_ADDR);