当前位置:网站首页>蓝桥杯单片机数码管技巧
蓝桥杯单片机数码管技巧
2022-07-02 03:29:00 【超561】
数码管使用技巧
1如何运用数码管配合按键切换界面显示切换界面
方法:定义一个变量来显示显示界面;通过按键来改变变量的数值;从而切换显示界面。
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)//锁存器控制函数
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//共阳极段码
unsigned char display_mode;//界面显示
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)//显示第一个界面
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[1];
smg_display[2]=smg_du[2];
smg_display[3]=smg_du[3];
smg_display[4]=smg_du[4];
smg_display[5]=smg_du[5];
smg_display[6]=smg_du[6];
smg_display[7]=smg_du[7];
}
if(display_mode==1)//显示第二个界面
{
smg_display[0]=smg_du[1];
smg_display[1]=smg_du[2];
smg_display[2]=smg_du[3];
smg_display[3]=smg_du[4];
smg_display[4]=smg_du[5];
smg_display[5]=smg_du[6];
smg_display[6]=smg_du[7];
smg_display[7]=smg_du[8];
}
}
}
unsigned char trig_btn;
unsigned char con_btn;
unsigned int key_count;
void key_btn()//按键扫描函数
{
unsigned char readdate=P3^0xff;
trig_btn=readdate&(con_btn^0xff);
con_btn=readdate;
}
void key_process()//按键处理函数
{
if(key_count>10)//每十毫秒扫描一次按键
{
key_count=0;
key_btn();
if(trig_btn==0x08)//按键s4
{
display_mode++;
if(display_mode==2)//切换界面0 1
{
display_mode=0;
}
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
key_process();
}
}
void service_timer2() interrupt 12
{
smg_count++;
key_count++;
smg_show();
}2如何让数码管数据实现一秒亮灭
在定时器中设置一个一秒的定时;然后再用一个标志位显示,如果到了一秒就让数码管亮一秒没到的话就熄灭。
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char display_mode;
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
bit smg_blink;
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(smg_blink)
{
smg_display[0]=smg_du[0];
smg_display[1]=smg_du[1];
smg_display[2]=smg_du[2];
smg_display[3]=smg_du[3];
smg_display[4]=smg_du[4];
smg_display[5]=smg_du[5];
smg_display[6]=smg_du[6];
smg_display[7]=smg_du[7];
}
else
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=0x00;
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();//定时器初始化
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
}
}
void service_timer2() interrupt 12//定时器使用函数
{
unsigned int time_count;//定时器计时变量
time_count++;
if(time_count>1000)//定时一秒
{
time_count=0;
smg_blink=!smg_blink;
}
smg_count++;
smg_show();
}3数码管显示有用数据,没有显示的熄灭。
方法:按照十位百位千位判断
#include <STC15F2K60S2.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=(P2&0X1F)|p2date;
P2&=0X1F;
}
unsigned char smg_count;
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char display_mode;
unsigned int number=12345;
void smg_show()
{
unsigned int i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(number>9999)//如果大于9999
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=smg_du[number/10000];
smg_display[4]=smg_du[number/1000%10];
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>999) //如果大于999
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=smg_du[number/1000%10];
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>99)//如果大于99
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=smg_du[number/100%10];
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else if(number>9)//如果大于9
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=smg_du[number/10%10];
smg_display[7]=smg_du[number%10];
}
else
{
smg_display[0]=0x00;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=0x00;
smg_display[7]=smg_du[number%10];
}
}
}
void Timer2Init() //1毫秒@12.000MHz
{
AUXR &= 0xFB; //定时器时钟12T模式
T2L = 0x18; //设置定时初值
T2H = 0xFC; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
IE2|=0x04;
EA=1;
}
void main()
{
Timer2Init();//定时器初始化
Device_ctrl(0x80,0xff);
while(1)
{
smg_process();
}
}
void service_timer2() interrupt 12//定时器使用函数
{
smg_count++;
smg_show();
}边栏推荐
- [数据库]JDBC
- Use blocking or non blocking for streamline
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- Basic syntax of unity script (7) - member variables and instantiation
- [database]jdbc
- Unity脚本的基础语法(6)-特定文件夹
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- Verilog 过程连续赋值
- leetcode-1380. Lucky number in matrix
- Intersection vengraph
猜你喜欢

Introduction to Robotics II. Forward kinematics, MDH method

Gradle foundation | customize the plug-in and upload it to jitpack
![[mv-3d] - multi view 3D target detection network](/img/aa/741b36ead2dfaa5a165401b8d657b7.jpg)
[mv-3d] - multi view 3D target detection network

【DesignMode】建造者模式(Builder model)

Verilog 过程连续赋值

How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?

命名块 verilog

《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算

Named block Verilog

Review materials of project management PMP high frequency examination sites (8-1)
随机推荐
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
Verilog 过程赋值 区别 详解
Kotlin基础学习 16
Introduction to Robotics II. Forward kinematics, MDH method
[mv-3d] - multi view 3D target detection network
halcon图像矫正
MySQL advanced (Advanced) SQL statement (II)
Intersection vengraph
MySQL connection query and subquery
Kotlin basic learning 13
Kubernetes cluster storageclass persistent storage resource core concept and use
Global and Chinese market of bone adhesives 2022-2028: Research Report on technology, participants, trends, market size and share
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Global and Chinese markets for ultrasonic probe disinfection systems 2022-2028: Research Report on technology, participants, trends, market size and share
C # joint halcon out of halcon Environment and various Error Reporting and Resolution Experiences
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
The page in H5 shows hidden execution events
High performance and low power cortex-a53 core board | i.mx8m Mini
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Eight steps of agile development process