led00.c

来自「这是关于AVR单片机学习的初步开发」· C语言 代码 · 共 125 行

C
125
字号
//Designed by BUAA Alf

//I/O演示程序
//程序流程:全亮->全灭->PD隔一步进->全亮->全灭->PD隔二步进->全亮->全灭->PB全亮->pb0置位->pb0清零->PB0反转->全灭->(循环)

// Target : M8
// Crystal: 11.059Mhz

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

//起始全亮
void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0xFF;
 PORTC = 0x00; 
 DDRC  = 0x7F;
 PORTD = 0x00;
 DDRD  = 0xFF;
}

//延时函数,大约1ms;
void delay(char tim)
{
 unsigned int i,j;
 for(i=0;i<tim;i++)
 	for(j=0;j<10000;j++);
}

//led全亮
void led_on(void)
{
 PORTB = 0x00;
 PORTC = 0x00;
 PORTD = 0x00;
 
 delay(5000);
}
//led全灭
void led_off(void)
{
 PORTB = 0xFF;
 PORTC = 0xFF;
 PORTD = 0xFF;
 
 delay(5000);
}
//PB隔1步进
void pd_1(void)
{
 char i;
 for (i = 0; i < 8; i++)
 {
  	 PORTB = ~(1 << (i));//位操作结合移位操作
	 delay(5000);
 }
}
//PB隔2步进
void pd_2(void)
{
 char i;
 for (i = 0; i < 8; i+=2)
 {
  	 PORTB = ~(1 << (i));//位操作结合移位操作
	 delay(5000);
 }
}

//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 main(void)
{
 init_devices();
 
 while(1)
 {
  led_on();
  led_off();
  
  led_on();
  led_off();
  
  
  pd_1();
  
  led_on();
  led_off();
  
  
  pd_2();
  
  led_on();
  led_off();
  
  
  //赋值(会给所有的位以特定值),使pb0为0,led亮;
  PORTB = 0xFE;
  delay(5000);
  //置位(不影响其他位),使pb0为1,led灭;
  PORTB |= 0x01;
  delay(5000);
  //清零(不影响其他位),使pb0为0,led亮; 
  PORTB &= ~0x01;
  delay(5000);
  //反转(不影响其他位),使pb0为1,led灭;
  PORTB ^= 0x01;
  delay(5000);
 }
}

⌨️ 快捷键说明

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