- 本帖最后由 luzixing 于 2018-9-12 11:42 编辑 *
实验要求:本实验是在探索者开发板完成的
硬件:至少有一路GPIO, 能够用来接LED 灯至少有1路串口,用来做作为串口命令输入端口控制led灯
软件:
串口驱动
PIN 驱动
准备工作:由于硬件的限制用杜邦线跳到串口2作为接受字符的串口
实现的代码:
include
include
define BUFF_SIZE 64
define LED_R 21
define LED_Y 22
define LED_ON(x) rt_pin_write(x ,0)
define LED_OFF(x) rt_pin_write(x ,1)
static char uart_rx_buffer[BUFF_SIZE];
static rt_err_t uart_rx_ind(rt_device_t dev, rt_size_t size)
{
static rt_uint8_t pos = 0;
if(size == 1)
{
rt_device_read(dev, 0, &uart_rx_buffer[pos], size);
rt_device_write(dev, 0, &uart_rx_buffer[pos], size);
if((pos == 0)&&(uart_rx_buffer[pos] == 'L')) pos++;
else if((pos == 1)&&(uart_rx_buffer[pos] == 'E')) pos++;
else if((pos == 2)&&(uart_rx_buffer[pos] == 'D')) pos++;
else if((pos == 3)&&(uart_rx_buffer[pos] == ' ')) pos++;
else if (pos == 4)
{
if(uart_rx_buffer[pos] == 'R')
{
LED_ON(LED_R);
LED_OFF(LED_Y);
}
else if(uart_rx_buffer[pos] == 'Y')
{
LED_OFF(LED_R);
LED_ON(LED_Y);
}
pos = 0;
}
else pos = 0;
}
else
rt_kprintf("Please set the uart to interrupt RX mode!<br>");
return RT_EOK;
}
int uart_sample(void)
{
rt_device_t uart_device;
/*LED控制脚设置为输出模式*/
rt_pin_mode(LED_R, PIN_MODE_OUTPUT_OD);
rt_pin_mode(LED_Y, PIN_MODE_OUTPUT_OD);
/*初始化默认关闭LED灯,全彩灯共阳连接,高电平为关*/
rt_pin_write(LED_R ,1);
rt_pin_write(LED_Y ,1);
uart_device = rt_device_find("uart2");
rt_device_set_rx_indicate(uart_device, uart_rx_ind);
rt_device_open(uart_device, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX));
return 0;
}
/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(uart_sample, uart sample);
实验步骤:
首先移植pin驱动以及串口驱动,加载示例程序,编译,下载程序,观察现象
结果:


遇到问题及解决方法:串口只能输入,但是不能打印输入的字符,然后加上调用写设备函数,把输入的字符通过串口打印出来