一、实验目的
*掌握脉宽调制调速的原理与方法,学习频率/周期测量的方法;
*掌握使用ST7920控制器在液晶显示屏上进行图形显示的方法;
*了解闭环控制的原理。
二、实验设备
*单片机实验箱
*直流电机模块
*液晶显示屏模块
*Keil开发系统
三、实验原理
1、直流电机原理
直流电机具有良好的启动制动性能,可以在宽范围内平滑调速,应用范围广泛。影响直流电机转速的主要参数有电枢的供电电压和电机的励磁电流,可以通过调节这两个参数来达到调速的目的。小型直流电机没有励磁电流,使用调压的方式来控制,由电压决定了电机转速。这种控制方式需要模拟量输出,一般要采用D/A器件完成。
2、脉宽调制原理
脉宽调制(Pulse Width Modulation,PWM) 是一种能够通过开关量输出达到模拟量输出效果的方法。
PWM的基本原理是:通过输出一个高频率的0/1信号,其中有效信号的比例为d(也叫做占空比),在外围积分元件的作用下,使得总的效果相当于输出d×A(A为高电压)的电压。通过改变占空比就可以调整输出电压,从而使用1根线就可以达到模拟量输出的效果。
编程实现PWM,最简单的办法就是以某个时间单位(如0.1ms)为基准,在前N段输出1,后M-N段输出0,总体的占空比就是N/M。这种方法由于0和1分布不均匀,所以要求基准频率要足够高,否则会出现颠簸现象。累加进位法可以达到更稳定的效果。思路是将总的周期内的0和1均匀分散开。设置一个累加变量X,每次加N,若结果大于M,则输出1,并减去M;否则输出0。整体来看,占空比将是N/M。
3、电机测速原理
在电机的同轴转盘上,安装有磁块,在转盘下面由对磁体敏感的霍尔传感器,随着转盘转动,当磁体正对传感器时,传感器会输出一个低电平。单片机利用此电平变化触发中断,统计在固定时间(如1秒)内中断发生的次数,即得到电机的转速。
4、电机驱动原理
按照脉宽调制的原理,使用一个快速的定时中断(0.1ms左右),动态改变输出0/1,使得总体占空比d=N/M,d就是控制量。
注意实验箱的电机,低电平旋转,因此前述的0/1在使用时均要取反。
随着控制量d增大,电机转速就应该提高,但它们之间不是简单的对应关系,因此必须根据测量出来的实际转速进行闭环反馈调整。
5、闭环控制原理
闭环控制系统是控制系统的一种类型,又称反馈控制系统。把控制系统输出量,通过一定方法和装置反送回系统的输入端,然后将反馈信息与预定的输入目标量进行比较,再根据比较的结果对系统进行控制,避免系统偏离预定目标。
6、电机调速原理
使用闭环控制方法,如想将电机转速控制在预定值附近,在测量出当前转速之后,将其与目标值相对比,通过控制算法调整控制量,使当前值与目标值差距不断减少。
最简单的控制算法可以是加1减1法,即如当前转速小于目标转速,则增加d(N++),否则减少d(N--),也可以使用更加复杂的算法。
四、实验内容
使用脉宽调制调速方式驱动直流电机的转动,测量转速并根据目标转速进行控制,并将运行状态显示到液晶显示屏上。
实验具体内容
1、使用计数法测量直流电机每秒钟转动圈数;
2、使用脉宽调制调速方法,驱动电机旋转;
3、根据设定的目标转速,动态调整占空比,使电机转速能够稳定在目标转速附近;
4、通过输入开关改变目标转速,随后电机转速能够自动调整,稳定在目标转速附近。
5、在液晶显示屏上显示出系统状态,包括:当前转速、目标转速、占空比等;
6、同时在液晶显示屏上使用图形模式,显示一些提示和装饰信息。
五、实验步骤
设计电路连接方案,进行设备连线
参考连线:
液晶显示屏模块:RS连IO1(P0^0),WR连IO2(P0^1),DE连IO3(P0^2)。
直流电机模块:DRV连IO5(P0^4),SPEED连INT0(P3^2)。
输入模块:将8个开关连接至P2,最左侧开关接P2.7,最右侧开关接P2.0,可以表示0-255范围的无符号整数,用来输入目标转速。
建议按以下步骤依次编程和调试
1. 将模拟量输出线接到直流电机的DRV端,调节滑动变阻器,可以驱动电机转动,连接测速线,编写程序测量电机转速并显示;
2. 编写PWM驱动部分,将输出信号连到电机的DRV端,给一个固定占空比,测试该部分功能;
3. 编写闭环反馈的控制逻辑进行调速;
4. 对输出界面进行美观优化。
参考代码
//lcd12864.h
#ifndef _LCD12864_H_
#define _LCD12864_H_
/**************************************************************
iO口宏定义区
***************************************************************/
sbit CS =P0^0;//对应实验箱上IO1 RS
sbit SID=P0^1;//对应实验箱上IO2 RW
sbit SCK=P0^2;//对应实验箱上IO3 E
void write_command( unsigned char Cbyte ); //写入指令函数
void write_data( unsigned char Dbyte ); //写入指令数据
void lcd_init( void ); //显示屏初始化
void lcd_clear_txt( void ); //显示屏清屏
void location_xy_12864(unsigned char x,unsigned char y);
void put_str(unsigned char row,unsigned char col,unsigned char *puts);
void put_char(unsigned char row,unsigned char col,unsigned char put);
void lcd_display_picture(unsigned char p[][16]);
#endif
//lcd12864.c
#include <reg51.h>
#include <intrins.h>
#include "lcd12864.h"
/**************************************************************
//串行方式控制
/*******************************************************************
常量声明区
********************************************************************/
unsigned char code AC_TABLE[]={ //坐标编码
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
/****************************************************************
发送一个字节
*****************************************************************/
void send_byte(unsigned char dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK = 0;
dbyte=dbyte<<1;
SID = CY;
SCK = 1;
SCK = 0;
}
}
/**********************************************************
接收一个字节
***********************************************************/
unsigned char receive_byte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
/****************************************************************
检查忙状态
******************************************************************/
void check_busy( void )
{
do send_byte(0xfc); //11111,RW(1),RS(0),0
while(0x80&receive_byte());
}
/******************************************************************
写一个字节的指令
*******************************************************************/
void write_command( unsigned char Cbyte )
{
CS = 1;
check_busy();
send_byte(0xf8); //11111,RW(0),RS(0),0
send_byte(0xf0&Cbyte);
send_byte(0xf0&Cbyte<<4);
CS = 0;
}
/*************************************************************
写一个字节的数据
**************************************************************/
void write_data( unsigned char dbyte )
{
CS = 1;
check_busy();
send_byte(0xfa); //11111,RW(0),RS(1),0
send_byte(0xf0&dbyte);
send_byte(0xf0&dbyte<<4);
CS = 0;
}
/******************************************************************
lcd初始化函数
*******************************************************************/
void lcd_init( void )
{
write_command(0x30);
write_command(0x03);
write_command(0x0c);
write_command(0x01);
write_command(0x06);
}
/*******************************************************************************************************
设定光标函数
********************************************************************************************************/
void location_xy_12864(unsigned char x,unsigned char y)
{
switch(x)
{
case 0:
x=0x80;break;
case 1:
x=0x90;break;
case 2:
x=0x88;break;
case 3:
x=0x98;break;
default:
x=0x80;
}
y=y&0x07;
write_command(0x30);
write_command(y+x);
write_command(y+x);
}
/***********************************************************************************
清除文本
************************************************************************************/
void lcd_clear_txt( void )
{
unsigned char i;
write_command(0x30);
write_command(0x80);
for(i=0;i<64;i++)
write_data(0x20);
location_xy_12864(0,0);
}
/****************************************************************************************
显示字符串
*****************************************************************************************/
void put_str(unsigned char row,unsigned char col,unsigned char *puts)
{
write_command(0x30);
write_command(AC_TABLE[8*row+col]);
while(*puts != '\0')
{
if(col==8)
{
col=0;
row++;
}
if(row==4) row=0;
write_command(AC_TABLE[8*row+col]);
write_data(*puts);
puts++;
if(*puts != '\0')
{
write_data(*puts);
puts++;
col++;
}
}
}
/****************************************************************************************
显示字符
*****************************************************************************************/
void put_char(unsigned char row,unsigned char col,unsigned char put)
{
write_command(0x30);
write_command(AC_TABLE[8*row+col]);
write_data(put);
}
/****************************************************************************************
显示图片
*****************************************************************************************/
void lcd_display_picture(unsigned char p[][16])
{
unsigned char x,y,a,b,c;
write_command(0x34);
x = 0x80;
y = 0x80;
for(c=0;c<2;c++)//先画上半屏,再画下半屏
{
for(a=0;a<32;a++)
{
write_command(y+a);
write_command(x);
for(b=0;b<16;b++)
write_data(p[a+c*32][b]);
}
x=0x88;
}
write_command(0x36);
write_command(0x30);
}
//main.c
#include <reg51.h>
#include "lcd12864.h"
sbit DRV = P0^4;//对应实验箱IO5
unsigned char circle=0,current_speed_val=0,target_speed_val=30,flag_1s=0,counter=0,pwm_val=50; //counter计数,0-100。pwm_val指的是占空比(脉冲宽度调整)pwm_val 最大值为100。总份数在本程序中亦为100
char current_speed[4];
char target_speed[4];
char pwm[4];
unsigned char code img[][16] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x04,0x02,0x55,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x3B,0xB4,0x00,0x00,
0x00,0x00,0x04,0x0B,0xEA,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x00,0xAA,0xA8,
0x00,0x00,0x12,0x24,0x29,0xA3,0x00,0x00,0x00,0x00,0x0A,0x00,0x2C,0x00,0x10,0x04,
0x00,0x01,0x55,0x37,0x84,0x3B,0x00,0x00,0x00,0x00,0x42,0x20,0x26,0x80,0x0A,0x42,
0x00,0x02,0x00,0x80,0x04,0x4A,0x80,0x00,0x00,0x0A,0x99,0x10,0x01,0x78,0x00,0x10,
0x00,0x28,0xAF,0xDA,0x08,0x32,0x00,0x02,0x00,0x2D,0x62,0xC0,0x00,0xD7,0x80,0x08,
0x2C,0x95,0xB0,0xC0,0x05,0x6E,0x00,0x03,0x40,0xA2,0xBD,0x74,0x00,0x12,0x80,0x00,
0x02,0x41,0x54,0x14,0x84,0x26,0x00,0x01,0x2E,0xBA,0xAD,0x2E,0x80,0x5A,0xC0,0x00,
0x00,0x24,0xC0,0x0A,0x41,0x33,0xD0,0x00,0x13,0x46,0x95,0xB3,0x6D,0x56,0x80,0x00,
0x00,0x1B,0x00,0x00,0x52,0x9C,0x6A,0x00,0x4D,0x58,0x01,0x6C,0xAA,0xE9,0x00,0x00,
0x0A,0x80,0x00,0x00,0x29,0x4B,0x2D,0x56,0xA5,0x70,0x00,0x0A,0xAD,0x2A,0x00,0x00,
0x50,0x00,0x00,0x00,0x00,0x29,0xAA,0xAA,0xA5,0x00,0x00,0x60,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x84,0xDD,0x55,0x58,0x01,0x55,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x28,0x01,0x4A,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
void main(void){
IE=0x81;//EA 空 空 ES ET1 EX1 ET0 EX0
TCON=0x01;//TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0(IT0=1表示外部中断0位边沿触发)
TMOD=0x11;//计时器0和计时器1均为方式1
TL1 = 0xA4; //单片机CPU主频11.0892MHz,1/12是0.9241M
TH1 = 0xFF; //(65536-初值)/(0.9241M)=0.1ms,得到初值是65444=0xFFA4
TH0=0x4b;
TL0=0x83;// (65536-19331)/(0.9241M)=0.05s,19331=0x4b83 ,20次中断为1秒
lcd_init();
lcd_clear_txt();
lcd_display_picture(img);
while(1){
target_speed_val=P2;//将8个开关连接至P2,最左侧开关接P2.7,最右侧开关接P2.0,可以表示0-255范围的数
target_speed[2]='0'+target_speed_val%10;
target_speed[1]='0'+(target_speed_val/10)%10;
target_speed[0]='0'+(target_speed_val/100)%10;
target_speed[3]='\0';
put_str(0,0,"占空比");
put_str(0,6,pwm);
put_str(1,0,"目标转速");
put_str(1,6,target_speed);
put_str(2,0,"当前转速");
put_str(2,6,current_speed);
ET0=1;
TR0=1;
ET1=1;
TR1=1;
}
}
/****************************************************************
定时器T1实现pwm
****************************************************************/
void timer1_interrupt() interrupt 3 //每隔100us进来一次
{
TL1 = 0xA4; //单片机CPU主频11.0892MHz,1/12是0.9241M
TH1 = 0xFF; //(65536-初值)/(0.9241M)=0.1ms,得到初值是65444=0xFFA4
counter=(counter+1)%100;
if(counter<pwm_val)
{
DRV=0;//实验箱的电机低电平运行
}
else
{
DRV=1;
}
ET1=1;
TR1=1;
}
/****************************************************************
定时器T0实现每秒更新转速,并根据当前转速调整pwm
****************************************************************/
void timer0_interrupt() interrupt 1{
EA=0;
TH0=0x4b;
TL0=0x83;
flag_1s++;
if(flag_1s==20){
current_speed_val=circle;
current_speed[2]='0'+current_speed_val%10;
current_speed[1]='0'+(current_speed_val/10)%10;
current_speed[0]='0'+(current_speed_val/100)%10;
current_speed[3]='\0';
circle=0;
//根据当前转速和目标转速调整占空比
if(current_speed_val>target_speed_val){
if(pwm_val>0)pwm_val--;
}else{
if(pwm_val<99)pwm_val++;
}
pwm[1]='0'+pwm_val%10;
pwm[0]='0'+(pwm_val/10)%10;
pwm[2]='\0';
flag_1s=0;
}
ET0=1;
TR0=1;
EA=1;
}
/****************************************************************
外部中断0,脉冲技术记录电机的转速,电机转一圈circle加一
****************************************************************/
void int0_interrupt() interrupt 0{
circle++;
IE0=0;
}