/********************************************************/
我用的51单片机晶振频率是12MHz,我打算利用计数器每隔1秒让led轮流亮,数码管每隔两秒亮,
但是实际情况是数码管保持两秒一亮,但是led确非常混乱,所以我想让大家看看是什么问题,下面
贴代码
/**********************************************************/
#include <reg52.h>
#include <intrins.h>
typedef unsigned int uint;
typedef unsigned char uchar;
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit segment_selection = P2^1;//段选
sbit bit_selection = P2^0;//位选
uchar tt;
uchar temp;
uchar num;
void inter0() interrupt 1
{
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
tt++;
/*下面注释的代码在while(1)里面写有问题,但是在中断函数里写就没问题
// if(tt == 20 || tt == 40)
// {
// temp = _cror_(temp, 1);
// P1 = temp;
// if(tt == 40)
// {
// tt = 0;
// segment_selection = 1;
// P0 = table[num];/从0到f;
// segment_selection = 0;
// P0 = 0x00;
// num++;
// if(num == 16)
// {
// num = 0;
// }
// }
// }
}
void main()
{
num = 0;
temp = 0xfe;
tt = 0;
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;/单片机总中断开启
ET0 = 1;/定时器0中断开启
TR0 = 1;/定时器0中断允许位开启
while(1)
{
if( tt == 20)//这里我是打算每隔1秒led循环亮
{
P1 = temp;
temp = _crol_(temp, 1);
}
if(tt == 40)
{
tt = 0;
segment_selection = 1;
P0 = table[num]
segment_selection = 0;
P0 = 0x00;//消隐
num++;
if(num == 16)
{
num = 0;
}
}
//在while(1)循环里这么写有问题,在中断函数里写就没问题
}
}
|