51单片机C语言练习题
单片机型号
普中科技的
led练习题
led小灯闪烁
#include<reg52.h>
sbit LED = P0^0;
void main()
{unsigned int i=0;while(1){LED = 1;for(i=0;i<60000;i++);LED = 0;for(i=0;i<60000;i++);}
}
led流水灯
#include<reg52.h>void main()
{unsigned char LED = 0x01;unsigned int i=0;while(1){P0 = LED;LED = LED << 1;//从左向右依次点亮LEDif(LED == 0) //点亮第八盏后,点亮第一盏{LED = 0x01;}for(i=0;i<40000;i++);//延迟}
}
每隔一秒点亮一次 led,led闪烁,定时器实现
- 配置定时器工作模式
- 定时器赋初值
- 启动定时器
- 判断定时器是否溢出
#include<reg52.h>
sbit LED = P0^0;
void main()
{unsigned char cnt = 0;unsigned int i=0;TMOD = 0x01;TH0 = 0xD8;TL0 = 0xF0;TR0 = 1;LED = 0;while(1){if(TF0 == 1){TF0 = 0;TH0 = 0xD8;TL0 = 0xF0;cnt++;if(cnt == 100){cnt = 0;LED = ~LED;}}}}
数码管练习题
静态数码管依次显示0-F,每隔一秒更换一次
#include<reg52.h>
unsigned char code segCodeTable[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void main()
{unsigned char cnt = 0;unsigned int i=0;TMOD = 0x01;TH0 = 0xD8;TL0 = 0xF0;TR0 = 1;while(1){if(TF0 == 1){TF0 = 0;TH0 = 0xD8;TL0 = 0xF0;cnt++;if(cnt == 100){cnt = 0;P0 = segCodeTable[i];i++;}if(i == 16){i = 0;}}}}
数码管实现秒表
#include<reg52.h>
//共阴极数码管
unsigned char code segCodeTable[16] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79};
unsigned char code posCodeTable[8] = {
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//位选控制 查表的方法控制
//数码管显示缓冲区,初始时刻为全零
unsigned char LEDBuffer[6] = {0x3f,0x3f,0x3f,0x3f,0x3f,0x3f };
void main()
{ unsigned int cnt = 0;unsigned long sec = 0;unsigned char i = 0;TMOD = 0x01;TH0 = 0xFC;TL0 = 0x18;TR0 = 1;while(1){if(TF0 == 1){TF0 = 0;TH0 = 0xFC;TL0 = 0x18;cnt++;if(cnt == 1000){cnt = 0;sec++;//每隔一秒刷新一次数码管显示缓冲区LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];LEDBuffer[3] = segCodeTable[sec/1000%10];LEDBuffer[4] = segCodeTable[sec/10000%10];LEDBuffer[5] = segCodeTable[sec/100000];}//每隔1ms输出一次段码与位码P0 = LEDBuffer[i];P1 = posCodeTable[i];i++;if(i == 6){i = 0;}}}}
中断方式实现数码管秒表
#include<reg52.h>
//共阴极数码管
unsigned char code segCodeTable[16] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79};
unsigned char code posCodeTable[8] = {
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//位选控制 查表的方法控制
//数码管显示缓冲区,初始时刻为全零
unsigned char LEDBuffer[6] = {0x3f,0x3f,0x3f,0x3f,0x3f,0x3f };
unsigned char i = 0;
unsigned char flag1s = 0;unsigned int cnt = 0;
void main()
{ unsigned long sec = 0;TMOD = 0x01;TH0 = 0xFC;TL0 = 0x18;TR0 = 1;EA = 1;ET0 = 1;while(1){if(flag1s == 1){sec++;flag1s = 0;//每隔一秒刷新一次数码管显示缓冲区LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];LEDBuffer[3] = segCodeTable[sec/1000%10];LEDBuffer[4] = segCodeTable[sec/10000%10];LEDBuffer[5] = segCodeTable[sec/100000];}}}void InterruptTime0() interrupt 1
{TH0 = 0xFC;TL0 = 0x18;cnt++;if(cnt == 1000){cnt = 0;flag1s = 1;}//每隔1ms输出一次段码与位码P0 = LEDBuffer[i];P1 = posCodeTable[i];i++;if(i == 6){i = 0;}
}
数码管高位为零不显示
#include<reg52.h>
//共阴极数码管
unsigned char code segCodeTable[16] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79};
unsigned char code posCodeTable[8] = {
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//位选控制 查表的方法控制
//数码管显示缓冲区
unsigned char LEDBuffer[8] = {0x3f,0x00,0x00,0x00,
0x00,0x00,0x00,0x00};
unsigned int cnt = 0;
unsigned char i = 0;
unsigned char flag1s = 0;
unsigned char temp = 0x7f;
void main()
{unsigned long sec = 0;TMOD = 0x01;TH0 = 0xFC;TL0 = 0x18;TR0 = 1;EA =1;ET0 =1;while(1){if(flag1s == 1){flag1s = 0;//刷新数码管显示缓冲区sec++;if(sec>=0&&sec<10){LEDBuffer[0] = segCodeTable[sec%10];}else if(sec>=10&&sec<100) //sec小于10,不更新数码管高位显示缓冲区{LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];}else if(sec>=100&&sec<1000){LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];}else if(sec>=1000&&sec<10000){LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];LEDBuffer[3] = segCodeTable[sec/1000%10];}else if(sec>=10000&&sec<100000){LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];LEDBuffer[3] = segCodeTable[sec/1000%10];LEDBuffer[4] = segCodeTable[sec/10000%10];}else if(sec>=100000){LEDBuffer[0] = segCodeTable[sec%10];LEDBuffer[1] = segCodeTable[sec/10%10];LEDBuffer[2] = segCodeTable[sec/100%10];LEDBuffer[3] = segCodeTable[sec/1000%10];LEDBuffer[4] = segCodeTable[sec/10000%10];LEDBuffer[5] = segCodeTable[sec/100000];} }}
}void InterruptTimer0() interrupt 1
{TH0 = 0xFC;TL0 = 0x18;cnt++;if(cnt == 1000){cnt = 0;flag1s = 1;}//每隔一ms输出一次段码和位码P0 = LEDBuffer[i];P1 = posCodeTable[i];i++;if(i == 6){i = 0;}}