/*******************************************************/
//ProName:FL
//ICC-AVR-7.22
// MCU : ATmega128
// F_CPU: 11.0592MHz
//Author:Owen
//2018-04-17-Ver.A0.0
/******************************************************/
/*********************Include Files********************/
#include <iom128v.h>
#include <macros.h>
/**********************Definitions *******************/
#define uchar unsigned char
#define uint unsigned int
#define ReadPD(x) ((PIND&(1<<x)) ? 1 : 0)
#define ReadPE(x) ((PINE&(1<<x)) ? 1 : 0)
uchar WDG_Flag = 0;
uint Run_LED_Flag=0;
uchar const LED_tab[]={0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f};
/********************RUN-LED ON/OFF ********************/
#define RUN_LED_ON PORTB &= ~(1 << PB0)
#define RUN_LED_OFF PORTB |= (1 << PB0)
/******************WatchDog ON/OFF *******************/
#define WDG_ON PORTB &= ~(1 << PB7)
#define WDG_OFF PORTB |= (1 << PB7)
/**********************Delay_ms*********************/
void Delay_ms(uint z)
{
uint i=0,j=0;
for(i=0;i<z;i++)
{
for(j=1228; j>0; j--);
}
}
/******************I/O Port Init********************/
void M128_port_init(void)
{
DDRA = 0xFF; //Fish_LED_Port
PORTA = 0xFF;
DDRB = 0x81; //PB0:RUN_LED,PB7:WatchDog
PORTB = 0x00;
DDRD = 0xF8; // PD0:BLUE_in, PD1:GREEN_in, PD2:RED_in
PORTD = 0x07; //
DDRE = 0x1F; //PE5:IN3 PE6:IN2 PE7:IN1
PORTE = 0xE0;
}
/***************Timer0(1mSec)**********************/
void Timer0_init(void)
{
TCCR0 = 0x00;
TCNT0 = 0x54;
OCR0 = 0xAC;
TIMSK = 0x01;
TCCR0 = 0x04;
}
/******************Timer0 interrupt*****************/
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
if(WDG_Flag == 0) //Reset WatchDog
{
WDG_Flag = 100;
WDG_ON;
}
else if(WDG_Flag == 50)
{
WDG_OFF;
}
WDG_Flag--;
if(Run_LED_Flag == 0) //Run LED 500ms
{
Run_LED_Flag=1000;
RUN_LED_ON;
}
else if(Run_LED_Flag == 500)
{
RUN_LED_OFF;
}
Run_LED_Flag--;
TCNT0 = 0x54;
}
/*****************Fish_LED*******************/
void Fish_LED(void)
{
uchar a;
for(a=0;a<9;a++)
{
PORTA=LED_tab[a];
Delay_ms(240);
}
}
/*****************Fish_LED_Action***************/
void Fish_LED_Action(void)
{
uchar a;
if(ReadPD(1)==0)
{
for(a=0;a<9;a++)
{
PORTA=LED_tab[a];
Delay_ms(235);
}
}
else
{
PORTA=0xFF;
}
}
/*****************System Init********************/
void M128_MCU_init(void)
{
CLI(); //Disable interrupts
M128_port_init();
Timer0_init();
SEI(); //Enable interrupts
}
/**********************Main**********************/
int main(void)
{
M128_MCU_init();
Fish_LED();
while(1)
{
Fish_LED_Action();
}
return 0;
}
评论0