Code Basic4GL - [expand] 1 2 3 4 5 6 7 8 9 ----------------- picbasic pro: counter=0 loop: duty=sin[(counter/100)] ' to compesate decimal handle in above lines PWM pin,duty,1 ´ PWM pin,duty,cycle at 11,718 Hz counter=counter+ n ' 127 cicles n=1, n= 127/195= 0.65 -> 65 if counter= 19500 then counter=0 goto loop -----------------
16F628A has 128 EEPROM bytes, so I'd have to handle 1/4 of sine cycle and decrement pointer to handle about 100 data table. So it is possible but some tricky, and maybe I liked how short is this program without memory and table handles.Question is, do you have enough CPU / MCU cycles per PWM
>period to perform this calculation?
I don't have the machine cycles reference for each picbasic instruction, so I'm supossing that simple ones take 10 cycles and complex as if of goto takes 20 cycles. That plus 30 extra cycles gives me 100 cycles, which I estimate about 7 uS. Thats why I say I have to compensate it, otherwise it would be added to the 'switch off' of the IGBTs I plot to use, so I have to sustain 3 uS a high value of duty at each side of the sinus maximum. But I'd like to know first if this idea you think can work in order to go implementing with a little more confidence.
>Calculating it once and storing a table is more CPU-efficient,
>less memory-efficient. Which do you have more of, resource-wise?
Easier to store the sine table in program memory than EEPROM. I don't know PicBasic but the equivalent in 'C' of making it a 'const' table.16F628A has 128 EEPROM bytes, so I'd have to handle 1/4 of sine cycle and decrement pointer to handle about 100 data table.
I'm not experienced with PIC, but each instruction set (in the datasheet or in an extra document) should show the count of clock cycles per instruction.Is there a picbasic document who gives us the time of execution of each instruction in the loop as the program showed?.
Hi,
I recommend to use PWM ... and PWM hardware. ...
Klaus
I'd assume it is short for Hardware PWMHPWM .... never heared about it.....you mentioned it for the first time now.
Code dot - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 ARRAYWRITE SineWave {0,2,5,8,10,13,16,19,21,24,27,29,32,34,37,39,42,44,46,49,51,53,55,57,59,61,63,65,67,68,70,71,73,74,75,77,78,79,80,81,82,82,83,84,84,84,85,85,85} ' uS (excel calculated originally for HPWM but anyway it most work 'INITIAL SETTINGS OF 16F628A, WILL BE HERE CLOCK 12 MHz b0 VAR BYTE ' array's pointer Dir VAR BIT ' pointer direction MAXIT = [MAX SineWave Count] ' dir =0 b0=0 ´set off PINs MAIN: 'BRANCH 1 PIN = RB0 ' DEFINE PORT (ver circuito) DO HALF_CYCLE pauseus 40 'histeresys to set off IGBTs 'BRANCH 2 PIN = RB1 ' DEFINE PORT Do HALF_CYCLE pauseus 40 'histéresis GOTO MAIN HALF_CYCLE: ' each cycle = 1/195 pulses FIN=0 DO WHILE FIN=0 ON= SineWave[b0] OFF = 85 - ON 'this makes a fixed pulse width (uS) PULSOUT PIN, ON ' send pulse by PIN, width as table elem. 'pulse width 1/11,700 + calc Pauseus OFF IF Dir = 0 THEN ' increment or decrement pointer b0 = b0 + 1 ELSE b0 = b0 - 1 ENDIF IF (b0 = MAXIT) THEN 'changes pointer direction or ends half second of one branch Dir = 1 ENDIF IF (b0 = 0) AND (Dir = 1) THEN Dir = 0 FIN = 1 ENDIF END WHILE RETURN END SUB '----------------
I think it will work but the waveform will be quite distorted....
there may already be a command for doing that in PicBasic.
Brian.
Code Basic4GL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 'This is a version for Sinus wave inverter with hardware PWM module ' in uController 'It alternates pins, each one responsible of a branch on a H bridge 'half positive cycle on 96 program cycles 'Table of 1/4 cycle, 48 pulses then it decrement pointer to get 1/2 cycle 'Then it repeats but for the other H branch 'Pendant to shorten to compensate program time, taking some time 'to the off time of cycle Luis Jiménez 'It can be done a timers and interrupt version (not pulsout) ARRAYWRITE SineWave {0,8,16,24,32,40,48,56,64,72,80,88,96,103,111,118,125,132,139,146,152,159,165,171,177,183,189,194,199,204,209,214,218,222,226,230,233,236,239,242,244,246,248,250,251,253,253,254,254,255} ' duty 0 - 255 b0 VAR BYTE ' array's pointer Dir VAR BIT ' pointer direction Selector VAR BIT ' phisical branch selection RB2 -> Selector MAXIT = [MAX SineWave Count] ' dir =0 b0=0 ´Apagar PINs pendiente MAIN: 'BRANCH 1 Selector = 0 DO HALF_CYCLE pauseus 40 'histéresis para OFF de IGBTs 'BRANCH 2 Selector = 1 Do HALF_CYCLE pauseus 40 'histéresis GOTO MAIN HALF_CYCLE: ' each cycle = 1/195 pulses FIN=0 WHILE FIN=0 ON= SineWave[b0] HPWM chan1, ON, 11718 ' send pulse by chan1, duty as table elem. 'pulse width 1/11,700 + calc IF Dir = 0 THEN ' increment or decrement pointer b0 = b0 + 1 ELSE b0 = b0 - 1 ENDIF IF (b0 = MAXIT) THEN 'changes pointer direction or ends half second of one branch Dir = 1 ENDIF IF (b0 = 0) AND (Dir = 1) THEN Dir = 0 FIN = 1 ENDIF WEND RETURN RESUME '--------------------
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?