C语言八位数码管显示学号,51单片机,8位共阴极数码管动态显示12位学号,按键调整走动速度,C语言的...

#include

#include

sbit  k_add = P3^1;

sbit  k_sub = P3^4;

unsigned char code SEG7[] = {

0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0xff};

unsigned char DISP_BUFF[] = {

10, 10, 10, 10, 10, 10, 10, 10,

1, 3, 9, 2, 5, 8, 0, 7, 4, 6, 3, 2,

10, 10, 10, 10, 10, 10, 10, 10};

unsigned char  j = 0, k = 1;

void DelayMS(unsigned int x)

{

unsigned char  i;

while(x--)  for(i = 200; i > 0; i--);

}

void  display(unsigned char x)

{

unsigned char i, w = 128;

for(i = 0; i < 8; i++)  {

P0 = SEG7[DISP_BUFF[i + k]];

P2 = w;  DelayMS(1);   P2 = 0;

w = _cror_(w, 1);

}

j++;

if (j > x) {

j = 0;

k++; if (k == 20) k = 0;

}

}

void main()

{

unsigned char d = 30;

while(1)  {

if  (!k_add) {

display(d);

if  (!k_add) {

d += 2;  if (d > 50)  d = 50;

while (!k_add)  display(d);

}

}

if  (!k_sub) {

display(d);

if  (!k_sub) {

d -= 2;  if (d < 10)  d = 10;

while (!k_sub)  display(d);

}

}

display(d);

P1 = d;

}

}

f421de55ecaf318d18fc949d53c10953.png

  • 8
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的C语言程序,使用了51单片机的定时器和外部中断来实现独立按键长按加减的功能,同时通过数码管显示当前的计数值: ```C #include <reg52.h> #define DIGIT_PORT P0 // 数码管段选控制端口 #define KEY_PORT P1 // 按键输入端口 #define KEY_ADD 0x01 // 加键输入端口 #define KEY_SUB 0x02 // 减键输入端口 unsigned char digit_tbl[10] = { // 数码管显示表 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 }; unsigned int count = 0; // 计数器变量 unsigned char cnt_h, cnt_l; // 计数器高低 void delay_ms(unsigned int t) { // 延时函数 unsigned int i, j; for (i = 0; i < t; i++) for (j = 0; j < 125; j++); } void display(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) { // 数码管显示函数 DIGIT_PORT = digit_tbl[d1]; P2 = 0x01; delay_ms(1); DIGIT_PORT = digit_tbl[d2]; P2 = 0x02; delay_ms(1); DIGIT_PORT = digit_tbl[d3]; P2 = 0x04; delay_ms(1); DIGIT_PORT = digit_tbl[d4]; P2 = 0x08; delay_ms(1); } void timer0_isr() interrupt 1 { // 定时器中断服务程序 TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; count++; // 计数器加1 cnt_h = count/100; // 计算高数值 cnt_l = count%100; // 计算低数值 } void ext0_isr() interrupt 0 { // 外部中断0服务程序 unsigned char key = KEY_PORT & 0x03; // 读取按键状态 if (key == KEY_ADD) { // 加键按下 count++; // 计数器加1 cnt_h = count/100; // 计算高数值 cnt_l = count%100; // 计算低数值 } else if (key == KEY_SUB) { // 减键按下 if (count > 0) // 计数器减1,但不能小于0 count--; cnt_h = count/100; // 计算高数值 cnt_l = count%100; // 计算低数值 } else { // 长按键退出 while ((KEY_PORT & 0x03) == 0x00); // 等待按键释放 } } void main() { TMOD = 0x01; // 定时器0工作在模式1 TH0 = (65536-50000)/256; // 定时器初值,50ms中断一次 TL0 = (65536-50000)%256; ET0 = 1; // 允许定时器0中断 EA = 1; // 全局中断允许 TR0 = 1; // 启动定时器0 EX0 = 1; // 外部中断0允许 IT0 = 1; // 外部中断0下降沿触发 while (1) { display(cnt_h/10, cnt_h%10, cnt_l/10, cnt_l%10); // 显示计数值 } } ``` 在程序中,使用了一个计数器变量`count`来保存当前的计数值,通过外部中断0和定时器0的中断服务程序来实现独立按键长按加减的功能。当加键按下时,计数器加1,当减键按下时,计数器减1,但不能小于0。同时,通过数码管显示函数`display()`将计数值显示在四数码管上,以便用户观察。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值