Help with pulsin and count mathematics

andrd

New Member
Hi m8's, I need some help related with data conversion, I'm working on a device that must be able to measure the pwm characteristics.
I used COUNT to get the frequency, and pulsin to get the duty cycle time.
Can anyone help me with code/mathematics to convert frequency to period (ex. 125hz = 8ms/cycle)
Then with period value and pulse duration I must get the pulse duration in percentage.
(Ex. 125hz @ 50% duty = 4ms duty cicle @ 8ms period)
My code is similar like this, it's already working putting data to display, but without period in ms and pulse duration in percentage.

COUNT C.2, 500, w0
Hserout ("freq", #b0, #b1 "Hz")
w0 = ?
Hserout ("period" , #b0, #b1, "ms")

Pulsin c.2 w0
Hserout ("tON=" , #b0, #b1, "ms")
W0= ???
Hserout ("Duty", w0, "%")

Thanks
André

inglewoodpete

Senior Member
COUNT C.2, 500, w0 provides the number of pulses in the 500mS period, resulting in a number 0 to 65535.

You will have some difficulties dividing large numbers with PICAXE's integer mathematics but the following code should get you started. I have inserted line 3 so that I could test my code in the simulator.
Rich (BB code):
``````SetFreq M8
hSerSetup B9600_8, %00001        'Configure to suit your serial requirements
Count C.2, 500, w0               'Pulses in 1/2 a second
w0 = 480                         '<<< Used for testing in simulator <<<
w1 = w0 * 2                      'Pulses in 1 second

hSerOut 0, ("Freq=", #w1, "Hz", CR, LF)      'Frequency in hertz
w1 = w1 / 10
w2 = 10000 / w1
BinToAscii w2, b15, b14, b13, b12, b11       'Convert to ACSII characters
hSerOut 0, ("Period=", b15, b14, b13, ".", b12, b11, "mS", CR, LF)``````
This produces the output in the simulator:
Freq=960Hz
Period=001.04mS

andrd

New Member
Thanks Pete, you helped me alot, your example worked like a charm.

My code now
20x2 + nextion + pwm generator

Rich (BB code):
``````SYMBOL PWM_SERIAL = c.1
SYMBOL ID_VAR = B2
SYMBOL ID_VAR_1 = B3
SYMBOL PROG_VAR = B4
SYMBOL PROG_VAR_1 = B5
SYMBOL PROG_VAR_WORD = W2
SYMBOL ANALOG_LO = B6
SYMBOL ANALOG_HI = B7
SYMBOL ANALOG_DATA = W3
SYMBOL OUT_BYTE = B8
SYMBOL RANGE_MULTIPLIER = B9
SYMBOL WAVE_ID = B10
SYMBOL REFRESH_STAT = B12
SYMBOL REFRESH_STAT1 = B13
SYMBOL B2ASCII_0XXXX = B18
SYMBOL B2ASCII_X0XXX = B19
SYMBOL B2ASCII_XX0XX = B20
SYMBOL B2ASCII_XXX0X = B21
SYMBOL B2ASCII_XXXX0 = B22
SYMBOL RAW_WORD_0 = W12
SYMBOL RAW_WORD_1 = W13
SYMBOL RAW_WORD_2 = W14
SYMBOL OUT_WORD_0 = W15
SYMBOL OUT_WORD_1 = W16
SYMBOL OUT_2_CTRL = C.5 `PASSA A C.6
SYMBOL OUT_3_CTRL = C.2
SYMBOL _5V_SWITCH = B.0
SYMBOL _12V_SWITCH = B.2
SYMBOL V_SEL_RLY = B.4
SYMBOL YE_PIN = C.3
#NO_TABLE
#NO_DATA

SETFREQ M32
HSERSETUP B9600_32, %001 ` = SERIN C.7, T9600_32, (B4,B5) CONFIGURA BACKGROUND SERIAL RECIEVE
PAUSE 16000
`-----------------------------------------------------------------------------------------------------------------------------------------------------------
HSEROUT 0, ("bkcmd=0", \$FF, \$FF, \$FF)
_MAIN:
LOW _5V_SWITCH
LOW _12V_SWITCH
SETINTFLAGS %00100000,%00100000
DO

IF ID_VAR = \$A1 THEN GOTO _PWM_GEN
IF ID_VAR = \$A2 THEN GOTO _VOLT_METER
IF ID_VAR = \$A3 THEN GOTO _MEDIR_CARGA
IF ID_VAR = \$A4 THEN GOTO _TESTAR_SIG
IF ID_VAR = \$A5 THEN GOTO _TESTAR_RVC
IF ID_VAR = \$A6 THEN GOTO _MEDIR_FREQ

LOOP
`---------------------------------------------------------------------------------------------------------------------- MEDIR CARACT PWM----------
_MEDIR_FREQ:

DO
IF ID_VAR = \$AA THEN
GOTO _MAIN
ENDIF
COUNT YE_PIN, 8000, ADC_VAR_WORD                         `THANKS TO PETE
HSEROUT 0, ("n2.val=", #OUT_WORD_0, \$FF, \$FF, \$FF)

ADC_VAR_WORD = ADC_VAR_WORD / 10    `HERE I CONVERT Hz IN PERIOD mS
BINTOASCII ANALOG_DATA, B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX, B2ASCII_XXX0X, B2ASCII_XXXX0

PULSIN YE_PIN, 1, RAW_WORD_0
PULSIN YE_PIN, 0, RAW_WORD_1

RAW_WORD_2 = RAW_WORD_0 + RAW_WORD_1 / 100    `HERE I CONVERT PULSIN OUTPUT DATA IN PERCENTAGE
OUT_WORD_0 = RAW_WORD_0 / RAW_WORD_2
OUT_WORD_1 = RAW_WORD_1 / RAW_WORD_2

IF ID_VAR = \$AA THEN
GOTO _MAIN
ENDIF

GOSUB PRINT_WORD
LOOP

`---------------------------------------------------------------------------------------------------------------------- TESTE CARGA -----
_MEDIR_CARGA:
DO

RANGE_MULTIPLIER = 4
WAVE_ID = 5
GOSUB PRINT_CH0
GOSUB PRINT_WAVE0

IF ADC_VAR > 163 AND ADC_VAR < 189 AND REFRESH_STAT1 = 0 THEN
GOSUB VIS_OK
ENDIF

IF ADC_VAR < 162 OR ADC_VAR > 190 AND REFRESH_STAT1 = 1 THEN
GOSUB VIS_NOK
ENDIF

IF ID_VAR = \$AA THEN
GOTO _MAIN
ENDIF

LOOP
`------------------------------------------------------------------------------------------------------------------------------ TESTE SIG ------------------

_TESTAR_SIG:
PROG_VAR_WORD = 125
GOSUB PRINT_FREQ

LOW _5V_SWITCH
HIGH V_SEL_RLY
HIGH _12V_SWITCH
HIGH OUT_2_CTRL
LOW OUT_3_CTRL
DO

RANGE_MULTIPLIER = 4

GOSUB PRINT_CH0

IF ADC_VAR > 163 AND ADC_VAR < 189 AND REFRESH_STAT1 = 0 THEN
GOSUB VIS_OK
ENDIF

IF ADC_VAR < 162 OR ADC_VAR > 190 AND REFRESH_STAT1 = 1 THEN
GOSUB VIS_NOK
ENDIF

IF ID_VAR = \$1A OR ID_VAR = \$1B OR ID_VAR = \$1C OR ID_VAR = \$1D OR ID_VAR = \$1E AND REFRESH_STAT = 1 THEN
GOSUB PRINT_DUTY
ENDIF

IF ID_VAR = \$AA THEN
PROG_VAR_WORD = 0
GOSUB PRINT_DUTY
PAUSE 50
GOSUB PRINT_FREQ
LOW _12V_SWITCH, OUT_2_CTRL, V_SEL_RLY
GOTO _MAIN
ENDIF

LOOP
`------------------------------------------------------------------------------------------------------------------- TESTE RVC -----------------------------

_TESTAR_RVC:

PROG_VAR_WORD = 140
GOSUB PRINT_FREQ
LOW _5V_SWITCH
HIGH V_SEL_RLY
HIGH _12V_SWITCH
HIGH OUT_2_CTRL
LOW OUT_3_CTRL

DO

RANGE_MULTIPLIER = 4
GOSUB PRINT_CH0

IF ADC_VAR > 163 AND ADC_VAR < 189 AND REFRESH_STAT1 = 0 THEN
GOSUB VIS_OK
ENDIF

IF ADC_VAR < 162 OR ADC_VAR > 190 AND REFRESH_STAT1 = 1 THEN
GOSUB VIS_NOK
ENDIF

IF ID_VAR = \$19 OR ID_VAR = \$1A OR ID_VAR = \$1B OR ID_VAR = \$1C OR ID_VAR = \$1D AND REFRESH_STAT = 1 THEN
GOSUB PRINT_DUTY
ENDIF

IF ID_VAR = \$AA THEN
PROG_VAR_WORD = 0
GOSUB PRINT_DUTY
PAUSE 50
GOSUB PRINT_FREQ

LOW _12V_SWITCH, OUT_2_CTRL, V_SEL_RLY
GOTO _MAIN
ENDIF

LOOP
`---------------------------------------------------------------------------------------------------------------- VOLTMETER --------------------------------

_VOLT_METER:
DO

RANGE_MULTIPLIER = 4

IF ADC_VAR > 254 OR ADC_VAR1 > 254 AND RANGE_MULTIPLIER = 1 THEN
HSEROUT 0, ("click 36,0", \$FF, \$FF, \$FF)
RANGE_MULTIPLIER = 4
ENDIF

IF ID_VAR = \$12 AND PROG_VAR = \$01 THEN        `5V OUTPUT
LOW _12V_SWITCH
LOW V_SEL_RLY
HIGH _5V_SWITCH
RANGE_MULTIPLIER = 1
ENDIF

IF ID_VAR = \$12 AND PROG_VAR = \$00 THEN
LOW _5V_SWITCH
ENDIF

IF ID_VAR = \$13 AND PROG_VAR = \$01 THEN            `12V OUTPUT
LOW _5V_SWITCH
HIGH V_SEL_RLY
HIGH _12V_SWITCH
RANGE_MULTIPLIER = 4
ENDIF

IF ID_VAR = \$13 AND PROG_VAR = \$00 THEN
LOW _12V_SWITCH
LOW V_SEL_RLY
ENDIF

WAVE_ID = 6
GOSUB PRINT_CH0
GOSUB PRINT_CH1
GOSUB PRINT_WAVE0
GOSUB PRINT_WAVE1

IF ID_VAR = \$AA THEN

LOW _5V_SWITCH
LOW _12V_SWITCH
LOW V_SEL_RLY
GOTO _MAIN
ENDIF

LOOP
`-------------------------------------------------------------------------------------------------------------------------- PMW ----------------------------

_PWM_GEN:
HIGH _12V_SWITCH, OUT_2_CTRL, V_SEL_RLY
LOW _5V_SWITCH, OUT_3_CTRL
DO

IF ID_VAR = \$04  AND PROG_VAR_WORD < 1000 AND REFRESH_STAT = 1 THEN
GOSUB PRINT_FREQ
REFRESH_STAT = 0
ENDIF

IF ID_VAR = \$04 AND PROG_VAR_WORD > 999 AND REFRESH_STAT = 1 THEN
BINTOASCII PROG_VAR_WORD, B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX, B2ASCII_XXX0X, B2ASCII_XXXX0
SEROUT PWM_SERIAL, T9600_32, ("F", B2ASCII_X0XXX, ".", B2ASCII_XX0XX, B2ASCII_XXX0X)

REFRESH_STAT = 0
ENDIF

IF ID_VAR = \$09 AND REFRESH_STAT = 1 THEN
GOSUB PRINT_DUTY
REFRESH_STAT = 0
ENDIF

IF ID_VAR = \$AA THEN
PROG_VAR_WORD = 0
GOSUB PRINT_DUTY
PAUSE 50
GOSUB PRINT_FREQ
LOW _12V_SWITCH, OUT_2_CTRL, V_SEL_RLY
GOTO _MAIN
ENDIF

LOOP

`------------------------------------------------------------------------------------------------------------------ SUB-ROTINAS ----------------------------
PRINT_WORD:

HSEROUT 0, ("x0.val=", B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX, B2ASCII_XXX0X, B2ASCII_XXXX0, \$FF, \$FF, \$FF)

HSEROUT 0, ("n0.val=", #OUT_WORD_0, \$FF, \$FF, \$FF)

HSEROUT 0, ("n1.val=", #OUT_WORD_1, \$FF, \$FF, \$FF)

RETURN

PRINT_DUTY:

IF REFRESH_STAT = 1 THEN

BINTOASCII PROG_VAR, B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX
SEROUT PWM_SERIAL, T9600_32, ("D", B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX)
ENDIF

REFRESH_STAT = 0
RETURN

PRINT_FREQ:
BINTOASCII PROG_VAR_WORD, B2ASCII_0XXXX, B2ASCII_X0XXX, B2ASCII_XX0XX, B2ASCII_XXX0X, B2ASCII_XXXX0
SEROUT PWM_SERIAL, T9600_32, ("F", B2ASCII_XX0XX, B2ASCII_XXX0X, B2ASCII_XXXX0)

REFRESH_STAT = 0
RETURN

VIS_OK:
HSEROUT 0, ("vis t4,0", \$FF, \$FF, \$FF)
HSEROUT 0, ("vis t1,1", \$FF, \$FF, \$FF)
REFRESH_STAT1 = 1
RETURN

VIS_NOK:
HSEROUT 0, ("vis t1,0", \$FF, \$FF, \$FF)
HSEROUT 0, ("vis t4,1", \$FF, \$FF, \$FF)
REFRESH_STAT1 = 0
RETURN

PRINT_WAVE0:
HSEROUT 0, ("add " ,#WAVE_ID,",",#0,",",#ADC_VAR, \$FF, \$FF, \$FF) `ROTINA DE ENVIO DE WAVEFORM
RETURN

PRINT_WAVE1:
RETURN

PRINT_CH0:

ANALOG_HI = 0
ANALOG_DATA = ANALOG_DATA * 196 / 100

ANALOG_DATA = ANALOG_DATA * RANGE_MULTIPLIER    `15V RANGE

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n3.val=", OUT_BYTE, \$FF, \$FF, \$FF) `XX.X0

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n2.val=", OUT_BYTE, \$FF, \$FF, \$FF)  `XX.0X

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n1.val=", OUT_BYTE, \$FF, \$FF, \$FF)   `X0.XX

OUT_BYTE = ANALOG_DATA + \$30
HSEROUT 0, ("n0.val=", OUT_BYTE, \$FF, \$FF, \$FF)    `0X.XX

RETURN

PRINT_CH1:

ANALOG_HI = 0

ANALOG_DATA = ANALOG_DATA * 196 / 100

ANALOG_DATA = ANALOG_DATA * RANGE_MULTIPLIER    `15V RANGE

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n4.val=", OUT_BYTE, \$FF, \$FF, \$FF) `XX.X0

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n5.val=", OUT_BYTE, \$FF, \$FF, \$FF)  `XX.0X

OUT_BYTE = ANALOG_DATA // 10 + \$30
ANALOG_DATA = ANALOG_DATA / 10
HSEROUT 0, ("n6.val=", OUT_BYTE, \$FF, \$FF, \$FF)   `X0.XX

OUT_BYTE = ANALOG_DATA + \$30
HSEROUT 0, ("n7.val=", OUT_BYTE, \$FF, \$FF, \$FF)    `0X.XX

RETURN

INTERRUPT:
PAUSE 100
GET 0, ID_VAR
GET 1, PROG_VAR
GET 2, PROG_VAR_1

HSERINFLAG = 0
HSERPTR = 0
PTR = 0
SETINTFLAGS %00100000,%00100000

REFRESH_STAT = 1

RETURN``````