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

📄 main.c

📁 ATmega88 + 舵机 程序例子
💻 C
字号:
/*******************************************************************************
OKM-BLDC-SERVO-MOTOR-10W
作者:Alan, 2008-5-3
最后修改:Alan,2008-5-3
芯片:ATMEGA88,
编译器:avr-gcc (GCC) 4.1.2 (WinAVR 20070525)
版本说明:
V0.1  初版
*******************************************************************************/
#define _MAIN_C_H_
#include "globals_var.h"

inline void IoInit(void)
{
    DDRC=(1<<PC3)|(1<<PC4)|(1<<PC5);
    //PC3  AH   PC4 BH   PC5 CH
    PORTC=(1<<PC3)|(1<<PC4)|(1<<PC5);

    DDRB=(1<<PB4)|(1<<PB1)|(1<<PB0);
    //PB0  A_L  PB4  LED   PB1  PWM out
    PORTB=0;
    
    DDRD=(1<<PD5)|(1<<PD6)|(1<<PD7);
    //PD5   PWM_BOOST  PD6  B_L     PD7 C_L    
    PORTD=0;
}

inline void Timer2Init(void)
{
    TCCR2A=(1<<WGM21);
    TCCR2B=(1<<CS22)|(1<<CS21)|(1<<CS20);
    OCR2A=96;
    TIMSK2=(1<<OCIE2A);
    TIFR2=(1<<OCF2A);
}

SIGNAL(TIMER2_COMPA_vect)
{
    static uint timercnt=0;
    uchar i;
    if (timercnt>500)
    {
        timercnt=0;
        PORTB^=(1<<PB4);
        if ((vMotorStateFlag&(1<<fMotorStart))==0)
        {
            vMotorStateFlag|=(1<<fMotorStart);
            i=PINC&0x07;
            if (vMotorFwd==0)
            {
                i=pgm_read_byte(&cFwdOutPut[i]);
            }
            else
            {
                i=pgm_read_byte(&cRevOutPut[i]);
            }
            PORTC|=(1<<PC3)|(1<<PC4)|(1<<PC5);
            PORTB&=(~(1<<PB0));
            PORTD&=(~((1<<PD6)|(1<<PD7)));
            PORTC&=(i|0xc7);
            PORTD|=(i&0xc0);
            PORTB|=(i&0x01);
            TCCR1A|=(1<<COM1A1);
        }
/*
        DEBUG_OUT('P');
        DEBUG_OUT(':');
        DEBUG_PutHex(PINC&0x07);
        if ((PORTC&0x08)==0)
        {
            DEBUG_OUT('A');
            DEBUG_OUT('H');
            DEBUG_OUT(' ');
        }
        if ((PORTC&0x10)==0)
        {
            DEBUG_OUT('B');
            DEBUG_OUT('H');
            DEBUG_OUT(' ');
        }
        if ((PORTC&0x20)==0)
        {
            DEBUG_OUT('C');
            DEBUG_OUT('H');
            DEBUG_OUT(' ');
        }
        if ((PORTB&0x01)!=0)
        {
            DEBUG_OUT('A');
            DEBUG_OUT('L');
            DEBUG_OUT(' ');
        }
        if ((PORTD&0x40)!=0)
        {
            DEBUG_OUT('B');
            DEBUG_OUT('L');
            DEBUG_OUT(' ');
        }
        if ((PORTD&0x80)!=0)
        {
            DEBUG_OUT('C');
            DEBUG_OUT('L');
            DEBUG_OUT(' ');
        }
        DEBUG_OUT(0x0d);
        DEBUG_OUT(0x0a);
*/
    }
    else
    {
        timercnt+=1;
    }
}

//TIMER 0 INIT
inline void PWMBOSTInit(void)
{
    TCCR0A=(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
    TCCR0B=(1<<CS02)|(1<<WGM02);
    OCR0A=30;
    OCR0B=15;
}

inline void HallInputInit(void)
{
    PCICR=(1<<PCIE1);
    PCMSK1=(1<<PCINT8)|(1<<PCINT9)|(1<<PCINT10);
}

SIGNAL(PCINT1_vect)
{
    uchar i=PINC&0x07;

    if (vMotorFwd==0)
    {
        i=pgm_read_byte(&cFwdOutPut[i]);
    }
    else
    {
        i=pgm_read_byte(&cRevOutPut[i]);
    }
    PORTC|=(1<<PC3)|(1<<PC4)|(1<<PC5);
    PORTB&=(~(1<<PB0));
    PORTD&=(~((1<<PD6)|(1<<PD7)));
    PORTC&=(i|0xc7);
    PORTD|=(i&0xc0);
    PORTB|=(i&0x01);
}

inline void PWMCurtInit(void)
{
    OCR1AH=(200/256);
    OCR1AL=(200%256);
    ICR1H=(500/256);
    ICR1L=(500%256);
    TCCR1A=(1<<WGM11);
    TCCR1B=(1<<WGM12)|(1<<WGM13)|(1<<CS11);
}

int main( void )
{
    volatile uchar i;
    
    cli();
	IoInit();
	
    DEBUG_Init();
    
    PWMCurtInit();
    PWMBOSTInit();
    HallInputInit();
    Timer2Init();
    vMotorFwd=0; 
    vMotorStateFlag=0;
    
    sei();//总中断允许
    DEBUG_OUT(0x0d);
    DEBUG_OUT(0x0a);
    
    i=PINC&0x07;
    if (vMotorFwd==0)
    {
        i=pgm_read_byte(&cFwdOutPut[i]);
    }
    else
    {
        i=pgm_read_byte(&cRevOutPut[i]);
    }
    PORTC|=(1<<PC3)|(1<<PC4)|(1<<PC5);
    PORTB&=(~(1<<PB0));
    PORTD&=(~((1<<PD6)|(1<<PD7)));

	while(1)
	{
        ;
	}
}

⌨️ 快捷键说明

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