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

Commit d03b24a

Browse files
authored
Added range limit to ananlog ch1 and the docu-Example
Added range limit to analog ch1 and the docu-Example -Implemented range limit logic for analog out and changed the conversion model to include the limit case pulse = 0 and modified the digital programmable example - Added 4th PWM channel - Edited analog out example with comments
1 parent 9988510 commit d03b24a

File tree

3 files changed

+108
-16
lines changed

3 files changed

+108
-16
lines changed

examples/Analog_Out/Analog_Out.ino

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Machine Control - Analog out Example
3+
4+
This example shows how to use the Analog out channels on
5+
the Machine Control.
6+
The example sets the channels PWM period in the setup,
7+
then loops the channels voltage output value from 0V to 10.4V.
8+
9+
The circuit:
10+
- Portenta H7
11+
- Machine Control
12+
13+
created 17 November 2020
14+
by Riccardo Rizzo
15+
modified 09 December 2020
16+
by Silvio Navaretti
17+
This example code is in the public domain.
18+
*/
19+
20+
#include <AutomationCarrier.h>
21+
22+
using namespace automation;
23+
24+
void setup() {
25+
//analog_out.period_ms(CHANNEL, PERIOD_MILLISECONDS);
26+
analog_out.period_ms(0, 4);
27+
analog_out.period_ms(1, 4);
28+
analog_out.period_ms(2, 4);
29+
analog_out.period_ms(3, 4);
30+
31+
Serial.begin(9600);
32+
Serial.println("Analog out test");
33+
34+
}
35+
36+
//Output values which will be changed with this variable
37+
float counter = 1;
38+
39+
void loop() {
40+
//analog_out.write(CHANNEL, OUTPUT_VOLTAGE_VALUE);
41+
analog_out.write(0, counter);
42+
analog_out.write(1, counter);
43+
analog_out.write(2, counter);
44+
analog_out.write(3, counter);
45+
Serial.println("All channels set at "+String(counter)+"V");
46+
47+
counter = counter + 0.1;
48+
//Maximum output value is 10.4V
49+
if (counter >= 10.5)
50+
{
51+
counter = 0;
52+
//Additional 100ms delay introduced to manage 10.5V -> 0V fall time of 150ms
53+
delay(100);
54+
}
55+
delay(100);
56+
}

examples/Digital_programmable/Digital_programmable.ino

+22-2
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,37 @@ void setup() {
3434
}
3535

3636
void loop() {
37+
// Write the status value to On to Pin 3
38+
digital_programmables.set(IO_WRITE_CH_PIN_03, SWITCH_ON);
39+
delay(1000);
40+
41+
// Read from Pin 3
42+
Serial.println("Pin 03: " + String(digital_programmables.read(SWITCH_OFF)));
43+
delay(1000);
44+
45+
// Write the status value to Off to Pin 3
46+
digital_programmables.set(IO_WRITE_CH_PIN_03, SWITCH_OFF);
47+
delay(1000);
48+
49+
50+
// Write the status value to On to all the Output Pins
3751
setAll(SWITCH_ON);
52+
53+
// Reads from all Input Pins
3854
readAll();
3955
delay(1000);
56+
57+
// Write the status value to Off all to all the Output Pins
4058
setAll(SWITCH_OFF);
59+
60+
// Reads from all Input Pins
4161
readAll();
4262
delay(1000);
4363

4464
}
4565

4666
void setAll(PinStatus status) {
47-
// write the status value to each pin
67+
// Write the status value to each Pin
4868
digital_programmables.set(IO_WRITE_CH_PIN_00, status);
4969
digital_programmables.set(IO_WRITE_CH_PIN_01, status);
5070
digital_programmables.set(IO_WRITE_CH_PIN_02, status);
@@ -60,7 +80,7 @@ void setAll(PinStatus status) {
6080
}
6181

6282
void readAll() {
63-
// Reads from input pin. This API returns -1 if you try to read from a write channel.
83+
// Reads from input pins. This API returns -1 if you try to read from a write channel.
6484
Serial.println("Pin 00: " + String(digital_programmables.read(IO_READ_CH_PIN_00)));
6585
Serial.println("Pin 01: " + String(digital_programmables.read(IO_READ_CH_PIN_01)));
6686
Serial.println("Pin 02: " + String(digital_programmables.read(IO_READ_CH_PIN_02)));

src/AutomationCarrier.h

+30-14
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,15 @@ class AnalogOutPWMClass {
294294
HAL_HRTIM_TimeBaseConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base);
295295
}
296296

297-
bool write(uint8_t voltage) {
298-
sConfig_compare.CompareValue = voltage;
297+
bool write(uint8_t pulse) {
298+
if (pulse > 100) {
299+
pulse = 100;
300+
}
301+
sConfig_compare.CompareValue = pulse;
299302
if (HAL_HRTIM_WaveformCompareConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare) != HAL_OK)
300303
{
301304
return false;
302-
}
305+
}
303306
return true;
304307
}
305308

@@ -327,15 +330,22 @@ extern AnalogOutPWMClass analopwm;
327330
class AnalogOutClass {
328331
public:
329332
void write(int index, float voltage) {
333+
if (voltage < 0) {
334+
voltage = 0;
335+
}
336+
330337
switch (index) {
331338
case 0:
332-
out_0.write(voltage / 10.568);
339+
out_0.write(voltage / 10.5);
333340
break;
334341
case 1:
335-
out_1.write((voltage*9.3679) +1);
342+
out_1.write(voltage / 10.5);
336343
break;
337344
case 2:
338-
out_2.write(voltage / 10.568);
345+
out_2.write((voltage*9.5));
346+
break;
347+
case 3:
348+
out_3.write(voltage / 10.5);
339349
break;
340350
}
341351
}
@@ -345,27 +355,33 @@ class AnalogOutClass {
345355
out_0.period_ms(period);
346356
break;
347357
case 1:
348-
out_1.period_ms((period/4)*100);
358+
out_1.period_ms(period);
349359
break;
350360
case 2:
351-
out_2.period_ms(period);
361+
out_2.period_ms((period/4)*100);
362+
break;
363+
case 3:
364+
out_3.period_ms(period);
352365
break;
353366
}
354367
}
355368
mbed::PwmOut& operator[](int index) {
356369
switch (index) {
357370
case 0:
358371
return out_0;
359-
//case 1:
360-
//return out_1;
361-
case 2:
362-
return out_2;
372+
case 1:
373+
return out_1;
374+
//case 2:
375+
//return out_2;
376+
case 3:
377+
return out_3;
363378
}
364379
}
365380
private:
366381
mbed::PwmOut out_0 = mbed::PwmOut(PJ_11);
367-
AnalogOutPWMClass out_1 = AnalogOutPWMClass();
368-
mbed::PwmOut out_2 = mbed::PwmOut(PC_7);
382+
mbed::PwmOut out_1 = mbed::PwmOut(PK_1);
383+
AnalogOutPWMClass out_2 = AnalogOutPWMClass();
384+
mbed::PwmOut out_3 = mbed::PwmOut(PC_7);
369385
};
370386

371387
extern AnalogOutClass analog_out;

0 commit comments

Comments
 (0)