Attiny 2313A || PWM signal for DC motor control using RC receiver (Graupner 400)

72 Views Asked by At

I'm writing program in Microchip Studio to control DC 6V engine (graupner 400 max 4,0A) using RC radio receiver. I'm using Attiny 2313A with F_CPU 16 MHz.

RC receiver sends PWM signal to MCU on PD2 pin.

MCU must sends PWM signal to PINs PB3 + PB2 Engine turn left PB4 + PC5 Engine turn right

To control motor I would like to generate a PWM signal with a frequency of 1 kHz.

Below is my program and errors during compilation. Thanks a lot!

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000L
#define A PB3
#define D PB2
#define B PB4
#define C PD5

//Fast PWM
void PWM_100p_PRZOD()
{
    TCCR0A = (1<<COM0A1) | (1 << WGM02) | (1 << WGM01) | (1 << WGM00);
    TCCR1A = (1<<COM1A1) | (1 << WGM12) | (1 << WGM10);
    OCR0A= 255;
    OCR1A= 65535;
}

void PWM_0p_TYL()
{
    TCCR0B = (1<<COM0B1) | (1 << WGM02) | (1 << WGM01) | (1 << WGM00) | (1 << CS01);
    TCCR1B = (1<<COM1B1) | (1 << WGM12) | (1 << WGM10);
    OCR0B= 0;
    OCR1B= 0;
}
int main(void)
{
    DDRB |= (1 << DDB2) | (1 << DDB3) | (1 << DDB4);
    DDRD |= (1 << DDD5);
    PORTB &= ~((1 << A) | (1 << D) | (1 << B));
    PORTD &= ~(1 << C);
    DDRD &= ~(1 << PD2);
    PORTD &= ~(1 << PD2);
    
    while(1)
    {
        PWM_100p_PRZOD();
        _delay_ms(100);
    }
    return 0;
}
        }
        _delay_ms(100);
    }
    return 0;
}

errors

'ADMUX' undeclared (first use in this function)
'REFS0' undeclared (first use in this function)
'ADCSRA' undeclared (first use in this function)
'ADEN' undeclared (first use in this function)
'ADPS2' undeclared (first use in this function)
'ADPS1' undeclared (first use in this function)
'ADPS0' undeclared (first use in this function)
'ADMUX' undeclared (first use in this function)
'ADCSRA' undeclared (first use in this function)
'ADSC' undeclared (first use in this function)
'ADC' undeclared (first use in this function)
#error "Attempt to include more than one <avr/ioXXX.h> file."
#warning "F_CPU not defined for <util/delay.h>" [-Wcpp]
recipe for target 'main

enter image description here

2

There are 2 best solutions below

0
Peter Plesník On

You probably have the project properties set incorrectly. Select Project > Properties and check the Device setting. The programmer is a separate application that is only launched from the IDE and its settings can be completely different from the project being compiled. Therefore, mentioning it when searching for the causes of a compilation error is completely irrelevant.

I test compile your code a I get totally different errors My compilation result

0
Julius On

Thank you I managed to compile the program and upload it to the MCU. The current situation is that the MCU, regardless of the position of the lever on the remote control, sends a signal to pin D PB2 4.7V. On other pins A PB3, B PB2, C PD5 voltage is 0V.

I look at the catalog note of ATtiny 2313, I modeled on other programs using ATtiny 2313 to generate PWM signal, I watched tutorials about registers used in AVR. But there's still something wrong with the program and the engine's not running at all. Thank you very much in advance for your help.

Below is the code.

#define F_CPU 16000000L
#define MCU attiny2313
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define A PB3
#define D PB2
#define B PB4
#define C PD5
volatile uint16_t R = 0;
volatile uint8_t R0 = 0;
volatile uint16_t R1 = 0;
void measurePWM() {
    TCNT1 = 0;
    while (!(PIND & (1 << PD2)));
    while (PIND & (1 << PD2));
    R = TCNT1;
}
int main(void) {
TCCR0A = (1 << COM0A1) | (1 << WGM02) | (1 << WGM01) | (1 << WGM00);
TCCR1A = (1 << COM1A1) | (1 << WGM12) | (1 << WGM10);
TCCR0B = (1 << COM0B1) | (1 << WGM02) | (1 << WGM01) | (1 << WGM00) | (1 << CS01);
TCCR1B = (1 << COM1B1) | (1 << WGM12) | (1 << WGM10);
    DDRB |= (1 << DDB2) | (1 << DDB3) | (1 << DDB4);
    DDRD |= (1 << DDD5);
    PORTB &= ~((1 << A) | (1 << D) | (1 << B));
    PORTD &= ~(1 << C);
    DDRD &= ~(1 << PD2);
    PORTD &= ~(1 << PD2);
    while (1) {
        measurePWM();
        R0 = (R - 958) * 255 / (1815 - 958);
        R1 = (R - 958) * 65534 / (1815 - 958);
        if (R >= 1500) {
            OCR0A = 0;
            OCR1A = 0;
            OCR0B = R0;
            OCR1B = R1;
            } else if (R <= 1300) {
            OCR0A = 0;
            OCR1A = 0;
            OCR0B = R0;
            OCR1B = R1;
            } else {
            OCR0A = 0;
            OCR0B = 0;
            OCR1A = 0;
            OCR1B = 0;
        }
        _delay_ms(100);
    }
    return 0;
}