H-Bridge Controller Using Raspberry Pi Pico
Author | Message | ||||
Solar Mike Guru ![]() Joined: 08/02/2015 Location: New ZealandPosts: 963 |
Here is a sample of the code that generates a spwm lookup table of sine values; each row is in format of a tuple containing 60 values for 180 degrees of a sine wave at a fixed modulation depth using 10 bit modulation. The rows start at 70% mod depth and end at 100%. The data is passed into 2 of the high speed PIO state machines to output the SPWM on gp pins 0 1nd 1. All this occurs on CPU0. CPU1 is used to update the index into the table running on a separate thread, currently it just loops through the mod depths and outputs a number that causes the sine output loop to switch to a different table index; this would be part of a feedback loop. Note code on CPU1 must not use more memory, ie use globals as there is no proper thread OS to manage things. I hade to make use of the garbage collector to free up memory generating the table data, as the PICO was running out. Variable frequency is easily done by altering the sleep_us(value) in the main sine output loop, the current value of 150 = 50 Hz, lowering it speeds up proportionately. Note: this is my first attempt at writing ANY python code, so it may not be the most efficient way to accomplish things, any python experts out there feel free to let me know how to speed it up. Cheers Mike SPWM_1.zip |
||||