单片机代码

合集下载

单片机常用c代码

单片机常用c代码

用89s52单片机的机器车1.串口向计算机发送信息,串口和串口调试终端软件用到uart_Init();函数scanf函数与printf函数对应,在C51库的stdio.hprintf("Program Running!\n");printf(“Please input pulse number:\n”);scanf(“%d”,&PulseNumber);printf(“Please input pulse duration:\n”);scanf(“%d”,&PulseDuration);for(Counter=1;Counter<=PulseNumber;Counter++){P1_1=1;delay_nus(PulseDuration);P1_1=0;delay_nms(20);}for(Counter=1;Counter<=PulseNumber;Counter++){P1_0=1;delay_nus(PulseDuration);P1_0=0;delay_nms(20);}while(1);}2.for循环来实现电机速度逐渐增加到全速然后逐步减小的例子。

for(pulseCount=10;pulseCount<=200;pulseCount=pulseCount+1)加速{P1_1=1;delay_nus(1500+pulseCount);P1_1=0;P1_0=1;delay_nus(1500-pulseCount);P1_0=0;delay_nms(20);}for(pulseCount=1;pulseCount<=75;pulseCount++)全速{P1_1=1;delay_nus(1700);P1_1=0;P1_0=1;delay_nus(1300);P1_0=0;delay_nms(20); }for(pulseCount=200;pulseCount>=0;pulseCount=pulseCount-1)减速{P1_1=1;delay_nus(1500+pulseCount);P1_1=0;P1_0=1;delay_nus(1500-pulseCount);P1_0=0;delay_nms(20);}while(1);}3.子函数的书写类型标识符 函数名(形式参数列表){声明部分//函数体内部用到的变量的类型说明语句}void Forward(void)无参数无返回值void Forward(int PulseCount,int Velocity) 有参数无返回值int Forward(void)无参数有返回值用return x;返回int Forward(int PulseCount,int Velocity) 有参数有返回值用return x;返回void Move(int counter,int PC1_pulseWide,int PC0_pulseWide){int i;for(i=1;i<=counter;i++){P1_1=1;delay_nus(PC1_pulseWide);P1_1=0;P1_0=1;delay_nus(PC0_pulseWide);P1_0=0;delay_nms(20);}}int main(void){uart_Init();printf("Program Running!\n");Move(65,1700,1300);//前Move(26,1300,1300); //左Move(26,1700,1700); //右Move(65,1300,1700); //后while(1);}4.将复杂巡航运动记录在数组中,编写程序执行这些巡航运动类型说明符数组名[常量表达式]={值,值……值};//数组switch(表达式){case常量表达式1: 语句1;break;case常量表达式2: 语句2;break;case常量表达式n: 语句n;break;default: 语句n+1;break;}int main(void)//走方形的运动{char Navigation[10]={'F','L','F','F','R','B','L','B','B','Q'};int address=0;uart_Init();printf("Program Running!\n");while(Navigation[address]!='Q'){switch(Navigation[address]){case 'F':Forward();break;case 'L':Left_Turn();break;case 'R':Right_Turn();break;case 'B':Backward();break;}address++;}while(1);}以上需调用子函数,但为了方便,我们如此使用数组在本例程中,将不使用子函数,而是使用三个整型数组来存储控制机器人运动的三个变量,即循环的次数和控制左右电机运动的两个参数。

单片机时钟程序设计代码

单片机时钟程序设计代码

单片机时钟程序设计代码好的,由于你没有给出具体的单片机型号,下面是一个基于 51 单片机的时钟程序设计示例:```c#include <reg52.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>// 定义变量char str[20];int i=0;int j=0;int k=0;int time=0;int flag=0;int h=0,m=0,s=0;void delay(int xms){int i,j;for(i=xms;i>0;i--)for(j=110;j>0;j--);}void time0() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;time++;if(time==20){time=0;s++;if(s==60){s=0;m++;if(m==60){m=0;h++;}}}// 将时间数据转换为字符串sprintf(str," %02d:%02d:%02d",h,m,s);// 输出时间字符串for(i=0;i<16;i++){P0=str[i];delay(1000);}}void main(){// 初始化定时器 0 为 16 位定时器模式,计数初值为 50000 TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;while(1);}```该程序利用 51 单片机的定时器 0 来产生定时中断,每 20 次中断为 1 秒,通过对秒、分、时的累计来实现时钟功能。

在中断服务函数中,将当前时间转换为字符串,并通过循环输出到 P0 口。

单片机源代码

单片机源代码
TR1=1;
num=0;
while(1)
{
p2_0=num;
}
}
void int_1()interrupt 3
{
unsigned char n;
TH1=0x3c;
TL1=0xb0;
TR1=1;
n++;
if(n==10)
{
num=~num;
n=0;}
}
左右跑马灯
for(j=255;j>0;j--);
}
二进制加法实验
#include<reg52.h>
void delay(unsigned int i);//声明延时 函数
main()
{
unsigned char Num=oxff;
while(1)
{
p2=Num;
delay(1000);
#include<Function.h>
bit ISendstr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no)
{
unsigned char i;
start_12c();//启动总线
sendByte(sla);//发送器件地址
SendByte(suba);//发送器件子地址
if(ack==0)return(0);
for(i==o;i<no;i++)
{
SendByte(*s);//发送数据
if(ack==0)return(0);

单片机控制交通灯程序代码

单片机控制交通灯程序代码

#include<reg51.h>sbit P10=P1^0;sbit P11=P1^1;sbit P12=P1^2;sbit P13=P1^3;sbit P20=P2^0;sbit P21=P2^1;sbit P22=P2^2;sbit P23=P2^3;sbit P24=P2^4;sbit P25=P2^5;sbit P32=P3^2;unsigned int a1=0,a2=0,a3=0,time1=20,time2=3,flag=0;unsigned char code led[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};main(void){TMOD=0x11;TH0=0xee;TL0=0x00;TR0=1;ET0=1;EA=1;a1=time1;while(1);}void timer0() interrupt 1{static unsigned char Bit=0,i=0,huan=0,deng=0,m=0,n=0,l=0,k=1;TH0=0xee;TL0=0x00;if(flag==0||flag==3)if(a1>0 && a2==0 && a3==0){ i++;if(i%200==0){i=0;a1--;} P25=0;if(a1==0){a3=time2;P25=1; }}else if (a2>0 && a1==0 && a3==0) { i++;if(i%200==0){i=0;a2--;} P23=0;if(a2==0){a3=time2;P23=1; }}else if (a3>0 && a1==0 && a2==0) { i++;if(i%200==0){i=0;a3--;} P24=0;flag=3;if(a3==0)switch(huan){case(0):flag=0;huan=1;a2=time1;P24=1;break;case(1):flag=0;huan=0;a1=time1;P24=1;break;}}if(P20==1){m=1;}if(P20==0&&m==1){ flag++;m=0;a1=time1;a2=0;a3=0;P23=1;P24=1;P25=0;if(flag>=3)fla g=0; }if(P21==1){n=1;}if(P21==0&&n==1){n=0;switch(flag){case(1):if(time1<10000)time1++;break;case(2):if(time2<10000)time2++;break;}}if(P22==1){l=1;}if(P22==0&&l==1){l=0;switch(flag){case(1):if(time1>0)time1--;break;case(2):if(time2>0)time2--;break;}}if(P32==1){k=1;}if(P32==0&&k==1){k=0;switch(deng){case(0):deng=1;a2=a1;a1=0;;a3=0;P23=0;P24=1;P25=1;break;case(1):deng=0;a1=a2;a2=0;;a3=0;P23=1;P24=1;P25=0;break;}}if(Bit>3){Bit=0;}Bit++;P1|=0xff;switch(flag){case(0):switch(Bit-1){case(0):P0=led[a1%10]; P13=0;break;case(1):P0=led[a1/10]; P12=0;break;case(2):P0=(led[a2%10])|0x80; P11=0;break;case(3):P0=led[a2/10]; P10=0;break;}break;case(1):switch(Bit-1){case(0):P0=led[time1%10]; P13=0;break;case(1):P0=led[time1%100/10]; P12=0;break;case(2):P0=led[time1%1000/100];P11=0;break;case(3):P0=led[time1/1000]; P10=0;break;}break;case(2):switch(Bit-1){case(0):P0=led[time2%10]; P13=0;break;case(1):P0=led[time2%100/10]; P12=0;break;case(2):P0=led[time2%1000/100];P11=0;break;case(3):P0=led[time2/1000]; P10=0;break;}break;case(3):switch(Bit-1){case(0):P0=led[a3%10]; P13=0;break;case(1):P0=led[a3%100/10]; P12=0;break;case(2):P0=led[a3%1000/100];P11=0;break;case(3):P0=led[a3/1000]; P10=0;break;}break;}}。

51单片机50个实例代码

51单片机50个实例代码

51单片机50个例程代码程序里有中断,串口等驱动,直接复制即可使用1-IO输出-点亮1个LED灯方法1/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的一个LED灯该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义sbit LED=P1^0;// 用sbit 关键字定义 LED到端口,//LED是自己任意定义且容易记忆的符号/*------------------------------------------------主函数------------------------------------------------*/void main (void){//此方法使用bit位对单个端口赋值LED=1; //将口赋值 1,对外输出高电平LED=0; //将口赋值 0,对外输出低电平while (1) //主循环{//主循环中添加其他需要一直工作的程序 }}2-IO输出-点亮1个LED灯方法2/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的一个LED灯该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义/*------------------------------------------------主函数------------------------------------------------*/ void main (void){//此方法使用1个字节对单个端口赋值P1 = 0xFF; //P1口全部为高电平,对应的LED灯全灭掉,//ff换算成二进制是 1111 1111P1 = 0xfe; //P1口的最低位点亮,可以更改数值是其他的灯点亮//0xfe是16进制,0x开头表示16进制数,//fe换算成二进制是 1111 1110while (1) //主循环{//主循环中添加其他需要一直工作的程序}}3-IO输出-点亮多个LED灯方法1/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的多个LED灯该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义sbit LED0=P1^0;// 用sbit 关键字定义 LED到端口,sbit LED1=P1^1;//LED是自己任意定义且容易记忆的符号sbit LED2=P1^2;sbit LED3=P1^3;sbit LED4=P1^4;sbit LED5=P1^5;sbit LED6=P1^6;sbit LED7=P1^7;/*------------------------------------------------主函数------------------------------------------------*/ void main (void){//此方法使用bit位对单个端口赋值LED0=0; //将口赋值 0,对外输出低电平LED1=1;LED2=0;LED3=1;LED4=0;LED5=1;LED6=0;LED7=1;while (1) //主循环{//主循环中添加其他需要一直工作的程序 }}4-IO输出-点亮多个LED灯方法2/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的多个LED灯该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义/*------------------------------------------------主函数------------------------------------------------*/ void main (void){//此方法使用bit位对单个端口赋值P1=0x55; //换成二进制是0101 0101while (1) //主循环{//主循环中添加其他需要一直工作的程序 }}5-闪烁1个LED/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的1个LED灯闪烁该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/ #include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义sbit LED0=P1^0;// 用sbit 关键字定义 LED到端口,//LED是自己任意定义且容易记忆的符号void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/ void main (void){//此方法使用bit位对单个端口赋值while (1) //主循环{LED0=0; //将口赋值 0,对外输出低电平Delay(10000); //调用延时程序;更改延时数字可以更改延时长度; //用于改变闪烁频率LED0=1; //将口赋值 1,对外输出高电平Delay(10000);//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}6-不同频率闪烁1个LED灯/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的1个LED灯闪烁该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义sbit LED0=P1^0;// 用sbit 关键字定义 LED到端口,//LED是自己任意定义且容易记忆的符号void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){//此方法使用bit位对单个端口赋值unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255while (1) //主循环{for(i=0;i<10;i++) //加入 for循环,表明for循环大括号中的程序循环执行10次 {LED0=0; //将口赋值 0,对外输出低电平Delay(5000); //调用延时程序;更改延时数字可以更改延时长度;//用于改变闪烁频率LED0=1; //将口赋值 1,对外输出高电平Delay(5000);}for(i=0;i<10;i++){LED0=0; //将口赋值 0,对外输出低电平Delay(60000); //调用延时程序;更改延时数字可以更改延时长度; //用于改变闪烁频率LED0=1; //将口赋值 1,对外输出高电平Delay(60000);}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}7-不同频率闪烁多个LED灯/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:点亮P1口的多个LED灯闪烁该程序是单片机学习中最简单最基础的,通过程序了解如何控制端口的高低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义sbit LED0=P1^0;// 用sbit 关键字定义 LED到端口,LED是自己任意定义且容易记忆的符号sbit LED1=P1^1;sbit LED2=P1^2;sbit LED3=P1^3;sbit LED4=P1^4;sbit LED5=P1^5;sbit LED6=P1^6;sbit LED7=P1^7;void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){//此方法使用bit位对单个端口赋值unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255while (1) //主循环{for(i=0;i<10;i++) //加入 for循环,表明for循环大括号中//的程序循环执行10次{LED0=0; //将口赋值 0,对外输出低电平LED2=1;LED4=0;Delay(5000); //调用延时程序;更改延时数字可以更改延时长度;//用于改变闪烁频率LED0=1; //将口赋值 1,对外输出高电平LED2=0;LED4=1;Delay(5000);}for(i=0;i<10;i++){LED0=0; //将口赋值 0,对外输出低电平LED2=1;LED4=0;Delay(60000); //调用延时程序;更改延时数字可以更改延时长度;//用于改变闪烁频率LED0=1; //将口赋值 1,对外输出高电平LED2=0;LED4=1;Delay(60000);}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}8-8位LED左移/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:左移,直至LED全部点亮,左移符号 <<------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255Delay(50000);P1=0xfe; //赋初始值for(i=0;i<8;i++) //加入 for循环,表明for循环大括号中的程序循环执行8次 {Delay(50000);P1<<=1;}while (1) //主循环{//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}9-8位LED右移/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:右移,直至LED全部点亮,右移符号 >>------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255Delay(50000);P1=0x7f; //赋初始值for(i=0;i<8;i++) //加入 for循环,表明for循环大括号中的程序循环执行8次 {Delay(50000);P1>>=1;}while (1) //主循环{//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}10-LED循环左移/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:循环左移,始终一个led点亮,并循环执行流水动作,左移符号 << 逻辑或符号 |------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255Delay(50000);P1=0xfe; //赋初始值while (1) //主循环{for(i=0;i<8;i++) //加入 for循环,表明for循环大括号中的程序循环执行8次 {Delay(50000);P1<<=1;P1=P1|0x01; //左移后,最右端自动赋值0,所以需要该语句赋值1}P1=0xfe; //重新赋初始值//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}11-LED循环右移/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:循环右移,始终一个led点亮,并循环执行流水动作,右移符号 >> 逻辑或符号 |------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255Delay(50000);P1=0x7f; //赋初始值while (1) , //主循环{for(i=0;i<8;i++) //加入 for循环,表明for循环大括号中的程序循环执行8次 {Delay(50000);P1>>=1;P1=P1|0x80; //右移后,最左端自动赋值0,所以需要该语句赋值1}P1=0x7f; //重新赋初始值//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}12-查表显示LED灯/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:通过查表获得1个字节的值然后赋值给P1端口,ROM允许情况可以设置更多表格------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义/*------------------------------------------------查表表格------------------------------------------------*/unsigned char code table[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0x00,0x00,0x55,0x55,0xaa,0xaa};//rom允许情况可以无限添加/*------------------------------------------------函数声明------------------------------------------------*/void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255while (1) //主循环{for(i=0;i<16;i++) //加入 for循环,表明for循环大括号中的程序循环 //执行16次,表明表格中有16个元素{P1=table[i];Delay(30000);}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}13-双灯左移右移闪烁/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:2个led灯先左移,然后右移,然后同时闪烁------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i//取值范围 0~255unsigned char temp;//定义临时变量用于转换while (1) //主循环{temp=0xfc;P1=temp;for(i=0;i<7;i++) //加入 for循环,表明for循环大括号 //中的程序循环执行7次{Delay(50000); //调用延时程序;更改延时数字可//以更改延时长度;用于改变闪烁频率 temp<<=1;temp=temp|0x01; //也可以直接把temp换成P1P1=temp; //之所以加入temp做中间变量,转换 //完成后直接赋值到端口,防止直接 //操作端口造成的短暂闪烁}temp=0x3f;P1=temp;for(i=0;i<7;i++) //加入 for循环,表明for循环大括号 //中的程序循环执行7次{Delay(50000); //调用延时程序;更改延时数字可以 //更改延时长度;用于改变闪烁频率 temp>>=1;temp|=0x80; //等效于 temp=temp|0x80P1=temp;}for(i=0;i<3;i++) //加入 for循环,表明for循环大括号 //中的程序循环执行3次{P1=0xff;Delay(50000);P1=0x00;Delay(50000);} //主循环中添加其他需要一直工作的程序 }}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}14-花样灯/*-----------------------------------------------名称:IO口高低电平控制论坛:编写:shifang日期:修改:无内容:花样灯一般有两种方法实现:1、程序循环执行2、查表法这里我们两种都使用一下------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义/*------------------------------------------------花样表格------------------------------------------------*/unsigned char code seg[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0x00,0x00,0x55,0x55,0xaa,0xaa};//rom允许情况可以无限添加void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/main(){unsigned char i; //定义一个无符号字符型//局部变量 i 取值范围 0~255 while(1){P1=0xFE; //第1个LED亮for(i=0;i<8;i++){Delay(20000);P1 <<=1;}P1=0x7F; //第8个LED亮for(i=0;i<8;i++){Delay(20000);P1 >>=1;}P1=0xFE; //第1个LED亮for(i=0;i<8;i++){Delay(20000);P1 <<=1;P1 |=0x01;}P1=0x7F; //第8个LED亮for(i=0;i<8;i++){Delay(20000);P1 >>=1;P1 |=0x80;}//下面通过查表方法获得花样参数for(i=0;i<16;i++)//查表可以简单的显示各种花样实用性更强 {Delay(20000);P1=seg[i];}}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}15-PWM调光/*-----------------------------------------------名称:PWM调光论坛:编写:shifang日期:修改:无内容:通过PWM(脉宽调制)调节LED的亮度------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义sbit LED0=P1^0;// 用sbit 关键字定义 LED到端口,LED是自己任意定义且容易记忆的符号void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned int CYCLE=600,PWM_LOW=0;//定义周期并赋值while (1) //主循环{LED0=1;Delay(60000); //特意加延时,可以看到熄灭的过程for(PWM_LOW=1;PWM_LOW<CYCLE;PWM_LOW++){ //PWM_LOW表示低//电平时间,这个循环中低电平时长从1累加到CYCLE(周期)的值,即600次LED0=0; //点亮LEDDelay(PWM_LOW);//延时长度,600次循环中从1加至599LED0=1; //熄灭LEDDelay(CYCLE-PWM_LOW);//延时长度,600次循环中从599减至1}LED0=0;for(PWM_LOW=CYCLE-1;PWM_LOW>0;PWM_LOW--){ //与逐渐变亮相反的过程LED0=0;Delay(PWM_LOW);LED0=1;Delay(CYCLE-PWM_LOW);}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}16-共阳数码管静态显示/*-----------------------------------------------名称:单个共阳数码管静态显示论坛:编写:shifang日期:修改:无内容:通过赋值给P1,让数码管显示特定的字符或者数字------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义void main (void){P1=0xc0; //二进制为 1100 0000 参考数码管排列,//可以得出0对应的段点亮,1对应的段熄灭,结果显示数字"0" while(1){}}17-1个共阳数码管显示变化数字/*-----------------------------------------------名称:单个共阳数码管动态显示变化数字论坛:编写:shifang日期:修改:无内容:通过循环赋值给P1,让数码管显示特定的字符或者数字------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义unsigned char code dofly_table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x77,0x7c,0x 39,0x5e,0x79,0x71,};// 显示数值表0-9void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255while (1) //主循环{for(i=0;i<16;i++) //加入 for循环,表明for循环大括号中的程序循环执行10次 {P1=dofly_table[i]; //循环调用表中的数值Delay(60000); //延时,方便观看数字变化}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}18-单个数码管模拟水流/*-----------------------------------------------名称:单个共阳数码管模拟水流论坛:编写:shifang日期:修改:无内容:通过循环赋值给P1,让数码管显示特定流动样式------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义void Delay(unsigned int t); //函数声明/*------------------------------------------------主函数------------------------------------------------*/void main (void){unsigned char i; //定义一个无符号字符型局部变量 i 取值范围 0~255while (1) //主循环{P1=0xfe;for(i=0;i<6;i++) //加入 for循环,表明for循环大括号中的程序循环执行6次 {Delay(10000);P1<<=1;P1|=0x01;}//主循环中添加其他需要一直工作的程序}}/*------------------------------------------------延时函数,含有输入参数 unsigned int t,无返回值unsigned int 是定义无符号整形变量,其值的范围是0~65535------------------------------------------------*/void Delay(unsigned int t){while(--t);}19-按键控制单个数码管显示/*-----------------------------------------------名称:单个共阳数码管动态显示变化数字论坛:编写:shifang日期:修改:无内容:通过循环检测P1口按键输入值,然后用数码管输出,没有按键按下时原值不变------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义unsigned char code dofly_table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};// 显示数值表0-9/*------------------------------------------------主函数------------------------------------------------*/void main (void){while (1) //主循环{switch(P3) //P3口作为独立按键输入端,检测端口电平并做如下判断 {case 0xfe:P1=dofly_table[1];break;//0xfe = 1111 1110,//说明连接在端口的按键被按下,显示对应的数字然后跳出循环case 0xfd:P1=dofly_table[2];break;//调用表中的第三个//元素 0xa4 下标 0 才表示数组中的第一个元素case 0xfb:P1=dofly_table[3];break;case 0xf7:P1=dofly_table[4];break;case 0xef:P1=dofly_table[5];break;case 0xdf:P1=dofly_table[6];break;case 0xbf:P1=dofly_table[7];break;case 0x7f:P1=dofly_table[8];break;default:break; //如果都没按下,直接跳出}//主循环中添加其他需要一直工作的程序}}20-单个数码管指示逻辑电平/*-----------------------------------------------名称:单个共阳数码管指示逻辑电平论坛:编写:shifang日期:修改:无内容:通过循环检测口电平输入值,然后用数码管输出"H" or "L",表示该端口现在连接的是高电平还是低电平------------------------------------------------*/#include<> //包含头文件,一般情况不需要改动,//头文件包含特殊功能寄存器的定义sbit IO_IN=P3^2; //定义IO 信号输入端/*------------------------------------------------主函数------------------------------------------------*/void main (void){while (1) //主循环{if(IO_IN==1)//如果端口检测到1表示高电平P1=0x89; //"H"elseP1=0xc7; //"L"//否则表示低电平,这里使用条件语句 if 。

单片机程序代码

单片机程序代码

闪烁灯一/***********************************************************************/#include<reg52.h>#define uchar unsigned char //这里用"uchar"代替"unsigned char","uchar"用来定义无符号字符型数。

#define uint unsigned int //"uint"用来定义无符号整型数。

sbit Show_LED = P0^0; //用来声明P0.0口为程序所要控制的端口,"sbit"是KEIL专门用来声明某位IO口/********************************************************************* 名称: Delay()* 功能: 延时,延时时间为10ms * del。

这是通过软件延时,有一定误差。

* 输入: del* 输出: 无***********************************************************************/void Delay(uint del){uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++) //这个是通过软件仿真得出的数;}/********************************************************************* 名称: Main()* 功能: 实现灯的闪烁* 输入: 无* 输出: 无***********************************************************************/void Main(void){P0 = 0xff; //关闭所有LEDwhile(1){Show_LED = 1;Delay(20); //Delay(del)中的数为延时的时间参数,延时时间为10ms * del.Show_LED = 0;Delay(20);}}闪烁灯二/**********************************************************************/#include<reg52.h>#define uchar unsigned char#define uint unsigned int/********************************************************************* 名称: Delay()* 功能: 延时,延时时间为10ms * del* 输入: del* 输出: 无***********************************************************************/ void Delay(uint del){uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}/********************************************************************* 名称: Main()* 功能: 实现灯的闪烁* 输入: 无* 输出: 无***********************************************************************/ void Main(void){P0 = 0xff; //关闭所有LEDwhile(1){/*下面四行用于控制LED每隔一个亮*/P0 = 0xaa;Delay(50);P0 = 0x55;Delay(50);/*下面四行用于控制LED每隔一个亮*/P0 = 0xcc;Delay(50);P0 = 0x33;Delay(50);/*下面四行用于控制LED 高四位或者低四位亮*/P0 = 0x0f;Delay(50);P0 = 0xf0;Delay(50);/*下面八行用于控制LED的两次全亮全灭*/P0 = 0x00;Delay(25);P0 = 0xff;Delay(25);P0 = 0x00;Delay(25);P0 = 0xff;Delay(25);Delay(200); //延时两秒}}流水灯一/***********************************************************************/ #include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int/********************************************************************* 名称: Delay()* 功能: 延时,延时时间为10ms * del* 输入: del* 输出: 无***********************************************************************/ void Delay(uint del){uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}/********************************************************************* 名称: Main()* 功能: 实现灯的闪烁* 输入: 无* 输出: 无***********************************************************************/void Main(void){uchar i;while(1){P0 = 0x7f; //P0.7亮亮for(i=0; i<7; i++) //移位7次{P0 = _cror_(P0, 1); //_crol_()这个函数在intrins.h中定义了,用于循环右移Delay(15);}P0 = 0xfe;for(i=0; i<7; i++){P0 = _crol_(P0, 1); //用于循环左移Delay(15);}}}流水灯二/**********************************************************************/#include<reg52.h>#define uchar unsigned char#define uint unsigned int/********************************************************************* 名称: Delay()* 功能: 延时,延时时间为10ms * del* 输入: del* 输出: 无***********************************************************************/void Delay(uint del){uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}/********************************************************************* 名称: Main()* 功能: 实现灯的闪烁* 输入: 无* 输出: 无* 说明:也可以使用例1的的方法来关闭数码管***********************************************************************/void Main(void){uchar i, temp;while(1){temp = 0x7f;for(i=0; i<7; i++){temp = temp >> 1; //值右移一位temp = temp | 0x80;P0 = temp; //把值赋给P0口Delay(15); //延时0.15秒}temp = 0xfe;for(i=0; i<7; i++){temp = temp << 1; //值左移一位temp = temp | 0x01;P0 = temp; //把值赋给P0口Delay(15); //延时0.15秒}}}数码管静态显示一/**********************************************************************/#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY = P3^4; //独立按键的T0/*下一行你可以试着把code 去掉看看试验结果。

单片机常用代码大全

单片机常用代码大全

1、位码(共阴):0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f2、断码(共阳):0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x713、独立管码(共阳):0x02,0x9e,0x24,0x0c,0x98,0x48,0x40,0x1e,0x00,0x08,0x10,0xc0,0x62,0x84,0x60,0x704、循环函数intrins_crol_字符循环左移_cror_字符循环右移_irol_整数循环左移_iror_整数循环右移_lror_长整数循环左移_lror_长整数循环右移_nop_空操作8051 NOP 指令_testbit_测试并清零位8051 JBC 指令5、中段代码interrupt 0 外部中断0interrupt 1 定时器0中断interrupt 2 外部中断1interrupt 3 定时器1中断interrupt 4 串口中断interrupt 5 定时器2中断(52里面,51没有)6、IIC总线开始信号:void IICstart(){scl=1;delay();sda=1;delay();sda=0;delay();scl=0;}结束信号void IICstop(){scl=1;delay();sda=0;delay();sda=1;}应答信号void IICyingda(){uchar j;scl=1;delay();while((sda==1)&&j<250)j++;//sda=0;//delay();scl=0;delay();}写信号(写入一8位数据)void write_byte(uchar w){uchar i,temp;temp=w;for(i=0;i<8;i++){scl=0;delay();temp=temp<<1;sda=CY;delay();scl=1;delay();}scl=0;delay();sda=0;delay();}读信号(读出一个8位数据)uchar read_byte(void){uchar i,j,k;sda=1;for(i=0;i<8;i++){scl=1;delay();j=sda;k=(k<<1)|j;scl=0;delay();}scl=0;delay();return k;}7、时钟调试键盘扫描void keyscan(){if(k1==0){delay(5);if(k1==0){while(!k1); //k1num++;}if(k1num==1){TR0=0;write_zhiling(0x80+0x40+11);write_zhiling(0x0f);}if(k1num==2){TR0=0;write_zhiling(0x80+0x40+8);}if(k1num==3){TR0=0;write_zhiling(0x80+0x40+5);}if(k1num==4){k1num=0;write_zhiling(0x0c);TR0=1;//}}if(k1num!=0){if(k2==0){delay(5);if(k2==0){while(!k2);if(k1num==1){miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;write_jingwei(7,fen);}write_jingwei(10,miao);write_jingwei(7,fen);write_zhiling(0x80+0x40+11);}write_jingwei(10,miao);write_zhiling(0x80+0x40+11);}if(k1num==2){fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;write_jingwei(4,shi);}write_jingwei(7,fen);write_jingwei(4,shi);write_zhiling(0x80+0x40+8);}write_jingwei(7,fen);write_zhiling(0x80+0x40+8);}if(k1num==3){shi++;if(shi==24){shi=0;write_jingwei(4,shi);write_zhiling(0x80+0x40+5);}write_jingwei(4,shi);write_zhiling(0x80+0x40+5);}}}}if(k1num!=0){if(k3==0){delay(5);if(k3==0){while(!k3);if(k1num==1){miao--;if(miao==-1){miao=59;fen--;if(fen==-1){fen=59;write_jingwei(7,fen);}write_jingwei(10,miao);write_jingwei(7,fen);write_zhiling(0x80+0x40+11);}write_jingwei(10,miao);write_zhiling(0x80+0x40+11); }if(k1num==2){fen--;if(fen==-1){fen=59;shi--;if(shi==-1){shi=23;write_jingwei(4,shi);}write_jingwei(7,fen);write_jingwei(4,shi);write_zhiling(0x80+0x40+8);}write_jingwei(7,fen);write_zhiling(0x80+0x40+8); }if(k1num==3){shi--;if(shi==-1){shi=23;write_jingwei(4,shi);write_zhiling(0x80+0x40+5);}write_jingwei(4,shi);write_zhiling(0x80+0x40+5);}}}}}。

单片机代码

单片机代码

方波#include<reg52.h>#define uint unsigned intbit F_10ms; //位变量uint data n=0; //无符号整型变量void Timer0_Init() //定时器工作方式设定{TMOD=0x01; //定时器工作在方式1下TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节ET0=1; //T0中断允许EA=1; //CPU中断允许TR0=1; //T0开始工作}void ISR_T0(void) interrupt 1{TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节F_10ms=1; //标志位}void Mn_Timer_center(void){if(F_10ms==1) //10MS标志位为1{F_10ms = 0 ; //10MS标志位为1n++; //LED计数器加一if(n==50) //10ms基准*n(10)产生500ms计时{P1=0x00; //LED亮}if(n==100) //10ms基准*n(10)产生1000ms计时{P1=0xff; //LED灭n=0; //LED计数器清零}}}void main(){Timer0_Init(); //定时器初始化while(1){Mn_Timer_center(); //调用函数}矩形波#include<reg52.h>#define uint unsigned intbit F_10ms; //位变量uint data n=0; //无符号整型变量void Timer0_Init() //定时器工作方式设定{TMOD=0x01; //定时器工作在方式1下TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节ET0=1; //T0中断允许EA=1; //CPU中断允许TR0=1; //T0开始工作}void ISR_T0(void) interrupt 1{TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节F_10ms=1; //标志位}void Mn_Timer_center(void){if(F_10ms==1) //10MS标志位为1{F_10ms = 0 ; //10MS标志位为1n++; //LED计数器加一if(n==90) //10ms基准*n(10)产生900ms计时{P1=0x00; //LED亮}if(n==100) //10ms基准*n(10)产生1000ms计时{P1=0xff; //LED灭n=0; //LED计数器清零}}}void main(){Timer0_Init(); //定时器初始化while(1)Mn_Timer_center(); //调用函数}}20ms 100ms 1s波#include<reg52.h>#define uint unsigned intbit F_10ms; //位变量uint data n=0; //无符号整型变量void Timer0_Init() //定时器工作方式设定{TMOD=0x01; //定时器工作在方式1下TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节ET0=1; //T0中断允许EA=1; //CPU中断允许TR0=1; //T0开始工作}void ISR_T0(void) interrupt 1{TH0=0xdc; //10MS定时高字节TL0=0x00; //10MS定时低字节F_10ms=1; //标志位}void Mn_Timer_center(void){if(F_10ms==1) //10MS标志位为1{F_10ms = 0 ; //10MS标志位为1n++; //LED计数器加一if(n==80) //10ms基准*n(10)产生800ms计时{P1=0x00; //LED亮}if(n==82) //10ms基准*n(10)产生820ms计时{P1=0xff; //LED灭}if(n==162) //10ms基准*n(10)产生1620ms计时{P1=0x00; //LED亮}if(n==172) //10ms基准*n(10)产生1720ms计时{P1=0xff; //LED灭}if(n==252) //10ms基准*n(10)产生2520ms计时{P1=0x00; //LED亮}if(n==352) //10ms基准*n(10)产生3520ms计时{P1=0xff; //LED灭n=0; //LED计数器清零}}}void main(){Timer0_Init(); //定时器初始化while(1){Mn_Timer_center(); //调用函数}}。

单片机常用c代码

单片机常用c代码

单片机常用c代码在单片机领域,C语言是最常用的编程语言之一。

它具有简单易学、灵活高效的特点,被广泛应用于单片机系统的开发中。

本文将介绍一些常用的单片机C代码,为读者提供参考和学习的资源。

一、IO口控制单片机的IO口是与外部设备连接的重要接口,通过控制IO口的高低电平来实现与外部设备的通信。

以下是常见的IO口控制代码示例:1. 设置IO口为输出模式:```c#define LED_PIN 0 // 指定IO口引脚号void setup() {pinMode(LED_PIN, OUTPUT); // 设置IO口为输出模式}void loop() {digitalWrite(LED_PIN, HIGH); // 设置IO口为高电平delay(1000); // 延迟1秒digitalWrite(LED_PIN, LOW); // 设置IO口为低电平delay(1000); // 延迟1秒```2. 设置IO口为输入模式:```c#define BUTTON_PIN 1 // 指定IO口引脚号void setup() {pinMode(BUTTON_PIN, INPUT); // 设置IO口为输入模式}void loop() {if (digitalRead(BUTTON_PIN) == HIGH) { // 判断IO口电平是否为高电平// 执行相应操作}}```二、定时器控制定时器是单片机中的重要组件,可用于实现精确的时间控制和周期性任务。

以下是常见的定时器控制代码示例:1. 设置定时器计数器和预分频值:void setup() {TCCR1B = (1 << CS12) | (1 << CS10); // 设置定时器1的预分频为1024}void loop() {// 执行相应操作}```2. 设置定时器中断服务程序:```cISR(TIMER1_COMPA_vect) {// 定时器1比较匹配中断服务程序}void setup() {TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); // 设置定时器1的CTC模式和预分频为1024OCR1A = 15624; // 设置定时器1的比较匹配值,实现1秒中断一次TIMSK1 = (1 << OCIE1A); // 允许定时器1比较匹配中断}void loop() {// 执行相应操作}```三、串口通信串口通信是单片机与计算机或其他外部设备进行数据交互的常用方式。

单片机c语言代码及应用

单片机c语言代码及应用

单片机c语言代码及应用单片机是一种集成了中央处理器、内存和各种外设接口的微型计算机。

它广泛应用于家用电器、工控设备、汽车电子等各个领域。

C语言是单片机最常用的编程语言,它具有代码简洁、运行效率高的特点。

下面我将为你介绍一些单片机C 语言代码的应用。

首先,我们可以使用C语言编写一个LED闪烁的程序。

这个程序可以通过单片机的GPIO口控制LED的亮灭,实现LED的闪烁效果。

以下是一个简单的LED 闪烁的C语言代码:c#include <reg51.h>void delay(unsigned int count) 延时函数{while (count);}int main(){while (1) {P1 = 0xFF; 点亮LEDdelay(50000);P1 = 0x00; 熄灭LEDdelay(50000);}return 0;}上面的代码中,我们首先定义了一个延时函数`delay`,它通过循环来实现延时。

然后在`main`函数中,通过控制P1口的电平来控制LED的亮灭,并调用延时函数来控制闪烁的时间间隔。

这样就实现了一个简单的LED闪烁程序。

另外一个常见的应用是通过单片机控制液晶显示屏。

液晶显示屏可以用来显示各种信息,比如温度、湿度、时间等。

以下是一个使用单片机控制液晶显示屏显示字符串的程序:c#include <reg51.h>#define LCD_DATA P0 数据口sbit LCD_RS = P1^0; RS口sbit LCD_RW = P1^1; RW口sbit LCD_EN = P1^2; EN口void delay(unsigned int count) 延时函数{while (count);}void lcd_write_command(unsigned char command) 写指令{LCD_RS = 0; 设置为指令模式LCD_RW = 0; 设置为写入模式LCD_DATA = command;LCD_EN = 1;delay(5);LCD_EN = 0;}void lcd_write_data(unsigned char data) 写数据{LCD_RS = 1; 设置为数据模式LCD_RW = 0; 设置为写入模式LCD_DATA = data;LCD_EN = 1;delay(5);LCD_EN = 0;}void lcd_init() LCD初始化{lcd_write_command(0x38); 初始化显示模式lcd_write_command(0x08); 关闭显示lcd_write_command(0x01); 清屏lcd_write_command(0x06); 设置光标移动方向lcd_write_command(0x0C); 显示开启}void lcd_display_string(unsigned char x, unsigned char y, unsigned char *string) 在指定位置显示字符串{unsigned char address;if (y == 0) {address = 0x80 + x;} else {address = 0xC0 + x;}lcd_write_command(address);while (*string) {lcd_write_data(*string++);}}int main(){unsigned char string[] = "Hello, World!";lcd_init(); LCD初始化lcd_display_string(0, 0, string); 在第一行显示字符串while (1);return 0;}上面的代码中,我们首先定义了一些宏和端口变量,用来控制液晶显示屏。

单片机程序源代码完整版.doc

单片机程序源代码完整版.doc
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void main()
{
uint i;
while(1)
{
P1=0xff;
delay(500);
for(i=0;i<4;i++)
{
P1=table[i];
delay(500);
{uint j;
for(j=0;j<120;j++);
}
}
void main()
{
while(1)
{
P0=0xfe;
while(P0!=0x7f)
{
//P1=temp;
//delayms(500);
P0=_crol_(P0,1);
delayms(250);
}
temp=0x7f;
while(P0!=0xfe)
利用89c51单片机的端口控制8个LED(D0~D7)循环点亮,刚开始时D0点亮,延时片刻后,接着D1点亮,然后依次点亮D2->D3->D4->D5 ->D6->D7,然后再点亮D7->D6->D5->D4 ->D3->D2->lt;reg52.h>
#include <intrins.h>
}
for(i=3;i>0;i--)
{
P1=table[i-1];
delay(500);
}
}
}
任务四:复杂广告灯的设计
利用89c51单片机的端口实现8个LED(D0~D7)复杂广告灯的控制,要求显示规律为:正向流水->反向流水->隔灯闪烁3次->高四盏、低四盏闪烁2次->隔两盏闪烁3次,再重复循环。

单片机程序源代码

单片机程序源代码

第二章任务一:闪烁广告灯的设计利用89c51单片机的端口控制两个LED(D0和D1),编写程序,实现两个LED互闪。

#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit LED1=P0^0;sbit LED2=P0^1;void delayms(uint ms){uint i;while(ms--){for(i=0;i<120;i++);}}void main(){while(1){LED1=0;LED2=1;delayms(500);LED1=1;LED2=0;delayms(500);}}任务二:流水广告灯的设计利用89c51单片机的端口控制8个LED(D0~D7)循环点亮,刚开始时D0点亮,延时片刻后,接着D1点亮,然后依次点亮D2->D3->D4->D5 ->D6->D7 ,然后再点亮D7->D6->D5->D4 ->D3->D2->D1->D0,重复循环。

#include<reg52.h>#include <intrins.h>#define uint unsigned int#define uchar unsigned charuint i;uchar temp;uint a[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void delayms(uint ms){while(ms--){ uint j;for(j=0;j<120;j++);}}void main(){while(1){P0=0xfe;while(P0!=0x7f){//P1=temp;//delayms(500);P0=_crol_(P0,1);delayms(250);}temp=0x7f;while(P0!=0xfe){P0=temp;delayms(500);temp=_cror_(temp,1);}}}任务三:拉幕式与闭幕式广告灯的设计利用89c51单片机的P0端口实现8个LED D0~D7的拉幕式与闭幕式广告灯设计。

单片机仿真代码

单片机仿真代码

单片机仿真代码1. 简介单片机(Microcontroller)是一种集成了微处理器、内存和外围设备的集成电路芯片。

它广泛应用于嵌入式系统中,能够实现各种控制和处理功能。

在开发单片机应用程序时,我们通常需要进行仿真测试,以确保代码的正确性和稳定性。

本文将介绍单片机仿真代码的相关知识,并提供一些常见的仿真代码示例。

2. 单片机仿真工具在编写单片机仿真代码之前,我们需要选择合适的仿真工具。

以下是几个常用的单片机仿真工具:•Proteus:Proteus是一款功能强大的电子电路设计软件,它提供了完整的单片机仿真环境。

•Keil:Keil是一款集成开发环境(IDE),提供了强大的编译、调试和仿真功能。

•MPLAB X IDE:MPLAB X IDE是为Microchip系列单片机设计的IDE,支持多种编程语言和调试工具。

选择合适的仿真工具可以根据自己熟悉程度、项目需求以及软件特点等因素进行权衡。

3. 单片机仿真代码示例3.1 LED闪烁程序以下是一个使用C语言编写的LED闪烁程序的仿真代码示例:#include <reg51.h>sbit LED = P1^0;void delay(unsigned int count) {unsigned int i, j;for(i = 0; i < count; i++)for(j = 0; j < 1000; j++);}void main() {while(1) {LED = 0;LED = 1;delay(100);}}这段代码使用了51系列单片机的寄存器定义和延时函数,通过控制LED引脚的电平实现LED的闪烁效果。

3.2 温度传感器读取程序以下是一个使用C语言编写的温度传感器读取程序的仿真代码示例:#include <reg51.h>sbit SDA = P2^0;sbit SCL = P2^1;void delay_us(unsigned int us) {while(us--);}void start() {SDA = 1;SCL = 1;delay_us(5);SDA = 0;delay_us(5);SCL = 0;}void stop() {SDA = 0;SCL = 1;delay_us(5);SDA = 1;}unsigned char read_byte() {unsigned char i, byte = 0;for(i=0; i<8; i++) {byte <<= 1;SCL = 1;byte |= SDA;SCL = 0;delay_us(5);}return byte;}void main() {unsigned char temperature;while(1) {start();write_byte(0x90); // 发送温度传感器地址write_byte(0x00); // 发送温度传感器数据地址start();write_byte(0x91); // 发送温度传感器地址(读取模式)temperature = read_byte(); // 读取温度值stop();// 在这里进行温度值的处理和显示delay_ms(1000);}}这段代码通过I2C总线协议与温度传感器通信,读取并处理温度值,并可以在外部设备上显示出来。

单片机常用代码

单片机常用代码
************************************
#include"reg51.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
uchar num;
date_date[3]=yea%10;
date_date[2]=yea%100/10;
date_date[1]=yea/100%10;
date_date[0]=yea/1000;
date_date[4]=date_date[7]=15;
write_cmd(0x80+5);
sbit RW=P3^6;
sbit EN=P3^7;
void DelayMs(uint ms)//延时
{
uchar i;
while(ms--)for(i=0;i<120;i++);
}
void write_com(uchar com)//写指令
{
RS=0;//选择指令
P2=com;
DelayMs(1000);DelayMs(1000);DelayMs(1000);
write_cmd(0x01);al(2);//复位
for(i=0;i<5;i++)write_dat(date[i]);
write_cmd(0x80+40);//换到第二行
for(i=0;i<5;i++)write_dat(time[i]);

单片机代码

单片机代码

单片机代码单个发光二极管的点亮#include#define uchar unsigned char#define uint unsigned int //{ P1 =0xfe; //点亮最后一个LEDwhile(1){}}顺时针流水灯#include //头文件#define uchar unsigned char //宏定义#define uint unsigned intvoid Delay(uint del) //时延函数{ uint i,j;for(i=0; i#define uchar unsigned char#define uint unsigned intuchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e, 0x79,0x71};//下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d[16] ={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9 ,0xde, 0xf9,0xf1};void main(){ uchar i = 0;P2 = 0xfe;P0 = table[5]; //显示5,大家可以修改里面的数据。

while(1){}}任务2 两位数码管动态显示#include#define uchar unsigned char#define uint unsigned intUchar code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void Delay(uint i){ uint x,j;for(j=0;j#define uchar unsigned char#define uint unsigned intuchar code table[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; uchar code Birth[8]={1,9,4,0,0,8,1,6};void Delay(uint i){ uchar x,j;for(j=0;j#define uchar unsigned char#define uint unsigned int//数码管的段码编码uchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e, 0x79,0x71};void Delay(uint i){ uint x,j;for(j=0;j#define uchar unsigned char#define uint unsigned int//数码管的段码编码uchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e, 0x79,0x71};void Delay(uint del){ uint i,j;for(i=0; i#define uchar unsigned char#define uint unsigned intuchar code table[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义北面的LEDsbit N_Red = P1^0;sbit N_Yellow = P1^1;sbit N_Green = P1^2;//定义东面的LEDsbit E_Red = P1^3;sbit E_Yellow = P1^4;sbit E_Green = P1^5;//定义南面的LEDsbit S_Red = P1^6;sbit S_Yellow = P1^7;sbit S_Green = P3^4;//定义西面的LEDsbit W_Red = P3^5;sbit W_Yellow = P3^6;sbit W_Green = P3^7;uchar Count = 0;uchar Sec = 0;void Delay(uint del){ uint i,j;for(i=0; i#include#define uint unsigned int#define uchar unsigned char//心形显示unsigned char code tabP0[]={0x00,0x1C,0x22,0x42,0x84,0x42,0x22,0x1C};unsigned char code tabP2[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};void Delay_1ms(uchar i)//1ms延时{ uchar x,j;for(j=0;j#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;uchar Count = 0;uchar code table[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};void Delay(uint del){ uint i,j;for(i=0; i#define uchar unsigned char#define uint unsigned intuint Count = 0;uchar code table[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void Time0_Init() { TMOD = 0x00;TH0 = 0xE3;TL0 = 0x06;ET0 = 1;EA = 1;TR0 = 1;}void Time0_Int() interrupt 1{ TH0 = 0xE3;TL0 = 0x06;Count++; //长度加1}void main(){ uchar i = 0;Time0_Init();P2 = 0xfe;while(1){ P0 = table[i % 10]; //取 i 的个位if(Count == 1000) //当Count为 1000 时,i 自加一次,1000 * 0.1MS = 1S{ Count = 0;i++;}}}#include#define uchar unsigned char#define uint unsigned intvoid Delay(uint del) //延时函数{ uint i,j;for(i=0; i#include#define uint unsigned int#define uchar unsigned charuchar tabP2[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; uchar tabP0[6][8]={{0x00,0xDF,0xDF,0xDB,0xDB,0xFB,0xFB,0x00},{0x00,0x1F,0x1F,0x18,0x18,0xFF,0xFF,0x00},{0x00,0xDB,0xDB,0xDB,0xDB,0xFF,0xFF,0x00},{0x00,0xFB,0xFB,0xDB,0xDB,0xDF,0xDF,0x00},{0x00,0x00,0x84,0xFF,0xFF,0x80,0x00,0x00},{0x00,0xFF,0xFF,0xC3,0xC3,0xFF,0xFF,0x00},};void Delay_1ms(uchar i)//1ms延时{ uchar x,j;for(j=0;j#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;void Delay(uint del){ uint i,j;for(i=0; i#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;uint i=88;//数码管的段码编码uchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0 x79,0x71};//下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d[16] ={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0 xf9,0xf1};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void Delay_1ms(uint i){ uint x,j;for(j=0;j#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;//数码管的段码编码uchar table[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void Delay_1ms(uint i){ uint x,j;for(j=0;j#include#define uint unsigned int#define uchar unsigned char#define ulong unsigned longuchar code table[10] = {0x03, 0x9f, 0x25, 0x0d, 0x99, 0x49, 0x41,0x1f, 0x01, 0x09}; sbit E=P2^7; //1602使能引脚sbit RW=P2^6; //1602读写引脚sbit RS=P2^5; //1602数据/命令选择引脚void Delay_1ms(uint del)//1ms延时{ uint i,j;for(i=0;i#include#define uint unsigned int#define uchar unsigned charuchar code table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0 x79,0x71};void Delay_1ms(uint del)//1ms延时{ uint i,j;for(i=0;i#define uchar unsigned char#define uint unsigned intsbit KEY = P3^2;uchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0 x79,0x71};void Delay(uint del){ uint i,j;for(i=0; i#include#define uchar unsigned char#define uint unsigned intsbit E=P2^7; //1602使能引脚sbit RW=P2^6; //1602读写引脚sbit RS=P2^5; //1602数据/命令选择引脚void delay(){ _nop_();_nop_();}void Delay(uint del){ uint i,j;for(i=0;i相关文档:更多相关文档请访问:。

51单片机LED程序代码

51单片机LED程序代码
for(x=sm;x>0;x--) for(y=110;y>0;y--); } 3 流水灯. #include<reg52.h> //头文件 #include<intrins.h> sbit Leden=P1^2; //led 灯控制端,高导通 sbit wei=P1^1; // 数码管位控制,点阵列控制 sbit Line=P1^3; //点阵行控制 int x,y,k; void delay(unsigned int); unsigned char i; //定义变量 void main() {
1.点亮一个 LED #include<reg52.h> //包含头文件,一般情况不需要改动,
//头文件包含特殊功能寄存器的定义
void delay(unsigned int sm); sbit Leden=P1^2;// led 灯控制端 sbit LED=P0^0; //第一个 led 灯
sbit wei=P1^1; // 数码管位控制,点阵列控制 sbit Line=P1^3; //点阵行控制
Line=0; P0=0XFF; //关闭数码管 wei=0;
while (1) {
//主循环
LED=0; delay(150);
} } void delay(unsigned int sm) {
for(i=sm;i>0;i--) for(j=110;j>0;j--); } 2.LED 右移 #include<reg52.h> //头文件 #include<intrins.h> sbit Leden=P1^2; sbit wei=P1^1; // 数码管位控制,点阵列控制 sbit Line=P1^3; //点阵行控制 int x,y; void delay(unsigned int); unsigned char i; //定义变量

单片机汇编程序代码(闪烁灯、流水灯、蜂鸣器、点阵)

单片机汇编程序代码(闪烁灯、流水灯、蜂鸣器、点阵)
db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h
db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h
db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h
;TP0: DB 00H,3CH,42H,42H,42H,42H,3CH,00H ;圆形
;TP2: DB 07Fh,0BFh,0DFh,0EFh,0F7h,0FBh,0FDh,0FEh
END
MOV 20H,#00H ;中断计数器清0
MOV B,#00H ;表序号清0
MUSIC1:
NOP
CLR A
MOVC A,@A+DPTR ;查表取代码
JZ END0 ;是00H,则结束
CJNE A,#0FFH,MUSIC5
LJMP MUSIC3
MUSIC5:
NOP
MOV R6,A
db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h
db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h
db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h
一、闪烁灯
ORG 0000H
AJMP MAIN
ORG 0030H
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

单个发光二极管的点亮#include<reg52.h>#define uchar unsigned char#define uint unsigned int //"uint"用来定义无符号整型数。

void Main(void){ P1 =0xfe; //点亮最后一个LEDwhile(1){}}顺时针流水灯#include<reg52.h> //头文件#define uchar unsigned char //宏定义#define uint unsigned intvoid Delay(uint del) //时延函数{ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void Main(void){ uchar i, temp;while(1){ temp = 0xfe; //点亮最后一位led灯for(i=0; i<8; i++){ P1=temp;Delay(10);Temp=temp<<1 //向左移一位Temp=temp|0x01;}P1=0xff; //消影temp = 0xfe;for(i=0; i<4; i++){ P2=temp;Delay(10);Temp=temp<<1;Temp=temp|0x01;}P2 = 0xff;}}单位八段数码管的点亮#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e,0x79,0x71};//下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d[16] ={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9 ,0xde,0xf9,0xf1};void main(){ uchar i = 0;P2 = 0xfe;P0 = table[5]; //显示5,大家可以修改里面的数据。

while(1){}}任务2 两位数码管动态显示#include<reg52.h>#define uchar unsigned char#define uint unsigned intUchar code table[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void Delay(uint i){ uint x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void Main(void){ uchar i=200; //最大闪烁值为0.2秒while(1){ P0 = 0; //清零P2 = LED_W[1]; //第七位亮P0 = table[1]; // 第七个数码管显示1Delay(i);P0 = 0;P2 = LED_W[0]; //第八位亮P0 = table[2]; //第七个数码管显示2Delay(i);if(i!=2){ i=i-2; //每次减2}}}任务3 八位数码管显示生日(动态显示)#include <reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; uchar code Birth[8]={1,9,4,0,0,8,1,6};void Delay(uint i){ uchar x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void main(){ uchar i;while(1){ for(i=0;i<8;i++){ P0 = 0;P2 = LED_W[i];P0 = table[Birth[7-i]];Delay(2);}}}任务4 数码管倒计时显示#include<reg52.h>#define uchar unsigned char#define uint unsigned int//数码管的段码编码uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e,0x79,0x71};void Delay(uint i){ uint x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void DisLED(uchar temp){ P0 = 0xff; //消隐P2 = 0xfb; //第6位数码管显示P0 = table[temp/100]; //在这里取 i 的百位数Delay(2); //延时0.3秒后显示下一个数P0 = 0xff; //消隐P2 = 0xfd; //第7位数码管显示P0 = table[temp/10%10]; //在这里取 i 的十位数P0 = P0 | 0x80; //送入段选信号Delay(2); //延时0.3秒后显示下一个数P0 = 0xff; //消隐P2 = 0xfe; //第8位数码管显示P0 = table[temp%10]; //在这里取 i 的个位数Delay(2);}void main(){uchar i=150,j;while(1){ if(i != 0) //i不等于0,那么进行数码管的显示,10* 10=100毫秒{ for(j=0;j<15;j++){ DisLED(i); //调用数码管显示代码}i--;}else{ for(j=0;j<10;j++){ DisLED(i); //数码管显示}P0 = 0x00; //数码管不显示Delay(300);}}}任务5单位数码管自加,每隔0.5秒依次显示0~F#include<reg52.h>#define uchar unsigned char#define uint unsigned int//数码管的段码编码uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39 ,0x5e,0x79,0x71};void Delay(uint del){ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void main(){ uchar i = 0;P2 = 0xfe; //最后一位数码管被点亮while(1){ P0=table[i%16];i++;delay(50);}}任务一交通灯实验#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//定义北面的LEDsbit N_Red = P1^0;sbit N_Yellow = P1^1;sbit N_Green = P1^2;//定义东面的LEDsbit E_Red = P1^3;sbit E_Yellow = P1^4;sbit E_Green = P1^5;//定义南面的LEDsbit S_Red = P1^6;sbit S_Yellow = P1^7;sbit S_Green = P3^4;//定义西面的LEDsbit W_Red = P3^5;sbit W_Yellow = P3^6;sbit W_Green = P3^7;uchar Count = 0;uchar Sec = 0;void Delay(uint del){ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void Delay_1ms(uint i){ uint x,j;for(j=0;j<i;j++)for(x=0;x<148;x++){}}void LED(uchar dict,uchar rled,uchar yled,uchar gled){ if(dict == 1)//控制南北方向{ N_Red = rled;N_Yellow = yled;N_Green = gled;S_Red = rled;S_Yellow = yled;S_Green = gled;}if(dict == 2)//控制东西方向{ E_Red = rled;E_Yellow = yled;E_Green = gled;W_Red = rled;W_Yellow = yled;W_Green = gled;}}void Time0_Init(void){ TMOD = 0x01; //设置定时器0为工作方式1TH0 = 0x4c; //装初值TL0 = 0x00;IE = 0x82;TR0 = 1; //启动定时器0}void Time0_Int(void) interrupt 1 //50ms*20=1s{ TH0 = 0x4c; //定时计数器赋初值TL0 = 0x00;Count++; //中断溢出一次,计数器就加1if(Count == 20) //如果满足20次中断,即1秒到{ Count = 0;Sec--; //倒计时每次中断减1}}void LEDSeg(void){ P0 = 0; //清零P2 = LED_W[6]; //第二个LED灯亮P0 = table[Sec/10]; //十位显示值处理Delay_1ms(2); //延时2秒P0 = 0;P2 = LED_W[7]; //第一个LED灯亮P0 = table[Sec%10]; //个位显示值处理Delay_1ms(2);P2 = 0xff; //数码管不会被点亮}/********************************************************************* 名称 : Main()* 功能 : 实现灯的闪烁***********************************************************************/ void Main(void){ uchar i;Time0_Init();while(1){ Sec = 10; /*南北红灯亮、东西绿灯亮、持续时间10秒*/while(Sec != 0){ LED(1,0,1,1);LED(2,1,1,0);LEDSeg();}/*南北红灯亮、东西黄灯亮、持续时间5秒*/Sec = 5;while(Sec != 0){ for(i=0;i<100;i++){ LED(1,0,1,1);LED(2,1,0,1);LEDSeg();}LED(1,1,1,1);LED(2,1,1,1);Delay(50);}/*南北绿灯亮、东西红灯亮、持续时间10秒*/Sec = 10;while(Sec != 0){ LED(1,1,1,0);LED(2,0,1,1);LEDSeg();}/*南北黄灯亮、东西红灯亮、持续时间5秒*/Sec = 5;while(Sec != 0){ for(i=0;i<100;i++){ LED(1,1,0,1);LED(2,0,1,1);LEDSeg();}LED(1,1,1,1);LED(2,1,1,1);Delay(50);}}}任务二点阵实验#include <reg52.H>#include<intrins.h>#define uint unsigned int#define uchar unsigned char//心形显示unsigned char code tabP0[]={0x00,0x1C,0x22,0x42,0x84,0x42,0x22,0x1C};unsigned char code tabP2[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};void Delay_1ms(uchar i)//1ms延时{ uchar x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void main(){ int i;while(1){ for(i=0;i<8;i++){ P0=0;P2=tabP2[i];P0=tabP0[i];Delay_1ms(2);}}}外部中断0的电平触发#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;uchar Count = 0;uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};void Delay(uint del){ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void Outside_Init(void){ EX0 = 1; //开外部中断0IT0 = 0; //电平触发EA = 1; //开总中断}void Outside_Int0(void) interrupt 0 using 1{ if(KEY1 == 0){ Delay(2);while(1){ if(KEY1 == 1){ Delay(2);if(KEY1 == 1){ Count++;break;}}}}}void Main(void){ Outside_Init();P2 = 0xfe;while(1){ P0 = table[Count % 10];Delay(2);}}任务二定时器0的使用#include<reg52.h>#define uchar unsigned char#define uint unsigned intuint Count = 0;uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void Time0_Init(){ TMOD = 0x00;TH0 = 0xE3;TL0 = 0x06;ET0 = 1;EA = 1;TR0 = 1;}void Time0_Int() interrupt 1{ TH0 = 0xE3;TL0 = 0x06;Count++; //长度加1}void main(){ uchar i = 0;Time0_Init();P2 = 0xfe;while(1){ P0 = table[i % 10]; //取 i 的个位if(Count == 1000) //当Count为 1000 时,i 自加一次,1000 * 0.1MS = 1S{ Count = 0;i++;}}}#include<reg52.h>#define uchar unsigned char#define uint unsigned intvoid Delay(uint del) //延时函数{ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void Main(void){ uchar i,j=20, temp;while(1){ temp = 0xfe;for(i=0; i<8; i++){ P1=temp;Delay(j);temp=temp<<1;temp=temp|0x01;}P1=0xff;temp = 0xfe;for(i=0; i<4; i++){ P2=temp;Delay(10);temp=temp<<1;temp=temp|0x01;}P2 = 0xff;}}点阵倒计时#include <reg52.H>#include <intrins.h>#define uint unsigned int#define uchar unsigned charuchar tabP2[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; uchar tabP0[6][8]={{0x00,0xDF,0xDF,0xDB,0xDB,0xFB,0xFB,0x00},{0x00,0x1F,0x1F,0x18,0x18,0xFF,0xFF,0x00},{0x00,0xDB,0xDB,0xDB,0xDB,0xFF,0xFF,0x00},{0x00,0xFB,0xFB,0xDB,0xDB,0xDF,0xDF,0x00},{0x00,0x00,0x84,0xFF,0xFF,0x80,0x00,0x00},{0x00,0xFF,0xFF,0xC3,0xC3,0xFF,0xFF,0x00},};void Delay_1ms(uchar i)//1ms延时{ uchar x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void main(){ uchar i,j,k;while(1){ for(j=0;j<6;j++){ for(k=0;k<125;k++){ for(i=0;i<8;i++){ P0 = 0;P2 = tabP2[i];P0 = tabP0[j][i];Delay_1ms(1);}}}}}独立按键控制LED#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;void Delay(uint del){ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}uchar KEY(void){ if(KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0) { Delay(2);if(KEY1 == 0){ while(1){ if(KEY1 == 1){ Delay(2);if(KEY1 == 1){ break;}}}return 1;}if(KEY2 == 0){ while(1){ if(KEY2 == 1){ Delay(2);if(KEY2 == 1){ break;}}}return 2;}if(KEY3 == 0){ while(1){ if(KEY3 == 1){ Delay(2);if(KEY3 == 1){ break;}}}return 3;}if(KEY4 == 0){ while(1){ if(KEY4 == 1){ Delay(2);if(KEY4 == 1){ break;}}}return 4;}}return 0;}void Main(void){ uchar i = 0,j = 50;while(1){ i = KEY();if(i == 1){ P1 = 0xff;P2 = 0xf1;}if(i == 2){ P1 = 0x3f;P2 = 0xfe;}if(i == 3){ P1 = 0xc7;P2 = 0xff;}if(i == 4){ P1 = 0xf8;P2 = 0xff;}}}独立按键控制数码管加减#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;uint i=88;//数码管的段码编码uchar table[16] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//下一行的数组可以显示数值外,还可以显示数码管的点uchar table_d[16] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void Delay_1ms(uint i){ uint x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void Delay(uint del){ uint i,j;for(i=0;i<del;i++)for(j=0;j<1827;j++);}uchar KEY(void){ if(KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0){ Delay(2);if(KEY1 == 0){ while(1){ if(KEY1 == 1){ Delay(2);if(KEY1 == 1){ break;}}}return 1;}if(KEY2 == 0){ while(1){ if(KEY2 == 1){ Delay(2);if(KEY2 == 1){ break;}}}return 2;}if(KEY3 == 0){ while(1){ if(KEY3 == 1){ Delay(2);if(KEY3 == 1){ break;}}}return 3;}if(KEY4 == 0){ while(1){ if(KEY4 == 1){ Delay(2);if(KEY4 == 1){ break;}}}return 4;}}return 0;}void LEDShow(uchar temp){ P0 = 0;P2 = LED_W[1];P0 = table[(temp/10)%10];Delay_1ms(2);P0 = 0;P2 = LED_W[0];P0 = table[temp%10];Delay_1ms(2);}void Main(void){ uchar i = 0,j = 50;while(1){ i = KEY();if(i == 2){ j--;}if(i == 4){ j++;}LEDShow(j);}}独立按键抢答器#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY1 = P3^2;sbit KEY2 = P3^3;sbit KEY3 = P3^4;sbit KEY4 = P3^5;//数码管的段码编码uchar table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void Delay_1ms(uint i){ uint x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}uchar KEY(void){ if(KEY1==0 || KEY2==0 || KEY3==0 || KEY4==0){ Delay_1ms(20); //20毫秒软件防抖if(KEY1 == 0){ while(1){ if(KEY1 == 1){ Delay_1ms(20);;if(KEY1 == 1){ break;}}}return 1;}else if(KEY2 == 0){ while(1){ if(KEY2 == 1){ Delay_1ms(20);if(KEY2 == 1){ break;}}}return 2;}else if(KEY3 == 0){ while(1){if(KEY3 == 1){ Delay_1ms(20);if(KEY3 == 1){ break;}}}return 3;}else if(KEY4 == 0){ while(1){ if(KEY4 == 1){ Delay_1ms(20);if(KEY4 == 1){ break;}}}return 4;}}return 0;}/********************************************************************* 名称: Main()* 功能: 实现按键控制LED的显示* 输入: 无* 输出: 无***********************************************************************/ void Main(void){ uchar temp;while(1){ temp = KEY(); //判断是否有按键按下if(temp != 0){ break; //如果按下,那么退出循环}}P0 = table[temp]; //显示是哪个按键按下了P2 = 0xfe;while(1); //程序停止}矩阵键盘密码锁使用液晶1602#include<reg51.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longuchar code table[10] = {0x03, 0x9f, 0x25, 0x0d, 0x99, 0x49, 0x41, 0x1f, 0x01, 0x09}; sbit E=P2^7; //1602使能引脚sbit RW=P2^6; //1602读写引脚sbit RS=P2^5; //1602数据/命令选择引脚void Delay_1ms(uint del)//1ms延时{ uint i,j;for(i=0;i<del;i++)for(j=0;j<=148;j++){}}void delay(){ _nop_();_nop_();}void Busy(void){ bit busy_flag = 1;P0 = 0xff;RS = 0;delay();RW = 1;delay();E = 1;//Delay(1);while(1){ busy_flag = (bit)(P0 & 0x80);if(busy_flag == 0){ break;}}E = 0;}void wcmd(uchar del){ RS = 0;delay();RW = 0;delay();E = 0;delay();P0 = del;delay();delay();E = 0;}void wdata(uchar del){ delay();RS = 1;delay();RW = 0;delay();E = 0;delay();P0 = del;delay();E = 1;delay();E = 0;}void L1602_init(void){ Delay_1ms(15);wcmd(0x38);Delay_1ms(5);wcmd(0x38);Delay_1ms(5);wcmd(0x38);wcmd(0x38);Busy();wcmd(0x08);Busy();wcmd(0x01);Busy();wcmd(0x06);Busy();wcmd(0x0c);}void L1602_char(uchar hang,uchar lie,char sign) { uchar a;if(hang == 1){ a = 0x80;}if(hang == 2){ a = 0xc0;}a = a + lie - 1;wcmd(a);Busy();wdata(sign);}void L1602_string(uchar hang,uchar lie,uchar *p){ uchar a;if(hang == 1){ a = 0x80;}if(hang == 2){ a = 0xc0;}a = a + lie - 1;while(1){ Busy();wcmd(a);Busy();wdata(*p);a++;p++;if((*p == '\0')||(a==0x90)||(a==0xd0)){ break;}}}uchar Keyscan(void){ uchar i,j,k,temp, Buffer[4] = {0x0e, 0x0d, 0x0b, 0x07}; //让矩阵键盘的每行分别为低电平for(j=0; j<4; j++) //每行进行循环{ temp = 0x10;for(i=0; i<4; i++){ P1 = Buffer[j];P1 = P1 | temp;k = P1 & 0xf0;if((k & 0xf0)==0) //判断P1口高4位某一行为低电平{ while(1){ if(P1&0xf0){ Delay_1ms(20);if(P1&0xf0){ break;}}}return (i+j*4); //返回键码}temp <<= 1;}}}void Main(void){ uchar i=6,j=0; //读出的键值uchar Key_Value;ulong codeinitial = 0x123456;ulong codecompare = 0;L1602_init();L1602_string(1,1," The code is:");while(1){ Key_Value=10;P1 = 0xf0;if(P1 != 0xf0){ Delay_1ms(20); //按键消抖if(P1 != 0xf0){ Key_Value = Keyscan();}}if(Key_Value < 10){ L1602_char(2,i,Key_Value + 48);codecompare = codecompare | ((ulong)Key_Value <<((5-j)*4));i++;j++;Delay_1ms(300);}if(j==6){ if(codecompare == codeinitial){ Busy();wcmd(0x01);L1602_string(1,1,"right!!!");L1602_string(2,1,"welcome back!!!");while(1);}else{ Busy();wcmd(0x01);L1602_string(1,1,"wrong!!!");L1602_string(2,1,"Input afresh!!!");Delay_1ms(2000);j = 0;i = 6;Busy();wcmd(0x01);L1602_string(1,1," The code is:");codecompare = 0;}}}}矩阵键盘数码管显示#include<reg51.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned charuchar code table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};void Delay_1ms(uint del)//1ms延时{ uint i,j;for(i=0;i<del;i++)for(j=0;j<=148;j++);}uchar Keyscan(void){ uchar i,j,k,temp, Buffer[4] = {0x0e, 0x0d, 0x0b, 0x07}; //让矩阵键盘的每行分别为低电平for(j=0; j<4; j++) //每行进行循环{ temp = 0x10;for(i=0; i<4; i++){ P1 = Buffer[j];P1 = P1 | temp;k = P1 & 0xf0;if((k & 0xf0)==0) //判断P1口高4位某一行为低电平{ while(1){ if(P1&0xf0){ Delay_1ms(20);if(P1&0xf0){ break;}}}return (i+j*4); //返回键码}temp <<= 1;}}}void Main(void){ uchar Key_Value; //读出的键值P2 = 0xfe;while(1){ P1 = 0xf0;if(P1 != 0xf0) //判断有无按键按下{ Delay_1ms(20); //按键消抖if(P1 != 0xf0) //第二次判断有无按键按下{ Key_Value = Keyscan();}}P0 = table[Key_Value]; //数码管赋值}}数码管字符显示#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit KEY = P3^2;uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};void Delay(uint del){ uint i,j;for(i=0; i<del; i++)for(j=0; j<1827; j++);}void Main(void){ uchar i = 0;P2 = 0xfe;while(1){ P0 = table[i%16]; //这里i%16是防止table中的数据在0--15,不会溢出if(KEY == 0){ Delay(2);if(KEY == 0){ i++;while(1){ if(KEY == 1){ Delay(2);if(KEY == 1){ break;}}}}}}液晶1602显示#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intsbit E=P2^7; //1602使能引脚sbit RW=P2^6; //1602读写引脚sbit RS=P2^5; //1602数据/命令选择引脚void delay(){ _nop_();_nop_();}void Delay(uint del){ uint i,j;for(i=0;i<del;i++)for(j=0;j<=148;j++){}}void Busy(void){ bit busy_flag = 1;P0 = 0xff;RS = 0;delay();RW = 1;delay();E = 1;//Delay(1);while(1){ busy_flag = (bit)(P0 & 0x80);if(busy_flag == 0){ break;}}E = 0;}void wcmd(uchar del){ RS = 0;delay();RW = 0;delay();delay();P0 = del;delay();E = 1;delay();E = 0;}void wdata(uchar del){ delay();RS = 1;delay();RW = 0;delay();E = 0;delay();P0 = del;delay();E = 1;delay();E = 0;}void L1602_init(void){ Delay(15);wcmd(0x38);Delay(5);wcmd(0x38);Delay(5);wcmd(0x38);wcmd(0x38);Busy();wcmd(0x08);Busy();wcmd(0x01); Busy();wcmd(0x06); Busy();wcmd(0x0c);}void L1602_char(uchar hang,uchar lie,char sign) { uchar a;if(hang == 1){ a = 0x80;}if(hang == 2){ a = 0xc0;}a = a + lie - 1;Busy();wcmd(a);Busy();wdata(sign);}/********************************************************************* 名称: L1602_string(uchar hang,uchar lie,uchar *p)* 功能: 改变液晶中某位的值,如果要让第一行,第五个字符开始显示"ab cd ef" ,调用该函数如下L1602_string(1,5,"ab cd ef;")* 输入: 行,列,需要输入1602的数据* 输出: 无***********************************************************************/void L1602_string(uchar hang,uchar lie,uchar *p){ uchar a;if(hang == 1){ a = 0x80;}if(hang == 2){ a = 0xc0;}a = a + lie - 1;while(1){ Busy();wcmd(a);Busy();wdata(*p);a++;p++;if((*p == '\0')||(a==0x90)||(a==0xd0)){ break;}}}void Main(){ L1602_init();L1602_string(1,1," Welcome To ");L1602_string(2,1," The MCU World ");L1602_char(1,1,'*');L1602_char(1,16,'*');while(1);}。

相关文档
最新文档