Code:
'****************************************************************
'* Name : freqgen.BAS *
'* Author : Rob Lane *
'* Notice : Copyright (c) 2011 *
'* : All Rights Reserved *
'* Date : 09/08/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
eeprom 4,[$10,$0e] ' program in first run freq value 3600Hz (lowest)
include "Modedefs.bas" ' for lcd baud rate
define OSC 10 ' for lcd baud rate 9600
Duty var byte 'duty cycle value (max 255)
Freqval var word 'frequency variable
A2 var byte 'Button' variable
Counter1 var byte
PercVal var word
PercVal2 Var byte
' Control Buttons/Lines
' ----------------------
TRISA=%00011111 ' button pins
TRISB=%00000000 ' port b as outputs
CMCON=7 ' comparator ports to digital
Pause 500 ' lcd to settle
read 4,Freqval.byte0 'read previously stored freq
read 5,Freqval.byte1 'from eeprom at power up
SERout PortB.7,N9600,[254,1] 'lcd CLS
pause 10 ' needed after cls command
Duty=64 ' set HPWM duty cycle
A2=0 ' set button debounce
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1
PercVal=(Duty*1000)/256
PercVal2=PercVal dig 0
PercVal=(Duty*100)/256
mainloop:
serout PortB.7,N9600,[254,128] 'lcd line 1 home
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
serout PortB.7,N9600,[254,192] 'lcd line 1 home
SERout PortB.7,N9600,["Duty cycle: ",#PercVal ,".",#PercVal2,"%" ]
Button PortA.0,0,255,0,A2,1,upfaster
Button PortA.1,0,255,0,A2,1,downfaster
Button PortA.3,0,255,0,A2,1,upslow
Button PortA.4,0,255,0,A2,1,downslow
high PortB.0 '== LED on
pause 500 '== blink led to show in loop
low PortB.0 '== LED off
pause 500 '== for testing purposes
goto mainloop
freqgenoff:
TRISB=%00001000 'turn off hpwm register
goto mainloop
freqsave:
write 4,Freqval.byte0 'save freq to eeprom
write 5,Freqval.byte1 'at button press end and
goto mainloop 'return to main loop
'UP FAST ===============================
upfaster:
read 4,Freqval.byte0
read 5,Freqval.byte1
upfast:
if PortA.0=1 then freqsave ' 1st trap exit to 'save' loop
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval+100 'increment freq
if Freqval>=32000 then freqval=32000 'hold if at highest freq figure
pause 5 'slows looping
if PortA.0=1 then freqsave '2nd trap exit to 'save' loop
goto upfast
'DOWN FAST ===============================
downfaster:
read 4,Freqval.byte0
read 5,Freqval.byte1
downfast:
if PortA.1=1 then freqsave ' 1st trap exit to 'save' loop
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval-100 'decrement freq
if Freqval<=700 then freqval=700 'hold if at highest freq figure
pause 5 'slows looping
if PortA.1=1 then freqsave '2nd trap exit to 'save' loop
goto downfast
'UP Routine ============================
upslow:
read 4,Freqval.byte0
read 5,Freqval.byte1
counter1=1
slowup:
For counter1 = 1 to 5 ' if press longer than 5 then speedup
if PortA.3=1 then freqsave ' 1st trap exit to 'save' loop
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval+1 'increment freq
if Freqval>=32000 then freqval=32000 'hold if at highest freq figure
pause 500 'slows looping
if PortA.3=1 then freqsave '2nd trap exit to 'save' loop
next counter1
goto frequp
frequp:
if PortA.3=1 then freqsave ' 1st trap exit to 'save' loop
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1 @ 25% duty cycle(64)
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval+10 'increment freq
if Freqval>=32000 then freqval=32000 'hold if at highest freq figure
pause 50 'slows looping
if PortA.3=1 then freqsave '2nd trap exit to 'save' loop or
if PortA.3=0 then frequp 'loop frequp
'DOWN Routine ==========================
downslow:
read 4,Freqval.byte0
read 5,Freqval.byte1
counter1=1
slowdown:
For counter1 = 1 to 5
if PortA.4=1 then freqsave ' 1st trap exit to 'save' loop
hpwm 1,Duty,Freqval 'output freq to hardware pwm channel 1 @ 25% duty cycle(64)
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval-1 'decrement freq
if Freqval<=700 then freqval=700 'hold if at lowest freq figure
pause 500 'slows looping
if PortA.4=1 then freqsave '2nd trap exit to 'save' loop
next counter1
goto freqdown
freqdown:
if PortA.4=1 then freqsave
hpwm 1,Duty,Freqval
serout PortB.7,N9600,[254,128]
SERout PortB.7,N9600,["Frequency: ",#Freqval ]
Freqval=Freqval-10
if Freqval<=700 then freqval=700 ' hold if at lowest freq figure
pause 50
if PortA.4=1 then freqsave
if PortA.4=0 then freqdown
end
Bookmarks