Software testing lpcanvca.h
[LPC2xxx_and_RobotSpejbl.git] / app / eb_ebb / engine.c
blobb0eae53cc3442aac3a979020022aef5ddd9e5c17
5 #include <lpc21xx.h>
6 #include <deb_led.h>
7 #include <system_def.h>
8 #include "engine.h"
10 #define ENGINE_PIN_A 7 ///< PWM output for engine A, pin P0.7
11 #define ENGINE_PIN_B 8 ///< PWM output for engine B, pin P0.8
13 #define ENGINE_ENA 28 ///< Enable output for engine A, pin P1.28
14 #define ENGINE_ENB 30 ///< Enable output for engine B, pin P1.30
16 #define ENGINE_IN2A 27 ///< Direction output for engine A , pin P1.27
17 #define ENGINE_IN2B 29 ///< Direction output for engine A , pin P1.29
19 #define PWM_FREQ 20000 ///< PWM frequency
20 #define PWM_RES 100 ///< PWM resolution
22 #define PWM_STEP ((CPU_APB_HZ / PWM_FREQ)/PWM_RES + 1) ///< Calculates smallest step PWM for resolution 0~100%
26 unsigned int pwm_A = 0; ///< Actual value of PWM A
27 unsigned int pwm_B = 0; ///< Actual value of PWM B
30 /**
31 * Enables or disables the ouput on engine A. Causes smooth stop, without break.
33 * @param status status parameters are defined in engine.h
35 */
36 void engine_A_en(unsigned status)
38 if (status == ENGINE_EN_ON)
39 IO1SET |= (1<<ENGINE_ENA);
40 else
41 IO1CLR |= (1<<ENGINE_ENA);
44 /**
45 * Enables or disables the ouput on engine B. Causes smooth stop, without break.
47 * @param status status parameters are defined in engine.h
49 */
50 void engine_B_en(unsigned status)
52 if (status == ENGINE_EN_ON)
53 IO1SET |= (1<<ENGINE_ENB);
54 else
55 IO1CLR |= (1<<ENGINE_ENB);
58 /**
59 * Changes motor A direction
61 * @param dir direction parameters are defined in engine.h
63 */
64 void engine_A_dir(unsigned dir)
66 if (dir == ENGINE_DIR_BW)
67 IO1SET |= (1<<ENGINE_IN2A);
68 else
69 IO1CLR |= (1<<ENGINE_IN2A);
71 engine_A_pwm(pwm_A);
75 /**
76 * Changes motor B direction
78 * @param dir direction parameters are defined in engine.h
80 */
81 void engine_B_dir(unsigned dir)
83 if (dir == ENGINE_DIR_BW)
84 IO1SET |= (1<<ENGINE_IN2B);
85 else
86 IO1CLR |= (1<<ENGINE_IN2B);
88 engine_B_pwm(pwm_B);
91 /**
92 * Sets motor A PWM value
94 * @param pwm Unsigned int, 0~100%
96 * @note 0 causes fast stop
97 */
98 void engine_A_pwm(unsigned pwm)
100 if (pwm>100) pwm = 100;
102 pwm_A = pwm;
103 if (IO1PIN & (1<<ENGINE_IN2A)) PWMMR2 = PWM_STEP * (100 - pwm);
104 else PWMMR2 = PWM_STEP * (pwm);
105 PWMLER |= PWMLER_LA2_m;
110 * Sets motor B PWM value
112 * @param pwm Unsigned int, 0~100%
114 * @note 0 causes fast stop
116 void engine_B_pwm(unsigned pwm)
118 if (pwm>100) pwm = 100;
119 pwm_B = pwm;
120 if (IO1PIN & (1<<ENGINE_IN2B)) PWMMR4 = PWM_STEP * (100 - pwm);
121 else PWMMR4 = PWM_STEP * (pwm);
122 PWMLER |= PWMLER_LA4_m;
128 * Inicializes Engine A - enables PWM outputs and sets IOs. Uses PWM channel - PWMMR2.
129 * If is somthing other set on PWM channels, it will affect the main PWM frequency
130 * If the engine B is set it will afect its output for one cycle
134 void init_engine_A()
136 PINSEL0 &= ~((PINSEL_3 << 14) );
137 PINSEL0 |= (PINSEL_2 << 14) ;
139 IO0DIR |= (1<<ENGINE_PIN_A);
140 IO1DIR |= (1<<ENGINE_ENA) | (1<<ENGINE_IN2A);
141 IO1SET |= (1<<ENGINE_ENA) | (1<<ENGINE_IN2A);
143 PWMPR = 0;
144 PWMMR0 = CPU_APB_HZ / 20000;
146 PWMMR2 =0;
148 PWMPCR |= PWMPCR_PWMENA2_m;
149 PWMLER |= PWMLER_LA0_m | PWMLER_LA2_m;
150 PWMMCR |= PWMMCR_PWMMR0R_m;
151 PWMTCR |= PWMTCR_CE_m | PWMTCR_EN_m;
155 * Inicializes Engine B - enables PWM outputs and sets IOs. Uses PWM channel - PWMMR2.
156 * If is somthing other set on PWM channels, it will affect the main PWM frequency
157 * If the engine A is set it will afect its output for one cycle
161 void init_engine_B()
163 PINSEL0 &= ~((PINSEL_3 << 16));
164 PINSEL0 |= (PINSEL_2 << 16);
166 IO0DIR |= (1<<ENGINE_PIN_B);
167 IO1DIR |= (1<<ENGINE_ENB) | (1<<ENGINE_IN2B);
169 IO1SET |= (1<<ENGINE_ENB) | (1<<ENGINE_IN2B);
172 PWMPR = 0;
173 PWMMR0 = CPU_APB_HZ / 20000;
175 PWMMR4 = 0;
177 PWMPCR |= PWMPCR_PWMENA4_m;
178 PWMLER |= PWMLER_LA0_m | PWMLER_LA4_m;
179 PWMMCR |= PWMMCR_PWMMR0R_m;
180 PWMTCR |= PWMTCR_CE_m | PWMTCR_EN_m;