/* * dcMotor.c * A program that turns turns the dcMotor, first clockwise, then * counter-clockwise * * Author: C.Schramm * Date: February 10, 2006 */ #define _IO_BASE 0 #define _ADDR(off) (unsigned char volatile *)(_IO_BASE + off) #define _P(off) *(unsigned char volatile *)(_IO_BASE + off) #define DDRP _P(0x025A) #define PTP _P(0x0258) #define PWME _P(0xA0) #define PWMPOL _P(0xA1) #define PWMCLK _P(0xA2) #define PWMPRCLK _P(0xA3) #define PWMCAE _P(0xA4) #define PWMCTL _P(0xA5) #define PWMSCLA _P(0xA8) #define PWMSCNTA _P(0xAA) #define PWMCNT7 _P(0xB3) #define PWMPER7 _P(0xBB) #define PWMDTY7 _P(0xC3) #define PORTK _P(0x32) #define DDRK _P(0x33) void delay(int n); void main(void) { int duty, i, fanDuty; int maxSpeed; // DO NOT ALTER THE INITIALIZATION CODE //Setup PWM Control of Motor //Output for all channels is high at beginning //of Period and low when the duty count is reached PWMPOL = 0xFF; PWMCLK |= 0x10; //Select Clock SB for channel 7 PWMPRCLK = 0x70; //Prescale ClockB by busclock/128 PWMSCLA = 0; //Total Divide: EClock/512 PWMCAE &= 0x7F; //Make sure Chan7 is in left aligned output mode PWMCTL &= 0xF3; //Allow PWM in Wait and Freeze Modes PWMPER7 = 100; //Set period for PWM7 PWME = 0x80; //Enable PWM Channel 7 DDRP |= 0x60; // So we can set the Motor Direction Control // NOTE: We have a conflict: Lightbar will also turn on. DDRK |= 0x0F; // So we can use LEDs as messages. // END OF INITIALIZATION CODE. // Turn on DC motor in clockwise direction, with given maxSpeed. maxSpeed = 20; // YOU MAY CHANGE THIS VALUE // WARNING: NEVER SET maxSpeed TO MORE THAN 35. // IT WILL DAMAGE THE MOTOR. // TIP: NEVER SET maxSpeed TO LESS THAN 20. // IT WON'T GO THAT SLOW! // Select direction using Port P, Bit 6 (1=Clock wise,0=Counter-Clockwise) PTP = 0b00000000; // LEAVE THE REST OF THE CODE UNTOUCHED. READ IT TO SEE WHAT TO EXPECT // Turn motor on **incrementally** until reached maxSpeed. PORTK = 0b00000001; // Turn on RED LED for speedup for(duty = 10; duty <= maxSpeed; duty++) { PWMDTY7 = duty; delay(4); } PORTK = 0b00000010; // Turn on WHITE LED for steady-speed delay(40); // Let motor run for a little while. // Turn off motor (incrementally) -- slow braking PORTK = 0b00001000; // Turn on YELLOW LED for braking for(duty = maxSpeed; duty >= 0; duty--) { PWMDTY7 = duty; delay(4); } PORTK = 0b00001000; // Turn on GREEN LED for done } /* delay * Does idle work for a period of time, keeping the CPU busy * but doing nothing useful */ void delay(int n) { int x; int y; int dummy = 1000; for (;n>0;n--) { for(y=0; y<0x1F; y++) { for(x=0; x<0x1FF; x++) { dummy = dummy + 0; dummy = dummy * 1; dummy = dummy / 1; } } } }