昨天把所有单片机组要考试的内容看完后独立的写出了led和独立key,现在进行下一个模块的代码编写,编写数码管的模块
首先我简单说明下单片机组考试的算是范围吧:
上篇简单熟悉下CT107D的开发平台的流程这次相对来说就比较好操作,话不多说了直接上视频给的练习吧
8位数码管点亮显示01234567
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar seg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,
0xff
};
uchar num=8;
void allinit();
void display(uchar yi,er,san,si,wu,liu,qi,ba);
void Delay1ms();
void Delayms(uint ms);
void main()
{
allinit();
while(1)
{
display(num,num+1,num+2,num+3,num+4,num+5,num+6,num+7);
}
}
void allinit()
{
P2=0xa0;P0=0x00;
P2=0x80;P0=0xff;
P2=0xe0;P0=0xff;
P2=0xc0;P0=0xff;
}
void display(uchar yi,er,san,si,wu,liu,qi,ba)
{
P2=0xe0;//段选
P0=seg[yi];
P2=0xc0;//位选
P0=0x01;
Delay1ms();
P2=0xe0;//段选
P0=seg[er];
P2=0xc0;//位选
P0=0x02;
Delay1ms();
P2=0xe0;//段选
P0=seg[san];
P2=0xc0;//位选
P0=0x04;
Delay1ms();
P2=0xe0;//段选
P0=seg[si];
P2=0xc0;//位选
P0=0x08;
Delay1ms();
P2=0xe0;//段选
P0=seg[wu];
P2=0xc0;//位选
P0=0x10;
Delay1ms();
P2=0xe0;//段选
P0=seg[liu];
P2=0xc0;//位选
P0=0x20;
Delay1ms();
P2=0xe0;//段选
P0=seg[qi];
P2=0xc0;//位选
P0=0x40;
Delay1ms();
P2=0xe0;//段选
P0=seg[ba];
P2=0xc0;//位选
P0=0x80;
Delay1ms();
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delayms(uint ms) //@11.0592MHz
{
unsigned char i, j,k;
for(k=ms;k>0;k--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
99秒倒计时当计时完成时蜂鸣器报警实验:
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar seg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,
0xff
};
uint tim;
uchar num=10;
uchar yi=16, er=16;
uchar guan=16;
void allinit();
void display(uchar yi,er,san,si,wu,liu,qi,ba);
void Delay1ms();
void Delayms(uint ms);
void Timer0Init();
void main()
{
allinit();
Timer0Init();
while(1)
{
yi=num/10;
er=num%10;
display(yi,er,guan,guan,guan,guan,guan,guan);
}
}
void allinit()
{
P2=0xa0;P0=0x00;
P2=0x80;P0=0xff;
P2=0xe0;P0=0xff;
P2=0xc0;P0=0xff;
}
void display(uchar yi,er,san,si,wu,liu,qi,ba)
{
P2=0xe0;//段选
P0=seg[yi];
P2=0xc0;//位选
P0=0x01;
Delay1ms();
P2=0xe0;//段选
P0=seg[er];
P2=0xc0;//位选
P0=0x02;
Delay1ms();
P2=0xe0;//段选
P0=seg[san];
P2=0xc0;//位选
P0=0x04;
Delay1ms();
P2=0xe0;//段选
P0=seg[si];
P2=0xc0;//位选
P0=0x08;
Delay1ms();
P2=0xe0;//段选
P0=seg[wu];
P2=0xc0;//位选
P0=0x10;
Delay1ms();
P2=0xe0;//段选
P0=seg[liu];
P2=0xc0;//位选
P0=0x20;
Delay1ms();
P2=0xe0;//段选
P0=seg[qi];
P2=0xc0;//位选
P0=0x40;
Delay1ms();
P2=0xe0;//段选
P0=seg[ba];
P2=0xc0;//位选
P0=0x80;
Delay1ms();
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delayms(uint ms) //@11.0592MHz
{
unsigned char i, j,k;
for(k=ms;k>0;k--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA = 1;//总中断开关
ET0 = 1;//定时器0中断开始
}
void timer0() interrupt 1
{
tim++;
if(tim==1000)
{
tim=0;
num--;
if(num==0)
{
ET0 =0;
P2=0x80;P0=0xfe;
}
}
}