单片机程序.doc_第1页
单片机程序.doc_第2页
单片机程序.doc_第3页
单片机程序.doc_第4页
单片机程序.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 发光二极管闪动实验#define X_FREQ11059200/#define X_FREQ12000000#define NULL0#define FALSE 0#define TRUE 1#include sbit LED1 = P10;sbit LED2 = P11;void delay(unsigned char j) unsigned char i; for(;j0;j-) for(i=250;i0;i-) ; main( ) while(1) LED1 = !LED1; LED2 = !LED2; delay(255); delay(255);2、 发光二极管流水闪动实验#define X_FREQ11059200/#define X_FREQ12000000#define NULL0#define FALSE 0#define TRUE 1#include sbit LED1 = P10;sbit LED2 = P11;void delay(unsigned char j) unsigned char i; for(;j0;j-) for(i=250;i0;i-) ; main( ) LED1 = 0;LED2 = 1;while(1) LED1 = !LED1; LED2 = !LED2; delay(255); delay(255);3、 蜂鸣器实验#define X_FREQ11059200/#define X_FREQ12000000#define NULL0#define FALSE 0#define TRUE 1#include sbit LED1 = P10;sbit LED2 = P11;/老板/sbit FMQ = P13;/新板sbit FMQ = P16;void delay(unsigned char j) unsigned char i; for(;j0;j-) for(i=250;i0;i-) ; main( ) LED1 = 0;LED2 = 1;while(1) LED1 = !LED1; LED2 = !LED2; FMQ = !FMQ; delay(255); delay(255);4、 动态数码管显示实验#includereg52.h#include intrins.h/老板数码管控制/sbit led=P12;/新板数码管控制sbit led=P17;const unsigned char ss=0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x1,0x9;void dey(unsigned char j) unsigned char i; for(;j0;j-) for(i=0;i124;i+) ; main() unsigned char i;while(1)for(i=0;i0;j-) for(i=0;i0)l=l-1; elsel=9; led=1; P2=ssl; led=0; dey(0xf0); dey(0xf0); dey(0xf0); #ifdef NEWBORD while( P1&0xf0)=0x20); #else while( P1&0xf0)=0xe0); #endif/End of if(0xe0=(P1&0XF0)break;#ifdef NEWBORDcase 0x10:#elsecase 0xD0:#endifdey(0x1);#ifdef NEWBORDif(0x10=(P1&0x30)/对应P1.5,即K1按下#elseif(0xD0=(P1&0xF0)/对应P1.5,即K1按下#endif if(l=9) l=0; else l=l+1; led=1; P2=ssl; led=0; dey(0xf0); dey(0xf0); dey(0xf0); #ifdef NEWBORD while( P1&0xf0)=0x10); #else while( P1&0xf0)=0xd0); #endif/End of if(0xd0=(P1&0XF0)break;#ifdef NEWBORDcase 0x00:#elsecase 0xb0:#endifdey(0x1);#ifdef NEWBORDif(0x00=(P1&0x30)/对应P1.4和P1.5同时按下即K1K2按下#elseif(0xB0=(P1&0xF0)/对应P1.6,即K3按下#endif fmq=fmq; dey(0xf0); dey(0xf0); dey(0xf0); #ifdef NEWBORD while( P1&0xf0)=0x00); #else while( P1&0xf0)=0xb0); #endif/End of if(0xb0=(P1&0XF0)break;case 0x70: /老板的K4键,对应新板可以采取其它方式来区分,如通过按键时间长短来区分是那种状态,决定蜂鸣器是否响。dey(0x1);if(0x70=(P1&0XF0) led1=led1; led2=led2; dey(0xf0); dey(0xf0); dey(0xf0); while( P1&0xf0)=0x70); /End of if(0x70=(P1&0XF0)break;default : break; /End of switch(i) /End of while(1)/End of main()6、 通讯实验/#define X_FREQ11059200#define X_FREQ12000000#define NULL0#define FALSE 0#define TRUE 1/T_SET_VAL是在设置状态下无操作时间(秒),超过此值则回到正常运行状态#define T_SET_VAL 30#define StatInputCom0#define StatExeCom1#define MaxLenWord10/*20*/ /单词最大长度#define MaxLenWordTable10/*10*/ /词表最大长度#define MaxLenComBuf32/*256*/ /命令缓冲区最大长度#define MaxComNum10 /最大命令种类数#define MaxLenCom10 /命令最大长度#include /#include #include /#include /#include #include /#include typedef unsigned char uchar;typedef unsigned int uint;idata uchar cnt;void shell(void);void putbuf(char);bit time_tick, display = 0;data uchar t_set = T_SET_VAL;#define OLEN 32 /串行发送缓冲区大小unsigned char ostart; /发送缓冲区起始索引unsigned char oend; /发送缓冲区结束索引char idata outbufOLEN; /发送缓冲区存储数组#define ILEN 32 /串行接收缓冲区大小unsigned char istart; /接收缓冲区起始索引unsigned char iend; /接收缓冲区结束索引char idata inbufILEN; /接收缓冲存储数组bit bdata sendfull; /发送缓冲区满标志bit bdata sendactive; /发送有效标志void myprintf(const char*);/*/* Prototypes */*/* - */* Time0中断服务程序,使用寄存器组2 */voidmTime0Interrupt( ) interrupt 1 /using 2static unsigned char t_1000ms = 10;/TH0 = 0xDC;/* TH TL = 0xDC00 在11.0592MHz 周期是 10ms*/static unsigned char t_100ms = 2;TH0 = 0x4C;/* TH TL = 0x4C00 在11.0592MHz 周期是 50ms*/TL0 = 0x00; 不需要,这样可以得到更精确定时if(!(-t_100ms) t_100ms = 2; if(!(-t_1000ms) time_tick = 1; t_1000ms = 10; if(!(-t_set)&display) display = 0;/目的是在显示计数值T_SET_VAL秒后,不再发送计数值 /End of if(!(-t_set) /End of if(!(-t_1000ms)/End of if(!(-t_100ms)/* 串行口中断服务程序,使用寄存器组1 */voidmSelInterrupt( ) interrupt 4 using 1 / using 1 /-接收数据中断- char c; if(RI) /接收中断 c=SBUF; /读字符 RI=0; /清接收中断请求标志/if(istart+ILEN!=iend) 不能用此句,应该用if(unsigned char)(istart+ILEN)!=iend)或 if(istart+ILEN)iend)/if(unsigned char)(istart+ILEN)!=iend)if(istart+ILEN)iend) inbufiend+&(ILEN-1)=c; /缓冲区接收数据 /-发送数据中断- if(TI) TI=0; /清发送中断标志 if(ostart!=oend) SBUF=outbufostart+&(OLEN-1);/向发送缓冲区传送字符 sendfull=0; /设置缓冲区满标志位 else sendactive=0; /设置发送无效 main( ) /SCON = 0xD0;/* SM0=1 SM1=1 SM2=0 REN=1 TB8 = RB8 = 0 TI = 0 RI = 0 */SCON = 0xD2;/* SM0=1 SM1=1 SM2=0 REN=1 TB8 = 1 RB8 = 0 TI = 1 RI = 0 */TB8 = 1;/SCON = 0x52;/* SM0=0 SM1=1 SM2=0 REN=1 TB8 = RB8 = 0 TI = 1 RI = 0 */8bit数据/此是由于采用RCAP2L = 65536-(X_FREQ/32/19200)计算时得到值是0xED带来误差,如果赋值0xEC则没问题/如果采用11.0592M晶振则不存在此误差/T2为波特率发生器时,波特率=振荡频率/32/(65536-RCAP2H,L)。6时钟周期时,32换为16/RCAP2H = 0xFF;/RCAP2L = 0xEE;/19200 Baud RateRCAP2H = (65536-(unsigned int)(X_FREQ/32/19200)+0.5)8;RCAP2L = 65536-(unsigned int)(X_FREQ/32/19200+0.5);/RCAP2H = 0xFF;/RCAP2L = 0xEC;/19200 Baud Rate/TH2 = 0xFF;/TL2 = 0xEE;T2CON = 0x34;/*TF2=0 EXF2=0 RCLK=1 TCLK=1 EXEN2=0 TR2=1 C/T2=0 CP/RL2=0*/TMOD = 0x11;/* GATE=0 C/T-=0 M1 M0=2 GATE C/T- M1 M0 */*采用T1为波特率发生器/如果采用11.0592M晶振则不存在此误差/T1为波特率发生器时,波特率=T1溢出率*2SMOD/32。/DB0FDH,0FAH,0F7H,0EEH,0DCH,0B8H,070H T1波特率发生器时/BAUD115200,57600,38400,19200,9600,4800,2400TMOD = 0x21;/* GATE=0 C/T-=0 M1 M0=2 GATE C/T- M1 M0 TH1 = 0xFD;/19200 11.0592M时,SMOD=1PCON |= 0x80;TR1 = 1;*/TH0 = 0xDC;/* TH TL = 0xDC00 在11.0592MHz 周期是 10ms*/TL0 = 0x00;/TCON = 0x10;/* 01101001 TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 */TR0 = 1;/*EA Null ET2 ES ET1 EX1 ET0 EX0*/REN = 1;ET2 = 0;/TR2 = 1;ES = 1;/允许串口中断/EX1 = 1;EX0 = 0;/AUXR = 0x10;/使用片内XRAMEA = 1; /* 允许中断 */ET0 = 1;myprintf(tt*nr);myprintf(tt* Welcom to use this program *nr);myprintf(tt* Its a sample for using serial port and shell.*nr);myprintf(tt* Author:Lin 2010.10.15 *nr);myprintf(tt*nrnrnr);myprintf(# );while(1) if(time_tick) if(display) putchar(cnt/100+0); putchar(cnt%100)/10+0); putchar(cnt%10+0); myprintf(nr);/, (unsigned)cnt-); /End of if(display) time_tick = 0; /cnt |= 0x40; /putbuf(cnt); cnt+; shell();void DisplayHelpMenu()/显示帮助采单 myprintf(n); myprintf(tS Select active line.n); myprintf(tC connect command.n); myprintf(td display tempterate.C breakn); myprintf(ts Set channel.n); myprintf(tc Clear all setting.n); myprintf(tx Exit Set. Enter Running.n); myprintf(th Display this menu.nn);void clr_setting(void)myprintf(Real? (y/n).nr);if(_getkey () = y) myprintf(clr settingnr);else myprintf(no operate.nr);void shell(void)ucharch;if(iend=istart)return;/如果接收缓冲区无数据则返回ch = _getkey ();if(ch = 13)/n)/)/Enter return keymyprintf(nr);myprintf(# );else if(ch = 3)/C 停止显示温度 if(display) display = 0; myprintf(nr# ); else if(ch = d) t_set = T_SET_VAL; display = 1; else if(ch = h) DisplayHelpMenu();else if(ch = c) clr_setting();/PUTBUF: 写字符到SBUF或发送缓冲区void putbuf(char c) if(!sendfull) /如果缓冲区不满就发送 ES=0; /暂时串行口关闭中断 if(!sendactive) sendactive=1; /直接发送一个字符 SBUF=c; /写到SBUF启动缓冲区 else /ES=0; /暂时串行口关闭中断。原来关中断在此处,有时会出现发送颠倒次序的问题,将关中断改变到外面 outbufoend+&(OLEN-1)=c; /向发送缓冲区传送字符 if(oendostart)&(OLEN-1)=0) sendfull=1; /设置缓冲区满标志 / ES=1; /打开串行口中断 ES=1; /打开串行口中断 /putchar:中断控制putchar函数/替换标准库函数putchar程序/printf函数使用putchar输出一个字符char putchar (char c) if (c=n) /增加新的行 while(sendfull); /等待发送缓冲区空 putbuf(0x0D); /对新行在LF前发送CR while(sendfull); putbuf(c); return(c);/_getkey:中断控制_getkey函数/替换标准库函数_getkey程序/getchar和gets函数使用_getkeychar _getkey(void) char

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论