备注:无具体说明则数码管为供阳数码管。
一、设单片机的晶振为12MHz,采用定时器编程实现P1.0和P1.1分别输出1KHz和5KHz方波,在示波器显示方波。
![4d076ea39372e4e4d8006f2aab740413.png](https://img-blog.csdnimg.cn/img_convert/4d076ea39372e4e4d8006f2aab740413.png)
说明:频率和周期的关系式
#include <REGX51.H>
sbit L0 = P1^0;
sbit L1 = P1^1;
void Timer_init(void)
{
EA = 1;
ET0 = 1; ET1 = 1;
TR0 = 1; TR1 = 1;
TMOD = 0x11;
TH0 = (65536-500)/256;
TL0 = (65536-500)%256;
TH1 = (65536-100)/256;
TL1 = (65536-100)%256;
}
void main(void)
{
Timer_init();
while(1);
}
void Timer0(void) interrupt 1
{
L0 = ~L0;
}
void Timer1(void) interrupt 3
{
L1 = ~L1;
}
二、利用单片机的串行通信,实现单片机I/O的扩展,并行数据通过并入串出输入到单片机,并在LED中显示出输入数据的信息。
![b3f024b26f1696ec5708d34c4b2d6d35.png](https://img-blog.csdnimg.cn/img_convert/b3f024b26f1696ec5708d34c4b2d6d35.png)
首先,这是一个串行口的题,在Proteus中排阻q为RESPACK-8,寄存器为74LS165型号的,开关为DIPSW_8,连接电路
#include <REGX51.H>
sbit S1 = P0^7; //给74LS165寄存器一个脉冲
unsigned int i;
void Uart_init(void)
{
SCON = 0x10;
EA = 1;
ES = 1;
S1 = 0;
S1 = 1;
}
void main(void)
{
Uart_init();
while(1);
}
void uart(void) interrupt 4
{
if(RI == 1)
{
RI = 0;
i = SBUF;
P1 = i;
S1 = 0;
S1 = 1;
}
}
三、用数码管动态显示,显示任意一个数
![d0b17408aa106d5d4a4dce32d06aa923.png](https://img-blog.csdnimg.cn/img_convert/d0b17408aa106d5d4a4dce32d06aa923.png)
数码管为共阳数码管,符号为7SEG,P0口加上拉电阻,最小系统中其他电路如晶振电路,复位电路在Proteus仿真中不需要画出来
#include <REGX51.H>
unsigned char code table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(unsigned char xms)
{
unsigned char x,y;
for(x=0;x<xms;x++)
for(y=0;y<120;y++);
}
void main(void)
{
unsigned char i=0;
while(1)
{
P2 = 0x01<<i;
P0 = table[i];
if(++i==8) i = 0;
delay(10);
}
}
四、实现两个单片机之间的串行通信,单片机1的4个按键分别按下时,在单片机2的数码管显示1,2,3,4;单片机2的4个按键分别按下时,在单片机1的数码管显示1,2,3,4。要求:系统时钟频率为11.0592MHz,串行通信的波特率为4800bps。(备注:数码管为供阴)
![cae928a0130b577282dfb8cd0372f441.png](https://img-blog.csdnimg.cn/img_convert/cae928a0130b577282dfb8cd0372f441.png)
#include <REGX51.H>
sbit S1 = P1^0;
sbit S2 = P1^1;
sbit S3 = P1^2;
sbit S4 = P1^3;
unsigned char key,cnt;
unsigned char code table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Uart_init(void)
{
TMOD=0X20;
SCON=0X50;
TH1=0XE6;
TL1=0XE6;
TR1=1;
ES=1;EA=1;
}
void button()
{
if(S1 == 0) key = 1;
else if(S2 == 0) key = 2;
else if(S3 == 0) key = 3;
else if(S4 == 0) key = 4;
}
void main(void)
{
Uart_init();
while(1)
{
button();
SBUF=key;
while(TI == 0); //发送
TI = 0;
P2 = ~table[cnt]; //显示