1
+ /* !
2
+ * \name directRegisterControl
3
+ * \author Infineon Technologies AG
4
+ * \copyright 2025 Infineon Technologies AG
5
+ * \brief This example demonstrates how to control two motors via direct register setting
6
+ * \details
7
+ * If motors are connected to the TLE94112 on certain half bridges, than they can be controlled via direct register
8
+ * setting which reduces greatly the overhead and allows fast motor controlling.
9
+ * therefore the half bridges HB1 to HB4, HB5 to HB8 and HB9 to HB12 must be used together, either for controlling
10
+ * two motors on one tuple (0.9 A), or one motor on one tuple (1.8 A).
11
+ *
12
+ * SPDX-License-Identifier: MIT
13
+ *
14
+ */
15
+
16
+ #include < tle94112-ino.hpp>
17
+
18
+ // ! Tle94112 Object
19
+ Tle94112Ino controller = Tle94112Ino();
20
+
21
+ // ! Tle94112 registers for motor 1 and 2
22
+ uint8_t motor[2 ][2 ] = {
23
+ {REG_ACT_1, REG_PWM_DC_1},
24
+ {REG_ACT_3, REG_PWM_DC_3}
25
+ };
26
+
27
+ // ! Tle94112 register for motor direction
28
+ volatile uint8_t oldDirection [] = { LL_HH, HH_LL };
29
+
30
+ /* *
31
+ * @brief Changes the motor direction and speed
32
+ * The motor direction is set by the change of the High/Low switching of the motor half bridges in one register.
33
+ * This needs some time to change the direction, so its avoided if not needed.
34
+ * Setting the speed is done by setting the PWM register.
35
+ *
36
+ * @param motorNum uint8_t motor number (0 or 1)
37
+ * @param dir uint8_t direction (HH_LL or LL_HH for forward/backward of one motor on four half bridges)
38
+ * @param speed uint8_t speed (0-255)
39
+ * @param errorCheck bool if true, the error register is cleared after setting the speed
40
+ */
41
+ void motorSet (uint8_t motorNum, uint8_t dir, uint8_t speed, bool errorCheck = false )
42
+ {
43
+ if (dir != oldDirection[motorNum]) {
44
+ controller.directWriteReg (motor[motorNum][0 ], dir);
45
+ oldDirection[motorNum] = dir;
46
+ }
47
+ controller.directWriteReg (motor[motorNum][1 ], speed);
48
+ if (errorCheck) {
49
+ controller.clearErrors ();
50
+ }
51
+ }
52
+
53
+
54
+ // ! connect motor between halfbridge 1 and halfbridge 2
55
+ void setup ()
56
+ {
57
+
58
+ Serial.begin (9600 );
59
+ while (!Serial) {
60
+ ; // wait for serial port to connect. Needed for native USB port only
61
+ }
62
+ Serial.println (" TLE94112 Motor Speed Control Example" );
63
+ delay (1000 );
64
+
65
+ // Enable MotorController Tle94112
66
+ // Note: Required to be done before starting to configure the motor
67
+ controller.begin ();
68
+
69
+ // four half bridges and two PWM channels are used to control two motors
70
+ controller.configHB (controller.TLE_HB1 , controller.TLE_HIGH , controller.TLE_PWM1 );
71
+ controller.configHB (controller.TLE_HB2 , controller.TLE_HIGH , controller.TLE_PWM1 );
72
+ controller.configHB (controller.TLE_HB3 , controller.TLE_LOW , controller.TLE_NOPWM );
73
+ controller.configHB (controller.TLE_HB4 , controller.TLE_LOW , controller.TLE_NOPWM );
74
+
75
+ controller.configHB (controller.TLE_HB9 , controller.TLE_HIGH , controller.TLE_PWM3 );
76
+ controller.configHB (controller.TLE_HB10 , controller.TLE_HIGH , controller.TLE_PWM3 );
77
+ controller.configHB (controller.TLE_HB11 , controller.TLE_LOW , controller.TLE_NOPWM );
78
+ controller.configHB (controller.TLE_HB12 , controller.TLE_LOW , controller.TLE_NOPWM );
79
+
80
+ // all stop
81
+ controller.configPWM (controller.TLE_PWM1 , controller.TLE_FREQ200HZ , 0 );
82
+ controller.configPWM (controller.TLE_PWM3 , controller.TLE_FREQ200HZ , 0 );
83
+ controller.clearErrors ();
84
+
85
+ delay (1000 );
86
+
87
+ }
88
+
89
+
90
+ void loop () {
91
+ controller.clearErrors ();
92
+
93
+ Serial.println (" forward / backward" );
94
+ motorSet (0 , HH_LL, 100 );
95
+ motorSet (1 , LL_HH, 100 );
96
+ delay (1000 );
97
+
98
+ Serial.println (" speed up / down" );
99
+ motorSet (0 , HH_LL, 255 );
100
+ motorSet (1 , LL_HH, 50 );
101
+ delay (1000 );
102
+
103
+ Serial.print (" backward / forward " );
104
+ motorSet (0 , LL_HH, 255 );
105
+ motorSet (1 , HH_LL, 255 );
106
+ delay (1000 );
107
+
108
+ }
0 commit comments