News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Simple Pwm Detection?

Started by Maxi, Jun 14, 2025, 06:57 AM

Previous topic - Next topic

Maxi

My input signal is 0-5 volt pwm,
How can I detect whether there is a signal (change) at the input end in a simple way?
I can measure frequency, but I need something very simple,
I will get information from the speed sensor of a vehicle, is the car going or stopping?
I just need to make such a simple determination.

So I think there is no need for lines of code when measuring frequency using interrupt.

david

Hi,
You could integrate the pwm through an RC filter and treat it as variable DC which could then be read by an ADC input or if you only want an indication of running or stopping then maybe a Schmitt logic input or comparator may suffice.  Maybe I've misunderstood your application.

Cheers,
David

Maxi

No, no, I think it's a good idea, but the frequency (pwm) that will occur when the vehicle is moving very very slowly may not provide us with enough voltage.
It seems to me that it will happen with a few logic commands, but I haven't managed it yet.

david

Ok.  If the pwm is like a sensor picking up a wheel turn then the frequency is too low to conveniently integrate.

Add a small GPS sensor and use a few lines of code to parse the NMEA sentences-

Dim kph As string*5   'captured string
HRSIn Wait ("VTG,")       'Wait for VTG sentence
        For comma=1 To 6          'Step out 6 fields
          While HRSIn<>",": Wend
        Next comma
        HRSIn kph     'take next 5 char as kph string

Cheers,
David

RGV250

Hi,
Could you not use CCP to capture the pulse width?

Bob

Maxi

Quote from: RGV250 on Jun 14, 2025, 01:51 PMHi,
Could you not use CCP to capture the pulse width?

Bob

I will do that

trastikata

Quote from: Maxi on Jun 14, 2025, 06:57 AMHow can I detect whether there is a signal (change) at the input end in a simple way?


Depending on the PIC, the application and the pin ... keep in mind that IOC flag is always set and can be monitored whether or not the IOC has been enabled.

Pepe

demo proteus and code


Device = 12F629

Config HS_OSC, WDT_ON, PWRTE_OFF, MCLRE_OFF, BODEN_OFF, CP_OFF, CPD_OFF

Declare Xtal = 20
Declare Optimiser_Level = 3
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Reminders Off
Declare Hints Off
Declare Create_Coff On
Declare Serial_Baud =9600
Declare RSOut_Pin =GPIO.0
Declare RSOut_Mode = 0
Declare Watchdog On


Symbol   LCD_CLR        0x01
Symbol   LINE1          0x80
Symbol   LINE2          0xC0
Symbol   COMMAND        0xFE
Symbol PWM_PIN = GPIO.2
Dim PulsoActual As Word
Dim PulsoAnterior As Word
Dim FlancoSubida As Bit  = 1
Dim flag As Bit
Dim temp As Word
Dim wTimer1 As TMR1L.Word
Dim change As SWord

' Configuración inicial

GPIO = 0x0
TRISIO = 0x04

T1CON = %00000001            ' Timer1 encendido, sin prescaler
INTCON = %10010000           ' Habilitar interrupciones globales y externas
OPTION_REG.6 = 1             ' Interrupción por flanco de subida inicialmente
On_Interrupt GoTo Isr

LCD_Init()

Do
 Clrwdt
 DelayMS 10
 flag = 0
 LCD_COMMAND(LINE1)
     
 change = PulsoActual - PulsoAnterior     ' Comparar valores fuera de la interrupción
       
 If change > 2 Then
                    RsOut "PWM up    "
               ElseIf change < -2 Then
                    RsOut "PWM down  "
               Else
                    RsOut "PWM stable"
 EndIf
 If flag = 0 Then
                   PulsoActual=0
 EndIf                       
 LCD_COMMAND(LINE2)
 RsOut "Pwm: ",Dec1  PulsoActual /25.5 ," %   " 
 PulsoAnterior  =  PulsoActual
Loop

Proc LCD_Init()
    LCD_COMMAND(0xC0) 
    LCD_COMMAND(0x01)
    DelayMS 2
    LCD_COMMAND(0x06)   
    LCD_COMMAND(LCD_CLR)
    DelayMS 200
EndProc

Proc LCD_COMMAND(cmd As Byte)
RsOut COMMAND,cmd
EndProc

'----------------------------
Isr:
 Context Save

    If INTCON.1 = 1 Then  ' INT0 interrupción
        flag = 1
        If FlancoSubida = 1 Then
           wTimer1 =  0              ' Reinicia temporizador
            OPTION_REG.6 = 0         ' Cambia a flanco de bajada
            FlancoSubida = 0
        Else
            temp = wTimer1
            If temp < 2550 Then
                                PulsoActual = temp
                       
            EndIf                   
            OPTION_REG.6 = 1         ' Vuelve a flanco de subida
            FlancoSubida = 1
        EndIf
        INTCON.1 = 0                 ' Limpia bandera de INT
    EndIf

Context Restore

Fanie

#8
Detecting pulses from automotive pickups can be a pain in the whatsmecallit.  I would do the input detection different.
Set up a down counter.
Set up a flag (bit) for high or low.
every time the main program loop, it counts down.
read the A/D pin
if the A-D input > say 20 you have an input else it will be zero or less than 20 (out of 1024, 10-bit A-D)
if the counter counted out then you did not have vehicle movement
if you get an A/D input and the counter did not count out you have movement.
If the flag changed, and the counter did not count out you have movement
Re-set the counter value

Inductive pickups output very small pulses at low speed because it is a magnet moving past a pickup coil, hence the detection is difficult. 
Same can be used for vehicles like some bee em trouble u's that use a reed switch, input is either high or low, the low voltage read still works and the counter timeout will still detect movement or not.  For this you will use the flag set for high input or low for zero input while the counter counts out.  The input can then be either high or low at no movement.

For the hardware you can use a series resistor and divider clamped by a unipolar transzorb (fast) to prevent the A-D from exceeding power supply, some pics hang up if this happens.