|
用ad0832 電壓改變實(shí)現(xiàn)對(duì)轉(zhuǎn)速的控制
測(cè)轉(zhuǎn)速利用定時(shí)器0+計(jì)數(shù)器
將測(cè)得的轉(zhuǎn)速與輸入值相比較 如果轉(zhuǎn)速小于輸入值電壓增大 反之則減小電壓。
具體代碼如下
// 文件名:
// 功能:
// 輸入?yún)?shù):
// 功能: cs0--keyledcs cs2--dacs
// 出口函數(shù):
#include<reg51.h>
xdata unsigned char OUTBIT _at_ 0x8002; // 位控制口
xdata unsigned char OUTSEG _at_ 0x8004; // 段控制口
xdata unsigned char IN _at_ 0x8001; // 鍵盤(pán)讀入口
unsigned char LEDBuf[6]; // 顯示緩沖
unsigned int Keyval[3]; // 顯示緩沖
code unsigned char KeyTable[] = { // 鍵碼定義
0x16, 0x15, 0x14, 0xff,
0x13, 0x12, 0x11, 0x10,
0x0d, 0x0c, 0x0b, 0x0a,
0x0e, 0x03, 0x06, 0x09,
0x0f, 0x02, 0x05, 0x08,
0x00, 0x01, 0x04, 0x07
};
code unsigned char LEDMAP[] = { // 八段管顯示碼
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
#define Tick 10000 // 10000 x 100us = 1s
#define T100us (256-50) // 100us時(shí)間常數(shù)(6M)
unsigned int C100us; // 100us記數(shù)單元
unsigned int count; // 頻率記數(shù)單元
xdata unsigned char CS0832 _at_ 0xa000; //DAC
unsigned int speedval; // 轉(zhuǎn)速控制字
//########################################
//延時(shí)
//########################################
void delay(unsigned char CNT)
{
unsigned char i;
while (CNT-- !=0)
for (i=10; i !=0; i--);
}
//########################################
//初始化LED
//########################################
void InitLED()
{
unsigned char i;
unsigned char Pos;
unsigned char LED;
LEDBuf[0] = 0x7f; // 初始化檢測(cè)顯示的值
LEDBuf[1] = 0x7f;
LEDBuf[2] = 0x7f;
LEDBuf[3] = 0x7f;
LEDBuf[4] = 0x7f;
LEDBuf[5] = 0x7f;
Pos = 0x20; // 檢測(cè)LED,從左邊開(kāi)始掃描顯示
for (i = 0; i < 6; i++) {
OUTBIT = 0; // 關(guān)所有八段管
OUTBIT = Pos; // 顯示一位八段管
LED = LEDBuf[i];
OUTSEG = LED;
Pos >>= 1; // 顯示下一位
delay(100);
}
}
//########################################
//LED顯示程序
//########################################
void DisplayLED()
{
unsigned char i;
unsigned char Pos;
unsigned char LED;
Pos = 0x20; // 從左邊開(kāi)始顯示
for (i = 0; i < 6; i++) {
LED = LEDBuf[i];
OUTSEG = LED;
OUTBIT = Pos; // 顯示一位八段管
Pos >>= 1; // 顯示下一位
delay(1);
}
}
//########################################
//Key檢測(cè)
//########################################
unsigned char TestKey()
{
OUTBIT = 0; // 輸出線置為0
return (~IN & 0x0f); // 讀入鍵狀態(tài)(高四位不用)
}
unsigned char GetKey()
{
unsigned char Pos;
unsigned char i;
unsigned char k;
i = 6;
Pos = 0x20; // 找出鍵所在列
do {
OUTBIT = ~ Pos;
Pos >>= 1;
k = ~IN & 0x0f;
} while ((--i != 0) && (k == 0));
// 鍵值 = 列 x 4 + 行
if (k != 0) {
i *= 4;
if (k & 2)
i += 1;
else if (k & 4)
i += 2;
else if (k & 8)
i += 3;
OUTBIT = 0;
do DisplayLED(); while (TestKey());
return(KeyTable[i]); // 取出鍵碼
} else return(0xff);
}
//########################################
// 將 Number 拆為三個(gè) BCD 碼, 并存入 Result 數(shù)組
//########################################
void BCD(int Number)
{
LEDBuf[3] = LEDMAP[Number / 100]; // 數(shù)除以 100, 得百位數(shù)
LEDBuf[4] = LEDMAP[(Number % 100) / 10]; // 余數(shù)除以 10, 得十位數(shù)
LEDBuf[5] = LEDMAP[ Number % 10 ]; // 數(shù)除以 10, 余數(shù)得個(gè)位數(shù)
}
//########################################
//定時(shí)器中斷
//########################################
void T0Int() interrupt 1
{
C100us--;
if (C100us == 0)
{
C100us = Tick; // 100us 記數(shù)器為0, 重置記數(shù)器
count=TH1;
count=(count<<8)+TL1;
TH1 = 0;
TL1 = 0;
BCD(count);
}
}
//########################################
//速度檢測(cè)
//########################################
void chesu_Init()
{
TMOD = 0x52; // T0方式1,記數(shù)器;T1方式1,記數(shù)器
TH1 = 0;
TL1 = 0;
TH0 = T100us;
TL0 = T100us;
IE = 0x82; // EA=1, IT0 = 1
C100us = Tick;
TR0 = 1; // 開(kāi)始定時(shí)
TR1 = 1; // 開(kāi)始記數(shù) // 方式2, 定時(shí)器
}
//########################################
//主程序
//########################################
void main()
{
unsigned char i;
speedval = 160;
InitLED();
chesu_Init();
while (1)
{
if (TestKey())
{
Keyval[i] = GetKey() & 0x0f;
LEDBuf[i] = LEDMAP[Keyval[i]];
if ( i < 2 ) i++;
else i=0;
}
if ((Keyval[1]*10 + Keyval[2]) > count)
{ if (speedval < 255) speedval++; }
else if ((Keyval[1]*10 + Keyval[2]) < count)
{ if (speedval > 128) speedval--; }
else speedval=speedval ;
CS0832 = speedval;
DisplayLED();
}
}
|
評(píng)分
-
查看全部評(píng)分
|