1 | /* Includes *******************************************************************/
|
2 | #include <avr/io.h>
|
3 | #include <inttypes.h>
|
4 | #include "motor.h"
|
5 | #include "defs.h"
|
6 | #include "adc.h"
|
7 | #include "timer.h"
|
8 |
|
9 |
|
10 | /* Constants ******************************************************************/
|
11 |
|
12 | /* Macros *********************************************************************/
|
13 |
|
14 | /* Type definitions ***********************************************************/
|
15 |
|
16 | /* Prototypes *****************************************************************/
|
17 |
|
18 | /* Variables ******************************************************************/
|
19 | static volatile uint8_t MotorState; ///< Actual Motor State
|
20 |
|
21 | /* Functions ******************************************************************/
|
22 |
|
23 | //------------------------------------------------------------------------------
|
24 | //--- Initialization of module
|
25 | //---
|
26 | //--- Initialize Timer0, Set Port Directions, Preset Internal Variables
|
27 | //--- Reset Position Counter and Install Timer Callback
|
28 | //---
|
29 | //------------------------------------------------------------------------------
|
30 | extern void MOTOR_Init (void)
|
31 | {
|
32 | // Set Control Lines as Output
|
33 | // PB5 - Direction 1
|
34 | // PB6 - Direction 2
|
35 | // PB7 - PWM
|
36 | DDRB |= (1 << PB5) | (1 << PB6) | (1 << PB7);
|
37 | PORTB &= ~(1 << PB5);
|
38 | PORTB &= ~(1 << PB6);
|
39 | // Initialize Timer 0
|
40 | OCR0A = 0;
|
41 | TCCR0A |= (1 << WGM00) | (1 << COM0A1) | (1 << CS00);
|
42 | MotorState = MOTOR_STOP;
|
43 | }
|
44 | //------------------------------------------------------------------------------
|
45 | //--- Set Direction Control Line
|
46 | //---
|
47 | //--- dir -> Direction (UP,DOWN,STOP)
|
48 | //------------------------------------------------------------------------------
|
49 | static void MOTOR_SetDirection (uint8_t dir)
|
50 | {
|
51 |
|
52 | switch (dir)
|
53 | {
|
54 | case STOP: PORTB &= ~(1 << PB5);
|
55 | PORTB &= ~(1 << PB6);
|
56 | break;
|
57 | case UP: PORTB &= ~(1 << PB5);
|
58 | PORTB |= (1 << PB6);
|
59 | break;
|
60 | case DOWN: PORTB |= (1 << PB5);
|
61 | PORTB &= ~(1 << PB6);
|
62 | break;
|
63 | }
|
64 | }
|
65 |
|
66 |
|
67 | //------------------------------------------------------------------------------
|
68 | //-- Get Actual State of the Motor
|
69 | //--
|
70 | //-- return Actual State of the Motor
|
71 | //------------------------------------------------------------------------------
|
72 | extern uint8_t GetMotorState (void)
|
73 | {
|
74 | return MotorState;
|
75 | }
|
76 |
|
77 | //------------------------------------------------------------------------------
|
78 | //-- Set Actual State of the Motor
|
79 | //--
|
80 | //-- set Actual State of the Motor
|
81 | //------------------------------------------------------------------------------
|
82 | extern void SetMotorState (uint8_t state)
|
83 | {
|
84 | MotorState = state;
|
85 | }
|
86 |
|
87 | //------------------------------------------------------------------------------
|
88 | //-- Move Motor
|
89 | //------------------------------------------------------------------------------
|
90 | extern void MOTOR_Move (uint8_t dir)
|
91 | {
|
92 | switch (dir)
|
93 | {
|
94 | case STOP: MOTOR_SetDirection (STOP);
|
95 | OCR0A = 0;
|
96 | MotorState = MOTOR_STOP;
|
97 | ADC_Enable (DISABLE);
|
98 | break;
|
99 | case UP: MOTOR_SetDirection (UP);
|
100 | OCR0A = 38;
|
101 | MotorState = MOTOR_MOVES_UP;
|
102 | TIMER_Start(START);
|
103 | break;
|
104 | case DOWN: MOTOR_SetDirection (DOWN);
|
105 | OCR0A = 38;
|
106 | MotorState = MOTOR_MOVES_DOWN;
|
107 | TIMER_Start (START);
|
108 | break;
|
109 | }
|
110 |
|
111 | }
|