在线时间428 小时
UID3006061
注册时间2015-3-23
NXP金币76
TA的每日心情 | 开心 2025-1-21 08:52 |
---|
签到天数: 861 天 连续签到: 1 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 5867
- 最后登录
- 2025-1-22
|
本帖最后由 leo121_3006061 于 2017-8-4 12:37 编辑
上一篇修改了led驱动,万利的板子带有四个按钮,本次实验,把四个key的驱动添加上来,依葫芦画瓢,创建两个文件,drv_key.c,drv_key.h,代码比较简单,如下drv_key.c
- #include <rtthread.h>
- #include "board.h"
- #define KEY_NUM 4
- const uint8_t KEY_GPIO_PORT[KEY_NUM] = { 1, 1, 1, 1 };
- const uint8_t KEY_GPIO_PIN [KEY_NUM] = { 8, 9, 10, 11 };
- struct key_ctrl
- {
- uint32_t num;
- uint32_t port;
- };
- struct lpc_key
- {
- /* inherit from rt_device */
- struct rt_device parent;
- struct key_ctrl ctrl[KEY_NUM];
- };
- static struct lpc_key key;
- static rt_err_t rt_key_init(rt_device_t dev)
- {
-
- for(int i=0;i<KEY_NUM;i++)
- {
- Chip_IOCON_PinMuxSet(LPC_IOCON, KEY_GPIO_PORT[i], KEY_GPIO_PIN [i], (IOCON_FUNC0 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN));
- Chip_GPIO_SetPinDIRInput(LPC_GPIO, KEY_GPIO_PORT[i], KEY_GPIO_PIN [i]);//方向是输入
- Chip_GPIO_SetPinState(LPC_GPIO, KEY_GPIO_PORT[i], KEY_GPIO_PIN [i], false);
- key.ctrl[i].num = KEY_GPIO_PIN [i];
- key.ctrl[i].port = KEY_GPIO_PORT[i];
-
- }
- return RT_EOK;
- }
- int rt_key_hw_init(void)
- {
- key.parent.type = RT_Device_Class_Char;
- key.parent.rx_indicate = RT_NULL;
- key.parent.tx_complete = RT_NULL;
- key.parent.init = rt_key_init;
- key.parent.open = RT_NULL;//rt_key_on;
- key.parent.close = RT_NULL;//rt_key_off;
- key.parent.read = RT_NULL;//rt_key_read;
- key.parent.write = RT_NULL; //rt_key_write;
- key.parent.control = RT_NULL;//rt_key_control;
- key.parent.user_data = RT_NULL;
- /* register a character device */
- rt_device_register(&key.parent, "key", RT_DEVICE_FLAG_RDWR);
- /* init key device */
- rt_key_init(&key.parent);
- return 0;
- }
- bool key_switch(rt_uint32_t pb_key)
- {
-
- return Chip_GPIO_GetPinState(LPC_GPIO,key.ctrl[pb_key].port, key.ctrl[pb_key].num);//获取key的状态
- }
复制代码
drv_key.h
- #ifndef _DRV_KEY_H_
- #define _DRV_KEY_H_
- #include "stdint.h"
- int rt_key_hw_init(void);
- uint8_t key_switch(rt_uint32_t Set_key);//此处定义bool类型会报错
- #endif
复制代码 为了验证驱动是否正确,修改demo_thread.c,在thread1里添加代码,如下
- static void thread1_entry(void* parameter)
- {
- while(1)
- {
- Led_Control(7,1);
- rt_thread_delay(RT_TICK_PER_SECOND);
- Led_Control(7,0);
- rt_thread_delay(RT_TICK_PER_SECOND);
- if(key_switch(0)==0) //测试key
- {
- Led_Control(5,1);
- rt_kprintf("PB2 is on,led5 is ligth on" );
-
- }
- else
- {
- Led_Control(5,0);
- }
-
- }
- }
复制代码- int demo_init(void)
- {
- rt_thread_t thread1 = RT_NULL;
- rt_thread_t thread2 = RT_NULL;
-
-
- rt_led_hw_init();
- rt_key_hw_init();//初始化加载驱动
-
- thread1 = rt_thread_create("t1",thread1_entry, RT_NULL,512,10,5);
- if (thread1 != RT_NULL)
- rt_thread_startup(thread1);
- thread2 = rt_thread_create("t2",thread2_entry, RT_NULL,512,10,5);
- if (thread2 != RT_NULL)
- rt_thread_startup(thread2);
- return 0;
-
- }
复制代码
编译下载后,打开串口助手,当按下PB2的时候,led5(LD6)绿色灯亮,串口助手也有输出提示,至此key的驱动初步添加完毕,后续可以自己定义这四个按键的功能(播放,录音等)
- f7:0000 f8:0019finsh>>PB2 is on,led5 is ligth onPB2 is on,led5 is ligth on//只截取了结尾输出部分
复制代码 后续看时间安排,可以把温度,滑变电阻(控制音量大小)都添加上来
|
|