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

📄 timer0.c

📁 此程序是无传感器无刷电机控制程序(mege8)
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "main.h"

volatile unsigned int CountMilliseconds = 0;
volatile static unsigned int tim_main;
volatile unsigned char UpdateMotor = 0;
volatile unsigned int cntKompass = 0;
volatile unsigned int beeptime = 0;
volatile unsigned char SendSPI = 0, ServoActive = 0;

unsigned int BeepMuster = 0xffff;
int ServoValue = 0;

volatile int16_t        ServoNickValue = 0;
volatile int16_t        ServoRollValue = 0;


enum {
  STOP             = 0,
  CK               = 1,
  CK8              = 2,
  CK64             = 3,
  CK256            = 4,
  CK1024           = 5,
  T0_FALLING_EDGE  = 6,
  T0_RISING_EDGE   = 7
};


SIGNAL (SIG_OVERFLOW0)    // 8kHz
{
    static unsigned char cnt_1ms = 1,cnt = 0;
    unsigned char pieper_ein = 0;
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
   if(SendSPI) SendSPI--;
   if(!cnt--)
    {
     cnt = 9;
     cnt_1ms++;
     cnt_1ms %= 2;
     if(!cnt_1ms) UpdateMotor = 1;
     CountMilliseconds++;
    }

     if(beeptime > 1)
        {
        beeptime--;
        if(beeptime & BeepMuster)
         {
          pieper_ein = 1;
         }
         else pieper_ein = 0;
        }
     else
      {
       pieper_ein = 0;
       BeepMuster = 0xffff;
      }


     if(pieper_ein)
        {
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
        }
     else
        {
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
         else                      PORTC &= ~(1<<7);
        }

 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
 {
  if(PINC & 0x10)
   {
    cntKompass++;
   }
  else
   {
    if((cntKompass) && (cntKompass < 362))
    {
     cntKompass += cntKompass / 41;
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
    }
//     if(cntKompass < 10) cntKompass = 10;
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
    cntKompass = 0;
   }
 }
}


// -----------------------------------------------------------------------

unsigned int SetDelay (unsigned int t)
{
//  TIMSK0 &= ~_BV(TOIE0);
  return(CountMilliseconds + t + 1);
//  TIMSK0 |= _BV(TOIE0);
}

// -----------------------------------------------------------------------
char CheckDelay(unsigned int t)
{
//  TIMSK0 &= ~_BV(TOIE0);
  return(((t - CountMilliseconds) & 0x8000) >> 9);
//  TIMSK0 |= _BV(TOIE0);
}

// -----------------------------------------------------------------------
void Delay_ms(unsigned int w)
{
 unsigned int akt;
 akt = SetDelay(w);
 while (!CheckDelay(akt));
}

void Delay_ms_Mess(unsigned int w)
{
 unsigned int akt;
 akt = SetDelay(w);
 while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;}
}

/*****************************************************/
/*              Initialize Timer 2                   */
/*****************************************************/
// The timer 2 is used to generate the PWM at PD7 (J7)
// to control a camera servo for nick compensation.
void TIMER2_Init(void)
{
        uint8_t sreg = SREG;

        // disable all interrupts before reconfiguration
        cli();

        // set PD7 as output of the PWM for nick servo
        DDRD  |= (1<<DDD7);
        PORTD &= ~(1<<PORTD7);  // set PD7 to low

        DDRC  |= (1<<DDC6);     // set PC6 as output (Reset for HEF4017)
    HEF4017R_ON;
        // Timer/Counter 2 Control Register A

        // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1)
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
    TCCR2A |= (1<<WGM21)|(1<<WGM20);

    // Timer/Counter 2 Control Register B

        // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz
        // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us
        // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms

    // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1)
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22));
    TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22);

        // Initialize the Timer/Counter 2 Register
    TCNT2 = 0;

        // Initialize the Output Compare Register A used for PWM generation on port PD7.
        OCR2A = 255;
        TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0

        // Timer/Counter 2 Interrupt Mask Register
        // Enable timer output compare match A Interrupt only
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
        TIMSK2 |= (1<<OCIE2A);

    SREG = sreg;
}

//----------------------------
void Timer_Init(void)
{
    tim_main = SetDelay(10);
    TCCR0B = CK8;
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
    OCR0A =  0;
    OCR0B = 120;
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
    //OCR1  = 0x00;

    TIMSK0 |= _BV(TOIE0);
}


/*****************************************************/
/*              Control Servo Position               */
/*****************************************************/

ISR(TIMER2_COMPA_vect)
{

        // frame len 22.5 ms = 14063 * 1.6 us
        // stop pulse: 0.3 ms = 188 * 1.6 us
        // min servo pulse: 0.6 ms =  375 * 1.6 us
        // max servo pulse: 2.4 ms = 1500 * 1.6 us
        // resolution: 1500 - 375 = 1125 steps

        #define IRS_RUNTIME 127
        #define PPM_STOPPULSE 188
//      #define PPM_FRAMELEN (14063
    #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh)
        #define MINSERVOPULSE 375
        #define MAXSERVOPULSE 1500
        #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE)

        static uint8_t  PulseOutput = 0;
        static uint16_t RemainingPulse = 0;
        static uint16_t ServoFrameTime = 0;
        static uint8_t  ServoIndex = 0;

⌨️ 快捷键说明

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