【国民技术N32G457评测】6 GPIO + I2C 按键中断+SSD1306显示
[复制链接]
成功驱动SSD1306后,利用板上的两个按键做了一个按键输入中断的程序。按右边的KEY3 屏靠右显示RIGHT,按左边的KEY1 显示LEFT。
main.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2015-07-29 Arda.Fu first implementation
*/
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <string.h>
#include <stdio.h>
#include "ssd1306.h"
void ssd1306_TestRectangle1()
{
uint32_t delta;
for(delta = 0; delta < 5; delta ++)
{
ssd1306_DrawRectangle(1 + (5*delta),1 + (5*delta) ,SSD1306_WIDTH-1 - (5*delta),SSD1306_HEIGHT-1 - (5*delta),White);
}
ssd1306_UpdateScreen();
return;
}
/* defined the LED1 pin: PB5 */
#define LED1_PIN 91
#define KEY1_PIN_NUM 29 /* PA4 */
#define KEY3_PIN_NUM 31 /* PA6 */
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5
int flag_key = 0; /* 用于存取按键状态的 */
static rt_thread_t tid1 = RT_NULL;
/* 左键中断回调函数 */
void disp_left(void *args)
{
flag_key = 0;
}
/* 右键中断回调函数 */
void disp_right(void *args)
{
flag_key = 1;
}
void disp_flag(void)
{
if (flag_key == 0) {
ssd1306_Fill(Black);
ssd1306_SetCursor(2,16);
ssd1306_WriteString("LEFT", Font_16x26, White);
ssd1306_UpdateScreen();
flag_key == 3;
}
else if (flag_key == 1) {
ssd1306_Fill(Black);
ssd1306_SetCursor(48,16);
ssd1306_WriteString("RIGHT", Font_16x26, White);
ssd1306_UpdateScreen();
flag_key == 3;
}
}
/* 线程1的入口函数 */
static void thread1_entry(void *parameter)
{
while (1)
{
disp_flag();
rt_thread_mdelay(10);
}
}
void init_Key(void)
{
/* 按键1引脚为输入模式 */
rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
/* 绑定中断,上升沿模式,回调函数名为beep_on */
rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, disp_left, RT_NULL);
/* 使能中断 */
rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
/* 按键1引脚为输入模式 */
rt_pin_mode(KEY3_PIN_NUM, PIN_MODE_INPUT_PULLUP);
/* 绑定中断,上升沿模式,回调函数名为beep_on */
rt_pin_attach_irq(KEY3_PIN_NUM, PIN_IRQ_MODE_FALLING, disp_right, RT_NULL);
/* 使能中断 */
rt_pin_irq_enable(KEY3_PIN_NUM, PIN_IRQ_ENABLE);
}
int main(void)
{
uint32_t Speed = 200;
/* set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_kprintf("start ssd1306\r\n");
ssd1306_Init();
ssd1306_Fill(Black);
init_Key();
tid1 = rt_thread_create("thread1",
thread1_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
while (1)
{
disp_flag();
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(Speed);
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_thread_mdelay(Speed);
}
}
感沉这个flag 应该用操作系统的信号量,或者是邮箱来设置,可能更合理,还没有学到,学完后再来优化一下。
|