
Originally Posted by
HenrikOlsson
Hi,
Several things.....
TMR2 is the timer used for the PWM generation. The fact that you initially load it with 117 doesn't matter for either the PWM frequency or your interrupt frequency since you're using TMR1 to generate the interrupts.
You have the prescaler for TMR2 set to 1:4 and PR2 set to 13 which indeed gives you ~18kHz PWM frequency but it'll only give you a give you 56 discrete dutycycles. Ie. the maximum value you can put in the dutycycle register is 56. The highest value in your sine table is 242 which you then shift right one time bofore putting in the CCP1L, so you're trying to put 121 in there when 56 is max.
Changing the prescaler to 1:1 and putting 55 in PR2 gives you the same PWM frequency but allows dutycycle values of 0-222.
TMR1 is now setup with a prescaler of 1:1 and you reload it with 64980 making it interrupt every 65536-64980=556us or at a frequency of 1798Hz, there's now 36 steps to your "cycle" so 1798/36=49.96Hz so it seems to be correct. I don't think you should expect to be able to run much faster than that at 4Mhz, the DT-ints takes quite afew cycles to save and restore all the system variables on each interrupt so you won't have much time left.
The "accuracy thing" needs to be performed on each reload or it won't have much effect. It should be in your ISR instead of the reload code you have there now.
/Henrik.
i've the changes in the code as you suggested above,but the waveform is still not smooth....Simulation results..pdf
Code:
define OSC 4
;*****************VARIABLE DECLARATION******************************************
wsave VAR BYTE $70 SYSTEM ' alternate save location for W
wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
STEPCOUNT var byte 'Define stepcount as byte type variable
stepcount = 36
;**************SETTING THE REGISTERS WITH APPROPIATE BIT DEFINITION*************
ADCON0 = %00000000
ADCON1 = %00000000 ;all anolog output
trisb = %11111111 ;define porta as input
trisc = %11111011 ;make ccp1/portc.2 an output pin
trisa = %11111111 ;define porta as input
TMR2 = 117
PR2 = 55 ;set for 18Khz HPWM(=36 steps*10 times*50hz)
CCP1CON = %000001100 ;set to pwm mode
T2CON=%00000100 ;enable timer2 and set timer2 prescaler value of 1:1
;****************A sine lookup table in an array********************************
sineval var byte[36]
sineval[0] = 128
sineval[1] = 148
sineval[2] = 167
sineval[3] = 185
sineval[4] = 201
sineval[5] = 215
sineval[6] = 227
sineval[7] = 235
sineval[8] = 240
sineval[9] = 242
sineval[10] = 240
sineval[11] = 235
sineval[12] = 227
sineval[13] = 215
sineval[14] = 201
sineval[15] = 185
sineval[16] = 167
sineval[17] = 148
sineval[18] = 128
sineval[19] = 108
sineval[20] = 89
sineval[21] = 71
sineval[22] = 55
sineval[23] = 41
sineval[24] = 29
sineval[25] = 21
sineval[26] = 16
sineval[27] = 14
sineval[28] = 16
sineval[29] = 21
sineval[30] = 29
sineval[31] = 41
sineval[32] = 55
sineval[33] = 71
sineval[34] = 89
sineval[35] = 108
timerone var word
TimerShadow VAR WORD
INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
;********Define INT_Handler as ISR**********************************************
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _sine, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
T1CON = %000001 ; Prescaler = 1, TMR1ON
TMR1L = 255
TMR1H = 254
@ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
timerone = 64980 ;gives about 50 hz sine
Main:
pause 5
GOTO Main
'---[TMR1_INT - interrupt handler]------------------------------------------
sine:
'accuracy thing
T1CON.0 = 0 'Stop TMR1
TimerShadow.HighByte = TMR1H
TimerShadow.LowByte = TMR1L
TimerShadow = TimerShadow + Timerone
TMR1H = TimerShadow.HighByte
TMR1L = TimerShadow.LowByte
T1CON.0 = 1 'Restart TMR1
TMR1L = timerone.byte0
TMR1H = timerone.byte1
CCPR1L = sineval[STEPCOUNT]>>1
stepcount = stepcount -1
if stepcount = 0 then stepcount = 36
@ INT_RETURN
Bookmarks