微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > MCU和DSP > 风速传感器程序

风速传感器程序

时间:02-29 来源:互联网 点击:
/***************************************************
Chip type           : ATmega8
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256   
***************************************************/
#include <mega8.h>
#include <delay.h>  
#include <math.h>#define T1OCR1AH  0x07     
#define T1OCR1AL  0xCFunsigned char Timer0Counter; //一个脉冲周期溢出的次数
unsigned int T;
void InitDevice(void);
void InitWork(void);
void main(void)
{   
   unsigned int Ocr1aValue;
   delay_ms(500);
   InitDevice();
   Timer0Counter=153;
   T=39060;
   while(1)
   {   
      #asm("wdr")
      if(T<78)T=78;
      if(T>=39060)Ocr1aValue=1999;
      else Ocr1aValue=(long)2000*T/(312+T)-1;
      OCR1AH=Ocr1aValue>>8;
      OCR1AL=Ocr1aValue;  
   }
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
   unsigned char data;
   data=TCNT0;TCNT0=0;
   T=(unsigned int)Timer0Counter*256+data;
   Timer0Counter=0;
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
   #asm("sei")
   if(Timer0Counter<153)Timer0Counter++;
   else T=39060;
}
//****************************************************************************
//初始化程序
void InitDevice(void)
{
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=T
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xF7;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 3.906 kHz
TCCR0=0x05;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 4000.000 kHz
// Mode: Fast PWM top=OCR1A
// OC1A output: Toggle
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x43;
TCCR1B=0x19;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=T1OCR1AH;
OCR1AL=T1OCR1AL;
// External Interrupt(s) initialization
// INT0: Off
// INT1: On
// INT1 Mode: Rising Edge
GICR|=0x80;
MCUCR=0x08;
GIFR=0x80;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
WDTCR=0x1F;
WDTCR=0x0F;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Global enable interrupts
#asm("sei")   
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top