STC89C5xRC
#include
unsigned char value[22] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','P','U','T','Y','L','H'};//可显示内容
unsigned char led[22] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x73,0x3E,0x31,0x6E,0x38,0x76}; //显示内容对应字段码
unsigned char where[] = {0,1,2,3,4,5,6,7};//灯的位置
// 延时函数,delay_time是延时的毫秒数
void delay(unsigned int delay_time) {
unsigned int i, j;
for (i = 0; i < delay_time; i++) {
for (j = 0; j < 1000; j++) {};
}
}
//输入内容,获得索引
int LED_value_index(char content){
switch (content) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
case 'P': return 16;
case 'U': return 17;
case 'T': return 18;
case 'Y': return 19;
case 'L': return 20;
case 'H': return 21;
default: return 0; // 如果未找到,返回0
};
}
//根据索引,获得字段码
char LED_value_code(int index){
return led[index];
}
//整合_实现输入内容,获得字段码
char LED_value(char content){
return LED_value_code(LED_value_index(content));
}
//输入要显示的位置:0-7 返回位置码
char LED_where(int w){
return where[w];
}
// 依次显示0-7
void result_light_0_to_7(){
int i;
for(i = 0;i<8;i++){
P2 = led[i];//P2 控制LED显示的内容 unsigned char led[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
P1 = where[i] ;//P1 控制要显示内容的LED的位置
delay(10);
}
}
void my_birth_day() {
int dy = 1000;
P2 = LED_value('2');
P1 = LED_where(7);
delay(dy);
P2 = LED_value('0');
P1 = LED_where(6);
delay(dy);
P2 = LED_value('0');
P1 = LED_where(5);
delay(dy);
P2 = LED_value('1');
P1 = LED_where(4);
delay(dy);
P2 = LED_value('1');
P1 = LED_where(3);
delay(dy);
P2 = LED_value('0');
P1 = LED_where(2);
delay(dy);
P2 = LED_value('2');
P1 = LED_where(1);
delay(dy);
P2 = LED_value('4');
P1 = LED_where(0);
delay(dy);
P2 = LED_value('H');
P1 = LED_where(4);
delay(dy);
P2 = LED_value('A');
P1 = LED_where(3);
delay(dy);
P2 = LED_value('A');
P1 = LED_where(2);
delay(dy);
P2 = LED_value('P');
P1 = LED_where(1);
delay(dy);
P2 = LED_value('Y');
P1 = LED_where(0);
delay(dy);
}
void main() {
while(1) {
my_birth_day() ;
//result_light_0_to_7();
}
}
相关视频:
[单片机]实验:数码管显示器使用https://www.bilibili.com/video/BV12C4y1j7vW/
[单片机]如何在实体设备上运行自己编写的代码https://www.bilibili.com/video/BV16b4y1M76t
由于实验只能在实验室进行,课后我没法进行实验,所以后文采用了仿真的方式来模拟实验。
仿真实验
原理图
代码如下
#include
//需要复制代码的话 请去 博客园(https://cnblogs.com/mllt)获取
/*
博客园:萌狼蓝天
CSDN:萌狼蓝天 (文章都是从博客园自动同步过去的,可能会存在格式问题,建议去博客园查看)
哔哩哔哩:萌狼蓝天 (视频首发平台 | 可以私信)
抖音:弃文从理萌狼蓝天 (经常不在线)
快手:萌狼蓝天 (经常不在线)
小红书:弃文从理萌狼蓝天 (经常不在线)
*/
//可以显示的字符
unsigned char value[22] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','P','U','T','Y','L','H'};
//可以显示的字符对应的字段码
unsigned char led[22] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x73,0x3E,0x31,0x6E,0x38,0x76}; //灯的内容
unsigned char where[] = {0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE};//灯的位置
//unsigned char where[] = {0,1,2,3,4,5,6,7};//灯的位置
// 延时函数
void delay(unsigned int delay_time) {
unsigned int i, j;
for (i = 0; i < delay_time; i++) {
for (j = 0; j < 1000; j++) {};
}
}
//输入内容,获得索引
int LED_value_index(char content){
switch (content) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
case 'P': return 16;
case 'U': return 17;
case 'T': return 18;
case 'Y': return 19;
case 'L': return 20;
case 'H': return 21;
default: return 0; // 如果未找到,返回0
};
}
//根据索引,获得字段码
char LED_value_code(int index){
return led[index];
}
//整合_实现输入内容,获得字段码
char LED_value(char content){
return LED_value_code(LED_value_index(content));
}
//输入要显示的位置:0-7 返回位置码
char LED_where(int w){
return where[w];
}
//显示数字0-7
void result_light_0_to_7(){
int i;
for(i = 0;i<8;i++){
P2 = led[i];//P2 控制LED显示的内容
P3 = where[i] ;//P3 控制要显示内容的LED的位置
delay(10);
}
}
//每个位置都把所有字符显示一遍
void result_every_where(){
int i,j;
for(i = 0;i<8;i++){
for(j=0;j<22;j++){
P2 = LED_value_code(j);
P3 = LED_where(i);
delay(10);
}
}
}
//指定位置显示指定内容
void my_birth_day() {
int dy = 50;//方便调整延时
P2 = LED_value('2');//指定内容
P3 = LED_where(7);//指定位置
delay(dy);//延时,太快了的话会出现显示的异常,所以不宜太快了。
P2 = LED_value('0');
P3 = LED_where(6);
delay(dy);
P2 = LED_value('0');
P3 = LED_where(5);
delay(dy);
P2 = LED_value('1');
P3 = LED_where(4);
delay(dy);
P2 = LED_value('1');
P3 = LED_where(3);
delay(dy);
P2 = LED_value('2');
P3 = LED_where(2);
delay(dy);
P2 = LED_value('0');
P3 = LED_where(1);
delay(dy);
P2 = LED_value('8');
P3 = LED_where(0);
delay(dy);
P2 = LED_value('H');
P3 = LED_where(4);
delay(dy);
P2 = LED_value('A');
P3 = LED_where(3);
delay(dy);
P2 = LED_value('P');
P3 = LED_where(2);
delay(dy);
P2 = LED_value('P');
P3 = LED_where(1);
delay(dy);
P2 = LED_value('Y');
P3 = LED_where(0);
delay(dy);
}
void main() {
while(1) {
my_birth_day() ;
delay(100);
result_light_0_to_7();
delay(100);
result_every_where();
delay(100);
}
}
[单片机]数码管显示器仿真效果展示:https://www.bilibili.com/video/BV12u411F7cL/