单片机实用程序.docx

上传人:b****8 文档编号:10124244 上传时间:2023-05-23 格式:DOCX 页数:31 大小:228.33KB
下载 相关 举报
单片机实用程序.docx_第1页
第1页 / 共31页
单片机实用程序.docx_第2页
第2页 / 共31页
单片机实用程序.docx_第3页
第3页 / 共31页
单片机实用程序.docx_第4页
第4页 / 共31页
单片机实用程序.docx_第5页
第5页 / 共31页
单片机实用程序.docx_第6页
第6页 / 共31页
单片机实用程序.docx_第7页
第7页 / 共31页
单片机实用程序.docx_第8页
第8页 / 共31页
单片机实用程序.docx_第9页
第9页 / 共31页
单片机实用程序.docx_第10页
第10页 / 共31页
单片机实用程序.docx_第11页
第11页 / 共31页
单片机实用程序.docx_第12页
第12页 / 共31页
单片机实用程序.docx_第13页
第13页 / 共31页
单片机实用程序.docx_第14页
第14页 / 共31页
单片机实用程序.docx_第15页
第15页 / 共31页
单片机实用程序.docx_第16页
第16页 / 共31页
单片机实用程序.docx_第17页
第17页 / 共31页
单片机实用程序.docx_第18页
第18页 / 共31页
单片机实用程序.docx_第19页
第19页 / 共31页
单片机实用程序.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

单片机实用程序.docx

《单片机实用程序.docx》由会员分享,可在线阅读,更多相关《单片机实用程序.docx(31页珍藏版)》请在冰点文库上搜索。

单片机实用程序.docx

单片机实用程序

数码管程序

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitP20=P2^0;

sbitP21=P2^1;

sbitP22=P2^2;

sbitP23=P2^3;

sbitLIGHT_ON1=P0^1;

sbitLIGHT_ON2=P0^2;

sbitLIGHT_ON3=P0^3;

sbitLIGHT_ON4=P0^4;

uintcount=4425;

uintcounth,countl,counth1,countl1,icount,k;

constuinttab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};

voiddelay()

{

uinti,j;

for(i=0;i<1024;i++);

for(j=0;j<1024;j++);

}

voidint0()interrupt0using1

{

count++;

if(count==10000)

count=9999;

}

voidint1()interrupt2using2

{

if(count!

=0)

{count--;}

}

voidmain(void)

{

icount=0;

TH0=0x3C;

TL0=0xB0;

LIGHT_ON1=1;

LIGHT_ON2=1;

LIGHT_ON3=1;

LIGHT_ON4=1;

TMOD=0x01;

IT0=1;

IT1=1;

EX0=1;

EX1=1;

EA=1;

ET0=1;

TR0=1;

PX1=1;

while

(1)

{

counth=count%100/10;

countl=count%10;

countl1=count/1000;

counth1=(count%1000)/100;

P1=tab[counth];

P21=1;

delay();

P21=0;

P1=tab[countl];

P20=1;

delay();

P20=0;

P1=tab[counth1];

P22=1;

delay();

P22=0;

P1=tab[countl1];

P23=1;

delay();

P23=0;

}

}

voidtimer0isr(void)interrupt1using0

{

TH0=0x3C;

TL0=0xB0;

icount++;

k++;

if(icount==10)

{

icount=0;

LIGHT_ON1=!

LIGHT_ON1;

LIGHT_ON2=!

LIGHT_ON2;

LIGHT_ON3=!

LIGHT_ON3;

LIGHT_ON4=!

LIGHT_ON4;

}

if(k>60)

{

k=61;

icount=0;

}

}

交通灯

#include

#definePORTAXBYTE[0X7CFC]

#defineCONTROLXBYTE[0X7FFF]

voiddelaylong(void);

voiddelayshort(void);

voidmain(void)

{

CONTROL=0X80;//设定工作方式

PORTA=0XFF;//关闭所有LED

while

(1)

{

PORTA=0X36;//南北绿,东西红

delaylong();

PORTA=0X37;//东西红

delayshort();

PORTA=0X35;//南北黄,东西红南北黄灯闪第1次

delayshort();

PORTA=0X37;//东西红

delayshort();

PORTA=0X35;//南北黄,东西红南北黄灯闪第2次

delayshort();

PORTA=0X37;//东西红

delayshort();

PORTA=0X35;//南北黄,东西红南北黄灯闪第3次

delayshort();

PORTA=0X1B;//东西绿,南北红

delaylong();

PORTA=0X3B;//南北红

delayshort();

PORTA=0X2B;//东西黄,南北红南北黄灯闪第1次

delayshort();

PORTA=0X3B;//南北红

delayshort();

PORTA=0X2B;//东西黄,南北红南北黄灯闪第2次

delayshort();

PORTA=0X3B;//南北红

delayshort();

PORTA=0X2B;//东西黄,南北红南北黄灯闪第3次

delayshort();

}

}

//延时函数

voiddelaylong()

{

unsignedj,k,m;

for(j=0;j<30000;j++);

for(k=0;k<30000;k++);

for(m=0;m<30000;m++);

for(j=0;j<30000;j++);

for(k=0;k<30000;k++);

for(m=0;m<30000;m++);

for(j=0;j<30000;j++);

for(k=0;k<30000;k++);

for(m=0;m<30000;m++);

}

voiddelayshort()

{

unsignedj,k;

for(j=0;j<30000;j++);

for(k=0;k<30000;k++);

}

键盘电压

#include

#include

#defineRELOAD_H(65536-50000)/256;//定义定时器0重装值

#defineRELOAD_L(65536-50000)%256;

#defineucharunsignedchar//uchar代表无符号字符

#defineuintunsignedint//unit代表无符号整型数据类型

externvoidBEEP(unsignedinttime);

externvoidTimer0_Init(void);

externvoidDelay(unsignedintn);

externvoidScan_Key(void);

unsignedcharptr,m;

unsignedchardispcode[6];//数码管显示数值的存放数组

sbitBeep=P2^0;

voidUart_Init(void);//输出口初始化函数

main()

{

ucharcDigital=0;

Timer0_Init();

BEEP(200);

while

(1)

{

Scan_Key();//扫描键盘

}

}

voidTimer0_Init(void)//定时器0初始化

{

TMOD=0x01;//定时器0,方式1

TL0=RELOAD_L;//TL0定时器0低8位计数器

TH0=RELOAD_H;//TH0定时器0高8位计数器

TR0=1;//定时器/计数器0启动位.

//TR0=1:

开始计时或计数

ET0=1;//定时器/计数器0中断使能

EA=1;//全局中断使能

}

voidTimer0_ISR(void)interrupt1//定时器0中断服务程序

{

staticunsignedcharj=0;

P0=(j<<4)|dispcode[j];//动态扫描每个数码管

if((++j)>1){j=0;}

TL0=RELOAD_L;//定时器0初值重装

TH0=RELOAD_H;

}

unsignedchartab[4]={0x01,0x02,0x03,0x04};//键盘码数组

voidScan_Key(void)//键盘扫描程序

{

chara1,i;

bitFLAG0=0;

a1=0xfd;//列扫描初值

ptr=0;

for(i=0;i<2;i++)//键盘4个扫描列

{P1=a1;

Delay(10);

m=P1;

switch(m&0xd0)//取行的高4位,侦测哪一行有按键被按下

{

case0x50:

ptr=i*2;//第一行被按否?

如有则是tab[i*4]键

FLAG0=1;//表示有按键

XBYTE[ptr]=(ptr+1)*16;

break;

case0x90:

ptr=i*2+1;//第二行被按否?

如有则是tab[i*4+1]键

FLAG0=1;

XBYTE[ptr]=(ptr+1)*16;

break;

}

if(FLAG0)

{//将键盘编码转换成显示编码

dispcode[0]=tab[ptr]%10;//求个位的数值

BEEP(200);

break;

}

a1=a1>>1|0x80;//高位补1,由于P1.4~P1.7未接+5V,

//通过指令加载高电平

}

}

voidDelay(unsignedintn)//延时

{

unsignedinti,j;

for(i=0;i

{

for(j=0;j<80;j++);

}

}voidBEEP(unsignedinttime)

{

Beep=0;

Delay(time);

Beep=1;

}

电机

#include

#include

#defineunitunsignedint

#defineucharunsignedchar

#defineADXBYTE[0x7FF8]

#defineCLK_WISE0//顺时针方向转动

#defineINVERSE1//逆时针方向转动

#defineHIGH2//高电平

#defineLOW1//低电平

#defineNULL0//低电平

sbitCTRL0=P3^0;//CTRL0为高电平,CTRL1为低电平时顺时针方向转动

sbitCTRL1=P3^1;//CTRL0为低电平,CTRL1为高电平时逆时针方向转动

ucharlevelflag=0;//高低电平标志,取值为HIGH或LOW,分别代表高电平和低电平,或为NULL

bitdirection=CLK_WISE;/***方向标志,取值为CLK_WISE或INVERSE*/

inti;

sbitled1=P2^0;//定义数码管位控制线

sbitled2=P2^1;

sbitled3=P2^2;

sbitled4=P2^3;

sbitad_busy=P3^2;//P3.2用于检测EOC信号,判断是否转换结束

staticucharspeedcount=0;//加速标志,越大转速越快,最大到10,然后回归到0,循环。

//程序中可以依据它来改变占空比

ucharhighcount=0;//记录高电平定时次数

unsignedcharad_data,LED1,LED2,LED3;

ucharcodeled_segment[12]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x00};//段码

/***定时器t0***/

voidtime0(void)interrupt1using1

{

TH0=0xff;

TL0=0xCE;

highcount++;//记录一出次数

//频率为固定的2kHZ左右,只是占空比发生变化

if(highcount<=ad_data)

levelflag=HIGH;//高电平次数

if((highcount>ad_data)&&(highcount<=255))

levelflag=LOW;//低电平次数

if(highcount==255)//最大为10,然后从0开始循环。

highcount=0;

}

/****改变转向标志*****/

voidint1_srv(void)interrupt2using2

{

if(INT1==0)

{

while(!

INT1);

direction=!

direction;

}

}

/*****改变转向*****/

voidrun(void)

{

if(direction==CLK_WISE)//顺时针转

{

CTRL1=1;

if(levelflag==HIGH)//高电平

{

levelflag=NULL;

CTRL0=0;

}

if(levelflag==LOW)//低电平

{

levelflag=NULL;

CTRL0=1;

}

}

if(direction==INVERSE)//逆时针转

{

CTRL0=1;

if(levelflag==HIGH)//高电平

{

levelflag=NULL;

CTRL1=0;

}

if(levelflag==LOW)//低电平

{

levelflag=NULL;

CTRL1=1;

}

}

}

voiddelay(unsignedinti)

{

while(i)

{

i--;

}

}

voiddisplay(void)//显示设置

{

LED1=ad_data%10;//求个位上的数字

LED2=(ad_data/10)%10;//求十位上的数字

LED3=ad_data/100;//求百位上的数字

P2=0xff;

delay

(2);

P1=led_segment[LED1];//向数码管个位上段码

led1=0;

delay(100);

led1=1;

if((LED3==0)&&(LED2==0))//如果十位和百位上的数字都为0,则十位上

LED2=10;//的数码管不显示

P1=led_segment[LED2];

led2=0;

delay(100);

led2=1;

if(LED3==0)//如果百位上的数字为0,则不显示

LED3=10;

P1=led_segment[LED3];

led3=0;

delay(100);

led3=1;

}

voidad0808(void)interrupt0//中断0响应函数

{

EA=0;

EX0=0;

ad_data=AD;//将AD转换数据送给变量ad_data

EA=1;

EX0=1;

}

voidmain(void)

{

EA=1;//中断总允许

EX0=1;//打开外部中断0

PT0=1;

PX1=1;

TMOD=0x01;

ET0=1;//定时器0初始化

TR0=1;

TH0=0xff;

TL0=0xCE;//定时器0初始值

//外部中断1

EX1=1;

IT1=1;

highcount=0;

ad_data=0;

ad_busy=0;

while

(1)

{

speedcount=ad_data/25;

display();

run();

AD=0;

}

}

点阵

#include

unsignedcharcodetab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

unsignedcharcodegraph[3][8]=

{

{0x00,0x04,0x0c,0x14,0x24,0x7E,0x04,0x00},

{0x00,0x31,0x43,0x45,0x49,0x31,0x01,0x00},

{0x00,0x00,0xF1,0x51,0x51,0x4E,0x00,0x00}

};//图形码

unsignedcharcount;//定义变量

unsignedcharcnta;

sbitbutton=P2^0;

voidmain(void)

{unsignedchari,j;

TMOD=0x01;//定时器设置

TH0=(65536-1000)/256;//设置定时计数初值

TL0=(65536-1000)%256;

TR0=1;//打开定时器0中断

ET0=1;

EA=1;

while

(1)

{

if(button==0)//按键处理:

判断是否有键按下

{

for(i=5;i>0;i--)

for(j=248;j>0;j--);

if(button==0)

{

count++;

if(count==3)

{count=0;}

while(button==0);

}

}

}

}

voidt0(void)interrupt1using0//定时器中断设置

{TH0=(65536-1000)/256;

TL0=(65536-1000)%256;

P3=tab[cnta];//扫描列

P1=graph[count][cnta];//扫描行,送图形控制码

cnta++;

if(cnta==8)

{cnta=0;}

}

摩托车

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitp20=P2^0;

sbitp21=P2^1;

sbitp22=P2^2;

sbitp23=P2^3;

uintcount;

ucharcounth,counth1,counth2,counth3;

constuchartab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};

voiddelay()

{

uinti;

for(i=0;i<256;i++);

}

voidint0()interrupt1using1

{

count++;

if(count==10000)

count=9999;

}

voidmain(void)

{

TMOD=0x06;

P1=0xff;

count=0;

TH0=0xFB;

TL0=0xFB;

EA=1;

ET0=1;

TR0=1;

while

(1)

{

counth=count/1000;

counth1=count/10/10%10;

counth2=count/10%10;

counth3=count%10;

P1=tab[counth];

p21=1;

delay();

p21=0;

P1=tab[counth1];

p20=1;

delay();

p20=0;

P1=tab[counth2];

p23=1;

delay();

p23=0;

P1=tab[counth3];

p22=1;

delay();

p22=0;

}

}

前二后二数码管

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitP20=P2^0;

sbitP21=P2^1;

sbitP22=P2^2;

sbitP23=P2^3;

sbitP30=P3^0;

ucharcount,count2;

ucharcounth,countl;

ucharcounthl,countll;

constuchartab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};/*显示0~9*/

voiddelay()

{

uinti,j;

for(i=0;i<256;i++);

}

voidint0()interrupt0using1

{

if(P30==0)

{count++;

if(count==100)

count=99;}

else{count2++;

if(count2==100)

count2=99;}

}

voidint1()interrupt2using2

{

if(P30==0)

{

if(count!

=0)

{count--;}

}

else

{

if(count2!

=0)

{count2--;}

}

}

voidmain(void)

{

IT0=1;

IT1=1;

EX0=1;

EX1=1;

EA=1;

PX1=1;

while

(1)

{

counth=count/10;

countl=count%10;

counthl=count2/10;

cou

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2