一、實(shí)驗(yàn)?zāi)康?br />
*掌握脈寬調(diào)制調(diào)速的原理與方法,學(xué)習(xí)頻率/周期測(cè)量的方法;
*掌握使用ST7920控制器在液晶顯示屏上進(jìn)行圖形顯示的方法;
*了解閉環(huán)控制的原理。
二、實(shí)驗(yàn)設(shè)備
*單片機(jī)實(shí)驗(yàn)箱
*直流電機(jī)模塊
*液晶顯示屏模塊
*Keil開發(fā)系統(tǒng)
三、實(shí)驗(yàn)原理
1、直流電機(jī)原理
直流電機(jī)具有良好的啟動(dòng)制動(dòng)性能,可以在寬范圍內(nèi)平滑調(diào)速,應(yīng)用范圍廣泛。影響直流電機(jī)轉(zhuǎn)速的主要參數(shù)有電樞的供電電壓和電機(jī)的勵(lì)磁電流,可以通過調(diào)節(jié)這兩個(gè)參數(shù)來達(dá)到調(diào)速的目的。小型直流電機(jī)沒有勵(lì)磁電流,使用調(diào)壓的方式來控制,由電壓決定了電機(jī)轉(zhuǎn)速。這種控制方式需要模擬量輸出,一般要采用D/A器件完成。
2、脈寬調(diào)制原理
脈寬調(diào)制(Pulse Width Modulation,PWM) 是一種能夠通過開關(guān)量輸出達(dá)到模擬量輸出效果的方法。
PWM的基本原理是:通過輸出一個(gè)高頻率的0/1信號(hào),其中有效信號(hào)的比例為d(也叫做占空比),在外圍積分元件的作用下,使得總的效果相當(dāng)于輸出d×A(A為高電壓)的電壓。通過改變占空比就可以調(diào)整輸出電壓,從而使用1根線就可以達(dá)到模擬量輸出的效果。
編程實(shí)現(xiàn)PWM,最簡(jiǎn)單的辦法就是以某個(gè)時(shí)間單位(如0.1ms)為基準(zhǔn),在前N段輸出1,后M-N段輸出0,總體的占空比就是N/M。這種方法由于0和1分布不均勻,所以要求基準(zhǔn)頻率要足夠高,否則會(huì)出現(xiàn)顛簸現(xiàn)象。累加進(jìn)位法可以達(dá)到更穩(wěn)定的效果。思路是將總的周期內(nèi)的0和1均勻分散開。設(shè)置一個(gè)累加變量X,每次加N,若結(jié)果大于M,則輸出1,并減去M;否則輸出0。整體來看,占空比將是N/M。
3、電機(jī)測(cè)速原理
在電機(jī)的同軸轉(zhuǎn)盤上,安裝有磁塊,在轉(zhuǎn)盤下面由對(duì)磁體敏感的霍爾傳感器,隨著轉(zhuǎn)盤轉(zhuǎn)動(dòng),當(dāng)磁體正對(duì)傳感器時(shí),傳感器會(huì)輸出一個(gè)低電平。單片機(jī)利用此電平變化觸發(fā)中斷,統(tǒng)計(jì)在固定時(shí)間(如1秒)內(nèi)中斷發(fā)生的次數(shù),即得到電機(jī)的轉(zhuǎn)速。
4、電機(jī)驅(qū)動(dòng)原理
按照脈寬調(diào)制的原理,使用一個(gè)快速的定時(shí)中斷(0.1ms左右),動(dòng)態(tài)改變輸出0/1,使得總體占空比d=N/M,d就是控制量。
注意實(shí)驗(yàn)箱的電機(jī),低電平旋轉(zhuǎn),因此前述的0/1在使用時(shí)均要取反。
隨著控制量d增大,電機(jī)轉(zhuǎn)速就應(yīng)該提高,但它們之間不是簡(jiǎn)單的對(duì)應(yīng)關(guān)系,因此必須根據(jù)測(cè)量出來的實(shí)際轉(zhuǎn)速進(jìn)行閉環(huán)反饋調(diào)整。
5、閉環(huán)控制原理
閉環(huán)控制系統(tǒng)是控制系統(tǒng)的一種類型,又稱反饋控制系統(tǒng)。把控制系統(tǒng)輸出量,通過一定方法和裝置反送回系統(tǒng)的輸入端,然后將反饋信息與預(yù)定的輸入目標(biāo)量進(jìn)行比較,再根據(jù)比較的結(jié)果對(duì)系統(tǒng)進(jìn)行控制,避免系統(tǒng)偏離預(yù)定目標(biāo)。
6、電機(jī)調(diào)速原理
使用閉環(huán)控制方法,如想將電機(jī)轉(zhuǎn)速控制在預(yù)定值附近,在測(cè)量出當(dāng)前轉(zhuǎn)速之后,將其與目標(biāo)值相對(duì)比,通過控制算法調(diào)整控制量,使當(dāng)前值與目標(biāo)值差距不斷減少。
最簡(jiǎn)單的控制算法可以是加1減1法,即如當(dāng)前轉(zhuǎn)速小于目標(biāo)轉(zhuǎn)速,則增加d(N++),否則減少d(N--),也可以使用更加復(fù)雜的算法。
四、實(shí)驗(yàn)內(nèi)容
使用脈寬調(diào)制調(diào)速方式驅(qū)動(dòng)直流電機(jī)的轉(zhuǎn)動(dòng),測(cè)量轉(zhuǎn)速并根據(jù)目標(biāo)轉(zhuǎn)速進(jìn)行控制,并將運(yùn)行狀態(tài)顯示到液晶顯示屏上。
實(shí)驗(yàn)具體內(nèi)容
1、使用計(jì)數(shù)法測(cè)量直流電機(jī)每秒鐘轉(zhuǎn)動(dòng)圈數(shù);
2、使用脈寬調(diào)制調(diào)速方法,驅(qū)動(dòng)電機(jī)旋轉(zhuǎn);
3、根據(jù)設(shè)定的目標(biāo)轉(zhuǎn)速,動(dòng)態(tài)調(diào)整占空比,使電機(jī)轉(zhuǎn)速能夠穩(wěn)定在目標(biāo)轉(zhuǎn)速附近;
4、通過輸入開關(guān)改變目標(biāo)轉(zhuǎn)速,隨后電機(jī)轉(zhuǎn)速能夠自動(dòng)調(diào)整,穩(wěn)定在目標(biāo)轉(zhuǎn)速附近。
5、在液晶顯示屏上顯示出系統(tǒng)狀態(tài),包括:當(dāng)前轉(zhuǎn)速、目標(biāo)轉(zhuǎn)速、占空比等;
6、同時(shí)在液晶顯示屏上使用圖形模式,顯示一些提示和裝飾信息。
五、實(shí)驗(yàn)步驟
設(shè)計(jì)電路連接方案,進(jìn)行設(shè)備連線
參考連線:
液晶顯示屏模塊:RS連IO1(P0^0),WR連IO2(P0^1),DE連IO3(P0^2)。
直流電機(jī)模塊:DRV連IO5(P0^4),SPEED連INT0(P3^2)。
輸入模塊:將8個(gè)開關(guān)連接至P2,最左側(cè)開關(guān)接P2.7,最右側(cè)開關(guān)接P2.0,可以表示0-255范圍的無符號(hào)整數(shù),用來輸入目標(biāo)轉(zhuǎn)速。
1.jpg (2.47 MB, 下載次數(shù): 28)
下載附件
2024-1-5 12:53 上傳
建議按以下步驟依次編程和調(diào)試
1. 將模擬量輸出線接到直流電機(jī)的DRV端,調(diào)節(jié)滑動(dòng)變阻器,可以驅(qū)動(dòng)電機(jī)轉(zhuǎn)動(dòng),連接測(cè)速線,編寫程序測(cè)量電機(jī)轉(zhuǎn)速并顯示;
2. 編寫PWM驅(qū)動(dòng)部分,將輸出信號(hào)連到電機(jī)的DRV端,給一個(gè)固定占空比,測(cè)試該部分功能;
3. 編寫閉環(huán)反饋的控制邏輯進(jìn)行調(diào)速;
4. 對(duì)輸出界面進(jìn)行美觀優(yōu)化。
參考單片機(jī)代碼
- //lcd12864.h
- #ifndef _LCD12864_H_
- #define _LCD12864_H_
- /**************************************************************
- iO口宏定義區(qū)
- ***************************************************************/
- sbit CS =P0^0;//對(duì)應(yīng)實(shí)驗(yàn)箱上IO1 RS
- sbit SID=P0^1;//對(duì)應(yīng)實(shí)驗(yàn)箱上IO2 RW
- sbit SCK=P0^2;//對(duì)應(yīng)實(shí)驗(yàn)箱上IO3 E
-
- void write_command( unsigned char Cbyte ); //寫入指令函數(shù)
- void write_data( unsigned char Dbyte ); //寫入指令數(shù)據(jù)
- 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
復(fù)制代碼- //lcd12864.c
- #include <reg51.h>
- #include <intrins.h>
- #include "lcd12864.h"
- /**************************************************************
-
-
- //串行方式控制
- /*******************************************************************
- 常量聲明區(qū)
- ********************************************************************/
- unsigned char code AC_TABLE[]={ //坐標(biāo)編碼
- 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,
- };
- /****************************************************************
- 發(fā)送一個(gè)字節(jié)
- *****************************************************************/
- 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;
- }
- }
- /**********************************************************
- 接收一個(gè)字節(jié)
- ***********************************************************/
-
- 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));
- }
- /****************************************************************
- 檢查忙狀態(tài)
- ******************************************************************/
- void check_busy( void )
- {
- do send_byte(0xfc); //11111,RW(1),RS(0),0
- while(0x80&receive_byte());
- }
-
- /******************************************************************
- 寫一個(gè)字節(jié)的指令
- *******************************************************************/
- 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;
- }
- /*************************************************************
- 寫一個(gè)字節(jié)的數(shù)據(jù)
- **************************************************************/
- 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初始化函數(shù)
- *******************************************************************/
- void lcd_init( void )
- {
- write_command(0x30);
- write_command(0x03);
- write_command(0x0c);
- write_command(0x01);
- write_command(0x06);
- }
-
- /*******************************************************************************************************
- 設(shè)定光標(biāo)函數(shù)
- ********************************************************************************************************/
- 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);
- }
復(fù)制代碼- //main.c
- #include <reg51.h>
- #include "lcd12864.h"
- sbit DRV = P0^4;//對(duì)應(yīng)實(shí)驗(yàn)箱IO5
- unsigned char circle=0,current_speed_val=0,target_speed_val=30,flag_1s=0,counter=0,pwm_val=50; //counter計(jì)數(shù),0-100。pwm_val指的是占空比(脈沖寬度調(diào)整)pwm_val 最大值為100。總份數(shù)在本程序中亦為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位邊沿觸發(fā))
- TMOD=0x11;//計(jì)時(shí)器0和計(jì)時(shí)器1均為方式1
- TL1 = 0xA4; //單片機(jī)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個(gè)開關(guān)連接至P2,最左側(cè)開關(guān)接P2.7,最右側(cè)開關(guān)接P2.0,可以表示0-255范圍的數(shù)
- 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,"目標(biāo)轉(zhuǎn)速");
- put_str(1,6,target_speed);
- put_str(2,0,"當(dāng)前轉(zhuǎn)速");
- put_str(2,6,current_speed);
-
- ET0=1;
- TR0=1;
- ET1=1;
- TR1=1;
- }
- }
-
-
-
- /****************************************************************
- 定時(shí)器T1實(shí)現(xiàn)pwm
- ****************************************************************/
- void timer1_interrupt() interrupt 3 //每隔100us進(jìn)來一次
- {
- TL1 = 0xA4; //單片機(jī)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;//實(shí)驗(yàn)箱的電機(jī)低電平運(yùn)行
- }
- else
- {
- DRV=1;
- }
- ET1=1;
- TR1=1;
- }
-
- /****************************************************************
- 定時(shí)器T0實(shí)現(xiàn)每秒更新轉(zhuǎn)速,并根據(jù)當(dāng)前轉(zhuǎn)速調(diào)整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;
-
- //根據(jù)當(dāng)前轉(zhuǎn)速和目標(biāo)轉(zhuǎn)速調(diào)整占空比
- 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,脈沖技術(shù)記錄電機(jī)的轉(zhuǎn)速,電機(jī)轉(zhuǎn)一圈circle加一
- ****************************************************************/
- void int0_interrupt() interrupt 0{
- circle++;
- IE0=0;
- }
復(fù)制代碼 |