//ICC-AVR application builder : 2009-7-2 19:43:32
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
/******************数据类型宏定义******************/
#define uchar unsigned char
#define uint unsigned int
/******************数码管和LED常量******************/
#define LEDLK PA2 //LED锁存器控制端
#define SEGLK PA3 //数码管段选锁存器控制端
#define BITLK PA4 //数码管位选锁存器控制端
#define TRUE 1
#define FALSE 0
#define ALLLED 0XFF
/******************按键常量******************/
#define KEY_DDR DDRD //按键方向定义
#define KEY_PORTO PORTD //按键断口输出定义
#define KEY_PORTI PIND //按键断口输入定义
#define OUT 0xFF //输出常量
#define IN 0xC3 //输入常量11000011
int y;
/*******************************************
函数名称: Delayus
功 能: 延时指定微秒(8M晶振)
参 数: US--延时的微秒数(大约,不是很精确,MS越大越准确)
返回值 : 无
/********************************************/
void Delayus(uint US)
{
uint i;
US=US*5/4; //5/4是在8MHz晶振下,通过软件仿真反复实验得到的数值
for( i=0;i<US;i++);
}
/*******************************************
函数名称: Delayms
功 能: 延时指定毫秒(8M晶振)
参 数: MS--延时的毫秒数
返回值 : 无
/********************************************/
void Delayms(uint MS)
{
uint i,j;
for( i=0;i<MS;i++)
for(j=0;j<1141;j++); //1141是在8MHz晶振下,通过软件仿真反复实验得到的数值
}
/*******************************************
函数名称: Board_init
功 能: 初始化实验板,关闭全部功能
参 数: 无
返回值 : 无
/********************************************/
void Board_init(void)
{
DDRA=0xFF; //将四个IO口全部配置为输出
DDRB=0xFF;
DDRC=0xFF;
DDRD=0xFF;
SPCR=0x00; //还原SPI口,使其为正常端口
PORTC&=~BIT(PC6); //关闭SPI总线DA
PORTB=0xFF; //关闭LED
PORTA|=BIT(LEDLK); //锁存数据,使LED熄灭
Delayus(5);
PORTA&=~BIT(LEDLK);
PORTB=0xFF; //输出段选
PORTA|=BIT(SEGLK);
Delayus(50);
PORTA&=~BIT(SEGLK);
PORTB=0x00; //输出位选
PORTA|=BIT(BITLK);
Delayus(50);
PORTA&=~BIT(BITLK);
}
/*******************************************/
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init(); //TC1初始化
MCUCR = 0x00;
GICR = 0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
Board_init();
}
void delay_ms(unsigned int time)
{
unsigned char c;
for(;time;time--)
{
for(c=400;c;c--);
}
}
/***************TC1初始化*******************/
void timer1_init(void)
{
TCCR1A = 0xA2; //两路PWM,匹配清零
TCCR1B = 0x19; //快速PWM模式,位数可调,预分频1
ICR1 = 0xFFFF; //计数上限值,此数为16位PWM
}
//
/*******************************************
函数名称: main
功 能: 完成初始化,等待TIMER0中断
参 数: 无
返回值 : 无
/********************************************/
void main(void)
{
unsigned int a=32768,b=32768;
init_devices();
//insert your functional code here...
OCR1A=a; //匹配初值
OCR1B=b;
PORTA|=BIT(BITLK) ;
while(1)
{
a-=10; //两个值向不同方向变化
b+=10; //PWMA宽度减小,PWMB宽度增加
if(a<=20) //上下限
{
a=65530;
b=5;
}
OCR1A=a;
OCR1B=b;
delay_ms(10);
}
}