Code:
@ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF
@ __CONFIG _CONFIG2, _LVP_OFF
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
Osccon = %01111010 'sets the internal oscillator to 16Mhz
DEFINE OSC 16
TrisC.5 = 0 'Pin2 (HPWM) an output
CM1CON0 = 0 ' COMPARATORS OFF
CM2CON0 = 0 ' COMPARATORS OFF
tuning_word VAR word
accumulator VAR WORD
out VAR BYTE
'HPWM SETTINGS uses timer 2
CCP1CON = %00001100 'Turn HPWM on on CCP1
CCPR1L.6 = 0 'only using 8 bit PWM so clear the top two bits
CCPR1L.7 = 0 'only using 8 bit PWM so clear the top two bits
PR2 = 79 'this PWM frequency of 50khzKHz allows a maximum of 320 values
T2CON = %00000100 'TIMER2 ON 1:1 PRESCALER 1:1 POSTSCALER
' setsup an interrupt based on Timer4 overflowing (timer4 will overflow at 20,000 times per second, see further)
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR4_INT, _DDS, asm, YES
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
T4CON.2 = 1 ' Timer4 on
PR4 = 199 ' this should yield an exact 'interrupt rate of 20khz' at 16Mhz.
ACCUMULATOR = 0 ' clear down the accumulator before starting.
@ INT_ENABLE TMR4_INT
TUNING_WORD = 1638 ' this sets the required output frequency (tuning_word value = req_freq/20,000 * 65536) 1638 = 500hz
'CCP1CON.4 = Test.0 'Bit 0
'CCP1CON.5 = Test.1 'Bit 1
'CCPR1L = Test >> 2 'Bit 2-7
Main:
pause 1
goto main
END
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DDS:
'toggle PortA.5
ACCUMULATOR = ACCUMULATOR + tuning_word
' ok, lookup the high byte of the accumulator....
Lookup Accumulator.HighBYTE, [$80,$83,$86,$89,$8C,$8F,$92,$95,$98,$9C,$9F,$A2,$A5,$A8,$AB,$AE,$B0,$B3,$B6,$B9,$BC,$BF,$C1,$C4,_
$C7,$C9,$CC,$CE,$D1,$D3,$D5,$D8,$DA,$DC,$DE,$E0,$E2,$E4,$E6,$E8,$EA,$EC,$ED,$EF,$F0,$F2,$F3,$F5,$F6,$F7,$F8,$F9,$FA,$FB,$FC,$FC, _
$FD,$FE,$FE,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FE,$FE,$FD,$FC,$FC,$FB,$FA,$F9,$F8,$F7,$F6,$F5,$F3,$F2,$F0,$EF,$ED,$EC, _
$EA,$E8,$E6,$E4,$E2,$E0,$DE,$DC,$DA,$D8,$D5,$D3,$D1,$CE,$CC,$C9,$C7,$C4,$C1,$BF,$BC,$B9,$B6,$B3,$B0,$AE,$AB,$A8,$A5,$A2,$9F,$9C, _
$98,$95,$92,$8F,$8C,$89,$86,$83,$7F,$7C,$79,$76,$73,$70,$6D,$6A,$67,$63,$60,$5D,$5A,$57,$54,$51,$4F,$4C,$49,$46,$43,$40,$3E,$3B, _
$38,$36,$33,$31,$2E,$2C,$2A,$27,$25,$23,$21,$1F,$1D,$1B,$19,$17,$15,$13,$12,$10,$0F,$0D,$0C,$0A,$09,$08,$07,$06,$05,$04,$03,$03, _
$02,$01,$01,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01,$01,$02,$03,$03,$04,$05,$06,$07,$08,$09,$0A,$0C,$0D,$0F,$10,$12,$13, _
$15,$17,$19,$1B,$1D,$1F,$21,$23,$25,$27,$2A,$2C,$2E,$31,$33,$36,$38,$3B,$3E,$40,$43,$46,$49,$4C,$4F,$51,$54,$57,$5A,$5D,$60,$63, _
$67,$6A,$6D,$70,$73,$76,$79,$7C],Out
' now use that 'Out' value to change HPWM. duty cycle...
CCP1CON.4 = Out.0 'Bit 0
CCP1CON.5 = Out.1 'Bit 1
CCPR1L = Out >> 2 'Bit 2-7
@ INT_RETURN
Bookmarks