News:

Let's find out together what makes a PIC Tick!

Main Menu

COUNTER

Started by Dolci, Dec 12, 2023, 11:51 AM

Previous topic - Next topic

Dolci

Here is the situation, the signal is a 1 pulse and a 2 pulse. Third signal is a 1 minute high pulse. Using the Counter command I can track the 1 pulse and the 2 pulse.
 
 Dim Mypulse As Byte
 Symbol MyPort = PortA.0
 Mypulse = Counter MyPort, 100
 Print Dec Mypulse

Is there a way I can monitor the PERIOD that it reach the max count? When the long pulse produce 1 count and the Period reach the max count then i can assume it is a long pulse of 1 minute.

Dolci

Yasin

I may not have fully understood the question. If so, I apologize in advance. Have you examined the "PulseIn" command?

Stephen Moss

Looking at the manual in regard to using the Counter command which is what you appear to be doing it states that the the Rising edge is counted, so assuming that say...
Pulse 1 = 10mS
Pulse 2 = 20ms
Pulse 3 = 1 minute
then depending on the timing between pluses, for example let us assume the signal is a square wave then for a 100mS sample period I would expect...
Pulse 1 should result in a value of 9 or 10
Pulse 2 should result in a value of 4 or 5 and
Pulse 3 result in value of 1 or 0
as the count will be dependant on the the timing between when the first rising edge in seen with regard to the start of the sampling period.

Personally I would not use the Counter command for that, I would either...
  • Use the pulse to gate the clock signal into a timer. Then using either a second timer or a Delay command to initiate a read of the value in the Pulse timer after a fixed time period. The number of times the timer input was clocked would be relative to the pulse width, or
  • Connect your pulse a pin with that has Interrupt on Change. When the pin goes high, start a timer, when it goes low, stop the timer and read its value.

In both cases if you get the timer clock frequency correct then for the short pulses the timer count should indicate the relevant pulse width allowing you to identify which of them it is.
For the longer 1 minute pulse either the timer count would be much longer than for the other two or the timer would overflow causing a timer interrupt, either way it would identify the pulse as the 1 minute pulse.
Hopefully, one of those options should work for you depending if you just need to identify which pulse is which (so you can identify the long pulse and move on long before it finishes) or are looking to wait until the longer pulse finishes.

However, as you did not state what the approximate pulse width are, or the source/the likely duration between them it is difficult to know what to suggest as the best solution for your problem.

Dolci

Thank you Yasin and Stephen,

Sorry english is not my first language. The pulse comes from a Burglar Alarm. When you press the remote control to ARMED the unit it will give you a 2 pulse logic level. When you press the remote to DISARMED the unit will give you 1 pulse logic level. And when the alarm trips (detect an intruder) the unit will give you a 1 long pulse about 1 minute. See ABC pulse in the image.

I have done it in a normal way (If blah blah blah = 1 then blah blah blah and so on...) and it is working as intended. I got 1 pulse and tell the pic that it is an DISARMED command. 2 pulse tell the picmicro it is a ARMED command. Long pulse tells the picmicro that there is a intruder.

Yes my question is about the Counter Command of positron.

Overview
Count the number of pulses that appear on pin during period, and store the result in variable.

Parameters
Variable is a user-defined variable.
Pin is a Port.Pin constant declaration i.e. PORTA.0.
Period may be a constant, variable, or expression.

Is there a way to know if the Period has reach its count? There must be some variables or register associated with the Counter command which I can't know since I barely understand assembler.

Do
MyWord = Counter MyPin, 100 ' Variable MyWord now contains the Count
  HRsoutLn Dec MyWord, " " ' Display the decimal result on a serial terminal
Loop ' Do it indefinitely

Dolci

 

trastikata

Hello,

what do you mean by this:

QuoteIs there a way to know if the Period has reach its count?

Dolci

Quote from: trastikata on Dec 13, 2023, 01:06 PMHello,

what do you mean by this:

QuoteIs there a way to know if the Period has reach its count?

Now I get it sorry for the confusion.. whatever the amount of pulse detected it will wait for the PERIOD to finish its count. so a single short pulse will be count as 1 and a single long pulse will be count as 1 also. I was thingking that I can recgnized a short pulse to a long pulse. Stephen is right I need timers. I will just stick to my old working code.

Thanks eveyone
Dolci