|
StepperII
Dual Axis Stepper Controller
|
00001 00002 #pragma once 00003 00004 #include <avr/io.h> 00005 00006 #include "CEvent.h" 00007 00008 //---------------------------------------------------------------------------- 00010 //---------------------------------------------------------------------------- 00011 /* 00012 Prescaler Values 00013 00014 CS12 CS11 CS10 Value 00015 0 0 0 timer1 stopped 00016 0 0 1 CK 00017 0 1 0 CK/8 00018 0 1 1 CK/64 00019 1 0 0 CK/256 00020 1 0 1 CK/1024 00021 1 1 0 T1 falling edge 00022 1 1 1 T1 rising edge 00023 */ 00024 00025 00026 // Note: For a 16Mhz crystal and divide by 8 prescale, 00027 // OC register - 1 is just # of usecs in a period (mode 4) 00028 // i.e. 800 hz is 1250 usec - 1 or OCR1A = 1249 simple! 00029 00030 //#if defined (_AVR_IOMX8_H_) || defined (_AVR_IOUSBXX2_H_) // ATMega48,88,168, AT90USB162 00031 #if defined TIMSK0 00032 // Register names change in this architecture 00033 #define TIMSK TIMSK0 00034 #define TIFR TIFR0 00035 00036 #endif 00037 00038 class CTimer0 : public CEvent 00039 { 00040 private: 00041 static const long PRESCALE = 64; // Prescale is divide by 4 00042 static const long FREQ = 1000; // 1 Khz 00043 static const char COMPARE = (F_CPU / (PRESCALE * FREQ)) - 1; 00044 00045 // Define Prescale Values 00046 static const char STOPPED = 0; 00047 static const char DIV1 = 1; 00048 static const char DIV8 = 2; 00049 static const char DIV64 = 3; 00050 static const char DIV256 = 4; 00051 static const char DIV1024 = 5; 00052 static const char EXT_FALLING = 6; 00053 static const char EXT_RISING = 7; 00054 00055 static const char prescaleMask = ~(_BV(CS02) | _BV(CS01) | _BV(CS00)); 00056 00057 public: 00058 char prescale; // Save prescale here for on/off 00059 00060 public: 00061 void off(void); // Timer off 00062 void on(void); // Timer on 00063 void setCompare(unsigned int x); // Set compare register A 00064 void square(void); // Output square wave 00065 void setTop(int x); 00066 }; 00067 00068 inline void CTimer0::off(void) 00069 { 00070 TCCR0B = (TCCR0B & prescaleMask) | STOPPED; // Stop the clock 00071 } 00072 00073 inline void CTimer0::on(void) 00074 { 00075 TCNT0 = one; // Reset counter - HUGE TRICK: Don't set to 0 to avoid int. 00076 TCCR0B = (TCCR0B & prescaleMask) | prescale; // Load prescale 00077 } 00078 00079 inline void CTimer0::setCompare(unsigned int x) 00080 { 00081 OCR0A = x; // Load compare A register 00082 } 00083 00084 inline void CTimer0::square(void) 00085 { 00086 TCCR0B = _BV(WGM02); // Set CTC mode 4 00087 prescale = DIV64; // Set prescale 00088 setCompare(COMPARE); // Load timer with defined period 00089 TIMSK |= _BV(OCIE0A); // Enable output compare A interrupt 00090 on(); 00091 }
1.7.3