PWM _ Duty Cycle in Tenths
I need to be able to get duty cycle of a PWM signal in Tenths. I currently am getting the duty cycle but only the whole number.
I get 37% instead of 37.2 %
Here is my code, any help is appreciated.
This program gets the duty cycle from the input, displays it, then converts it to a 0-5 vdc output using RC.
Here is the Clip.....
DEFINE OSC 20
'Frequency Input Channels
'Voltage output channels
VOut1 VAR PORTA.0
'Variables and Constants
pHigh VAR WORD ' W3 high pulse width
pLow VAR WORD ' W2 low pulse width
period VAR WORD ' cycle time (high + low)
T2 VAR WORD
T3 VAR WORD
Channel VAR BYTE
PulseLen CON 30 'Pulsein Time Less than 30 causes no readings
'Setup Max Channels to Scan
' -------------------------------------------------------------------------
DEFINE DEBUG_REG PORTB
DEFINE DEBUG_BIT 7
DEFINE DEBUG_BAUD 38400
DEFINE DEBUG_MODE 1
DEFINE PULSIN_MAX 750
'Set initial pin states
Output VOut1
'MAIN
Loop:
T2 = 0
T3 = 0
PulsIn F_pin1, 1, pHigh ' get high portion of input
PulsIn F_pin1, 0, pLow ' get low portion of input
Period = pHigh + pLow ' calculate cycle width
IF Period = 0 Then
Period = 0
T2 = 0
T3 = 0
GoTo DoIt
EndIF
t2 = plow
t3 = phigh
t3 = t2 + t3
t3 = t3 / 10
t2 = t2 * 10
t2 = t2/t3
DoIt:
Debug "Ch#:",#channel," DC:",#t2,13,10
t2 = t2 * 255 ' Converts % Duty cycle to a 255 step resolution
t2 = t2 / 100
PWM VOut1, t2,PulseLen
GoTo Loop