单片机C语言程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
void delay(unsigned char i);
void main()
{
while(1){
P1=0x00;
delay(200);
P1=0xff;
delay(200);
}
}
void delay(unsigned char i)






//功能:控制一个信号灯闪烁程序
#include //包含头文件REG51.H,定义了MCS-51单片机的特殊功能寄存器
sbit P1_0=P1^0; //定义位名称
void delay(unsigned char i); //延时函数声明
void main() //主函数
{
while(1) {
P1_0=0; //点亮信号灯
delay(10); //调用延时函数,实际变量为10
P1_0=1; //熄灭信号灯
delay(10); //调用延时函数,实际变量为10
}
}
//函数功能:实现软件延时
void delay(unsigned char i) //延时函数,无符号字符型变量i为形式参数
{
unsigned char j,k; //定义无符号字符型变量j和k

for(k=0;kfor(j=0;j<255;j++);
}




//功能:控制八个信号灯闪烁程序
#include
void delay(unsigned char i); //延时函数声明
void main() //主函数
{
while(1) {
P1=0x00; //将P1口的八位引脚清0 delay(200); //延时
P1=0xff; //将P1口的八位引脚置1 delay(200); //延时
}
}
//函数名:delay
void delay(unsigned char i) //延时函数,无符号字符型变量i为形式参数
{
unsigned char j,k; //定义无符号字符型变量j和k
for(k=0;kfor(j=0;j<255;j++);
}





例如,下面程序是通过while语句实现计算并输出1-100的累加和。
#include //包含特殊功能寄存器库
#include //包含I/O函数库
void main(void) //主函数
{
int i.s=0;
i=1;
SCON=0x52; //串口初始化
TMOD=0x20;
TH1=0xf3;
TR1=1;
while (i<=100) //累加1-100之和在s中
{
s=s+i; i++;
}
printf(“1+2+3……+100=%d\n”,s);
while(1);
}





使用格式输入输出函数的例子
#include //包含特殊功能寄存器库
#include //包含I/O函数库
void main(void) //主函数
{
int x,y; //定义整型变量x和y
SCON=0x52; //串口初始化
TMOD=0x20;
TH1=0XF3;
TR1=1;
printf(“input x,y:\n”); //输出提示信息
scanf(“%d%d”,&x,&y); //输入x和y的值
printf(“\n”); //输出换行
printf(“%d+%d=%d”,x,y,x+y); //按十进制形式输出
printf(“\n”); //输出换行
printf(“%xH+%xH=%XH”,x,y,x+y); //按十六进制形式输出
while(1); //结束
}




//功能:模拟汽车转向灯控制程序
#include
sbit P1_0=P1^0; //定义P1.0引脚位名称为P1_0
sbit P1_1=P1^1; //定义P1.1引脚位名称为P1_1
sbit P3_0=P3^0; //定义P3.0引脚位名称为P3_0
sbit P3_1=P3^1; //定义P3.1引脚位名称为P3_1
void delay(unsigned

char i); //延时函数声明
void main() //主函数
{
bit left,right; //定义位变量left、right表示左、右状态
while(1) { //while循环语句,由于条件一直为真,该语句为无限循环
left=P3_0; //读取P3.0引脚的(左转向灯)状态并赋值给left
right=P3_1; //读取P3.1引脚的(右转向灯)状态并赋值给right
P1_0=left; //将left的值送至P1.0引脚
P1_1=right; //将right的值送至P1.1引脚
delay(200); //调用延时函数,实际参数为200
P1_0=1; //将P1.0引脚置1输出(熄灭LED)
P1_1=1; //将P1.1引脚置1输出(熄灭LED)
delay(200); //调用延时函数,实际参数为200
}
}
//函数名:delay
//函数功能:实现软件延时
void delay(unsigned char i) //延时函数,无符号字符型变量i为形式参数
{ unsigned char j,k; //定义无符号字符型变量j和k
for(k=0;k}




//功能:采用循环结构实现的流水灯控制程序
#include //包含头文件REG51.H
void delay(unsigned char i); //延时函数声明
void main() //主函数
{
unsigned char i,w;
while(1)
{
w=0x01; // 信号灯显示字初值为01H
for(i=0;i<8;i++)
{
P1=~w; // 显示字取反后,送P1口
delay(200); // 延时
w<<=1; // 显示字左移一位
}
}
}
//函数功能:实现软件延时
void delay(unsigned char i) //延时函数,无符号字符型变量i为形式参数
{
unsigned char j,k; //定义无符号字符型变量j和k
for(k=0;k}





//功能:简易密码锁程序
#include
sbit P3_0=P3^0; //控制开锁,用发光二极管代替
void delay(unsigned char i); //延时函数声明
void main() //主函数
{
unsigned char button; //保存按键信息
unsigned char code tab[7]={0xc0,0xf9,0xa4,0xb0,0xbf,0x86,0x8c}; //定义显示段码表,分别对应显示字符:0、1、2、3,-、E、P
P0=0xff; //读P0口引脚状态,需先置全1
while(1) {
P1=tab[4]; //密码锁的初始显示状态“-”
P3_0=1; //设置密码锁初始状态为“锁定”,发光二极管熄灭
button=P0; //读取P0口上的按键状态并赋值到变量button
button&=0x0f; //采用与操作保留低4位的按键状态,其他




switch (button) //判断按键的键值
{
case 0x0e: P1=tab[0];delay(400);P1=tab[5];break; //0#键按下,密码输入错误,显示“E”
case 0x0d: P1=tab[1];delay(400);P1=tab[5];break; //1#键按下,密码输入错误,显示“E”
case 0x0b: P1=tab[2];delay(400);P1=tab[6];P3_0=0;break; //2#键按下,密码正确,开锁并显示“P”
case 0x07: P1=tab[3];delay(400);P1=tab[5];break; //3#键按下,密码输入错

误,显示“E”
}
delay(1200); //显示状态停留约3秒
}
}
//函数名:delay
//函数功能:实现软件延时
void delay(unsigned char i) //延时函数,无符号字符型变量i为形式参数
{
unsigned char j,k; //定义无符号字符型变量j和k
for(k=0;kfor(j=0;j<255;j++);
}




简易密码锁设计
#include
sbit P3_0=P3^0;
void delay(unsigned char i);
void main()
{
unsigned char button;
unsigned char code tab[7]={0xc0,0xf9,0xa4,0xb0,0xbf,0x86,0x8c};
P0=0xff;
while(1){
P1=tab[4];
P3_0=1;
button=P0;
button&=0x0f;
switch(button)
{
case 0x0e: P1=tab[0];delay(400);P1=tab[5];break;
case 0x0d: P1=tab[1];delay(400);P1=tab[5];break;
case 0x0b: P1=tab[2];delay(400);P1=tab[6];P3_0=0;break;
case 0x07: P1=tab[3];delay(400);P1=tab[5];break;
}
delay(1200);
}
}
void delay(unsigned char i)
{
unsigned char j,k;
for(k=0;kfor(j=0;j<255;j++);
}





时间间隔为1S的流水灯控制
#include
void delay1s()
{
unsigned char i;
for(i=0;i<0x14;i++){
TH1=0x3c;
TL1=0xb0;
TR1=1;
while(!TF1);
TF1=0;
}
}
void main()
{
unsigned char i,w;
TMOD=0x10;
while(1){
w=0x01;
for(i=0;i<8;i++){
P1=~w;
w<<=1;
delay1s();
}
}
}



简易秒表控制
#include
void delay1s();
void main()
{
unsigned char i,t;
TMOD=0X10;
while(1)
{
for(i=0;i<60;i++)
{
t=(((i/10)<<4)|(i%10));
P1=~t;
delay1s();
}
}
}





可控弥红灯控制
#include
void delay0_5s()
{
unsigned char i;
for(i=0;i<0x0a;i++)
{
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(!TF0);
TF0=0;
}
}
void delay_t(unsigned char t)
{
unsigned char i;
for(i=0;i}
void int_0() interrupt 0
{
P1=0x00;
delay0_5s();
P1=0xff;
delay0_5s();
}


void main()
{
unsigned char i,w;
EA=1;
EX0=1;
IT0=1;
TMOD=0x01;
while(1){
w=0x01;
for(i=0;i<8;i++){
P1=~w;
w<<=1;
delay_t(2);
}
}
}




模拟交通灯控制
#include
unsigned char t0,t1;
void delay0_5s1()
{
for(t0=0;t0<0x0a;t0++)
{
TH1=0x3c;
TL1=0xb0;
TR1=1;
while(!TF1);
TF1=0;
}
}
void delay_t1(unsigned char t)
{
for(t1=0;t1delay0_5s1();
}
void int_0() interrupt 0
{
unsigned char i,j,k,l,m;
i=P1;
j=t0;
k=t1;
l=TH1;
m=TL1;
P1=0xdb;
delay_t1(20);
P1=i;
t0=j;
t1=k;
TH1=1;
TL1=m;
}
void int_1() interrupt 2
{
unsigned char i,j,k,l,m;
EA=0;
i=P1;
j=t0;
k=t1;
l=TH1;
m=TL1;
EA=1;
P1=0xf3;
delay_t1(10);
EA=0;
P1=i;
t0=j;
t1=k;
TH1=1;
TL1=m;


EA=1;
}
void main()
{
unsigned char k;
TMOD=0x10;
EA=1;
EX0=1;
IT0=1;
EX1=1;
IT1=1;
while(1){
P1=0xf3;
delay_t1(110);
for(k=0;k<3;k++){
P1=0xf3;
delay0_5s1();
P1=0xfb;
delay0_5s1();
}
P1=0xeb;
delay_t1(4);
P1=0xde;
delay_t1(110);
for(k=0;k<3;k++){
P1=0xde;
delay0_5s1();
P1=0xdf;
delay0_5s1();
}
P1=0xdd;
delay_t1(4);
}
}





单片机双机通信
甲机
#include
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
TR1=1;
while(1)
{
do{SBUF=0x01;
while(!TI);
TI=0;
while(!RI);
RI=0;
}
while((SBUF^0x02)!=0);
do{
SBUF=P1;
while(!TI);
TI=0;
SBUF=0xff;
while(!TI);
TI=0;
while(!RI);
RI=0;
}
while((SBUF^0xff)!=0);
}
}
乙机
#include
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
TR1=1;
P1=0xff;
while(1)
{
do{
while(!RI);
RI=0;
}
while((SBUF^0x01)!=0);
do{
SBUF=0x02;
while(!TI);
TI=0;
while(!RI);
RI=0;
P1=SBUF;
while(!RI);
RI=0;
}
while((SBUF^0xff)!=0);
SBUF=0xff;
while(!TI);
TI=0;

}
}





LED 数码管显示的简易秒表设计
#include
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay1s()
{
unsigned char i;
for(i=0;i<0x14;i++){
TH1=0x3c;
TL1=0xb0;
TR1=1;
while(!TF1);
TF1=0;
}
}
void main()
{
unsigned char i;
TMOD=0x10;
while(1){

for(i=0;i<10;i++)
{
P1=led[i];
delay1s();
}
}
}
12广告牌
#include
void delay1ms();
void main()
{
unsigned char code led[]={0x18,0x24,0x24,0x24,0x24,0x24,0x24,0x18, //0
0x00,0x18,0x1c,0x18,0x18,0x18,0x18,0x18, //1
0x00,0x1e,0x30,0x30,0x1c,0x06,0x06,0x3e, //2
0x00,0x1e,0x30,0x30,0x1c,0x30,0x30,0x1e, //3
0x00,0x30,0x38,0x34,0x32,0x3e,0x30,0x30, //4
0x00,0x1e,0x02,0x1e,0x30,0x30,0x30,0x1e, //5
0x00,0x1c,0x06,0x1e,0x36,0x36,0x36,0x1c, //6
0x00,0x3f,0x30,0x18,0x18,0x0c,0x0c,0x0c, //7
0x00,0x1c,0x36,0x36,0x1c,0x36,0x36,0x1c, //8
0x00,0x1c,0x36,0x36,0x36,0x3c,0x30,0x1c}; //9
unsigned char w;
unsigned int i,j,k,m;
while(1)
{
for(k=0;k<10;k++)
{
for(m=0;m<400;m++)
{
w=0x01;
j=k*8;
for(i=0;i<8;i++)
{
P1=w;
P0=led[j];
delay1ms();
w<<=1;
j++;
}
}
}
}
}
void delay1ms()
{
unsigned char i;
for(i=0;i<0x10;i++);
}

键盘按键
#include "reg51.h"
void delay()
{
TH1=0xd8;
TL1=0xf0;
TR1=1;
while(!TF1);
TF1=0;
}
void main()
{
unsigned char i ;
TMOD=0x10;
P1=0xff;
i=0;

while(1)
{
while(i==0)
{
i=P1;
i=~i;
}
delay ();
do
{
i=P3;
i=~i;
}
while(i==0);
switch(i)
{
case 0x01: key1();break;
case 0x02: key2();break;
case 0x04: key3();break;
case 0x08: key4();break;
case 0x10: key5();break;
case 0x20: key6();break;
case 0x40: key7();break;
case 0x80: key8();break;
default:break;
}
}
}
led数码显示
#include
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay1s();
void main()
{
unsigned char i;
TMOD=0x10;
while(1){
for(i=0;i<10;i++)
{
P1=led[i];
delay1s();
}
}
}
void delay1s()
{
unsigned char i;
for(i=0;i<0x14;i++){
TH1=0x3c;
TL1=0xb0;
TR1=1;
while(!TF1);
TF1=0;
}
}
13广告牌
#include
#include
sbit RS=0xb0;
sbit RW=0xb1;
sbit E=0xb2;
void lcd_w_cmd(unsigned char com);
void lcd_w_dat(unsigned char dat);
unsigned char lcd_r_start();
void int1();
void delay(unsigned char t);
void delay1();
void main()
{
unsigned char lcd[]="SHEN ZHEN";
unsigned char i;
P1=0xff;
int1();
delay(255);
lcd_w_cmd(0x83);
delay(255);
for(i=0;i<9;i++)
{
lcd_w_dat(lcd[1]);
delay(200);
}
while(1);
}
void delay(unsigned char t)
{
unsigned char j,i;
for(i=0;ifor(j=0;i<50;j++);
}
void delay1()
{
_nop_();
_nop_();
_nop_();
}
void int1()
{
lcd_w_cmd(0x3c);
lcd_w_cmd(0x0e);
lcd_w_cmd(0x01);
lcd_w_cmd(0x06);
lcd_w_cmd(0x80);
}
unsigned char lcd_r_start()
{
unsigned char s;
RW=1;
delay1();
RS=0;
delay1();
E=1;
delay1();
s=P1;
delay1();
E=0;
delay1();
RW=0;
delay1();
return(s);
}
void lcd_w_cmd(unsigned char com)
{
unsigned char i;
do{
i=lcd_r_start();
i=i&0x80;
delay(2);
}while(i!=0);
RW=0;
delay1();
RS=0;
delay1();
E=1;
delay1();
P1=com;
delay1();
E=0;
delay1();
RW=1;
delay(255);
}
void lcd_w_dat(unsigned char dat)
{
unsigned char i;
do{
i=lcd_r_start();
i=i&0x80;
delay(2);
}while(i!=0);
RW=0;
delay1();
RS=1;
delay1();
E=1;
delay1();
P1=dat;
delay1();
E=0;
delay1();
RW=1;
delay(255);
}
8.22
#include
unsigned char da[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x0f8,0x80,0x90};
void delay(unsigned int i);
main()
{
unsigned char i;
P1=0xff;
SCON=0x00;
while(1){
for(i=0;i<8;i++)
{ TI=0;
SBUF=da[i];
while(!TI);
delay(2000);
}
}
}
void delay(unsigned int i)
{
unsigned char k;
unsigned int j;
for(j=0;jfor(k=0;k<255;k++);
}
5.13
#include
void delay50ms()
{
TH1=0x3c;
TL1=0xb0;
TR1=1;
while(!TF1);
TF1=0;
}
main()
{
unsigned char led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92};
unsigned char i,w;
TMOD=0x10;
while(1)
{
w=0x01;
for(i=0;i<6;i++)
{
P2=~w;
w<<=1;
P1=led[i];
del

ay50ms;
}
}
}


相关文档
最新文档