⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个关于AVR单片机的例程
💻 C
字号:
//ICC-AVR application builder : 2007-1-29 10:04:38
// Target : M16
// Crystal: 7.3728Mhz
// 作者:古欣
// AVR与虚拟仪器 http://www.avrvi.com

#include <iom16v.h>
#include <macros.h>

#define led_port PORTA
//定义输出端口
#define led_ddr DDRA
//定义输出控制寄存器
//可以自行修改,必须保持 led_port和led_ddr一致,PORTA对应DDRA



void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 led_ddr=0xff; //设置LED的端口为输出
}

//call this routine to initialize all peripherals
//此处为ICC系统生成,未做更改
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
}

void Delay(void)  //延时,没有详细计算
{
unsigned char i,j;
for(i=20;i>0;i--)
{
  for(j=200;j>0;j--)
;
}
}

void main()
{
 unsigned char i,j,k; //定义变量
 init_devices();      //初始化
 while(1)
 {
     i=0x80;
     for (j=0;j<8;j++) //循环8次,即PA0~~PA7轮流闪亮 
      {
        led_port=~i; //反相输出,低电平有效 位操作指令详解:http://bbs.avrvi.com/read.php?fid=30&tid=392&toread=1 
        for (k=0;k<10;k++) Delay(); //延时 可自行调节
        i=i>>1; //左移一位 移位算法详解:http://bbs.avrvi.com/read.php?fid=5&tid=897&toread=1
        // 0b00000001 PA0
        // 0b00000010 PA1
        // 0b00000100 PA2
        // 0b00001000 PA3
        // 0b00010000 PA4
        // 0b00100000 PA5
        // 0b01000000 PA6
        // 0b10000000 PA7
      }
	  i=1;
	 for (j=0;j<8;j++) //循环8次,即PA0~~PA7轮流闪亮 
      {
        led_port=~i; //反相输出,低电平有效 位操作指令详解:http://bbs.avrvi.com/read.php?fid=30&tid=392&toread=1 
        for (k=0;k<10;k++) Delay(); //延时 可自行调节
        i=i<<1; //左移一位 移位算法详解:http://bbs.avrvi.com/read.php?fid=5&tid=897&toread=1
        // 0b00000001 PA0
        // 0b00000010 PA1
        // 0b00000100 PA2
        // 0b00001000 PA3
        // 0b00010000 PA4
        // 0b00100000 PA5
        // 0b01000000 PA6
        // 0b10000000 PA7
      }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -