/*
* EX1031112.cpp
*
* Created: 2014/11/12 下午 09:14:16
* Author: Axon
*/
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
void my_delay_ms( unsigned int delay)
{
while(delay--)
{
_delay_ms(1);
}
}
int main(void)
{
// 宣告變數
enum { STATE0, HOLD0, STATE1, HOLD1, PAUSE, CHANGSPEED};
enum { SPEED0 = 100, SPEED1 = 100};
int STATE;
int OLDSTATE;
int SPEED;
bool LoopButtonFlag;
//bool PauseButtonFlag;
// 初始化IO
DDRA = 0xFF; // 定義Port A為輸出阜,並初始化 LED 燈全滅
PORTA = 0xFF;
DDRC = 0x00; // 定義Port C為輸入阜
STATE = STATE0; // 初始化狀態
SPEED = SPEED0;
LoopButtonFlag = false;
//PauseButtonFlag = false;
while(1)
{
switch(STATE)
{
case STATE0:
PORTA = (PORTA >> 1) & 0x7F;
my_delay_ms(SPEED);
if( PORTA == 0x00) // 是否達轉態條件
STATE = HOLD0;
break;
case HOLD0:
my_delay_ms(100);
PORTA = 0xFF;
STATE = STATE1;
break;
case STATE1:
PORTA = (PORTA << 2) & 0xFC;
my_delay_ms(SPEED);
if( PORTA == 0x00) // 是否達轉態條件
STATE = HOLD1;
break;
case HOLD1:
my_delay_ms(100);
PORTA = 0xFF;
STATE = STATE0;
break;
case PAUSE:
break;
case CHANGSPEED:
break;
}
// 檢查按鍵
if( PINC0 == 0)
LoopButtonFlag = true;
if( LoopButtonFlag == true && PINC0 == 1)
{
if( STATE == PAUSE)
{
STATE = OLDSTATE;
}
else
{
OLDSTATE = STATE;
STATE = PAUSE;
}
LoopButtonFlag = false;
}
}
}
|