Forum: Mikrocontroller und Digitale Elektronik Attiny841 PWM an DRV8833


von Attiny841 (Gast)


Lesenswert?

Hallo,

ich möchte einen kleinen Miniaturmotor mit einem Texas Instrument 
DRV8833 antreiben. Dazu nutze ich einen Atmel Attiny841.

Leider wurde mir aus dem Tutorial ersichtlich, welcher PWM Modus der 
richtige ist.
https://www.mikrocontroller.net/articles/Motoransteuerung_mit_PWM

Über einen Potentiometer soll die Richtung ausgelesenw erden und in die 
Register geschrieben werden.
Nur ändert sich hier weder die Motorrichtung, noch die Drehzahl.
1
#define F_CPU      8000000
2
3
#include <avr/io.h>
4
#include <avr/interrupt.h>
5
#include <util/delay.h>
6
7
#define MOTOR_DDR    DDRA
8
#define MOTOR_PORT    PORTA
9
#define MOTOR_FW_PIN  (1<<2)
10
#define MOTOR_RW_PIN  (1<<3)
11
#define MOTOR_FW_ON    TOCPMCOE |= (1<<TOCC1OE)
12
#define MOTOR_FW_OFF  TOCPMCOE &= ~(1<<TOCC1OE)
13
#define MOTOR_RW_ON    TOCPMCOE |= (1<<TOCC2OE)
14
#define MOTOR_RW_OFF  TOCPMCOE &= ~(1<<TOCC2OE)
15
16
uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
17
{
18
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
19
}
20
21
uint16_t adc_read(uint8_t channel)
22
{
23
  ADMUXA = (ADMUXA & ~(0x1F)) | (channel & 0x1F);
24
  ADCSRA |= (1<<ADSC);
25
  while(ADCSRA & (1<<ADSC));
26
  return ADCW;
27
}
28
29
int main(void)
30
{
31
  // ADC initialiseren
32
  ADCSRA |= (1<<ADEN) | (1<<ADSC);
33
  while(ADCSRA & (1<<ADSC));
34
  (void)ADCW;
35
  
36
  
37
  // Motor initialisieren
38
  MOTOR_DDR |= MOTOR_FW_PIN | MOTOR_RW_PIN;
39
  MOTOR_PORT &= ~(MOTOR_FW_PIN | MOTOR_RW_PIN);
40
  
41
  // CLK ~3,9kHz, Phasen und Frequenz korrekt, nicht invertierend
42
  TCCR1A=(1<<COM1A1) | (1<<COM1B1) | (1<<WGM10);
43
  TCCR1B= (1<<WGM13) | (1<<CS12);
44
45
  TOCPMSA0 = (1<<TOCC2S0) | (1<<TOCC1S0);
46
  MOTOR_FW_OFF;
47
  MOTOR_RW_OFF;
48
49
  sei();
50
51
  while(1)
52
  {
53
        const uint8_t MOTOR_FW_MIN = 50;
54
        const uint8_t MOTOR_FW_MAX = 200;
55
        const uint8_t MOTOR_RW_MIN = 50;
56
        const uint8_t MOTOR_RW_MAX = 100;
57
    
58
    uint8_t poti = adc_read(0);
59
      
60
    if(poti > 511)
61
    {
62
      MOTOR_RW_OFF;
63
      MOTOR_FW_ON;
64
      OCR1A = map(poti, 512, 1023, MOTOR_FW_MIN, MOTOR_FW_MAX);
65
    }
66
    else
67
    {
68
      MOTOR_FW_OFF;
69
      MOTOR_RW_ON;
70
      OCR1B = map(poti, 0, 511, MOTOR_RW_MAX, MOTOR_FW_MIN);
71
    }
72
    
73
    _delay_ms(100);
74
  }
75
}

von Attiny841 (Gast)


Lesenswert?

Keiner eine Idee? Ich hatte das ganze schon mal am laufen, finde das 
Projekt in meinen Backups leider nicht wieder.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.