|
#include "stc15.h"
#include "intrins.h"
#include "config.h"
#define CYCLE 0x1000L //定義PWM周期(最大值為32767)
unsigned char DUTY = 20L; //定義占空比為10%
void Delay30ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 2;
j = 67;
k = 183;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位數(shù)據(jù),可變波特率
AUXR &= 0xBF; //定時(shí)器1時(shí)鐘為Fosc/12,即12T
AUXR &= 0xFE; //串口1選擇定時(shí)器1為波特率發(fā)生器
TMOD &= 0x0F; //清除定時(shí)器1模式位
TMOD |= 0x20; //設(shè)定定時(shí)器1為8位自動(dòng)重裝方式
TL1 = 0xFD; //設(shè)定定時(shí)初值
TH1 = 0xFD; //設(shè)定定時(shí)器重裝值
ET1 = 0; //禁止定時(shí)器1中斷
TR1 = 1; //啟動(dòng)定時(shí)器1
}
void SndData(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
unsigned char TmpData;
void main()
{
UartInit();
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P_SW2 |= 0x80; //使能訪問XSFR
PWMCFG = 0x00; //配置PWM的輸出初始電平為低電平
PWMCKS = 0x00; //選擇PWM的時(shí)鐘為Fosc/(0+1)
PWMC = CYCLE; //設(shè)置PWM周期
PWM2T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM2T2 = CYCLE * DUTY / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM3T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM3T2 = CYCLE * 20 / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM4T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM4T2 = CYCLE * 20 / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
//占空比為(PWM2T2-PWM2T1)/PWMC
PWM2CR = 0x00; //選擇PWM2輸出到P3.7,不使能PWM2中斷
PWM3CR = 0x00; //選擇PWM3輸出到P3.7,不使能PWM2中斷
PWM4CR = 0x00; //選擇PWM4輸出到P3.7,不使能PWM2中斷
PWMCR = 0x07; //使能PWM信號輸出
PWMCR |= 0x80; //使能PWM模塊
P_SW2 &= ~0x80;
Lun1Set1Xiang(2);
Lun1Set2Xiang(1);
Lun1Set3Xiang(2);
SndData(DUTY);
while (1)
{
SetSpeed1(40);
SetSpeed2(10);
SetSpeed3(10);
/* if(!P17)
{
Delay30ms();
if(!P17)
{
while(P17 == 0 );
DUTY += 10L ;
SndData(DUTY);
if(DUTY >= 100) DUTY =0;
SetSpeed2(DUTY);
}
} */
}
}
//tmpcycle范圍0~100
void SetSpeed1(unsigned char tmpcycle)
{
P_SW2 |= 0x80; //使能訪問XSFR
PWM2T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM2T2 = CYCLE * tmpcycle / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
P_SW2 &= ~0x80;
}
//tmpcycle范圍0~100
void SetSpeed2(unsigned char tmpcycle)
{
P_SW2 |= 0x80; //使能訪問XSFR
PWM3T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM3T2 = CYCLE * tmpcycle / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
P_SW2 &= ~0x80;
}
//tmpcycle范圍0~100
void SetSpeed3(unsigned char tmpcycle)
{
P_SW2 |= 0x80; //使能訪問XSFR
PWM4T1 = 0x0000; //設(shè)置PWM2第1次反轉(zhuǎn)的PWM計(jì)數(shù)
PWM4T2 = CYCLE * tmpcycle / 100; //設(shè)置PWM2第2次反轉(zhuǎn)的PWM計(jì)數(shù)
P_SW2 &= ~0x80;
}
void Lun1Set1Xiang(unsigned char fangdat)
{
if(fangdat == 1) {P04 = 1; P05 = 0;}
else if(fangdat == 2) {P04 = 0; P05 = 1;}
else {P04 = 1; P05 = 1;}
}
void Lun1Set2Xiang(unsigned char fangdat)
{
if(fangdat == 1) {P00 = 1; P01 = 0;}
else if(fangdat == 2) {P00 = 0; P01 = 1;}
else {P00 = 1; P01 = 1;}
}
void Lun1Set3Xiang(unsigned char fangdat)
{
if(fangdat == 1) {P02 = 1; P03 = 0;}
else if(fangdat == 2) {P02 = 0; P03 = 1;}
else {P02 = 1; P03 = 1;}
}
|
|