当前位置:网站首页>[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 :

边栏推荐
- Material design component - use bottomsheet to show extended content (I)
- VIM color the catalogue
- [es practice] safe operation mode on ES
- 华为HMS Core携手超图为三维GIS注入新动能
- BlocProvider为什么感觉和Provider很相似?
- Openwrt enable kV roaming
- Is it safe to buy funds on Great Wall Securities?
- SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
- cookie、session、tooken
- [QT] test whether QT can connect to the database
猜你喜欢

PostgreSQL source code (57) why is the performance gap so large in hot update?

写给当前及未来博士研究生一些建议整理分享

【.Net Core】程序相关各种全局文件

回顾数据脱敏系统

【QT】對於Qt MSVC 2017無法編譯的問題解决

Postgresql源码(57)HOT更新为什么性能差距那么大?

比较通俗易懂的PID理解

Redis master-slave synchronization

Use vb Net to convert PNG pictures into icon type icon files

Deep learning | three concepts: epoch, batch, iteration
随机推荐
[QT] qtcreator uninstall and installation (abnormal state)
Matplotlib common charts
[leetcode] length of the last word [58]
Using SqlCommand objects in code
Iota in golang
边缘计算概述
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
13 MySQL-约束
How excel opens CSV files with more than one million lines
记录一下大文件上传偶然成功偶然失败问题
[C #] dependency injection and Autofac
Material design component - use bottomsheet to show extended content (I)
【QT】测试Qt是否能连接上数据库
vs2015 AdminDeployment.xml
Use vb Net to convert PNG pictures into icon type icon files
Windows 7 install MySQL error: 1067
在长城证券上买基金安全吗?
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
[cmake] cmake configuration in QT Creator
PostgreSQL notes (10) dynamically execute syntax parsing process