7 #include <system_def.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
31 * Enables or disables the ouput on engine A. Causes smooth stop, without break.
33 * @param status status parameters are defined in engine.h
36 void engine_A_en(unsigned status)
38 if (status == ENGINE_EN_ON)
39 IO1SET |= (1<<ENGINE_ENA);
41 IO1CLR |= (1<<ENGINE_ENA);
45 * Enables or disables the ouput on engine B. Causes smooth stop, without break.
47 * @param status status parameters are defined in engine.h
50 void engine_B_en(unsigned status)
52 if (status == ENGINE_EN_ON)
53 IO1SET |= (1<<ENGINE_ENB);
55 IO1CLR |= (1<<ENGINE_ENB);
59 * Changes motor A direction
61 * @param dir direction parameters are defined in engine.h
64 void engine_A_dir(unsigned dir)
66 if (dir == ENGINE_DIR_BW)
67 IO1SET |= (1<<ENGINE_IN2A);
69 IO1CLR |= (1<<ENGINE_IN2A);
76 * Changes motor B direction
78 * @param dir direction parameters are defined in engine.h
81 void engine_B_dir(unsigned dir)
83 if (dir == ENGINE_DIR_BW)
84 IO1SET |= (1<<ENGINE_IN2B);
86 IO1CLR |= (1<<ENGINE_IN2B);
92 * Sets motor A PWM value
94 * @param pwm Unsigned int, 0~100%
96 * @note 0 causes fast stop
98 void engine_A_pwm(unsigned pwm)
100 if (pwm>100) pwm = 100;
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;
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
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);
144 PWMMR0 = CPU_APB_HZ / 20000;
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
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);
173 PWMMR0 = CPU_APB_HZ / 20000;
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;