if they are constant... you can use HPWM on 2 channel... 2 different duty but the same frequency. They will run in background and they will not be interfered by anything else.
OR you can use the internal TIMER1 16bit and do you delay stuff when you get TIMER1 overflow interrupt.
at 3.6 KHZ period = 277.777 uSec
let's say you'll use a 20Mhz crystal... internal frequency 5MHZ period = 0.2 uSec = 1388.888 internal clock count. We want to change state every 1/2 period. 1388.888 uSec / 2 = 694.4444 uSec. So you'll have to preload Timer1 to 65535-694 = 64841 or $FD49.
here's a snippet to produce 3 different duty cycle with TIMER1
Code:
' program to generate Multiple duty cycle @ 5 mhz on PORTC
' Use of PIC18F2220
'
@ __CONFIG _CONFIG1H, _HS_OSC_1H
' HS
@ __CONFIG _CONFIG2L, _BORV_20_2L & _BOR_ON_2L & _PWRT_ON_2L
' BOR Voltage - 2.0v
' Brown-Out Reset enabled
' Power-Up Timer enabled
@ __CONFIG _CONFIG2H, _WDT_ON_2H
' Watch Dog Timer enabled
@ __CONFIG _CONFIG3H, _MCLRE_ON_3H & _PBAD_DIG_3H & _CCP2MX_ON_3H & _CCP2MX_B3_3H
' MCLR enabled
' PORTB<4:0> pins reset as digital pins
' CCP2 pin function on RC1
' CCP2 pin function on RB3 (alt defn)
@ __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVR_ON_4L
' DEBUGger disabled
' Low Voltage Prgramming disabled
' Stack over/underflow Reset enabled
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
' Block 0 readable/ may be writable
' Block 1 readable/ may be writable
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
' Boot Block readable / may be writable
' Data EE memory readable / may be writable
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
' Block 0 writable
' Block 1 writable
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
' Config registers writable
' Boot block writable
' Data EE writable
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
' Block 0 readable
' Block 1 readable
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H
' Boot block readable
DEFINE OSC 20
TRISC=0
INTCON = %01000000 ' enable peripheral interrupt
PIE1 = %00000001 ' enable TIMER1 overflow interrupt
TMR1L=$49
TMR1H=$FD
T1CON = %00000101 ' prescaler 1:1
' Clock source = Fosc/4
' start the timer1
on interrupt goto TMR1_Overflow
Passes var bit
clear
START:
goto start
disable
TMR1_Overflow:
T1CON.0=0 'stop timer
PIR1.0=0 ' reset interrupt flag
TMR1l=$49
TMR1H=$FD
T1CON.0=1 'start timer
if Passes then
PORTC = 6
pauseus 69
PORTC = 4
passes= 0
else
PORTC = 1
passes= 1
endif
resume
enable
you may need to adjust the value of the TIMR1L to fine tune your frequency.
To calculate or to know the time an instruction take, you can use MPLAB's stopwatch... give a great aprox of the time.
Bookmarks