调速时看不到快慢变化,重新上电才能看到。怎样改才能实现实时变化?
#include <reg51.h>
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
uint zkb,pwm,time;
//bit max=0;
uchar RunMode;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr WDT_CONTR = 0xc1;
sfr IE2 = 0xaf; //中断使能寄存器2
sfr AUXR = 0x8e; //辅助寄存器
sfr T2H = 0xD6; //定时器2高8位
sfr T2L = 0xD7; //定时器2低8位
sfr IAP_DATA = 0xC2;
sfr IAP_ADDRH = 0xC3;
sfr IAP_ADDRL = 0xC4;
sfr IAP_CMD = 0xC5;
sfr IAP_TRIG = 0xC6;
sfr IAP_CONTR = 0xC7;
sfr IAP_TPS = 0xF5;
unsigned char RunMode;
void cc(uint addr);
void xcx(uint addr,uchar dat);
uchar dcx(uint addr);
void Q0();
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:擦除某一扇区(每个扇区512字节)
入口:addr = 某一扇区首地址
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void cc(uint addr)// 0x83(晶振<5M) 0x82(晶振<10M) 0x81(晶振<20M) 0x80(晶振<40M)
{
IAP_CONTR = 0x81;
IAP_TPS = 12;
IAP_CMD = 3;
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
EA =0;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();
Q0();
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:写一字节
入口:addr = 扇区单元地址 , dat = 待写入数据
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void xcx(uint addr,uchar dat)
{
IAP_CONTR = 0x81;
IAP_TPS = 12;
IAP_CMD = 2;
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
IAP_DATA = dat;
EA = 0;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();
Q0(); // 关闭ISP/IAP
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:读一字节
入口:addr = 扇区单元地址
出口:dat = 读出的数据
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
uchar dcx(uint addr)
{
uchar dat;
IAP_CONTR = 0x81;
IAP_TPS = 12;
IAP_CMD = 1; // 用户可以对"Data Flash/EEPROM区"进行字节读
IAP_ADDRL = addr;
IAP_ADDRH = addr>>8;
EA = 0;
IAP_TRIG = 0x5a;
IAP_TRIG = 0xa5;
_nop_();
dat = IAP_DATA; // 取出数据
Q0(); // 关闭ISP/IAP
return dat;
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
函数:关闭ISP/IAP操作
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
void Q0()
{
IAP_CONTR = 0; // 关闭IAP功能
IAP_CMD = 0; // 待机模式,无ISP操作
IAP_TRIG = 0;
IAP_ADDRH=0x80;
IAP_ADDRL=0;
EA =1; // 关闭IAP功能, 清与ISP有关的特殊功能寄存器
}
//**********************************System Fuction*************************************************
void Delay1ms( uint count)
{
uint i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
uchar GetKey(void)
{
uchar KeyTemp,CheckValue,Key = 0x00;
CheckValue = P1&0x43;
if(CheckValue==0x43)
return 0x00;
Delay1ms(10);
KeyTemp = P1&0x43;
if(KeyTemp==CheckValue)
return 0x00;
if(!(CheckValue&0x01))
Key|=0x01;
if(!(CheckValue&0x02))
Key|=0x02;
if(!(CheckValue&0x40))
Key|=0x40;
return Key;
}
uint TimerCount ,SystemSpeed,SystemSpeedIndex,outtime;
void InitialTimer0(void) //100us@11.0592MHZ
{
AUXR &= 0x3F; //定时器时钟12T模式
TMOD = 0x00;
TL0 = (65536-1000)%256; //设置定时初始值
TH0 = (65536-1000)/256; //设置定时初始值
TL1 = (65536-time)%256; //设置定时初始值
TH1 = (65536-time)/256;
TR0 = 1;
TR1=1;
ET0=1;
ET1=1;
EA=1;
}
uint code SpeedCode[]={100,150,200,250,300,400,500,600,
700,1000};
uint code pwmtime[]={10,15,20,25,30,35,40,45,50,55};
void pwmspeed(uchar speed1)
{
time=pwmtime[speed1];
}
void SetSpeed( uchar Speed)
{
SystemSpeed =SpeedCode[Speed];
}
void LEDShow( uint LEDStatus)
{
P3 = (LEDStatus&0xFF);
}
void InitialCPU(void)
{
TimerCount = 0;
RunMode = dcx(0x0000);
SystemSpeedIndex =dcx(0x0200);
//SystemSpeedIndex = 0;
if(SystemSpeedIndex > 10)
{
SystemSpeedIndex =1;
}
P1 = 0xFF;
P3 = 0xFF;
Delay1ms(500);
P1 = 0xFF;
P3 = 0xFF;
SetSpeed(SystemSpeedIndex);
pwmspeed(SystemSpeedIndex);
}
unsigned int LEDIndex = 0;
bit LEDDirection = 1,LEDFlag = 1;
//Mode 0
void Mode_0(void)
{
if(LEDDirection)
LEDShow(~(0x0F<<LEDIndex));
else
LEDShow(~(0xF0>>LEDIndex));
if(LEDIndex==7)
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+4)%8;
}
void Mode_1(void)
{
LEDShow(~(0x01<<LEDIndex));
LEDIndex = (LEDIndex+1)%3;
}
void Mode_2(void)
{
LEDShow(0xff<<LEDIndex);
LEDIndex = (LEDIndex+1)%4;
}
void Mode_3(void)
{
if(pwm>zkb)
{
LEDShow(LEDIndex=0xff);
}
else
{
LEDShow(LEDIndex=0x00);
}
}
void TimerEventRun(void)
{
if(RunMode==0x00)
{
Mode_0();
}
else if(RunMode ==0x01)
{
Mode_1();
}
else if(RunMode ==0x02)
{
Mode_2();
}
}
void Timer0(void) interrupt 1 using 1
{
if(++TimerCount>=SystemSpeed)
{
TimerCount = 0;
TimerEventRun();
}
}
void Timer1(void) interrupt 3 using 1
{
static uchar max=0;
pwm++;
if(pwm>200)
{
pwm=0; //周期归零
if(max==0){zkb++;} //MAX占空比渐加渐减标志位 默认max为0.每一周期占空比渐加
if(max==1){zkb--;} //MAX占空比渐加渐减标志位 如果max为1.每一周期占空比渐减
if(zkb==200){max=1;} //如果占空比的值为100 标志位MAX置为1.渐减
if(zkb==0){max=0;} //如果占空比的值为0 标志位MAX置为0.渐加
}
if(RunMode ==0x03)
{
Mode_3();
}
}
unsigned char MusicIndex = 0;
void KeyDispose(unsigned char Key)
{
if(Key&0x01)
{
LEDDirection = 1;
LEDIndex = 0;
LEDFlag = 1;
RunMode = (RunMode+1)%4;
/*if( RunMode == 4);
{
SetSpeed(SystemSpeedIndex);
}*/
cc(0x0000);
xcx(0x0000, RunMode);
}
if(Key&0x02)
{
if(SystemSpeedIndex>0)
{
--SystemSpeedIndex;
pwmspeed(SystemSpeedIndex);
SetSpeed(SystemSpeedIndex);
cc(0x0200);
xcx(0x0200,SystemSpeedIndex);
}
}
if(Key&0x40)
{
if(SystemSpeedIndex<9)
{
++SystemSpeedIndex;
SetSpeed(SystemSpeedIndex);
pwmspeed(SystemSpeedIndex);
cc(0x0200);
xcx(0x0200,SystemSpeedIndex);
}
}
}
//***********************************************************************************
main()
{
unsigned char Key;
P1M0 = 0x00;
P1M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
// WDT_CONTR = 0x27;
InitialCPU();
InitialTimer0();
while(1)
{
//WDT_CONTR = 0x37;
Key = GetKey();
if(Key!=0x00)
{
KeyDispose(Key);
}
}
}
|