Forum: Mikrocontroller und Digitale Elektronik schrittmotor Richtung umdrehen


von Schmidt (Gast)


Lesenswert?

Hallo,

Ich muss von einem Nanotec Schrittmotor die Drehrichtung umkehren.
D.h. wenn mann einen Taste drückt, wird die Drehrichtung umgekehrt.

Meinem pin für die Drehrichtung ist auf PINC.2 angeschlossen.

Da hab ich mir einen überlegt (also siehe code unten).

Kann jemand mir sagen warum es nicht funktioniert. Die Umkehrung des 
Richtung wird einfach ignoriert.


Danke schonmal
1
int Rotation_Sense(void)
2
{   
3
   int Stepper_Tack =0;
4
   if((PINC & 0x02) == 0x00) //Link Drehung
5
   Stepper_Tack = 0x02;
6
   
7
   else if((PINC & 0x02) == 0x01) //Recht Drehung
8
   Stepper_Tack = 0x00;
9
   
10
   PORTD = 0xFD;         // Led
11
   return Stepper_Tack;
12
}
13
14
.....
15
//in main
16
17
if((PINB & 0x01) == 0x00)
18
{
19
  UART_Puts( "Stepper Motor is enable" );    
20
  UART_Puts("\r\n");
21
  PORTD = 0xFE;     // Led
22
  PORTC = 0x20;    // Pins Motor Enable
23
      
24
  _delay_ms(50);    // dirty "debounce"
25
26
}
27
else if((PINB & 0x02) == 0x00)
28
{
29
  UART_Puts( "The direction is Toggle " );    
30
     UART_Puts("\r\n");
31
32
  mDirection =Rotation_Sense();
33
      
34
  _delay_ms(50);     // dirty "debounce"
35
36
}
37
else if((PINB & 0x04) == 0x00)
38
{
39
  UART_Puts( "Motor turns with a 1ms speed and 1/2 Tour" );  
40
     UART_Puts("\r\n");
41
  for(int i = 0; i<100; i++) //entspricht 1 Umdrehung 200 Schritte
42
  {
43
    PORTD = 0xFB;   // Led
44
    PORTC = mDirection | 0xFF;//Pins Motor
45
    _delay_ms(1);    // dirty "debounce" 
46
    PORTC = mDirection | 0xFE;  //Pins Motor
47
        
48
  }
49
  _delay_ms(50);
50
}

von Lothar M. (Firma: Titel) (lkmiller) (Moderator) Benutzerseite


Lesenswert?

1
   if((PINC & 0x02) == 0x00) //Link Drehung
2
   Stepper_Tack = 0x02;
3
   
4
   else if((PINC & 0x02) == 0x01) //Recht Drehung
Sieh dir mal das Thema binäre Verknüpfungen
in deinem C-Buch nochmal genauer an.

> PINC.2
Ist zu finden mit der Maske 0x40 = 0b00000100
Oder (1 << 2)

von Christian H. (netzwanze) Benutzerseite


Lesenswert?

Du meinst 0x04

von Lothar M. (Firma: Titel) (lkmiller) (Moderator) Benutzerseite


Lesenswert?

Christian H. schrieb:
> Du meinst 0x04
Na gut, du hast recht  ;-)

von Schmidt (Gast)


Lesenswert?

Danke an alle
ich bin kein profis in C
aber mit den code geht auch nicht wo ist dann meinem Fehler?
1
if((PINC & (1 << 2)) //Link Drehung
2
   Stepper_Tack = 0x02;
3
   
4
   else if((PINC & (0 << 2)) //Recht Drehung

gruß

von Christian H. (netzwanze) Benutzerseite


Lesenswert?

(0 << 2) = 0
=> PINC & 0 = 0
=> else if (...) trifft niemals zu.

Aber:
1
if((PINC & (1 << 2)) //Link Drehung
2
   Stepper_Tack = 0x02;
3
else
4
   Stepper_Tack = 0x00;
sollte funktionieren (Achtung keine Tastenentprellung), wenn der Rest 
funktioniert (habe ich nicht überprüft).

von Schmidt (Gast)


Lesenswert?

danke es funktioniert!

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.