795us over 45ms is 1.76%, not 17.6% as I wrote earlier.
Anyway, here's a piece of code that may do what you want. It uses TMR1 and CCP1 to measure the input signal, calculates the dutycycle and displays it with .1% resolution. It compiles for a 16F88 but I have not tested it.
Code:
' Program expects 4MHz oscillator.
' CCP1 inputpin is either RB0 or RB3 depending on the CCPMX bit in CONFIG.
CapRise CON %00000101 ' Value to configure CCP1 for capturing every rising edge
CapFall CON %00000100 ' Value to configure CCP1 for capturing every falling edge
CCP1IF VAR PIR1.2 ' Alias to CCP1 Interrupt flag
Period VAR WORD
Width VAR WORD
Duty VAR WORD
Dummy VAR WORD
T1 VAR WORD
T2 VAR WORD
T3 VAR WORD
T1CON = %00100001 ' TMR1 clock from FOsc/4, Prescaler 1:4
Main:
' Capture the the time where the pulse starts
CCP1CON = CapRise
CCP1IF = 0
WHILE !CCP1IF : WEND
T1.HIGHBYTE = CCPR1H : T1.LOWBYTE = CCPR1L
' Capture the time where the pulse ends
CCP1CON = CapFall
CCP1IF = 0
WHILE !CCP1IF : WEND
T2.HIGHBYTE = CCPR1H : T2.LOWBYTE = CCPR1L
' Capture the time where the period ends (which is when the next pulse starts)
CCP1CON = CapRise
CCP1IF = 0
WHILE !CCP1IF : WEND
T3.HIGHBYTE = CCPR1H : T3.LOWBYTE = CCPR1L
Width = T2 - T1
Period = T3 - T1
Dummy = Width * 1000
Duty = DIV32 Period
' Duty is now in units of .1% (111 = 11.1%)
LCDOUT $FE, 1, "W:", DEC Width, " P:", DEC Period
LCDOUT $FE, $C0, "Duty:", DEC Duty/10, ".", DEC Duty//10, "%"
Goto Main
/Henrik.
Bookmarks