当前位置:网站首页>[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 :
边栏推荐
- 安全协议重点
- 比较通俗易懂的PID理解
- Material design component - use bottomsheet to show extended content (I)
- jpa手写sql,用自定义实体类接收
- 使用 pair 做 unordered_map 的键值
- Which securities company is the best to open a stock account? Is there a security guarantee
- sql 优化
- .env.xxx 文件,加了常量,却undefined
- How to display real-time 2D map after rviz is opened
- [QT] qtcreator uninstall and installation (abnormal state)
猜你喜欢
[es practice] safe operation mode on ES
比较通俗易懂的PID理解
2021 robocom world robot developer competition - semi finals of higher vocational group
边缘计算概述
TS初次使用、ts类型
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
. env. XXX file, with constant, but undefined
[cmake] cmake configuration in QT Creator
Various global files related to [.Net core] program
BlocProvider为什么感觉和Provider很相似?
随机推荐
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
Redis AOF日志
openwrt 开启KV漫游
PyTorch学习记录
在代码中使用SqlCommand对象
ADO. Net SqlDataAdapter object
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
【C#】依赖注入及Autofac
Redis master-slave synchronization
The essence of software architecture
门级建模—课后习题
求逆序数的三个方法
- Oui. Env. Fichier XXX, avec constante, mais non spécifié
PostgreSQL notes (10) dynamically execute syntax parsing process
[must] bm41 output the right view of the binary tree [medium +]
[es practice] safe operation mode on ES
关联性——组内相关系数
ADO.NET之sqlCommand对象
Why does blocprovider feel similar to provider?
Yunxin small class | common cognitive misunderstandings in IM and audio and video