用的是STC15W24S單片機,P1輸出,P3口輸入,三個按鍵,兩個調速度快慢,一個調模式。我想把void Mode_0(void)做成可調速的漸亮漸滅效果。
兩年的單片機學習之路卻搞不定,麻煩各位師傅們指點。謝謝了!
#include <STC15W204S.H>
unsigned char RunMode;
//**********************************System Fuction*************************************************
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
unsigned char GetKey(void)
{
unsigned char KeyTemp,CheckValue,Key = 0x00;
CheckValue = P3&0x32;
if(CheckValue==0x32)
return 0x00;
Delay1ms(10);
KeyTemp = P3&0x32;
if(KeyTemp==CheckValue)
return 0x00;
if(!(CheckValue&0x02))
Key|=0x01;
if(!(CheckValue&0x10))
Key|=0x02;
if(!(CheckValue&0x20))
Key|=0x04;
return Key;
}
unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;
void InitialTimer0(void)
{
TMOD = 0x00;
TH0 = TL0 = 0xFC;
ET0=1;
TR0=1;
EA=1;
}
unsigned int code SpeedCode[]={ 1, 2, 3, 5, 8, 10, 14, 17, 20, 30,
40, 50, 60, 70, 80, 90, 100, 120, 140, 160,
180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
void SetSpeed(unsigned char Speed)
{
SystemSpeed =SpeedCode[Speed];
}
void LEDShow(unsigned int LEDStatus)
{
P1 = ~(LEDStatus&0xFF);
}
void InitialCPU(void)
{
RunMode = 0x00;
TimerCount = 0;
SystemSpeedIndex = 10;
P1 = 0xFF;
P3 = 0xFF;
Delay1ms(500);
P1 = 0xFF;
P3 = 0xFF;
SetSpeed(SystemSpeedIndex);
}
//Mode 0
unsigned int LEDIndex = 0;
bit LEDDirection = 1,LEDFlag = 1;
void Mode_0(void)
{
}
//Mode 1
void Mode_1(void)
{
LEDShow(0x80>>LEDIndex);
LEDIndex = (LEDIndex+1)%8;
}
void TimerEventRun(void)
{
if(RunMode==0x00)
{
Mode_0();
}
else if(RunMode ==0x01)
{
Mode_1();
}
}
void Timer0(void) interrupt 1 using 1
{
TF0 = 0;
if(++TimerCount>=SystemSpeed)
{
TimerCount = 0;
TimerEventRun();
}
}
unsigned char MusicIndex = 0;
void KeyDispose(unsigned char Key)
{
if(Key&0x01)
{
LEDDirection = 1;
LEDIndex = 0;
LEDFlag = 1;
RunMode = (RunMode+1)%2;
}
if(Key&0x02)
{
if(SystemSpeedIndex>0)
{
--SystemSpeedIndex;
SetSpeed(SystemSpeedIndex);
}
else
{
}
}
if(Key&0x04)
{
if(SystemSpeedIndex<28)
{
++SystemSpeedIndex;
SetSpeed(SystemSpeedIndex);
}
else
{
}
}
}
//***********************************************************************************
main()
{
unsigned char Key;
InitialCPU();
InitialTimer0();
while(1)
{
Key = GetKey();
if(Key!=0x00)
{
KeyDispose(Key);
}
}
}
|