《基于Proteus的多功能单片机仿真开发板(内含多个程序模块).doc》由会员分享,可在线阅读,更多相关《基于Proteus的多功能单片机仿真开发板(内含多个程序模块).doc(21页珍藏版)》请在三一办公上搜索。
1、1. 流水灯同数码管报警实验程序:#include #include #define uint unsigned int #define uchar unsigned char uchar tt,num,temp; sbit dula=P26; sbit wela=P27; sbit BEEP = P23; /蜂鸣器控制端口P2.3 uchar code table= 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 ; void xu(uint z);void delay(un
2、signed int cnt)/延时 while(-cnt);void beep()/蜂鸣器响一声函数 unsigned char i; for (i=0;i0;x-)for(y=110;y0;y-); void time() interrupt 1 TH0=(65536-50000)/256;TL0=(65536-50000)%256;tt+;if(tt=10)tt=0; num+;if(num=16)num=0;dula=1;P0=tablenum;dula=0; 2. 矩阵键盘检测/4*4键盘检测程序,按下键后相应的代码显示在数码管上#includesbit beep=P23;sbit
3、dula=P26;sbit wela=P27; unsigned char i=100;unsigned char j,k,temp,key;void delay(unsigned char i) for(j=i;j0;j-) for(k=125;k0;k-);unsigned char code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;display(unsigned char num) P0=tablenum; / P0=0xff; dula=1; dula
4、=0; P0=0xc0; wela=1; wela=0;void main() dula=0; wela=0; while(1) P3=0xfe; temp=P3; temp=temp&0xf0; if(temp!=0xf0) delay(10); if(temp!=0xf0) temp=P3; switch(temp) case 0xee: key=0; break; case 0xde: key=1; break; case 0xbe: key=2; break; case 0x7e: key=3; break; while(temp!=0xf0) temp=P3; temp=temp&0
5、xf0; beep=0; beep=1; display(key); / P0=tablekey; /* P0=0xff; dula=1; while(i-); dula=0; P0=0xc0; wela=1; wela=0; dula=0; wela=0; P0=0xff; dula=1; dula=0; P0=0xc5; wela=1; wela=0; */ P1=0xfe; P3=0xfd; temp=P3; temp=temp&0xf0; if(temp!=0xf0) delay(10); if(temp!=0xf0) temp=P3; switch(temp) case 0xed:
6、key=4; break; case 0xdd: key=5; break; case 0xbd: key=6; break; case 0x7d: key=7; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; beep=0; beep=1; display(key);/* P0=tablekey; dula=1; / while(100-); dula=0; P0=0xc0; wela=1; wela=0; */ P1=0xfc; P3=0xfb; temp=P3; temp=temp&0xf0; if(temp!=0xf0) delay(
7、10); if(temp!=0xf0) temp=P3; switch(temp) case 0xeb: key=8; break; case 0xdb: key=9; break; case 0xbb: key=10; break; case 0x7b: key=11; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; beep=0; beep=1; display(key); /* P0=tablekey; dula=1; / while(100-); dula=0; P0=0xc0; wela=1; wela=0; */ P1=0xf8;
8、 P3=0xf7; temp=P3; temp=temp&0xf0; if(temp!=0xf0) delay(10); if(temp!=0xf0) temp=P3; switch(temp) case 0xe7: key=12; break; case 0xd7: key=13; break; case 0xb7: key=14; break; case 0x77: key=15; break; while(temp!=0xf0) temp=P3; temp=temp&0xf0; beep=0; beep=1; display(key); /* P0=tablekey; dula=1; /
9、 while(100-); dula=0; P0=0xc0; wela=1; wela=0; */ P1=0xf0; 3. ADC模数试验/拧动电位器,会在数码管的前三位显示0-255之间的数值。这就是把模拟电压转换成数字信号。/有些拧动电位器后数码管数可能不会动态变化,请用户按下实验板上的S22复位按键可更新数据/这种显象产生的原因是和AD芯片自身的一些特性有关。#include#include #define uint unsigned int#define uchar unsigned charsbit adrd=P37; /IO口定义sbit adwr=P36;sbit diola=P
10、25;sbit dula=P26;sbit wela=P27;unsigned char j,k,adval;void delay(unsigned char i)/延时程序 for(j=i;j0;j-) for(k=125;k0;k-);uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,/数码管编码 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void display(uchar bai_c,uchar sh_c,uchar g_c)/显示程序 P0=tablebai_c;/显示百位 dula
11、=1; dula=0; P0=0xfe; wela=1; wela=0; delay(5); dula=0; P0=tablesh_c;/显示十位 dula=1; dula=0; wela=0; P0=0xfd; wela=1; wela=0; delay(5); P0=tableg_c;/显示个位 dula=1; dula=0; P0=0xfb; wela=1; wela=0; delay(5);void main()/主程序 uchar a,A1,A2,A2t,A3; while(1) wela=1; P0=0; /选通ADCS adwr=0;/AD写入(随便写个什么都行,主要是为了启动A
12、D转换) _nop_(); adwr=1; P0=0xff; /关闭ADCS delay(10); wela=0; /关闭有AD片选信号锁存器的锁存端以防止在操作数码管时使AD的片选发生变化 for(a=20;a0;a-) /需要注意的是ADC0804在写和读之间的时间间隔要足够长否则无法读出数据 /这里把显示部分放这里的原因也是为了增加写读之间的时间间隔 display(A1,A2,A3); /送去显示各位。 wela=1; /重新打开有AD片选信号锁存器的锁存端 P1=0xff; /读取P1口之前先给其写全1 P0=0; /选通ADCS adrd=0; /AD读使能 adval=P1;/A
13、D数据读取赋给P1口 adrd=1; P0=0xff; /关闭ADCS adwr=0; P1=adval; /同时把AD的值送八个发光二极显示 A1=adval/100;/分出百,十,和个位 A2t=adval%100; A2=A2t/10; A3=A2t%10; ;4. 步进电机数码管显示控制试验/* 1、本程序用于测试4相步进电机常规驱动 2、HJ-3G红色的线接到J6右边第1脚VCC上 3、速度不可以调节的过快,不然就没有力矩转动了 4、HJ-1G红色的线接到J6第一脚上,VCC,共5条线,请不要接反了。 5、S1为换向健,S2为增大健,S3为减少健,可以能过数码管显出大小。*/#inc
14、lude #define uchar unsigned char sbit P30=P30;sbit dula=P26;sbit wela=P27;sbit jia_key=P36;sbit jian_key=P37;sbit zf_key=P35;bit flag=0;uchar num=0,show_num=2,maichong=4,table_begin=0;uchar code table1=0x01,0x02,0x04,0x08,0x08,0x04,0x02,0x01;/uchar code table1=0x09,0x03,0x06,0x0c,0x08,0x04,0x02,0x01
15、;/uchar code table1=0x01,0x04,0x02,0x08,0x08,0x02,0x04,0x01;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void delay(uchar i) uchar j,k; for(j=i;j0;j-) for(k=125;k0;k-);void display() dula=0; P0=tableshow_num; dula=1; dula=0; wela=0; P0=0xfe; wela=
16、1; wela=0; delay(5); P0=table0; dula=1; dula=0; P0=0xfd; wela=1; wela=0; delay(5); void key ()P30=0; if(jia_key=0) delay(5); if(jia_key=0) num+; if(num=4) num=3; while(jia_key=0); if(jian_key=0) delay(5); if(jian_key=0) if(num!=0) num-; else num=0; while(jian_key=0); if(zf_key=0) delay(5); if(zf_key
17、=0) flag=flag; while(zf_key=0); void dispose() switch(num) case 0: show_num=2; maichong=5; break;case 1: show_num=4; maichong=4;break;case 2: show_num=6; maichong=3;break;case 3: show_num=8; maichong=2;break; if(flag=0) table_begin=0; else table_begin=4;void qudong() uchar i,j; for(j=0+table_begin;j
18、4+table_begin;j+) P1=table1j; for(i=0;imaichong;i+) display(); void main() while(1) key(); dispose(); qudong(); 5. 红外接收LCD1602显示试验/LCD1602对比度已调试好,可以直接使用。/操作方法如下:/1.正确安装液晶1602,请查看使用手册目录下的液晶安装方法。/2.下载本程序/该程序使用LCD1602显示/注意:数码管锁存源码还没有写好,现在接收信号时数码管会有发光。还有继电器会响,这是正常的#include#include#include sbit dula=P26;
19、sbit wela=P27;sbit JD=P06;sbit FM=P23;/#define uchar unsigned char/#define uint unsigned int/#define TURE 1#define FALSE 0/sbit IR=P33;/红外接口标志sbit RS = P35;/Pin4sbit RW = P36; /Pin5sbit E = P34;/Pin6sbit JDC = P06;/#define Data P0 /数据端口void ioin() /IO口继电器、数码管锁存函数 wela=1; P0 = 1; JD = 0; wela=0; dula
20、=1; P0=0; dula=0; unsigned int hour,minute,second,count;char code Tab16=0123456789ABCDEF;char data TimeNum= ;char data Test1= ;/unsigned char irtime;/红外用全局变量bit irpro_ok,irok;unsigned char IRcord4;unsigned char irdata33;void ShowString (unsigned char line,char *ptr);/void Delay(unsigned char mS);voi
21、d Ir_work(void);void Ircordpro(void);/void Delay(unsigned char mS)/delay mS unsigned char us,usn; while(mS!=0) usn = 4; while(usn!=0) us=0xf0; while (us!=0) us-; usn-; mS-; /* / / void tim0_isr (void) interrupt 1 using 1/定时器0中断服务函数 irtime+;/void ex0_isr (void) interrupt 0 using 0/外部中断0服务函数 static un
22、signed char i; static bit startflag; if(startflag) if(irtime=33)/引导码 TC9012的头码 i=0; irdatai=irtime; irtime=0; i+; if(i=33) irok=1; i=0; elseirtime=0;startflag=1;/void TIM0init(void)/定时器0初始化 TMOD=0x02;/定时器0工作方式2,TH0是重装值,TL0是初值 TH0=0x00;/reload value TL0=0x00;/initial value ET0=1;/开中断 TR0=1;/void EX0i
23、nit(void) IT0 = 1; / Configure interrupt 0 for falling edge on /INT0 (P3.2) EX0 = 1; / Enable EX0 Interrupt EA = 1; /*/*/ void Ir_work(void)/红外键值散转程序 TimeNum5 = TabIRcord0/16; TimeNum6 = TabIRcord0%16; TimeNum8 = TabIRcord1/16; TimeNum9 = TabIRcord1%16; TimeNum11 = TabIRcord2/16; TimeNum12 = TabIRco
24、rd2%16; TimeNum14 = TabIRcord3/16; TimeNum15 = TabIRcord3%16; ShowString(1,TimeNum); irpro_ok=0; /*/void Ircordpro(void)/红外码值处理函数 unsigned char i, j, k; unsigned char cord,value; k=1; for(i=0;i4;i+)/处理4个字节 for(j=1;j7)/大于某值为1 value=value|0x80; else value=value; if(j1; k+; IRcordi=value; value=0; irpr
25、o_ok=1;/处理完毕标志位置1 /*/void DelayUs(unsigned char us)/delay us unsigned char uscnt; uscnt=us1;/* Crystal frequency in 12MHz*/ while(-uscnt);/*/void DelayMs(unsigned char ms)/delay Ms while(-ms) DelayUs(250); DelayUs(250); DelayUs(250); DelayUs(250); void WriteCommand(unsigned char c) DelayMs(5);/short
26、 delay before operation E=0; RS=0; RW=0; _nop_(); E=1; Data=c; E=0;/*/void WriteData(unsigned char c) DelayMs(5); /short delay before operation E=0; RS=1; RW=0; _nop_(); E=1; Data=c; E=0; RS=0;/*/void ShowChar(unsigned char pos,unsigned char c) unsigned char p; if (pos=0x10) p=pos+0xb0; /是第二行则命令代码高4位为0xc else p=pos+0x80; /是第二行则命令代码高4位为0x8 WriteCommand (p);/write command WriteData (c); /write data/*/void ShowString (unsigned char line,char *ptr) unsigned char l,i; l=line4; for (i=