M4 Micro OneNote
M4 Micro OneNote
To output triangular wave, the DAC also needs to output continuous curve, so a large number of
sampling points are required, but not as many as sine wave.
Here, I use python program to generate 128 sampling points, that is, a triangular wave period is
divided into 128 points.
The Python program for generating triangular wave sampling points is as follows:
POINT_NUM = 64
step=4096/POINT_NUM
a=[]
r=[]
for i in range(POINT_NUM):
a.append(int(i*step))
for i in range(POINT_NUM):
a.append(4096-int(i*step))
print(len(a))
At User Code Begin 0, create a new TriData array to store the generated sampling point data.
uint32_t TriData[]={0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 640,
704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472,
1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176, 2240,
2304, 2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816, 2880, 2944, 3008,
3072, 3136, 3200, 3264, 3328, 3392, 3456, 3520, 3584, 3648, 3712, 3776,
3840, 3904, 3968, 4032, 4090,4090, 4032, 3968, 3904, 3840, 3776, 3712,
3648, 3584, 3520, 3456, 3392, 3328, 3264, 3200, 3136, 3072, 3008, 2944,
2880, 2816, 2752, 2688, 2624, 2560, 2496, 2432, 2368, 2304, 2240, 2176,
2112, 2048, 1984, 1920, 1856, 1792, 1728, 1664, 1600, 1536, 1472, 1408,
1344, 1280, 1216, 1152, 1088, 1024, 960, 896, 832, 768, 704, 640, 576,
512, 448, 384, 320, 256, 192, 128,64,0};
for(int x=0;x<sizeof(TriData)/sizeof(TriData[0]);x++){
HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_
12B_R,TriData[x]);
}
}
The results are as follows:
1. Components Needed:
- Temperature sensor: You can use a thermistor or any other analog temperature sensor.
- Microcontroller or PLC: Choose a suitable microcontroller or PLC with ADC capabilities.
- Relay module: To control the heating or cooling device (e.g., a heater or a fan).
- Power supply and other necessary components.
2. Sensor Connection:
Connect the analog output of the temperature sensor to one of the ADC input pins of the
microcontroller or PLC. Ensure the power supply for the sensor is connected properly.
3. ADC Configuration:
Set up the ADC on the microcontroller or PLC to read analog values from the connected pin. Refer
to the documentation or programming guide of your specific controller to configure the ADC module
accordingly.
4. Setpoint and Hysteresis:
Determine the desired temperature setpoint for your system. Additionally, decide on the
hysteresis value, which is the temperature difference allowed before triggering the heating or
cooling device. For example, if the setpoint is 25°C and the hysteresis is 2°C, the device will turn on
when the temperature falls below 23°C and turn off when it exceeds 27°C.
5. Programming Logic:
Write the programming logic to read the ADC values, convert them to temperature values using
appropriate calibration, and compare them with the setpoint and hysteresis values. Based on the
comparison, control the relay module to turn the heating or cooling device on or off.
Here's a basic example pseudocode for the programming logic:
loop:
read ADC value
convert ADC value to temperature
if temperature < setpoint - hysteresis:
turn on heating/cooling device
else if temperature > setpoint + hysteresis:
turn off heating/cooling device
else:
maintain current state
delay for a certain period
This logic continuously reads the ADC values, converts them to temperature values, and controls the
heating or cooling device based on the setpoint and hysteresis.
6. Testing and Refinement:
Upload the code to the microcontroller or PLC and test the temperature controller. Make any
necessary adjustments to the setpoint, hysteresis, or programming logic to ensure accurate
temperature control.