Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 03a28a5

Browse files
authored
added PWM on channel 2
added PWM on channel 2
1 parent c3ceadc commit 03a28a5

File tree

2 files changed

+111
-10
lines changed

2 files changed

+111
-10
lines changed

examples/ThermocouplesReadSensors/ThermocouplesReadSensors.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ void setup() {
3535

3636
void loop() {
3737
temp_probes.selectChannel(0);
38-
float temp_ch0 = temp_probes.t.readTemperature();
38+
float temp_ch0 = temp_probes.tc.readTemperature();
3939
Serial.print("Temperature CH0 [°C]: ");
4040
Serial.print(temp_ch0);
4141
Serial.print(" ");
4242

4343
temp_probes.selectChannel(1);
44-
float temp_ch1 = temp_probes.t.readTemperature();
44+
float temp_ch1 = temp_probes.tc.readTemperature();
4545
Serial.print(", CH1 [°C]: ");
4646
Serial.print(temp_ch1);
4747
Serial.print(" ");
4848

4949
temp_probes.selectChannel(2);
50-
float temp_ch2 = temp_probes.t.readTemperature();
50+
float temp_ch2 = temp_probes.tc.readTemperature();
5151
Serial.print(", CH2 [°C]: ");
5252
Serial.print(temp_ch2);
5353
Serial.println();

src/AutomationCarrier.h

+108-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class AnalogInClass {
108108
return value;
109109
}
110110

111-
uint16_t set0_10V() {
111+
void set0_10V() {
112112
ch0_in1 = 1;
113113
ch0_in2 = 1;
114114
ch0_in3 = 0;
@@ -125,7 +125,7 @@ class AnalogInClass {
125125
ch2_in4 = 1;
126126
}
127127

128-
uint16_t set4_24mA() {
128+
void set4_20mA() {
129129
ch0_in1 = 1;
130130
ch0_in2 = 0;
131131
ch0_in3 = 1;
@@ -142,7 +142,7 @@ class AnalogInClass {
142142
ch2_in4 = 0;
143143
}
144144

145-
uint16_t setNTC() {
145+
void setNTC() {
146146
ch0_in1 = 0;
147147
ch0_in2 = 0;
148148
ch0_in3 = 1;
@@ -184,21 +184,122 @@ class AnalogInClass {
184184

185185
extern AnalogInClass analog_in;
186186

187+
class AnalogOutPWMClass {
188+
public:
189+
AnalogOutPWMClass() {
190+
GPIO_InitTypeDef GPIO_InitStruct;
191+
/*##-1- Enable peripherals and GPIO Clocks #################################*/
192+
/* HRTIM1 Peripheral clock enable */
193+
__HAL_RCC_HRTIM1_CLK_ENABLE();
194+
195+
/* Enable GPIO Channels Clock */
196+
__HAL_RCC_GPIOG_CLK_ENABLE();
197+
198+
/* Configure HRTIMA TIMA TA1/A2, TIMB TB1/2, TIMC TC1/2, TIMD TD1/2 and TIME TE1.2
199+
channels as alternate function mode */
200+
/* Common configuration for all channels */
201+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
202+
GPIO_InitStruct.Pull = GPIO_PULLUP;
203+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
204+
GPIO_InitStruct.Alternate = GPIO_AF2_HRTIM1;
205+
GPIO_InitStruct.Pin = GPIO_PIN_7;
206+
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
207+
208+
/*##-1- Configure the HRTIM peripheral ######################################################*/
209+
/* Initialize the HRTIM structure */
210+
HrtimHandle.Instance = HRTIM1;
211+
HrtimHandle.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
212+
HrtimHandle.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
213+
214+
HAL_HRTIM_Init(&HrtimHandle);
215+
}
216+
217+
~AnalogOutPWMClass(){}
218+
// TODO: check why chamnge PrescalerRatio broke the portenta
219+
void period_ms(int period) {
220+
221+
sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
222+
sConfig_time_base.Period = 100;
223+
sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
224+
sConfig_time_base.RepetitionCounter = 0;
225+
226+
HAL_HRTIM_TimeBaseConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base);
227+
}
228+
229+
bool write(uint8_t voltage) {
230+
sConfig_Channel.Polarity = HRTIM_OUTPUTPOLARITY_LOW;
231+
sConfig_Channel.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
232+
sConfig_Channel.Pulse = voltage*10;
233+
234+
HAL_HRTIM_SimplePWMChannelConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E,
235+
HRTIM_OUTPUT_TE2, &sConfig_Channel);
236+
if (HAL_HRTIM_SimplePWMStart(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2) != HAL_OK)
237+
{
238+
return false;
239+
}
240+
return true;
241+
}
242+
243+
bool stop() {
244+
if (HAL_HRTIM_SimplePWMStop(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2) != HAL_OK)
245+
{
246+
return false;
247+
}
248+
return true;
249+
}
250+
251+
private:
252+
HRTIM_HandleTypeDef HrtimHandle;
253+
254+
HRTIM_TimeBaseCfgTypeDef sConfig_time_base;
255+
HRTIM_SimplePWMChannelCfgTypeDef sConfig_Channel;
256+
};
257+
258+
259+
extern AnalogOutPWMClass analopwm;
260+
261+
187262
class AnalogOutClass {
188263
public:
264+
void write(int index, uint8_t voltage) {
265+
switch (index) {
266+
case 0:
267+
out_0.write(voltage / 10.568);
268+
break;
269+
case 1:
270+
out_1.write(voltage);
271+
break;
272+
case 2:
273+
out_2.write(voltage / 10.568);
274+
break;
275+
}
276+
}
277+
void period_ms(int index, uint8_t period) {
278+
switch (index) {
279+
case 0:
280+
out_0.period_ms(period);
281+
break;
282+
case 1:
283+
out_1.period_ms(period);
284+
break;
285+
case 2:
286+
out_2.period_ms(period);
287+
break;
288+
}
289+
}
189290
mbed::PwmOut& operator[](int index) {
190291
switch (index) {
191292
case 0:
192293
return out_0;
193294
//case 1:
194-
// return out_1;
295+
//return out_1;
195296
case 2:
196297
return out_2;
197298
}
198299
}
199300
private:
200301
mbed::PwmOut out_0 = mbed::PwmOut(PJ_11);
201-
//AnalogOutPWMClass out_1 = AnalogOutPWMClass();
302+
AnalogOutPWMClass out_1 = AnalogOutPWMClass();
202303
mbed::PwmOut out_2 = mbed::PwmOut(PC_7);
203304
};
204305

@@ -256,7 +357,7 @@ extern ProgrammableDIOClass programmable_digital_io;
256357

257358
class DigitalOutputsClass {
258359
public:
259-
uint8_t setAll(uint8_t val) {
360+
void setAll(uint8_t val) {
260361
for (int i = 0; i < 8; i++) {
261362
out[i] = val & 0x1;
262363
val = val >> 1;
@@ -314,4 +415,4 @@ extern RtcControllerClass rtc_controller;
314415

315416
}
316417

317-
#endif
418+
#endif

0 commit comments

Comments
 (0)