//ICC-AVR application builder : 2013/8/28 9:07:52
// Target : M128
// Crystal: 11.059Mhz
#include <iom128v.h>
#include <macros.h>
#include <math.h>
#include <stdio.h>
void delay(unsigned int ms);
void delay_ms(unsigned int ms);
#define run_led_h PORTF|=(1<<PF7) //置高电平
#define run_led_l PORTF&=~(1<<PF7) //拉低电平
#define comm_led_h PORTF|=(1<<PF6) //置高电平
#define comm_led_l PORTF&=~(1<<PF6) //拉低电平
void delay(unsigned int ms)
{
unsigned int i;
for(i=0;i<ms;i++)
{
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
}
}
void delay_ms(unsigned int ms)
{
unsigned int i;
for(i=0;i<ms;i++)
{
delay(780);
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();_NOP();
WDR();
}
}
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x03;
PORTC = 0xFF; //m103 output only
DDRC = 0xFF;
PORTD = 0xFF;
DDRD = 0xFF;
PORTE = 0xFE;
DDRE = 0xFE;
PORTF = 0x5F;
DDRF = 0xFF;
PORTG = 0x00;
DDRG = 0x00;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: (Reserved)
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x16;
UBRR0L = 0x47; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
/*
//Watchdog initialize
// prescale: 2048K
void watchdog_init(void)
{
WDR (); //this prevents a timeout on enabling
WDTCR |= (1<<WDCE) | (1<<WDE);// 30-Oct-2006 Umesh
WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
WDTCR = 0x00;
}
*/
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
PORTF = 0xEF;
DDRF = 0xFF;
uart0_init();
MCUCR = 0x00;
MCUCSR=0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
//SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
//CLI();
init_devices();
while(1) //在端口初始化中,F口是5F,有2个LED灯亮,主程序中将LED
//全熄灭。但实际只有2个灯一直亮,似乎进不到主程序中。
{
PORTF = 0xFF;
DDRF = 0xFF;
delay(2000);
PORTF = 0x0F;
DDRF = 0xFF;
delay(2000);
}
}
|