Forum: Mikrocontroller und Digitale Elektronik Servo per variable stellen


von Elias B. (bouni)


Lesenswert?

Hi,

Ich probier grad mit nem RS2 Servo vom großen C rum (am Atmega 8515).
1
#include <avr/io.h>
2
 
3
int main()
4
{
5
  DDRB = (1 << PD5 ); 
6
  TCCR1A = (1<<COM1A1) | (1<<WGM11);
7
  TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS01);
8
  ICR1 = 0x1200; 
9
  OCR1A = 0x0295; 
10
  while( 1 )
11
    ;  
12
}

Des funktioniert auch gut soweit

wenn ich aber OCR1A aus einer anderen Funktion heraus ändern möchte 
fährt der servo an den Anschlag im Uhrzeigersinn.
1
#include <avr/io.h>
2
3
uint16_t servostellung;
4
5
void servopos()
6
{
7
servostellung = 0x046C;
8
}
9
10
int main()
11
{
12
  DDRB = (1 << PD5 ); 
13
  TCCR1A = (1<<COM1A1) | (1<<WGM11);
14
  TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS01);
15
  ICR1 = 0x1200; 
16
  OCR1A = servostellung; 
17
  while( 1 )
18
  {
19
    servopos();  
20
  }
21
}

Woran liegt das bzw. was ist mein Fehler?

Bouni

von Sven P. (Gast)


Lesenswert?

Elias B. wrote:
1
 #include <avr/io.h>
2
 
3
 uint16_t servostellung;
4
 
5
 void servopos()
6
 {
7
 servostellung = 0x046C;
8
 }
9
 
10
 int main()
11
 {
12
   DDRB = (1 << PD5 );
13
   TCCR1A = (1<<COM1A1) | (1<<WGM11);
14
   TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS01);
15
   ICR1 = 0x1200;
16
/* servostellung ist hier mit Null initialisiert ---> Anschlag */
17
   OCR1A = servostellung;
18
/* PWM verändert, Servo fährt los */
19
   while( 1 )
20
   {
21
     servopos();
22
/* Verändere die Variable, aber nicht OCR1A. Servo steht. */
23
   }
24
 }

von ozo (Gast)


Lesenswert?

Ähm, du änderst zwar "servostellung" aber doch nie OCR1A.
Und nur dieses interessiert die PWM erzeugende Hardware.

von Elias B. (bouni)


Lesenswert?

Morgen
1
#include <avr/io.h>
2
 
3
 uint16_t servostellung;
4
 
5
 void servopos()
6
 {
7
 servostellung = 0x046C;
8
 }
9
 
10
 int main()
11
 {
12
   DDRB = (1 << PD5 );
13
   TCCR1A = (1<<COM1A1) | (1<<WGM11);
14
   TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS01);
15
   ICR1 = 0x1200;
16
   while( 1 )
17
   {
18
     servopos();
19
     OCR1A = servostellung;
20
   }
21
 }

Ist so besser ??

Ich kanns grad nicht probieren da ich mein STK und den Servo nicht hiere 
hab.

Bouni

von Elias B. (bouni)


Lesenswert?

das mit der Variable hab ich hinbekommen, aber das serielle empfangen 
eines Prozent wertes als stellwert macht mir noch Probleme :(
1
#include <avr/io.h>
2
#include <inttypes.h>
3
4
#ifndef F_CPU
5
#define F_CPU 3686400UL    
6
#endif
7
8
#define BAUD 9600UL 
9
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // clever runden
10
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // Reale Baudrate
11
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
12
         
13
    UCSRB |= (1<<TXEN) | (1<<RXEN);    // UART RX und TX einschalten
14
    UCSRC |= (1<<URSEL)|(3<<UCSZ0);    // Asynchron 8N1 
15
 
16
    UBRRH = UBRR_VAL >> 8;
17
    UBRRL = UBRR_VAL & 0xFF;
18
19
 uint8_t uart_getc(void)
20
 {
21
    while (!(UCSRA & (1<<RXC)))   // warten bis Zeichen verfuegbar
22
        ;
23
    return UDR;
24
 }
25
 
26
 int main()
27
 {
28
   DDRB = (1 << PD5 );
29
   TCCR1A = (1<<COM1A1) | (1<<WGM11);
30
   TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS01);
31
   ICR1 = 0x1200;
32
   uint16_t servopos;
33
   uint8_t uart_percent; // Empfangene Zahl von 0 bis 100
34
   while( 1 )
35
   {
36
     uart_percent = uart_getc();
37
     servopos = 0x0127 + (uart_percent * 8); // 0x0127 = 0% 0x447 = 100% -
38
                                             // differenz ist 800
39
                                             //deswegen empfangene 
40
                                             //prozent * 8
41
     OCR1A = servopos;
42
   }
43
 }

Hat wer ne Idee woran es liegen könnte?


Bouni

von STK500-Besitzer (Gast)


Lesenswert?

>servopos = 0x0127 + (uart_percent * 8);

Du multiplizierst eine 8Bit-Variable mit 8 bei 100% käme da 800 heraus.
Du mußt uart_percent auf u_int16 casten.

von Elias B. (bouni)


Lesenswert?

Morgen,

also so:
1
servopos = 0x0127 + ( (uint16_t) uart_percent * 8);

?

Bouni

von STK500-Besitzer (Gast)


Lesenswert?

Probier es aus!

von Bouni (Gast)


Lesenswert?

Bin grad auf Arbeit :)

Aber werd ich heut Abend tun!

Dank für den Tip mit dem Type Cast !!

Bouni

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.