当前位置:网站首页>九齐单片机NY8B062D单按键控制4种LED状态
九齐单片机NY8B062D单按键控制4种LED状态
2022-07-04 18:45:00 【51CTO】
/* =========================================================================
* Project: 单按键控制4种LED状态
* File: 单按键控制4种LED状态
* Description: 单按键控制4种LED状态
*
*
* Author: Jimp
* Version: V1.0
* Date: 2020/05/22
=========================================================================*/
//--------------- File Include ---------------------------------------------
//--------------------------------------------------------------------------
#include <ny8.h>
#include <stdint.h>
#include "ny8_constant.h"
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
#define unchar unsigned char
#define unint unsigned int
uint8_t myvar;
__sbit flag = myvar:0; //定义控制运行符
__sbit key = PORTB:0;
__sbit light1 = PORTA:7;
__sbit light2 = PORTA:6;
unchar Mod; //定义亮灯模式
void isr(void) __interrupt(0)
{
if(INTFbits.T0IF)
{
static unsigned int cnt= 0;
TMR0 = 0;
INTF= (unsigned char)~(C_INT_TMR0); // Clear T0IF flag bit
cnt ++;
if(cnt >= 250)
{
cnt = 0;
light1 = ~light1;
light2 = ~light2;
}
}
}
void lnit_Mod() //初始化函数
{
IOSTA = ~IOSTA; //设置A口为输出
PORTA = 0xFF;
IOSTB = C_PB0_Input;
flag = 1;
key = 1;
Mod = 0;
light1 = 0;
light2 = 0;
}
void lnit_Timer() //中断初始化
{
DISI(); //禁用中断
PCON1 = C_TMR0_Dis;
TMR0 = 0;
T0MD = C_PS0_TMR0 | C_PS0_Div8; //预分频分配给TMR0
INTE = C_INT_TMR0;
ENI(); //启用中断
}
void delay(unint z) //延时函数ms
{
unint x,y;
for(x = z;x > 0;x--)
for(y = 100;y > 0;y--);
}
void lnput_Mod() //状态模式标记函数
{
if(Mod == 4)
Mod = 0;
if(key == 0)
{
delay(5);
if(key == 0) //延时消抖
Mod++; //按键次数标记
}
while(!key) //松手检测
{;;}
}
void run() //4种模式下的执行
{
switch(Mod)
{
case 0:{PCON1 = C_TMR0_Dis;light1 = 0;light2 = 0;}break; //全亮
case 1:{PCON1 = C_TMR0_En;}break; //同步闪烁
case 2:{PCON1 = C_TMR0_Dis;light1 = 1;light2 = 1;}break; //全灭
case 3:{PCON1 = C_TMR0_Dis;light1 = 0;light2 = 1;PCON1 = C_TMR0_En;}break; //异步闪
}
}
void main(void)
{
lnit_Mod();
lnit_Timer();
while(1)
{
lnput_Mod();
if(flag == 1) //控制RUN运行
{
run();
if(Mod == 1||Mod == 3)
flag = 0;
}
if(Mod == 0||Mod == 2)
flag = 1;
}
}
边栏推荐
- 多表操作-内连接查询
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- Swagger突然发癫
- What financial products can you buy with a deposit of 100000 yuan?
- Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
- Huawei cloud store homepage banner resource bit application
- Jetpack compose tutorial
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- Wireshark network packet capture
猜你喜欢
随机推荐
1008 elevator (20 points) (PAT class a)
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Prometheus installation
JVM系列之对象的创建
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
Cbcgpprogressdlgctrl progress bar used by BCG
Neural network IOT platform construction (IOT platform construction practical tutorial)
Employment prospects of neural network Internet of things application technology [welcome to add]
HMM hidden Markov model and code implementation
Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
kotlin 条件控制
C # use stopwatch to measure the running time of the program
Kotlin cycle control
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
What does the neural network Internet of things mean? Popular explanation
原来这才是 BGP 协议
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板









