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

📄 键控led亮度.txt

📁 一个关于AVR单片机的例程
💻 TXT
字号:
//ICC-AVR application builder : 2007-4-18 12:46:03  
// Target : M16  
// Crystal: 4.0000Mhz  

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

#define uchar unsigned char 
#define uint unsigned int 

void port_init(void); 
void timer0_init(void); 
void init_devices(void); 
void delay_short(uint t); 
uchar scan_key(void); 


void port_init(void)  
{  
 PORTA = 0x00;  
 DDRA  = 0x00;  
 PORTB = BIT(PB3);  
 DDRB  = BIT(PB3);  
 PORTC = 0x00; //m103 output only  
 DDRC  = 0x00;  
 PORTD = 0x00;  
 DDRD  = 0x00;  
}  

// WGM: PWM Phase correct 
// desired value: 1KHz 
// actual value:  0.980KHz (-2.0%) 
void timer0_init(void)  
{  
 TCCR0 = 0x00; //stop  
 TCNT0 = 0x01; //set count  
 OCR0  = 0xFF;  //set compare  
 TCCR0 = 0x62; //start timer ; 相位修正, 8分頻 
}  

//call this routine to initialize all peripherals  
void init_devices(void)  
{  
 //stop errant interrupts until set up  
 CLI(); //disable all interrupts  
 port_init();  
 timer0_init();  

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

void delay_short(uint t) // 短延時 
{ 
  uint i; 
  for (i=0;i<t;i++); 
} 

uchar scan_key(void)  // 按鍵掃瞄 
{  
  uchar v; 
   
  v = 0;      
   
  if ((PIND & 0x07) != 0x07) 
  { 
  
  if ((PIND & 0x01) == 0)  
  { 
   v = 1; 
    delay_short(1000);   
  } 
   
  if ((PIND & 0x2) == 0)  
  { 
    v = 2; 
    delay_short(1000);   
  } 
    
  if ((PIND & 0x4) == 0)  
  { 
    v = 3; 
    delay_short(1000);   
  } 
  }; 
  while((PIND & 0x07) != 0x07);   // 判斷按鍵是不是放開    
  return v;   
} 

//  
void main(void)  
{   
 uchar key, OCR0_V; 
  
 init_devices();  
 OCR0_V = 0xff; 
  
 while(1) 
 { 
   key = scan_key(); 
    
   if (key > 0) 
   { 
     if (key==1) // 減少占空比 
    {  
      OCR0_V -= 10; 
      OCR0 = OCR0_V; 
    } 
     
     if (key==2) // 增加占空比 
    {  
      OCR0_V += 10; 
      OCR0 = OCR0_V; 
    }     
     
     if (key==3) // 全黑,占空比为100%  
    {  
      OCR0_V = 0xff; 
      OCR0 = OCR0_V; 
    }  
   } 
 }
}  

⌨️ 快捷键说明

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