Ja hallo,
vielen Dank für die interessanten Tipps.
Damit wir hier nicht so virtuell in den Äther diskutieren, hier mal etwas Code an dem ich gerade arbeite.
Und hier die ClStepMotor.cpp:Code:#ifndef _stepMotorClass_ #define _stepMotorClass_ #include <util/delay.h> #include <avr/io.h> class ClStepMotor { private: uint8_t stepNo; int8_t direction; // +1 / -1 uint16_t wait; public: ClStepMotor(); void setMotorStep(void); void setWait(uint16_t*); void setDirection(int8_t*); void setMotorOff(void); }; #endif // stepMotorClass
Wie Ihr sehen könnt, programmiere ich gerne in C++. Das bringt mir die Vorteile, daß ich einmal geschriebenen Code wirklich einfach wiederverwenden kann. Aber in diesem Fall wird in der Prozedur motorStep() eine delay-Routine aufgerufen. Das ist natürlich nicht gut, und diese möchte ich gerne gegen die ISR ersetzen.Code:#include <util/delay.h> #include <avr/io.h> #include "ClStepMotor.h" ClStepMotor::ClStepMotor() { // Den Schrittmotortreiber aktivieren. => Port D Bit 4 & 5 auf eins schalten DDRD |= (1<<PD4 | 1<<PD5); // Die Bits 4 und 5 auf Ausgang setzen. PORTD |= (1<<PD4 | 1<<PD5); // Die beiden Bits auf 1 setzen: High-Ausgang // Die Steuerleitungen fuer den Schrittmotortreiber aktivieren => Port B Bit 0 und 1, Port C Bit 6 und 7 DDRB |= (1<<PB0 | 1<<PB1); // Port B aktivieren DDRC |= (1<<PC6 | 1<<PC7); // Port C aktivieren wait = 80; stepNo = 0; direction = 1; } void ClStepMotor::setWait(uint16_t *w) { wait = *w; } void ClStepMotor::setDirection( int8_t *d ) { direction = *d; } void ClStepMotor::setMotorStep(void) { uint8_t valPrtB = 0; uint8_t valPrtC = 0; uint16_t w = wait; stepNo += direction; if( stepNo > 7 ) { // auch 2'er-Komplementzahlen (negative) sind usigned größer als 7 :) if ( direction > 0 ) stepNo = 0; if ( direction < 0 ) stepNo = 7; } switch( stepNo ) { case 0: valPrtB = 1<<PB0; // 10-10 valPrtC = 1<<PC6; break; case 1: valPrtB = 1<<PB0; // 10-00 valPrtC = 0<<PC6; break; case 2: valPrtB = 1<<PB0; // 10-01 valPrtC = 1<<PC7; break; case 3: valPrtB = 0<<PB0; // 00-01 valPrtC = 1<<PC7; break; case 4: valPrtB = 1<<PB1; // 01-01 valPrtC = 1<<PC7; break; case 5: valPrtB = 1<<PB1; // 01-00 valPrtC = 0<<PC7; break; case 6: valPrtB = 1<<PB1; // 01-10 valPrtC = 1<<PC6; break; case 7: valPrtB = 0<<PB1; // 00-10 valPrtC = 1<<PC6; break; default: stepNo = 0; break; } PORTB = (PORTB & !(1<<PB0 | 1<<PB1) ) | valPrtB; // Clear out old values and then set the new ones. PORTC = (PORTC & !(1<<PC6 | 1<<PC7) ) | valPrtC; while ( w-- ) _delay_us(1); } void ClStepMotor::setMotorOff(void) { PORTB = (PORTB & !(1<<PB0 | 1<<PB1) ); // Clear out old values. PORTC = (PORTC & !(1<<PC6 | 1<<PC7) ); }
Ansonten: Was haltet Ihr von meinem Code?
Gruß,
Lev







Zitieren

Lesezeichen