1、单片机流水灯实验代码:#include#include#define uint unsigned int#define uchar unsigned charvoid delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); void main() uint a=0xfe; while(1) P1=a; delayms(1000); a=_crol_(a,1); 双向流水灯实验代码:#include#include#define uint unsigned int#define uchar unsigned charsbit
2、key=P32;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); void main() uint a=0xfe; uchar i; while(1) for(i=1;i=7;i+) P1=a; delayms(1000); a=_crol_(a,1); for(i=1;i=7;i+) P1=a; delayms(1000); a=_cror_(a,1); 日期显示实验代码:#include#include#define uint unsigned int#define uchar unsigned char
3、uchar code DIS_CODE10=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); void main() uchar i=0; uchar a=0x11; while(1) P1=0x01; P2=DIS_CODE2; delayms(5); P1=0x02; P2=DIS_CODE0; delayms(5); P1=0x04; P2=DIS_CODE1; delayms(5); P1=0x08;
4、P2=DIS_CODE6; delayms(5); P1=0x10; P2=DIS_CODE0; delayms(5); P1=0x20; P2=DIS_CODE9; delayms(5); P1=0x40; P2=DIS_CODE1; delayms(5); P1=0x80; P2=DIS_CODE2; delayms(5); 日期显示代码优化#include#include#define uint unsigned int#define uchar unsigned char 仿真实验uchar code DIS_CODE10=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0
5、x82,0xf8,0x80,0x90;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); void show_time(uint year,uint month,uint day) uint temp; /显示年份 temp=year/1000; P1=0x01; P2=DIS_CODEtemp; delayms(5); temp=year%1000; temp=temp/100; P1=0x02; P2=DIS_CODEtemp; delayms(5); temp=year%100; temp=temp/10
6、; P1=0x04; P2=DIS_CODEtemp; delayms(5); temp=year%10; P1=0x08; P2=DIS_CODEtemp; delayms(5); /显示月份 temp=month/10; P1=0x10; P2=DIS_CODEtemp; delayms(5); temp=month%10; 排阻知识讲解 P1=0x20; P2=DIS_CODEtemp; delayms(5);/显示日 temp=day/10; P1=0x40; P2=DIS_CODEtemp; delayms(5); temp=day%10; 否 P1=0x80; P2=DIS_COD
7、Etemp; delayms(5); 是否void main() while(1) 是 show_time(1993,9,16); 按键控制流水灯实验代码#include 按键消抖流程图#include#define uint unsigned int#define uchar unsigned charsbit key= P32;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); 按键控制流水灯实验原理图void main() uchar i=0; uchar key_val=1; bit bFlash=1;
8、 uchar flag=0xfe; key=1; while(1) for(i=0;i8;i+) if(key_val!=key) key_val=key; if(key=0) bFlash=bFlash; if(bFlash) P1=flag; delayms(100); flag=_crol_(flag,1); functional description:按key时,数码管显示数字1且LED1亮,LED2灭;按key1时,数码管显示数字2且LED2亮,LED1灭。#include#include#define uint unsigned int#define uchar unsigned
9、 charsbit key= P32;sbit key1=P33;sbit LED1=P10;sbit LED2=P11; 仿真电路图uchar code DIS_CODE12=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x0c;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); void main() uchar key_val=0; P0=0xff; P3=0xff; key_val=0xff; P0=DIS_CODE11; while(1
10、) if(key_val!=P3) key_val=P3; if(key=0) delayms(10); if(key=0) LED1=0; LED2=1; P0=DIS_CODE1; if(key1=0) delayms(10); if(key1=0) LED1=1; LED2=0; P0=DIS_CODE2; 如何实现软件“消抖”外部中断控制实验代码:外部中断0的响应是让数码管加1,外部中断1的响应是让数码管原先显示的数字清零。#include#define uchar unsigned char#define uint unsigned intuint g_unDisNum;uchar
11、code DIS_CODE12=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x0c;void delayms(uint xms) uint i,j; for(i=xms;i0;i-) for(j=110;j0;j-); 仿真实验电路图void disp_num(uint num) uchar sep_num=0; uchar ten_num=0; uchar hund_num=0; uchar thou_num=0; sep_num=num%10; ten_num=(num%100)/10; hund_num=(num/100
12、)%10; thou_num=num/1000; P2=0x01; P0=DIS_CODEthou_num; delayms(5); P2=0x02; P0=DIS_CODEhund_num; delayms(5); P2=0x04; P0=DIS_CODEten_num; delayms(5); P2=0x08; P0=DIS_CODEsep_num; delayms(5); void int0_inter() interrupt 0 / interrupt function g_unDisNum+;void int1_inter() interrupt 2 g_unDisNum=0;voi
13、d main() g_unDisNum=0; P0=0xff; P2=0xff; P3=0xff; IT0=1; IT1=1; EX0=1; EX1=1; EA=1; while(1) disp_num(g_unDisNum); 说明:在该程序代码中并没有对按键做抖动处理,但是在实际应用中会在中断函数中做一个按键触发的标志,而在主函数中处理按键的抖动问题。另外中断都是比较紧急的任务,不能在中断处理函数中做太多的工作,特别是不能在中断函数中使用延时函数。定时器中断实验代码:外部中断0开启定时器中断,外部中断1关闭定时器中断。定时时长1s.#include#define uchar unsigne
14、d char#define uint unsigned intsbit LED=P20;uchar g_ucNum;void int0_inter() interrupt 0 /start timer g_ucNum=0; TH0=0x3c; TL0=0xb0; TR0=1;void int1_inter() interrupt 2 /close timer TR0=0; void timer0_inter() interrupt 1 / timer0 g_ucNum+; TH0=0x3c; TL0=0xb0;void main() g_ucNum=0; P2=0xff; /initial IT0=1; IT1=1; /外部中断下降沿触发 EX0=1; EX1=1; /open the interrupt externally TMOD=0x01; TH0=0x3c; TL0=0xb0; ET0=1; EA=1; while(1) if(g_ucNum=20) /1s g_ucNum=0; LED=LED;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1