51单片机实验笔记(AT89C51)
一.时钟电路与复位电路
时钟电路:cap(电容)(在时钟电路里一般设置为22pf)和cry(水晶体)(一般设置为12M)
上电复位(电阻res)
按键复位及上电复位:
二.流水灯的实现
共阳极、共阴极(共阴极接反了)
#include<reg51.h>
unsigned char leddat[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<120;j++);
}
}
void led()
{
int i=0;
for(i=0;i<8;i++)
{
P2=~leddat[i];
delay(100);
}
}
void main()
{
while(1)
{
led();
}
}
三.数码管的使用(7seg)
数码管的结构和原理(共阴极和共阳极)
静态显示与动态显示
实验演示电路
静态:
#include<reg51.h>
unsigned char leddat[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<120;j++);
}
}
void seg()
{
int i=0;
for(i=0;i<10;i++)
{
P2=leddat[i];
delay(1000);
}
}
void main()
{
while(1)
{
seg();
}
}
动态:显示hellocathode(阴极)
#include<reg51.h>
unsigned char str[]={0x76,0x79,0x38,0x38,0x3F};
unsigned char wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<120;j++);
}
}
void seg()
{
int i=0;
for(i=0;i<5;i++)
{
P3=~wei[i];
P2=str[i];
delay(5);
}
}
void main()
{
while(1)
{
seg();
}
}
添加74ls138译码器:将8位口减少为三位
#include<reg51.h>
unsigned char str[]={0x76,0x79,0x38,0x38,0x3F};
unsigned char wei[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<120;j++);
}
}
void seg()
{
int i=0;
for(i=0;i<5;i++)
{
P3=wei[i];
P2=str[i];
delay(5);
}
}
void main()
{
while(1)
{
seg();
}
}
四.键盘原理及按键的实现(button)
#include<reg51.h>
sbit key0=P1^0;
unsigned char leddat[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int num=0;
int flag=0;
void delay(unsigned int n)
{
unsigned int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<120;j++);
}
}
void key()
{
if(key0==0&&flag==0)
{
flag=1;
}
if(flag==1&&key0==1)
{
num++;
flag=0;
}
}
void seg()
{
P2=leddat[num];
if(num==10)
num=0;
}
void main()
{
while(1)
{
key();
seg();
}
}
#include"reg51.h"
unsigned char s[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6f};
unsigned char num=0;
void delay(unsigned int n)
{
unsigned char i=0,j=0;
for (i=0;i<n;i++)
{
for (j=0;j<120;j++);
}
}
void key_scan()
{
unsigned char temp0=0,temp1=0,temp;
P1=0xf0;
if(P1!=0xf0)
{
delay(20);
temp0=P1;
P1=0x0f;
if(P1!=0x0f)
{
delay(20);
temp1=P1;
}
}
temp=temp0+temp1;
if(temp==0xee)
{
num=0;
}
if(temp==0xed)
{
num=1;
}
}
void display()
{
P2=s[num] ;
}
void main()
{
while(1)
{
key_scan();
display();
}
}
五.定时器/计数器
工作方式1,16位定时器,定时50ms,每隔50ms进入一次中断,在中断中应该重置初值
#include"reg51.h"
unsigned char s[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int count=0;
void inittimer()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void display()
{
P2=s[count];
if(count==10)
{
count=0;
}
}
void main()
{
inittimer();
while(1)
{
display();
}
}
void timer_isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
定时1s:
#include"reg51.h"
unsigned char s[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int count=0,num=0;
void inittimer()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void display()
{
P2=s[num];
if(num==10)
{
num=0;
}
}
void main()
{
inittimer();
while(1)
{
display();
}
}
void timer_isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
num++;
count=0;
}
}
计数器(只能连接p3.4和p3.5)
#include"reg51.h"
unsigned char s[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//共阴0-9
unsigned int num=0;
void initcounter()
{
TMOD=0x06;//0000 0110 工作方式2,计数
TH0=256-3;//计数3次
TL0=256-3;
ET0=1;//开启定时计数器0中断
EA=1;//开启总中断
TR0=1;//启动定时计数器0
}
void display()
{
P2=s[num];
if(num==10)
{
num=0;
}
}
void main()
{
initcounter();
while(1)
{
display();
}
}
void counter_isr() interrupt 1 //0表示外部中断,1表示定时器中断0,2表示外部中断1,3表示定时器中断1,4表示串口中断
{
num++;
}
|