ON 20010723@2:06:21 PM at page: http://www.piclist.org/techref/member/HB-operamail-885/index.htm HB-operamail-885 Helge Buen added 'Code: ; PIC 3-Phase generator by Helge Buen - buen@operamail.com ; I borrowed and altered the table lookup and it's driver code. It is Copyright 1995 Eric Smith ; The code is not tested in practice. ; Fixed PWM frequency 3-phase sine generator for electric motors etc.. The output frequencies are generated by jumps in the ; sine lookup table (patterns). For example the lowest frequency will repeat each value 16 times (4096 values). ; The highest possible will have 15 and 16 step jumps (only 16.5 values, maybe too few, not tested). ; Electric motors needs the voltage to follow the frequency. Therefore each sine value is multiplied by a factor (gain). ; The frequency (speed) and the gain variables are INDEPENDENT in the code. The simplest and most suitable would be to ; right rotate the gain (without carry) into the speed variable. At a gain of 255 the controller would output 36 values at ; full voltage. If one want some voltage boost (motor torque) at particular conditions that is easy to implement too. ; Of course it would be interesting to monitor the speed of the motor and REDUCE voltage to save power on battry apps. ; This code generates 3 values ranging from 2 - 128 and 128-254. 128 is 0 volt and corresponds to the 50% PWM ratio (bridge). ; A separate PWM controller is required as it have to output some thousand pulses a second at 256 resolution ; The PWM controller should have fixed instruction number, master the time and clock the sine generator (handshake). ; It is an good idea to centre the PWM pulses to avoid simultaneously switching (noise). ; No speed ramping (frequency increment) is implemented, it should correspond the mecanical application. However increasing ; the speed variable by one each revolution would be no problem. It is possible to change the speed/gain at each PWM pulse, ; be aware! The 3 motor voltages should be output simultaneosly at the end. list p=16c55a ; Include file, change directory if needed include "p16c5x.inc" zero EQU H'0008' temp EQU H'0009' angle EQU H'000A' gain EQU H'000B' volt1 EQU H'000C' volt2 EQU H'000D' volt3 EQU H'000E' speed EQU H'000F' lsbpos EQU H'0010' ; Start at the reset vector org 0x000 GOTO start sinetbl: addwf PCL,f retlw 000h retlw 003h retlw 006h retlw 009h retlw 00Ch retlw 010h retlw 013h retlw 016h retlw 019h retlw 01Ch retlw 01Fh retlw 022h retlw 025h retlw 028h retlw 02Bh retlw 02Eh retlw 031h retlw 033h retlw 036h retlw 039h retlw 03Ch retlw 03Fh retlw 041h retlw 044h retlw 047h retlw 049h retlw 04Ch retlw 04Eh retlw 051h retlw 053h retlw 055h retlw 058h retlw 05Ah retlw 05Ch retlw 05Eh retlw 060h retlw 062h retlw 064h retlw 066h retlw 068h retlw 06Ah retlw 06Bh retlw 06Dh retlw 06Fh retlw 070h retlw 071h retlw 073h retlw 074h retlw 075h retlw 076h retlw 078h retlw 079h retlw 07Ah retlw 07Ah retlw 07Bh retlw 07Ch retlw 07Dh retlw 07Dh retlw 07Eh retlw 07Eh retlw 07Eh retlw 07Fh retlw 07Fh retlw 07Fh retlw 07Fh start: clrf zero clrf angle clrf lsbpos movlw d'127' movwf speed ; Desired motor frequency. Values above 127 may generate too few sine values?? (<36 a revolution) movlw d'255' movwf gain ; Desired motor voltage. Should follow the frequency (max desired speed -> gain=255) frame movf angle,w movwf temp ; copy the angle btfsc temp,6 ; is angle in the 2nd or 4th quadrant? subwf zero,w ; yes, complement it to reduce to 1st or 3rd andlw 07fh ; reduce to 1st quadrant call sinetbl ; get magnitude clrf volt1 ; empty the output bcf STATUS,C ; Multiply the gain.. btfsc gain,0 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,1 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,2 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,3 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,4 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,5 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,6 addwf volt1,f rrf volt1,f bcf STATUS,C btfsc gain,7 addwf volt1,f rrf volt1,w btfsc temp,7 ; was angle in 3rd or 4th quadrant? subwf zero,w ; yes, complement it xorlw d'128' ; align to center movwf volt1 movlw d'85' ; 120 degrees offset for phase 2 addwf angle,w movwf temp btfsc temp,6 ; is angle in the 2nd or 4th quadrant? subwf zero,w ; yes, complement it to reduce to 1st or 3rd andlw 07fh ; reduce to 1st quadrant call sinetbl ; get magnitude clrf volt2 ; empty the output bcf STATUS,C ; Multiply the gain.. btfsc gain,0 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,1 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,2 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,3 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,4 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,5 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,6 addwf volt2,f rrf volt2,f bcf STATUS,C btfsc gain,7 addwf volt2,f rrf volt2,w btfsc temp,7 ; was angle in 3rd or 4th quadrant? subwf zero,w ; yes, complement it xorlw d'128' ; align to center movwf volt2 movlw d'170' ; 240 degree offset for phase 3 addwf angle,w movwf temp btfsc temp,6 ; is angle in the 2nd or 4th quadrant? subwf zero,w ; yes, complement it to reduce to 1st or 3rd andlw 07fh ; reduce to 1st quadrant call sinetbl ; get magnitude clrf volt3 ; empty the output bcf STATUS,C ; Multiply the gain.. btfsc gain,0 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,1 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,2 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,3 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,4 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,5 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,6 addwf volt3,f rrf volt3,f bcf STATUS,C btfsc gain,7 addwf volt3,f rrf volt3,w btfsc temp,7 ; was angle in 3rd or 4th quadrant? subwf zero,w ; yes, complement it xorlw d'128' ; align to center movwf volt3 movf speed,w ; Lookup table address generator.. addwf lsbpos,f btfsc STATUS,DC incf angle,f swapf speed,w andlw d'15' addwf angle,f goto frame END ' ON 20010723@2:38:16 PM at page: http://www.piclist.com/techref/member/HB-operamail-885/index.htm HB-operamail-885 Helge Buen added 'See also Pages: /techref/microchip/io/dev/motors.htm '