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

📄 io.c

📁 这是关于AVR单片机学习的初步开发
💻 C
字号:
//ICC-AVR application builder : 2005-3-1 6:11:41
// Target : M8
// Crystal: 11.059Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0xFF;
 DDRB  = 0x00;
 PORTC = 0x7F; //m103 output only
 DDRC  = 0x20;
 PORTD = 0xFF;
 DDRD  = 0xFF;
}

//call this routine to initialise 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 initialised
}

void delay(char time)//延时函数,单位延时0.25s。
{	unsigned int i,j,k;
	for(k=0;k<time;k++)
		for(i=0;i<1500;i++)
			for(j=0;j<100;j++);
}

void main(void)
{
 char i;
 char data;
 char led[9] = {0x3F,0x06, 0x5B, 0x4F, 0x66 ,0x66, 0x7D, 0x07, 0x7F};  	   	   			  			 //数码管码值
 init_devices();
 
 while (1)
 {		if((PINB &0x10)==0)		  			 //数码管任务;
 		{	PORTC &= ~0x20;	 					 //PC5清零
			data = (PINB & 0x0F)>>1;			 //取PB1~PB3的数据;
			PORTD = led[data];					 //查表输出数码管值;			
		}
		else	  								 //跑马灯任务;
		{	if((PINB & 0x08) == 0 )				 //只亮一个灯
			{	data = (PINB & 0x06)>>1;		 //取PB1、PB2的数据;
				for(i=0;i<8;i++)				 //低位到高位
				{	PORTD = ~(1<<i);
					delay(data);				 //延时
				}
				for(i=0;i<8;i++)				 //高位到低位
				{	PORTD = ~(0x80>>i);
						delay(data);
				}
			}
			else 					   			 //亮两个灯
			{	data = (PINB & 0x06)>>1;		 //同上
				
				for(i=0;i<8;i++)
				{	PORTD = ~(3<<i);
						delay(data);
				}
				for(i=0;i<8;i++)
				{	PORTD = ~(0xC0>>i);
						delay(data);
				}					
			}			
		}
 }
}

⌨️ 快捷键说明

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