User Tools

Site Tools


servomoteur

Servomoteur

Actuellement, le code est super crade et pas du tout optimisé. De plus, on utilise pas la sortie PWM des attiy. Je referais plus tard quelques fonctions/libs pour piloter des servos modifiés ou non.

Code source

/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <frank[ASCII:64]villaro-dixon.eu> wrote this file. As long as you retain this
 * notice, you can do whatever you want with this stuff. If we meet some day,
 * and you think this stuff is worth it, you can buy me a beer in return.
 * Frank Villaro-Dixon
 * ----------------------------------------------------------------------------
 */
#include <avr/io.h>
#include <util/delay.h>
#define _BV(bit) (1 << (bit))
 
#define SERVO PB0 //E - SERVO
#define BOUTON1  PB1 //S - Interrupteur.
#define BOUTON2  PB2 //S - Interrupteur.
 
void allumer(int port)
{
    PORTB |= 1<<port;
    asm("NOP");
}
 
void eteindre(int port)
{
    PORTB &= ~(1<<port);
    asm("NOP");
}
 
void delayMS(int temps)
{
    while(temps > 0)
    {
        _delay_us(8);
        temps--;
    }
}
 
int main(void)
{
 
    /*LES I/O */
    DDRB |= 1<<SERVO; //EN S
    DDRB |= _BV(BOUTON1); //EN E
    DDRB |= _BV(BOUTON2); //EN E
 
    /* LES VAR */
 
    volatile unsigned int dureeSignalServo = 6;//en temps
    volatile unsigned int dureeSignalTotal = 180;//en en temps
    volatile unsigned int dureeSignalEtatBas = dureeSignalTotal - dureeSignalServo;
    volatile int etatBouton1 = bit_is_set(PINB, BOUTON1);
    volatile int etatBouton2 = bit_is_set(PINB, BOUTON2);
 
 
    /*LE LOOP*/
    while(1)
    {
        allumer(SERVO);
        delayMS(dureeSignalServo);
        eteindre(SERVO);
        delayMS(dureeSignalEtatBas);
        dureeSignalEtatBas = dureeSignalTotal - dureeSignalServo;
        if(bit_is_set(PINB, BOUTON1) != etatBouton1)
        {
            if(dureeSignalServo < 25)
                dureeSignalServo++;
            etatBouton1 = bit_is_set(PINB, BOUTON1);
        }
 
        if(bit_is_set(PINB, BOUTON2) != etatBouton2)
        {
            if(dureeSignalServo > 6)
                dureeSignalServo--;
            etatBouton2 = bit_is_set(PINB, BOUTON2);
        }
 
    }
 
 
    return 0;
}

Discussion

Enter your comment. Wiki syntax is allowed:
ORROH
 
servomoteur.txt · Last modified: 2011/12/26 20:51 (external edit)