×
单片机 > 单片机程序设计 > 详情

AVR之 ICC 与 winavr

发布时间:2020-08-24 发布时间:
|
ICC程序:


#include
#include
#define setb(val,bitn) (val|=(1< #define clrb(val,bitn) (val&=~(1< int main( void )
{
init_devices();
while(1){
setb(PORTB,0);
delay(1000);
clrb(PORTB,0);
delay(1000);

return 0;
}
void delay(unsigned int n)
{
   unsigned int i;
   for(i=0;i }

void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x01;
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();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

 

 

winavr程序:1. 增加 #include 和#include

                            1) WinAVR中延时函数可使用“#include ”头文件中的延时函数:

                      void _delay_us (double __us); //微秒级 
                      void _delay_ms (double __ms); //毫秒级

                   2) WinAVR中全局中断操作使用:

                    “cli(); //禁止所有中断”和“sei();  //开全局中断”,

                     它在“”中定义,需将其包含;而ICC中为大写:

                     “CLI(); //禁止所有中断”和“SEI();  //开全局中断”。

 

               2. 修改 #include -----》#include
             3. 去掉 delay()函数。



#include
#include
#include

#define setb(val,bitn) (val|=(1< #define clrb(val,bitn) (val&=~(1<

void init_devices(void);

int main( void )
{
init_devices();
while(1){
setb(PORTB,0);
_delay_ms(1000);
clrb(PORTB,0);
_delay_ms(1000);

return 0;
}


void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x01;
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();

 

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}

WinAVR和ICCAVR最大的不同在于它需要编写单独的Makefile文件,Makefile文件可用“MFile [WinAVR]”工具生成,稍作更改即可,而通常情况下只需更改以下几项:

①芯片类型,此例为“MCU = atmega16”

②芯片工具频率,此例为“F_CPU = 3686400”

③编译输入烧录文件格式,此例为“FORMAT = ihex”(默认)

④目标文件名,此例为“TARGET = main”(默认)

⑤C源程序清单,需以空格隔开,此例为“SRC = $(TARGET).c port_init.c led.c key_scan.c”,默认为“SRC = $(TARGET).c”,需依据程序结构进行添加。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机中断源的扩展方法