实验题
1、 由按键S1产生外部中断1,S1按一次L0、L2、L4、L6亮,再按一次L1、L3、L5、L7
亮。
2、 由按键S1产生外部中断1控制T0启停,T0产生100ms定时,使4个数码管循环显示
0中的一段,每段显示时间为1S。
3、 T1工作于方式2,产生250uS精确定时,产生0~9秒计时,并显示在数码管上。
4、 串口工作于方式1,波特率为9600bps,当单片机收到PC机串口发来的任何字符,都立
刻转发给PC。
5、 串口工作于方式1,波特率为9600bps,当单片机收到PC机串口发来的‘0’~‘9’,回
送0~9,当收到‘a’~‘z’时回送‘A’~‘Z’。
6、 串口工作于方式1,波特率为9600bps,当单片机收到PC机串口发来的‘1’,回送字符
串“Start”,并启动T0产生1S的方波控制L0~L7闪烁,当收到‘2’时回送字符串“Stop”,并停止控制L0~L7。
7、 串口工作于方式1,波特率为9600bps,当单片机收到PC机串口发来的‘Start’,回送
字符串“Ok1”,并启动T0产生1S的方波控制L0~L7闪烁,当收到‘Stop’时回送字符串“Ok2”,并停止控制L0~L7。
8、 当S1工作于独立按键方式时(需考虑按键抖动,软件滤波),按第1次,L0~L3以0.5
秒为周期闪烁,串口以9600bps波特率发送字符‘1’一次,按第2次,L4~L7以1秒为周期闪烁,串口以9600bps波特率发送字符‘2’一次,按第3次,L0~L7以2秒为周期闪烁,串口以9600bps波特率发送字符‘3’一次。再按一次回到第一次的模式,如此循环。
本人自己写的示例程序,验证可用,仅供参考学习用: 附部分原理图:
1、
#include
#define uchar unsigned char #define uint unsigned int
sbit P33=P3^3; sbit P35=P3^5;
bit flag=1;
void delay_115(int x) //延时函数 {
int i,j; for(i=x;i>0;i--) for(j=110;j>0;j--); }
void main() { EA=1; //中断总允许位 IT1=0; //设置外部中断1的触发方式为电平触发 EX1=1; //开外部中断 P35=0; P1=0xaa; //初始化P1,方法二中必须写这句 while(1); }
void t1_115() interrupt 2 {
/* delay_115(20); //按键消抖 while(!P33); //当松开按钮时程序才往下执行,否则一直停留在此处
delay_115(20); //按键消抖 */ //方法一: /* if(P1==0x55) P1=0xaa; else P1=0x55; */ //方法二:(若使用方法二则给P1初始化为0xaa或0x55) // P1=~P1; //取反 if(P33==0) { delay_115(15); if(P33==0) { if(flag) { P1=~P1; flag=0; } } else { // key=0; flag=1; } } else { // key=0; flag=1; } } 2、
#include
#define uchar unsigned char #define uint unsigned int
sbit P33=P3^3; sbit P35=P3^5;
uchar count_115=0;
void delay_115(int x) //延时函数
{
int i,j; for(i=x;i>0;i--) for(j=110;j>0;j--); }
void main() { uchar temp_115=0xfe; EA=1; //中断总允许位 TMOD=0x01; //设置T0的工作方式为方式1 IT1=0; //设置外部中断1的触发方式为电平触发 EX1=1; //开外部中断 ET0=1; //开定时器T0 TR0=1; //定时器T0启动 TH0=0x3C; //设置初值 TL0=0xB0; P35=0; P0=0x10; //打开4位数码管 while(1) { if(count_115==20) //该变量每50ms加1,当count_115等于20时,即时间间隔为1s { count_115=0; //将计数变量重置为0,以便开始下1s的定时 P1=temp_115; temp_115=temp_115<<1; //左移一位 temp_115=temp_115+0x01; //最低位补1 } if(temp_115==0xbf) //由于只需要点亮最外圈的六个管子,故此处重置temp_115为0xfe temp_115=0xfe; } }
void t0_115() interrupt 1 {
TH0=0x3C; //装入初值 此处为50ms执行一次中断 TL0=0xB0; count_115++; //每执行一次中断,该变量加1
}
void t1_115() interrupt 2 { delay_115(20); //按键消抖 while(!P33); //当松开按钮时程序才往下执行,否则一直停留在此处 delay_115(20); //按键消抖 TR0=~TR0; //暂停或启动定时器T0 }
3、此题过于简单,略。。。 4、
#include
#define uchar unsigned char #define uint unsigned int
uchar thedata; bit flag;
void main() {
TMOD=0x20; TH1=0xfd; TL1=0xfd; TR1=1; // SM0=0; // SM1=1; // REN=1; SCON=0x50; EA=1; ES=1; while(1) { if(flag==1) { ES=0; flag=0; SBUF=thedata; while(!TI); TI=0; ES=1;