/* io分配: * ;* OUTPUT: * ;* P1.0 ...... 模擬量輸入 * ;* P1.1 ...... DA輸入比較基準(zhǔn)電壓 * ;* P1.2~7..... R-2R DA電阻網(wǎng)絡(luò) * ;* P3.7 ...... LED 模擬亮度輸出 * ;* CPU CLOCK EQU 6M */ //xiaoqi last edit in 2001.11 //#pragma src #include "AT89x051.h" #include <stdlib.h> #include<math.h> #include<intrins.h>
//變量類型標(biāo)識(shí)的宏定義,大家都喜歡這么做 #define Uchar unsigned char #define Uint unsigned int
#define Ledlight() (P3 &= 0x7f) #define Leddark() (P3 = 0x80)
sbit P36 = P3^6; // 比較器內(nèi)部判斷腳 sbit LED = P3^7; // 一個(gè)發(fā)光二級(jí)管觀察亮度變化
// 內(nèi)部標(biāo)志位定義 bit less; // 比較是否大于 1.小于,0.大于
// 全局變量定義 Uchar timer1, // 通用延時(shí)計(jì)數(shù)器 timer2, // 按鍵蜂鳴器反應(yīng)定時(shí)器 adcdata, // ad轉(zhuǎn)換變量 pwm1; // PWM輸出比例
// 函數(shù)列表 void DelayMs(unsigned int number); // 毫秒延時(shí) void timers0(); // 在定時(shí)器中斷中做數(shù)碼管的掃描顯示(ct0) void Initall(void); // 系統(tǒng)初始化 void timers1(void); // TC1定時(shí)器中斷用于掃描顯示與鍵盤 Uchar adcread(void); // adc轉(zhuǎn)換程序
void main(void) using 0 { DelayMs(120); Initall(); pwm1 = adcread(); LED=1; while(1) { pwm1 = adcread(); timer2=10; while (timer2); } }
// 毫秒延時(shí) void DelayMs(unsigned int number) { unsigned char temp;
for(;number!=0;number--) for(temp=112;temp!=0;temp--); }
/************************************* 在定時(shí)器中斷中做LED的PWM輸出 *************************************/ void timers0() interrupt 1 using 1 { TH0 = 0xff; TL0 = 0xd0; timer1--; if (timer1==pwm1)LED=0; if (timer1==0){ LED=1; timer1=0x40; timer2--; }; }
/**************** ;* 6位 ADC轉(zhuǎn)換 ;****************/ Uchar adcread(void) { Uchar i=0x3f,temp=0; P36 =1; P1 = 3; _nop_();_nop_(); // 從零開始 while ((i--)&& (P36)) { temp += 4; P1 = temp 3; _nop_(); } temp >>= 2; return temp; }
/**************** ;* 系統(tǒng)初始化 ;****************/ void Initall(void) { TMOD = 0x11; // 0001 0001 16進(jìn)制計(jì)數(shù)器 IP = 0x8; // 0000 1000 t1優(yōu)先 IE = 0x8A; // 1000 1010 t0,t1中斷允許 TCON = 5; // 0000 0101 外部中斷低電平觸發(fā) TR0 = 1; // 打開定時(shí)器中斷,IE中已經(jīng)打開,在明示一下 TR1 = 0; ET0 = 1; ET1 = 0; P1 = 0xff; } /************************************* TC1定時(shí)器中斷用于掃描顯示與鍵盤(ct1) *************************************/ void timers1(void) interrupt 3 using 2 { _nop_(); //實(shí)驗(yàn)中沒(méi)有啟用 } |
|