将下列c语言程序改写为用中断方式控制51单片机数码管秒表0~59循环:#include <REG51.H> sbit sa=P2^2; sbit sb=P2^3; sbit sc=P2^4; unsigned char second=0; void delay5ms() { TH0=(65536-5000)/256; TL0=(65536-5000)%256; TR0=1; while(!TF0); TF0=0; } void sin(unsigned char i) { unsigned char led []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; sa=1,sb=0,sc=0; P0=led[i/10]; delay5ms(); P0=0x00; sa=0;sb=0;sc=0; P0=led[i%10]; delay5ms(); P0=0x00; } void main() { unsigned int i; TMOD=0x00; TH0=(65536-5000)/256; TL0=(65536-5000)%256; TR0=1; while(1) { for(i=0;i<100;i++) { sin(second); } second++; if(second==60) second=0; } }
时间: 2023-08-04 18:04:30 浏览: 123
以下是使用中断方式控制51单片机数码管秒表0~59循环的程序:
```c
#include <reg51.h>
sbit sa = P2^2;
sbit sb = P2^3;
sbit sc = P2^4;
unsigned char second = 0;
void delay5ms() {
TH0 = (65536 - 5000) / 256;
TL0 = (65536 - 5000) % 256;
TR0 = 1;
while (!TF0);
TF0 = 0;
}
void sin(unsigned char i) {
unsigned char led[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
sa = 1, sb = 0, sc = 0;
P0 = led[i / 10];
delay5ms();
P0 = 0x00;
sa = 0; sb = 0; sc = 0;
P0 = led[i % 10];
delay5ms();
P0 = 0x00;
}
void timer0_isr() interrupt 1 {
TH0 = (65536 - 5000) / 256;
TL0 = (65536 - 5000) % 256;
sin(second);
second++;
if (second == 60) {
second = 0;
}
}
void main() {
TMOD = 0x01;
TH0 = (65536 - 5000) / 256;
TL0 = (65536 - 5000) % 256;
TR0 = 1;
ET0 = 1;
EA = 1;
while (1);
}
```
这个程序使用了定时器0中断来控制数码管的显示,实现了秒表的循环计时功能。在主函数中设置了定时器0的工作模式为模式1,开启了定时器0中断,并开启总中断开关。在定时器0中断服务程序中,每次定时器0中断时更新定时器0的计数值,然后调用sin函数显示秒表时间,并更新秒表时间。注意,由于使用了中断方式,所以程序中没有while(1)循环。
阅读全文