【杰理AC632n】

【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);//输出1
}

my_led_timer1 = usr_timer_add(NULL, my_led_on, flashtime, 0);//添加

usr_timer_del(my_led_timer1);//删减

【2】ADC电压检测

//**************app_power_manage.c**************//
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_api.c**************//
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));

}
//**************adc_api.h**************//
//AD channel define
#define AD_CH_PA3    (0x1)



【3】利用定时器实现gpio高低电平交替

//gpio电平输出	1:高电平	0:低电平
#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);
//下面是LED事件的设置
//开灯(即高电平输出)
void my_led_on(void){//100ms
   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

//
/*软件IIC设置*/
#define TCFG_SW_I2C0_CLK_PORT               IO_PORTA_05//czb 220420 IO_PORTA_09          //软件IIC  CLK脚选择
#define TCFG_SW_I2C0_DAT_PORT               IO_PORTA_06// IO_PORTA_10                    //软件IIC  DAT脚选择
#define TCFG_SW_I2C0_DELAY_CNT              50    
/*硬件IIC端口选择
  SCL         SDA
  'A': IO_PORT_DP   IO_PORT_DM
  'B': IO_PORTA_09  IO_PORTA_10
  'C': IO_PORTA_07  IO_PORTA_08
  'D': IO_PORTA_05  IO_PORTA_06
 */
#define TCFG_HW_I2C0_PORTS                  'B'
#define TCFG_HW_I2C0_CLK                    100000                                  //硬件IIC波特率                                       //IIC延时参数,影响通讯时钟频率

//
#if 1 //czb 211108
#define INT_IO          IO_PORTA_03//中断脚
#define MY_READ_ADDR    0xC1
#define MY_WRITE_ADDR   0xC0
#define VCNL36826S_SLAVE_ADD 0xC0   //MY_WRITE_ADDR

#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        //INT_FLAG

#define LEVEL_LOW_THDH 		0x00	// 0x00	3.8cm E2
#define LEVEL_LOW_THDL 		0xC8	// 0x96 0x8f
#define LEVEL_HIGH_THDH 	0x01	//0x08	2.5cm E2 0x01 0x03
#define LEVEL_HIGH_THDL 	0x90	//	200817 0x66 0x06
#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 
	//VCNL36826 init
	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);

你可能感兴趣的:(单片机,嵌入式硬件)