shabaz designed a development kit for the recent Texas Instruments MSPM0 microcontroller series.
|
Goal of this 3nd post
- add PWM generation logic, based on PWM example for the EasyL1105 MSPM0 board
- drive duty cycle from PID's output signal
not a goal of this post: have the PID regulation working 100%.
Set up PWM SysConfig
The code uses timer TIMG1, channel 0, to drive PA26. There's no interrupt involved. The duty cycle gets adjusted in the regulation loop.
At this point, I have set the PWM period count to 65535, in an attempt to have the output range identical (but 32768 offset) to the input.
Code
note: this design doesn't regulate perfectly yet. At this point in the blog series, all modules are in place and they are tied to the PID. But finetuning PID parameters, input, output, ADC and PWM settings is for post 4.
void perform_pwm() { DL_TimerG_setCaptureCompareValue(PWM_0_INST, i32_Output_PID + 32768, DL_TIMER_CC_0_INDEX); // update ccr0 value } int main(void) { SYSCFG_DL_init(); // /* timer 5 interrupt ticks per second */ // /* Enable Timer0 NVIC */ NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN); NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN); gCheckADC = false; /* Initialize the parameters of PID */ Initialize_PID_Parameter(); /* Start PWM */ DL_TimerG_startCounter(PWM_0_INST); /* Start Timer counting */ DL_TimerG_startCounter(TIMER_0_INST); while (1) { if (perform) { perform = false; DL_GPIO_setPins(GPIO_GRP_LEDS_PORT, GPIO_GRP_LEDS_PIN_LED_GREEN_PIN); perform_adc(); perform_pid(); perform_pwm(); DL_GPIO_clearPins(GPIO_GRP_LEDS_PORT, GPIO_GRP_LEDS_PIN_LED_GREEN_PIN); } } }
void perform_pid() { /* Execute PID control in every TM0 interrupt. */ i32_Output_PID = PID(&PID_Var, i32_Target_Command, gAdcResult); }
If you compare the perform_pid() with the one from the previous post, you 'll see that it now uses ADC as feedback, and output to drive the PWM.
Demo circuit
I used the same (random) low pass filter as in MSP432 and TI-RTOS: PID Library Part 2 - Real World Example , to turn the PWM into a DC signal.
The filter sits between PWM out (P26) and ADC in (PA25).
ccs project for EasyL1105: pid_EasyL1105_20251002_02.zip