|
|
View previous topic :: View next topic |
Author |
Message |
erpgc82
Joined: 02 May 2020 Posts: 73
|
Monitor how long a pin is at a given level |
Posted: Mon Mar 15, 2021 8:43 pm |
|
|
Hello friends, I'm new to CCS programming, I'm learning and I'm having a hard time.
I can know how long a pin is at low level, does any function in CCS do this automatically?
I am testing as in the example below, it works, but every 10 or 20 tests, there is 1 failure, because they are confused.
Code: |
if(!input(PIN_A4))
{
delay_ms(300);
if(input(PIN_A4))
{
x=1;
printf("testX: %u",x);
}
}
if(!input(PIN_A4))
{
delay_ms(600);
if(!input(PIN_A4))
{
y=1;
printf("testY: %u",y);
}
}
|
I get another PIC, which someone did many years ago, and I'm testing it with this PIC.
x Then I get a small pulse, on a pin, that goes from 5 to 0v and is theoretically 250ms at low level and back to 5v.
y Then I get a bigger pulse, on a pin, that goes from 5v to 0v and is theoretically at 1000ms at low level and goes back to 5v.
So what do I do, at "x" I see if the pin is at 0v, I wait 300ms and I see if it is at a high level.
So what do I do, in "y" I see if the pin is at 0v, I wait 600ms and I see if it remains low.
But it turns out that there are failures ... sometimes he doesn't understand and gets confused, as I said above. _________________ Gradually you will go far with persistence, will and determination! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Mar 16, 2021 12:41 am |
|
|
No 'function' does this automatically, but there is a peripheral that can.
The CCP.
This gives the ability to record a number of counts from a selected clock
source, on 'events'. The events can be when a pin goes high, goes low,
or changes.
Now one 'key' thing with CCS, is the examples. These really are a vital
part of using the compiler. If you look at ex_pulse.c, this shows measuring a
time rather in the manner you are currently trying, but using timer0, to
do the actual measurement. Then ex_ccpmp.c shows measuring a pulse
width with the CCP (the name stands for 'ccp Measure Pulse'). This example
uses both CCP modules, and sets up the first to capture the rising edge,
with the second set to record the falling edge. Then when the falling edge
captures, the interrupt from this is used to calculate the time between
the edges. Using the hardware, the signal has to be fed to the specific
pins for this.
The problem with your current approach, is the delays, and that you don't
wait for an actual 'edge'. Think about it, when you start, if the pin is low
it'll do the print straight away. Nothing to do with when the signal actually
'dropped'. Then the signal could have gone up ages ago, or even raised
and dropped again, but if it is low, the second print will happen. |
|
|
erpgc82
Joined: 02 May 2020 Posts: 73
|
|
Posted: Tue Mar 16, 2021 6:45 am |
|
|
Thank you very much Ttelmah, great teacher ... Yes, I'm really enjoying CCS. I've already managed to do tests with buzzer using tones ... very easy, using the example of CCS. On this pin to lower the level ... I was actually using delay, and there is my problem.
Thank you very much!
Ttelmah wrote: | No 'function' does this automatically, but there is a peripheral that can.
The CCP.
This gives the ability to record a number of counts from a selected clock
source, on 'events'. The events can be when a pin goes high, goes low,
or changes.
Now one 'key' thing with CCS, is the examples. These really are a vital
part of using the compiler. If you look at ex_pulse.c, this shows measuring a
time rather in the manner you are currently trying, but using timer0, to
do the actual measurement. Then ex_ccpmp.c shows measuring a pulse
width with the CCP (the name stands for 'ccp Measure Pulse'). This example
uses both CCP modules, and sets up the first to capture the rising edge,
with the second set to record the falling edge. Then when the falling edge
captures, the interrupt from this is used to calculate the time between
the edges. Using the hardware, the signal has to be fed to the specific
pins for this.
The problem with your current approach, is the delays, and that you don't
wait for an actual 'edge'. Think about it, when you start, if the pin is low
it'll do the print straight away. Nothing to do with when the signal actually
'dropped'. Then the signal could have gone up ages ago, or even raised
and dropped again, but if it is low, the second print will happen. |
_________________ Gradually you will go far with persistence, will and determination! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|