单片机
单片机
小游单片机
1.单片机内部
- Flash 程序储存空间
- SFR 特殊功能寄存器
- RAM 内存
2.单片机的最小系统
- 电源电路:单片机常见的电源电压为5/3.3伏,电脑的USB接口的电压为5V
- 复位电路:使单片机每次开机时,都从一个固定的状态开始运行。当程序运行出错时,按复位按键重新开始。“开门狗”程序可自动复位。
- 晶振电路:控制单片机运行的节奏(?)
3.LED小灯
通常红色贴片LED:靠电流驱动,电压1.8V~2.2V,电流1到20mA,在1到5mA亮度有所变化,5mA以上亮度基本无变化,放置限流电阻为防止小灯烧坏
通过控制p0.0点的电压,来控制小灯亮灭
4.点亮LED灯实现
新建工程:projiect——第一个选项
新建文件夹保存
点击
右键后选择Add Files to Group…
输入代码
sbit LED = p0^0;
sbit ADDR0 = p1^0;
sbit ADDR0 = p1^1;
sbit ADDR0 = p1^2;
sbit ADDR0 = p1^3;
sbit ADDR0 = p1^4;
void main()
{
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
LED = 0;
while(1);
}单片机无法识别c语句,将语句转换
6.点击上方的方块,运行成功后会创建hex文件
7.连接板子,打开设备管理器查看板子连接的端口名称(一般是COM4)
8.打开STC_ISP_V483
9.按下开关,开始下载,LED小灯点亮,项目完成
5.硬件基础知识
电磁干扰
- 冬天的时候,空气比较干燥的城市,朋友们经常对电脑,铁柜等等放电,这就是“静电放电(ESD)”干扰。
- 使用电钻的时候听收音机,看电视有杂音,这就是“快速瞬间脉冲群(EFT)”的效果。
- 电脑性能不好,热插拔优盘等外围设备会出现蓝屏重启电脑等现象,这就是热插拔“浪涌(Surge)的效果。
去耦电容的使用
- 低频滤波电容,平常应用最多的是钽电容,电解电容,陶瓷电容,起到去除电源低频纹波,稳定电源的作用。
- 高频滤波电容,电源附近,通常用104电容来进行去除高频干扰。10*104pf
三极管:三极管是模拟电路和数字电路中经常会用到的一个器件。
三极管的型号记忆方式:箭头朝内PNP,导通电压顺箭头过,电压导通(0.7伏),电流控制 b:基极 e:射极 c:集电极
当p0.0是低电平时,三极管导通,小灯亮;当p0.0是高电平时,三极管截止,小灯灭。通过点平控制三极管状态,达到小灯亮灭的效果
电流缓冲器74HC245
38译码器74H138
通过三个数字控制八种状态
当使LED6亮起使
6.流水灯的实现
51单片机延时常用方法
小灯延时小于20ms时,肉眼不易察觉,
把软件的晶振改为与单片机相同的晶振
通过下边过程查看时间是否符合大于20ms
通过view-watch windows-watch1 可获取i的值
P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0小灯用十六进制表示为0xFE, 0xFD,0xFB,0xF7,0xEF,0xDF, 0xBF,0x7F
代码一
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void main()
{
unsigned int i =0;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
while(1){
p0 = 0xFE;
for(i=0; i<30000; i++);
p0 = 0xFD;
for(i=0; i<30000; i++);
p0 = 0xFB;
for(i=0; i<30000; i++);
p0 = 0xF7;
for(i=0; i<30000; i++);
p0 = 0xDF;
for(i=0; i<30000; i++);
p0 = 0xBF;
for(i=0; i<30000; i++);
p0 = 0x7F;
for(i=0; i<30000; i++);
p0 = 0xFE;
for(i=0; i<30000; i++);
}
}代码二
while(1){
p0 = ~(0x01<< cnt);
for(i=0; i<30000; i++);
cnt++;
if(cnt >=8)
{
cnt = 0;
}
}
7.定时器与数码管
逻辑电路与逻辑运算
定时器
- 时钟周期:单片机时序中的最小单位,具体计算的方法就是时钟源分之一。(也就是晶振分之一,咱们的晶振时11.0592)
- 机器周期:我们的单片机完成一个操作的最短时间。
- 定时器:打开定时器后,定时器”存储寄存器”的值经过一个机器周期自动加1,也就是说,机器周期是定时器的计数周期。
计算方式(lihuibear写的就是好
1. 频率:频率是单位时间内完成周期性变化的次数
2.一个时钟周期 = 12 个机器周期假设我们单片机的晶振是11.0592MHz,那么一秒钟可产生的机器周期数 11.0592MHz / 12 = 921600 个,
如果我们要定时50 ms,即0.05 s,所以需要921600 * 0.05 = 46080个机器周期。而如果我们的定时器工作
在16位定时器/计数器模式,那么最大值为 2^16=65536,所以初值设置为 65536-46080 = 19456。
十六进制写法为:
TH0 = 0X4c;
TL0 = 0x00;
十进制写法为:
TH0 = (65536-46080)/256;
TL0 = (65536-46080)%256;
(16位二进制数对256求模得到的是高八位,同理求余得到的是低八位)TCON——定时器控制寄存器
- TF0:定时器0溢出标记 ,记录溢出次数
- TR0:定时器0运行控制位,TR0 = 0停止,TR0 = 1 开始
TMOD——定时器模式寄存器
使用定时器的方法
- 设置特殊功能寄存器TMOD,配置好工作模式。
- 设置计数寄存器TH0和TL0的初值。
- 设置TCON,通过TR0置1来让定时器开始计数。
- 判断TCON寄存器的TF0位,监测定时器溢出情况。
代码
sbit LED = P0^0;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
void main()
{
unsigned char cnt = 0;
unsigned char flag = 0;
//unsigned int i=0;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
TMOD = 0x01;
TH0 = 0xB8; // x * 12 /11059200 = 0.2 x = 18432 TH0 = 65535 + 1 - x
TL0 = 0x00;
TR0 = 1;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xB8;
TL0 = 0x00;
cnt++;
if(cnt>=50)
{
cnt = 0;
P0 = ~(0x01<<flag);
flag++;
if(flag>=8){
flag = 0;
}
}
}
}
}数码管原理和结构图
数码管真值表
代码(数码管显示0-F)
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
void main()
{
unsigned char cnt = 0;
unsigned char sec = 0;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x00;
TR0 = 1;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xB8;
TL0 = 0x00;
cnt++;
if(cnt >= 50)
{
cnt = 0;
P0 = LedChar[sec];
if(sec >= 15)
{
sec = 0;
}
else
{
sec++;
}
}
}
}
}
8.中断与数码管动态显示
动态显示的基本原理
- 动态显示: 多个数码管显示数字的时候,我们实际上是轮流点亮数码管(一个时刻只有一个数码管是亮的),利用人眼的视觉暂留现象(也叫余晖效应)。
- 刷新周期<10ms(100Hz无闪烁)
- 刷新速度没必要过快
代码(多个数码管显示数字并计时)
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6]={
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
void main()
{
unsigned int cnt = 0;
unsigned long sec = 0;
unsigned char i = 0;
ENLED = 0;
ADDR3 = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
while(1)
{
if(TF0 ==1)
{
TF0 = 0;
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
sec++;
LedBuff[0] = LedChar[sec%10];
LedBuff[1] = LedChar[sec/10%10];
LedBuff[2] = LedChar[sec/100%10];
LedBuff[3] = LedChar[sec/1000%10];
LedBuff[4] = LedChar[sec/10000%10];
LedBuff[5] = LedChar[sec/100000%10];
}
if(i == 0)
{ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0];}
else if(i == 1)
{ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1];}
else if(i == 2)
{ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2];}
else if(i == 3)
{ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3];}
else if(i == 4)
{ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4];}
else if(i == 5)
{ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5];}
}
}
}中断
IE—中断使能寄存器的位分配(地址0xA8、可位寻址)
中断查询序列
进入中断的条件
1.打开中断(EA,ET0)
2.符合中断条件
3.中断入口正确 interrupt x
计算方法:
x * 8 + 3 = 中断向量地址中断优先级说明
1.当设置为默认中断固有优先级时:
当几个中断同时发生时,则先处理中断优先级高的中断程序,在处理任意中断期间发生中断,都不会响应
2.当配置了中断优先级,即抢占优先级
同时发生中断,优先级高的先响应,在处理任意中断时,发生同级别或低级的中断,则不响应,发生优先级更高的中断时,则先处理高优先级中断,处理完毕,再回来处理当前中断。使用中断优化后的代码
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6]={
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned int cnt = 0;
void main()
{
unsigned long sec = 0;
ENLED = 0;
ADDR3 = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
EA = 1;
ET0 = 1;
while(1)
{
if(cnt >= 1000)
{
cnt = 0;
sec++;
LedBuff[0] = LedChar[sec%10];
LedBuff[1] = LedChar[sec/10%10];
LedBuff[2] = LedChar[sec/100%10];
LedBuff[3] = LedChar[sec/1000%10];
LedBuff[4] = LedChar[sec/10000%10];
LedBuff[5] = LedChar[sec/100000%10];
}
}
}
unsigned char i = 0;
void InterruptTimer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
P0 = 0xFF;
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=LedBuff[0];break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=LedBuff[1];break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=LedBuff[2];break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=LedBuff[3];break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=LedBuff[4];break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i=0; P0=LedBuff[5];break;
default:break;
}
}
9.点阵led
点阵led原理
取模软件的应用
- 新建图像
- 点击模拟动画可放大格点
- p0如果是控制一列,就选纵向取模;如果是控制一行,就选横向取模。
- 字节倒叙:左大右小为正序,反之为倒叙
- 点击取模方式,生成点阵位置
- 可选择保存图像方便下次使用
代码(led灯显示图像(静态))
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code image[] = {
0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7
};
void main()
{
EA = 1;
ENLED = 0;
ADDR3 = 0;
TMOD =0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1);
}
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0;
TH0 = 0xfc;
TL0 = 0x67;
P0 = 0xFF;
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index][0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index][1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index][2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index][3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index][4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index][5]; break;
case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index][6]; break;
case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index][7]; break;
default: break;
}
}代码(led显示动画(纵向))
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code image[] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3,0xFF,
0x99,0x00,0x00,0x00,0x81,0xC3,0xE7,0xFF,
0x99,0x99,0x99,0x99,0x99,0x81,0x81,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
void main()
{
EA = 1;
ENLED = 0;
ADDR3 = 0;
TMOD =0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1);
}
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0;
static unsigned char tmr = 0;
static unsigned char index = 0;
TH0 = 0xfc;
TL0 = 0x67;
P0 = 0xFF;
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index+0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index+1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index+2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index+3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index+4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index+5]; break;
case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index+6]; break;
case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index+7]; break;
default: break;
}
tmr++;
if(tmr >= 250)
{
tmr = 0;
index++;
if(index>= 32)
{
index = 0;
}
}
}代码(led显示动画(纵向))
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code image[30][8] = {
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, //动画帧1
{0xFF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F},
{0xFF,0x3F,0x7F,0x7F,0x7F,0x7F,0x7F,0x3F},
{0xFF,0x1F,0x3F,0x3F,0x3F,0x3F,0x3F,0x1F},
{0xFF,0x0F,0x9F,0x9F,0x9F,0x9F,0x9F,0x0F},
{0xFF,0x87,0xCF,0xCF,0xCF,0xCF,0xCF,0x87},
{0xFF,0xC3,0xE7,0xE7,0xE7,0xE7,0xE7,0xC3},
{0xFF,0xE1,0x73,0x73,0x73,0xF3,0xF3,0xE1},
{0xFF,0x70,0x39,0x39,0x39,0x79,0xF9,0xF0},
{0xFF,0x38,0x1C,0x1C,0x1C,0x3C,0x7C,0xF8},
{0xFF,0x9C,0x0E,0x0E,0x0E,0x1E,0x3E,0x7C},
{0xFF,0xCE,0x07,0x07,0x07,0x0F,0x1F,0x3E},
{0xFF,0x67,0x03,0x03,0x03,0x07,0x0F,0x9F},
{0xFF,0x33,0x01,0x01,0x01,0x03,0x87,0xCF},
{0xFF,0x99,0x00,0x00,0x00,0x81,0xC3,0xE7},
{0xFF,0xCC,0x80,0x80,0x80,0xC0,0xE1,0xF3},
{0xFF,0xE6,0xC0,0xC0,0xC0,0xE0,0xF0,0xF9},
{0xFF,0x73,0x60,0x60,0x60,0x70,0x78,0xFC},
{0xFF,0x39,0x30,0x30,0x30,0x38,0x3C,0x7E},
{0xFF,0x9C,0x98,0x98,0x98,0x9C,0x1E,0x3F},
{0xFF,0xCE,0xCC,0xCC,0xCC,0xCE,0x0F,0x1F},
{0xFF,0x67,0x66,0x66,0x66,0x67,0x07,0x0F},
{0xFF,0x33,0x33,0x33,0x33,0x33,0x03,0x87},
{0xFF,0x99,0x99,0x99,0x99,0x99,0x81,0xC3},
{0xFF,0xCC,0xCC,0xCC,0xCC,0xCC,0xC0,0xE1},
{0xFF,0xE6,0xE6,0xE6,0xE6,0xE6,0xE0,0xF0},
{0xFF,0xF3,0xF3,0xF3,0xF3,0xF3,0xF0,0xF8},
{0xFF,0xF9,0xF9,0xF9,0xF9,0xF9,0xF8,0xFC},
{0xFF,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE},
{0xFF,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF} //动画帧30
};
void main()
{
EA = 1;
ENLED = 0;
ADDR3 = 0;
TMOD =0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1);
}
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0;
static unsigned char tmr = 0;
static unsigned char index = 0;
TH0 = 0xfc;
TL0 = 0x67;
P0 = 0xFF;
switch(i)
{
case 0: ADDR2=0; ADDR1=0; ADDR0=0; i++; P0=image[index][0]; break;
case 1: ADDR2=0; ADDR1=0; ADDR0=1; i++; P0=image[index][1]; break;
case 2: ADDR2=0; ADDR1=1; ADDR0=0; i++; P0=image[index][2]; break;
case 3: ADDR2=0; ADDR1=1; ADDR0=1; i++; P0=image[index][3]; break;
case 4: ADDR2=1; ADDR1=0; ADDR0=0; i++; P0=image[index][4]; break;
case 5: ADDR2=1; ADDR1=0; ADDR0=1; i++; P0=image[index][5]; break;
case 6: ADDR2=1; ADDR1=1; ADDR0=0; i++; P0=image[index][6]; break;
case 7: ADDR2=1; ADDR1=1; ADDR0=1; i=0; P0=image[index][7]; break;
default: break;
}
tmr++;
if(tmr >= 250)
{
tmr = 0;
index++;
if(index>= 30)
{
index = 0;
}
}
}
10.按键
单片机最小系统解析
电源: 常用单片机的电源系统有5V系统和3.3V系统这两种。
常用的数字电路系统的电源类别有24V、12V、5V、3.3V、2.5V、1.8V。工作电压:5V-3.4V(5V单片机)
3.8V-2.0V(3V单片机)晶振(我们多用无源晶振)
复位电路
上电复位
手动复位
硬件复位
复位时间计算(lihuibear说的就是好)
1.247000.0000001=564us
上电复位:
复位时间t = 1.2RC 故:t = 1.2 * 4.7K *0.1uF = 564us, 大于两个机器周期约2us,故能起到复位作用。
手动复位:人手按下按键的时间一般100ms以上,快的也有几十ms,故满足复位条件。18欧的电阻作用是放电时,K、R、C形成闭合回路,消除干扰。
独立按键原理
当开关打开时,KeyIn1的电压是5V;当开关闭合时,KeyIn1的电压是0V。可以通过该点的电压来判断开关状态。
当要判断开关状态时,自己必须要输出一个高电平:当内部输出是高电平时,取反为低电平,开关闭合时,单片机IO口检测到的是低电平。开关松开时,检测到高电平。
矩阵按键
当KeyOut1输出低电平,345输出高电平时,矩阵按键就变为了独立按键
代码(独立按键控制led灯亮灭)按键按下时小灯亮,松开小灯灭
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit LED9 = P0^7;
sbit LED8 = P0^6;
sbit LED7 = P0^5;
sbit LED6 = P0^4;
sbit KEY1 = P2^4;
sbit KEY2 = P2^5;
sbit KEY3 = P2^6;
sbit KEY4 = P2^7;
void main(){
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
P2 = 0xF7;
while(1){
LED9 = KEY1;
LED8 = KEY2;
LED7 = KEY3;
LED6 = KEY4;
}
}代码(按键控制led灯亮灭)按键按下时改变多个小灯亮灭状态
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit LED9 = P0^7;
sbit LED8 = P0^6;
sbit LED7 = P0^5;
sbit LED6 = P0^4;
sbit KEY4 = P2^7;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
void main(){
bit backup = 1;
unsigned char cnt = 0;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
P2 = 0xF7;
P0 = LedChar[cnt];
while(1){
if(KEY4 != backup)
{
if(backup == 0)
{
cnt++;
if(cnt >= 10)
{
cnt = 0;
}
P0 = LedChar[cnt];
}
backup = KEY4;
}
}
}代码(按键控制数码管数字显示)
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit LED9 = P0^7;
sbit LED8 = P0^6;
sbit LED7 = P0^5;
sbit LED6 = P0^4;
sbit KEY4 = P2^7;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
void main(){
bit backup = 1;
unsigned char cnt = 0;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
P2 = 0xF7;
P0 = LedChar[cnt];
while(1){
if(KEY4 != backup)
{
if(backup == 0)
{
cnt++;
if(cnt >= 10)
{
cnt = 0;
}
P0 = LedChar[cnt];
}
backup = KEY4;
}
}
}按键消抖
代码(通过代码延时解决抖动问题,数码管数字显示 )
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY4 = P2^7;
// 数码管显示的字符编码
unsigned char code LedChar[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
// 延时函数
void delay() {
unsigned int i = 1000;
while (i--);
}
void main() {
bit backup = 1; // 用于记录按钮按下状态的备份
bit keybuf = 1; // 用于记录按钮当前状态
unsigned char cnt = 0; // 计数器
ENLED = 0;
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
P2 = 0xF7;
P0 = LedChar[cnt]; // 初始化数码管显示
while (1) {
keybuf = KEY4; // 读取按钮当前状态
if (keybuf != backup) { // 如果按钮状态发生变化
delay(); // 延时防抖
if (keybuf == KEY4) { // 如果按钮仍然处于按下状态
if (backup == 0) { // 如果之前是按下的状态
cnt++; // 计数器递增
if (cnt >= 10) {
cnt = 0;
}
P0 = LedChar[cnt]; // 更新数码管显示
}
backup = KEY4; // 更新按钮状态备份
}
}
}
}代码(不用delay消抖)
以八个数字为一组,当检测到数组都是1时,为弹起的高电平状态,都是0时,为按下状态,01相间时,为抖动状态
通过中断消抖
void InterruptTimer0() interrupt 1
{
static unsigned char keybuf = 0xFF;
TH0 = 0xF8;
TL0 = 0xCD;
keybuf = (keybuf <<1) |KEY4;
if(keybuf == 0x00)
{
KeySta = 0;
}
else if(keybuf == 0xFF)
{
KeySta = 1;
}
else
{
}代码(矩阵按键用中断消抖)
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char code LedChar[]={
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char KeySta[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
void main()
{
unsigned char i, j;
// 初始值全为1(未按下)
unsigned char backup[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
// 允许全局中断
EA = 1;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
// 设置定时器0工作在方式1
TMOD = 0x01;
// 设置定时器0的初始值
TH0 = 0xFC;
TL0 = 0x67;
// 允许定时器0中断
ET0 = 1;
// 启动定时器0
TR0 = 1;
// 初始化LED显示为LedChar数组中的第一个字符
P0 = LedChar[0];
// 主循环
while(1)
{
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
// 检测状态是否发生变化
if(backup[i][j] != KeySta[i][j])
{
// 如果按键按下
if(backup[i][j] == 0)
{
// 在LED数码管上显示相应字符
P0 = LedChar[i*4+j];
}
// 更新备份数组
backup[i][j] = KeySta[i][j];
}
}
}
}
}
// 定时器0中断服务程序
void InterruptTimer0() interrupt 1
{
// 静态变量,记录当前扫描的行号
static unsigned char keyout = 0;
unsigned char i = 0;
// 静态二维数组,保存按键缓冲区,初始值为0xFF
static unsigned char keybuf[4][4] = {
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}
};
// 重置定时器0的初始值
TH0 = 0xFC;
TL0 = 0x67;
// 将键盘输入值放入按键缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] <<1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] <<1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] <<1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] <<1) | KEY_IN_4;
// 检测按键状态
for(i=0; i<4; i++)
{
if((keybuf[keyout][i] & 0x0F) == 0x00)
{
// 按键按下
KeySta[keyout][i] = 0;
}
else if((keybuf[keyout][i] & 0x0F) == 0x0F)
{
// 按键释放
KeySta[keyout][i] = 1;
}
}
// 切换到下一行
keyout++;
keyout = keyout & 0x03;
switch(keyout)
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
11.步进机与蜂鸣器
单片机IO口
上下拉电阻
上拉电阻就是将不确定的信号通过一个电阻拉到高电平,同时此电阻起到一个限流的作用,下拉就是下拉到低电平。
1、OC门要输出高电平,外部必须加上拉电阻。
2、加大普通IO口的驱动能力。
3、起到限流的作用。
4、抵抗电磁干扰。
上下拉电阻的选取原则
1、从降低功耗方面考虑应该足够大,因为电阻越大,电流越小。
2、从确保足够的引脚驱动能力考虑应该足够小,电阻越小,电流才能越大。
3、开漏输出时,过大的上拉电阻会导致信号上升沿变缓。
28BYJ-48步进电机
反应式步进电机:结构简单成本低,发热大可靠性低。
永磁式步进电机:动态性能好、力矩较大,误差相对大一些,价格低,广泛用在消费性产品。
混合式步进电机:力矩大、动态性能好、步距角小、精度高,但是结构复杂价格高,广泛用在工业。
28——步进电机的有效最大外径是28毫米
B——表示是步进电机
Y——表示是永磁式
J——表示是减速型
48——表示四相八拍
内部结构
控制参数
蜂鸣器
压电式蜂鸣器:电陶瓷片发音
电磁式蜂鸣器:线圈通电,电动发音,体积较小
有源蜂鸣器,指震荡源有源放鸣器内部带了震荡源
无源蜂鸣器,价格便宜,但是可以产生高低音
D4:蓄流二极管,防止关断时,对三极管造成损伤
蜂鸣器是感性元器件
代码(蜂鸣器发声)
sbit BUZZ = P1^6; //蜂鸣器控制引脚
unsigned int code NoteFrequ[] = { //中音1-7和高音1-7对应频率列表
523, 587, 659, 698, 784, 880, 988, //中音1-7
1047, 1175, 1319, 1397, 1568, 1760, 1976 //高音1-7
};
unsigned int code NoteReload[] = { //中音1-7和高音1-7对应的定时器重载值
65536 - (11059200/12) / (523*2), //中音1
65536 - (11059200/12) / (587*2), //2
65536 - (11059200/12) / (659*2), //3
65536 - (11059200/12) / (698*2), //4
65536 - (11059200/12) / (784*2), //5
65536 - (11059200/12) / (880*2), //6
65536 - (11059200/12) / (988*2), //7
65536 - (11059200/12) / (1047*2), //高音1
65536 - (11059200/12) / (1175*2), //2
65536 - (11059200/12) / (1319*2), //3
65536 - (11059200/12) / (1397*2), //4
65536 - (11059200/12) / (1568*2), //5
65536 - (11059200/12) / (1760*2), //6
65536 - (11059200/12) / (1976*2), //7
};
bit enable = 1; //蜂鸣器发声使能标志
bit tmrflag = 0; //定时器中断完成标志
unsigned char T0RH = 0xFF; //T0重载值的高字节
unsigned char T0RL = 0x00; //T0重载值的低字节
void PlayTwoTiger();
void main()
{
unsigned int i;
EA = 1; //使能全局中断
TMOD = 0x01; //配置T0工作在模式1
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
while (1)
{
PlayTwoTiger(); //播放乐曲--两支老虎
for (i=0; i<40000; i++); //停止一段时间
}
}
/* 两只老虎乐曲播放函数 */
void PlayTwoTiger()
{
unsigned char beat; //当前节拍索引
unsigned char note; //当前节拍对应的音符
unsigned int time = 0; //当前节拍计时
unsigned int beatTime = 0; //当前节拍总时间
unsigned int soundTime = 0; //当前节拍需发声时间
//两只老虎音符表
unsigned char code TwoTigerNote[] = {
1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,
5,6, 5,4, 3, 1, 5,6, 5,4, 3, 1, 1, 5, 1, 1, 5, 1,
};
//两只老虎节拍表,4表示一拍,1就是1/4拍,8就是2拍
unsigned char code TwoTigerBeat[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,
3,1, 3,1, 4, 4, 3,1, 3,1, 4, 4, 4, 4, 8, 4, 4, 8,
};
for (beat=0; beat<sizeof(TwoTigerNote); ) //用节拍索引作为循环变量
{
while (!tmrflag); //每次定时器中断完成后,检测并处理节拍
tmrflag = 0;
if (time == 0) //当前节拍播完则启动一个新节拍
{
note = TwoTigerNote[beat] - 1;
T0RH = NoteReload[note] >> 8;
T0RL = NoteReload[note];
//计算节拍总时间,右移2位相当于除4,移位代替除法可以加快执行速度
beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;
//计算发声时间,为总时间的0.75,移位原理同上
soundTime = beatTime - (beatTime >> 2);
enable = 1; //指示蜂鸣器开始发声
time++;
}
else //当前节拍未播完则处理当前节拍
{
if (time >= beatTime) //当前持续时间到达节拍总时间时归零,
{ //并递增节拍索引,以准备启动新节拍
time = 0;
beat++;
}
else //当前持续时间未达到总时间时,
{
time++; //累加时间计数
if (time == soundTime) //到达发声时间后,指示关闭蜂鸣器,
{ //插入0.25*总时间的静音间隔,
enable = 0; //用以区分连续的两个节拍
}
}
}
}
}
/* T0中断服务函数,用于控制蜂鸣器发声 */
void InterruptTimer0() interrupt 1
{
TH0 = T0RH; //重新加载重载值
TL0 = T0RL;
tmrflag = 1;
if (enable) //使能时反转蜂鸣器控制电平
BUZZ = ~BUZZ;
else //未使能时关闭蜂鸣器
BUZZ = 1;
}
12.实例练习与经验积累
13.UART串口通信 (重点)
1、并行通信:通信时数据的各个位同时传送,可以实现字节为单位通信,但是通信线多占用资源多,成本高。1110 0100 2、串行通信,一次只能发送一位,(每一位的持续时间----波特率baud)要发送8次才能发送一个字节。 3.通信时间为通信波特率分之一
设置