当前位置:网站首页>[embedded system course design] a single key controls the LED light
[embedded system course design] a single key controls the LED light
2022-07-01 23:51:00 【march of Time】
Course design of embedded system in Hefei University of Technology
The design requirements :
Control according to the input of a single key LED Different display effects of lights .K1 Press even times in succession , four LED The lamp (LED1~LED4) Press 1 second ( Timer interrupt implementation ) The corresponding number of flashes at the same time , And then keep LED1 and LED2 Changliang ,LED3 and LED4 Extinguish ;K1 Press an odd number of times in succession , Four light buttons 0.5 second ( Timer interrupt implementation ) The corresponding number of flashes at the same time , And then keep LED1 and LED2 Extinguish ,LED3 and LED4 Changliang .K2 When pressed , Four light buttons 1 second ( Timer interrupt implementation ) The intervals are lit one by one ( Only one light is on at the same time ), And cycle the display .K3 When pressed , Four light buttons 0.5 second ( Timer interrupt implementation ) The intervals are lit one by one ( Only one light is on at the same time ), And cycle the display .K4 When pressed , All lights go out , The system enters the initial state .
explain : The program needs to distinguish between continuous keys and interval keys . The interval between consecutive keys is self-determined , Reasonable can show .
The main codes are as follows :
main.c
main.c:
#include "stdio.h"
#define GPKCON0 (*(volatile unsigned long *)0x7F008800)
#define GPKDAT (*(volatile unsigned long *)0x7F008808)
#define GPNCON (*(volatile unsigned long *)0x7F008830)
#define GPNDAT (*(volatile unsigned long *)0x7F008834)
#define GPFCON (*(volatile unsigned int *)0x7F0080A0)
#define GPFDAT (*(volatile unsigned int *)0x7F0080A4)
int COUNT;
void timer_init(unsigned long utimer, unsigned long uprescaler, unsigned long udivider, unsigned long utcntb, unsigned long utcmpb, int mode_x, int countk1_x);
void delay_short(volatile unsigned int n) {
while (n--) {
}
}
void delay(volatile unsigned int n) {
while (n--) {
delay_short(0x7ff);
}
}
void buzzer_init(void)
{
// set GPF14 as output
GPFCON |= 1 << 28;
GPFCON &= ~(1 << 29);
}
int main()
{
int dat = 0;
// To configure GPK4-7 For output function
GPKCON0 = 0x11110000;
// all LED Extinguish
GPKDAT = 0x000000f0;
// To configure GPN For input function
GPNCON = 0;
// initialization buzzer
buzzer_init();
// Query key events by polling
while (1)
{
dat = GPNDAT;
if (dat & (1 << 0));
else {
// KEY1 Pressed
// Set the timer
int countk1 = 0;
int num = 1500000;
while (num > 0)
{
//GPNDAT, The lower four are effective , Press which one is 0
if (!(GPNDAT & (1 << 0))) // It means if k1 Press Then enter if
{
delay(10);
countk1++; // Press once Count once
num = 1500000; // Restore the countdown
while (!(GPNDAT & (1 << 0))) {
; }
}
num--;
}//while(num > 0)
COUNT = countk1;
if (countk1 % 2 == 1)
timer_init(0, 65, 4, 31250, 0, 0, countk1);
else if (countk1 % 2 == 0)
timer_init(0, 65, 4, 62500, 0, 0, countk1);
//if(GPNDAT & (1<<0))
//{
// dat = 0xfd;
//}
}
if (dat & (1 << 1));
else {
// KEY2 Pressed
// Set the timer
while (!(GPNDAT & (1 << 0))) {
; }
int big = 0x6000000;
while (big > 0)
big--;
timer_init(0, 65, 4, 62500, 0, 1, 0);
}
if (dat & (1 << 2));
else
// KEY3 Pressed
// Set the timer
{
while (!(GPNDAT & (1 << 0))) {
; }
timer_init(0, 65, 4, 31250, 0, 2, 0);
}
if (dat & (1 << 3));
else
// KEY4 Pressed
// Set the timer
{
while (!(GPNDAT & (1 << 0))) {
; }
int big = 0x6000000;
while (big > 0)
big--;
timer_init(0, 65, 4, 31250, 0, 3, 0);
}
}
}
timer.c:
#include "stdio.h"
#define GPKCON0 (*((volatile unsigned long *)0x7F008800))
#define GPKDATA (*((volatile unsigned long *)0x7F008808))
#define GPKDAT (*((volatile unsigned long *)0x7F008808))
#define EINT0CON0 (*((volatile unsigned long *)0x7F008900))
#define EINT0MASK (*((volatile unsigned long *)0x7F008920))
#define EINT0PEND (*((volatile unsigned long *)0x7F008924))
#define PRIORITY (*((volatile unsigned long *)0x7F008280))
#define SERVICE (*((volatile unsigned long *)0x7F008284))
#define SERVICEPEND (*((volatile unsigned long *)0x7F008288))
#define VIC0IRQSTATUS (*((volatile unsigned long *)0x71200000))
#define VIC0FIQSTATUS (*((volatile unsigned long *)0x71200004))
#define VIC0RAWINTR (*((volatile unsigned long *)0x71200008))
#define VIC0INTSELECT (*((volatile unsigned long *)0x7120000c))
#define VIC0INTENABLE (*((volatile unsigned long *)0x71200010))
#define VIC0INTENCLEAR (*((volatile unsigned long *)0x71200014))
#define VIC0PROTECTION (*((volatile unsigned long *)0x71200020))
#define VIC0SWPRIORITYMASK (*((volatile unsigned long *)0x71200024))
#define VIC0PRIORITYDAISY (*((volatile unsigned long *)0x71200028))
#define VIC0ADDRESS (*((volatile unsigned long *)0x71200f00))
#define PWMTIMER_BASE (0x7F006000)
#define TCFG0 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x00)) )
#define TCFG1 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x04)) )
#define TCON ( *((volatile unsigned long *)(PWMTIMER_BASE+0x08)) )
#define TCNTB0 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x0C)) )
#define TCMPB0 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x10)) )
#define TCNTO0 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x14)) )
#define TCNTB1 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x18)) )
#define TCMPB1 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x1C)) )
#define TCNTO1 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x20)) )
#define TCNTB2 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x24)) )
#define TCMPB2 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x28)) )
#define TCNTO2 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x2C)) )
#define TCNTB3 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x30)) )
#define TCMPB3 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x34)) )
#define TCNTO3 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x38)) )
#define TCNTB4 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x3C)) )
#define TCNTO4 ( *((volatile unsigned long *)(PWMTIMER_BASE+0x40)) )
#define TINT_CSTAT ( *((volatile unsigned long *)(PWMTIMER_BASE+0x44)) )
#define GPFCON (*(volatile unsigned int *)0x7F0080A0)
#define GPFDAT (*(volatile unsigned int *)0x7F0080A4)
typedef void (isr)(void);
extern void asm_timer_irq();
extern int COUNT;
int mode;
int count;
int countk1;
void irq_init(void)
{
/* Enable in the interrupt controller timer0 interrupt */
VIC0INTENABLE |= (1 << 23);
VIC0INTSELECT = 0;
isr** isr_array = (isr**)(0x7120015C);
isr_array[0] = (isr*)asm_timer_irq;
/* take GPK4-GPK7 The output port is configured as */
GPKCON0 = 0x11110000;
// Put out four LED The lamp
GPKDATA = 0xff;
// set GPF14 as output
GPFCON |= 1 << 28;
GPFCON &= ~(1 << 29);
}
// timer0 Interrupt handling function of interrupt
void do_irq()
{
// K1 Corresponding operation
if (mode == 0)
{
if (countk1 <= 0)
{
if (COUNT % 2 == 0)
GPKDATA = 0xc0;
else
GPKDATA = 0x30;
//GPKDATA=0xff;
}
else
{
int label = count % 2;
switch (label)
{
case 0:
// four LED All the lights go out
GPKDATA = 0xff;
break;
case 1:
// four LED All lights are on
GPKDATA = 0x0;
countk1 = countk1 - 1;
break;
default:break;
}
count++;
}
}
// K2 Corresponding operation
if (mode == 1)
{
int label = count % 4;
switch (label)
{
case 0:
// LED1 Light up ,LED2、LED3、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 4);
break;
case 1:
// LED2 Light up ,LED1、LED3、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 5);
break;
case 2:
// LED3 Light up ,LED1、LED2、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 6);
break;
case 3:
// LED4 Light up ,LED1、LED2、LED3 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 7);
break;
default:break;
}
// count Add one
count++;
}
// K3 Corresponding operation
if (mode == 2)
{
int label = count % 4;
switch (label)
{
case 0:
// LED1 Light up ,LED2、LED3、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 4);
break;
case 1:
// LED2 Light up ,LED1、LED3、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 5);
break;
case 2:
// LED3 Light up ,LED1、LED2、LED4 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 6);
break;
case 3:
// LED4 Light up ,LED1、LED2、LED3 Extinguish
GPKDATA = 0xff;
GPKDAT &= ~(1 << 7);
break;
default:break;
}
// count Add one
count++;
}
// K4 Corresponding operation
if (mode == 3)
{
GPKDATA = 0xff;
}
unsigned long uTmp;
// clear timer0 Interrupt status register
uTmp = TINT_CSTAT;
TINT_CSTAT = uTmp;
VIC0ADDRESS = 0x0;
}
// initialization timer
void timer_init(unsigned long utimer, unsigned long uprescaler, unsigned long udivider, unsigned long utcntb, unsigned long utcmpb, int mode_x, int countk1_x)
{
unsigned long temp0;
// Set up mode value , Used to determine which operation to perform
mode = mode_x;
countk1 = countk1_x;
// Input clock of timer = PCLK / ( {prescaler value + 1} ) / {divider value} = PCLK/(65+1)/16=62500hz
// Set the prescaler coefficient to 66
temp0 = TCFG0;
temp0 = (temp0 & (~(0xff00ff))) | (uprescaler << 0);
TCFG0 = temp0;
// 16 frequency division
temp0 = TCFG1;
temp0 = (temp0 & (~(0xf << 4 * utimer)) & (~(1 << 20))) | (udivider << 4 * utimer);
TCFG1 = temp0;
// 1s = 62500hz
TCNTB0 = utcntb;
TCMPB0 = utcmpb;
// Update manually
TCON |= 1 << 1;
// Clear the manual update bit
TCON &= ~(1 << 1);
// Automatically load and start timer0
TCON |= (1 << 0) | (1 << 3);
// Can make timer0 interrupt
temp0 = TINT_CSTAT;
temp0 = (temp0 & (~(1 << utimer))) | (1 << (utimer));
TINT_CSTAT = temp0;
}
result :
边栏推荐
- 2021 robocom world robot developer competition - preliminary competition of undergraduate group
- Is there a piece of code that makes you convinced by human wisdom
- Material Design组件 - 使用BottomSheet展现扩展内容(一)
- 13 MySQL-约束
- ADO. Net SqlCommand object
- Why is PHP called hypertext preprocessor
- 学成在线案例实战
- Which securities company is the best to open a stock account? Is there a security guarantee
- 写给当前及未来博士研究生一些建议整理分享
- Windows 7 安装MYSQL 错误:1067
猜你喜欢
Know --matplotlib
ConcurrentSkipListMap——跳表原理
2021 robocom world robot developer competition - preliminary competition of undergraduate group
. env. XXX file, with constant, but undefined
[QT] qtcreator uninstall and installation (abnormal state)
Why is PHP called hypertext preprocessor
RPA tutorial 01: Excel automation from introduction to practice
Key points of security agreement
多表操作-一对一,一对多与多对多
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
随机推荐
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
Algolia's search needs are almost closed
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Yunxin small class | common cognitive misunderstandings in IM and audio and video
2021 RoboCom 世界机器人开发者大赛-高职组初赛
华为HMS Core携手超图为三维GIS注入新动能
[LeetCode] 最后一个单词的长度【58】
cookie、session、tooken
Redis AOF log
Openwrt enable kV roaming
.env.xxx 文件,加了常量,却undefined
Pytorch learning record
在代码中使用SqlCommand对象
Material Design组件 - 使用BottomSheet展现扩展内容(一)
Is there a piece of code that makes you convinced by human wisdom
Development trend and future direction of neural network Internet of things
algolia 搜索需求,做的快自闭了...
使用 pair 做 unordered_map 的键值
Windows 7 install MySQL error: 1067