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

📄 -ͦ

📁 手把手教你学AVR单片机C程序设计实验程序
💻
字号:
#include <iom16.h>
#define uint unsigned int
#define uchar unsigned char
uint x;
uchar Flag;
/*************************/
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 500uSec
// actual value: 500.000uSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFE; //setup
 TCNT1L = 0x0C;
 OCR1AH = 0x01;
 OCR1AL = 0xF4;
 OCR1BH = 0x01;
 OCR1BL = 0xF4;
 ICR1H  = 0x01;
 ICR1L  = 0xF4;
 TCCR1A = 0x00;
 TCCR1B = 0x02; //start Timer
}
/**************************/
void port_init(void)
{
 PORTB = 0xff;
 DDRB  = 0xff;
}
/***************************/
void init_devices(void)
{
 //stop errant interrupts until set up
 port_init();
 timer1_init();
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x04; //timer interrupt sources
 SREG=0x80;
 //all peripherals are now initialized
}
/************************/
class Timer
{
public:
  Timer(uint t);
  ~Timer();
  uint input_mtime(void);
  void output_precess(uchar Nub,uint ON_Length,uint OFF_Length);
    
private:
  uint ms;
};
/*****************************/
uint Timer::input_mtime(void)
{
  ms=ms+x;
  return ms;
}
/*****************************/
void Timer::output_precess(uchar Nub,uint ON_Length,uint OFF_Length)
{
  if((ms>0)&&(ms<=ON_Length))
    PORTB&=~(1<<Nub);
  else if((ms>ON_Length)&&(ms<=(ON_Length+OFF_Length)))
    PORTB|=(1<<Nub);
  else if(ms>(ON_Length+OFF_Length))
    {ms=0;PORTB|=(1<<Nub);}
 }
/*****************************/
Timer::Timer(uint t)
{
  ms=t;
}
/*****************************/
Timer::~Timer()
{
  ms=0;
}
/*****************************/
int main(void)
{
  Timer TimerB[8]={0,0,0,0,0,0,0,0};
  init_devices();
  while(1)
  {
    if(Flag==1)
    {
      TimerB[0].input_mtime();
      TimerB[0].output_precess(0,1,1000);
      //**************************
      TimerB[1].input_mtime();
      TimerB[1].output_precess(1,30000,30000);
      //**************************
      TimerB[2].input_mtime();
      TimerB[2].output_precess(2,10000,10000);
      //**************************
      TimerB[3].input_mtime();
      TimerB[3].output_precess(3,100,10000);
      //**************************
      TimerB[4].input_mtime();
      TimerB[4].output_precess(4,500,10000);
      //**************************
      TimerB[5].input_mtime();
      TimerB[5].output_precess(5,1000,10000);
      //**************************
      TimerB[6].input_mtime();
      TimerB[6].output_precess(6,1000,1000);
      //**************************
      TimerB[7].input_mtime();
      TimerB[7].output_precess(7,100,100);
      //**************************
      Flag=0;
    }
  }
}
/***************************/
#pragma vector=TIMER1_OVF_vect
__interrupt void timer1(void)
{
 TCNT1H = 0xFE; //reload counter high value
 TCNT1L = 0x0C; //reload counter low value
 Flag=1;
 x++;
 if(x>1)x=0;
}





⌨️ 快捷键说明

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