|
|
View previous topic :: View next topic |
Author |
Message |
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
[Help] Detect Pulse 20ms using I/O function |
Posted: Mon Nov 25, 2013 12:25 am |
|
|
Dear all.
I have a project to check one pulse have period 20 ms.
I want to check it appear or disappear on I/O pin, I needn't measure period or frequency of this pulse, so that I don't want to uses CCP module to detect it.
If I/O input pin have a pulse 20ms, after that, haven't a pulse, PIC will count up and display on led7.
I using PIC18F4680 and CCS C ver 5.015
Code:
Code: |
#include <18F4680.h>
#fuses HS,PUT,NOPROTECT,NOLVP,NOWDT,NOPBADEN
#use delay(clock=10000000)
unsigned int16 count=0;
unsigned int8 temp=0;
#include <led7.c>
void main(void)
{
delay_ms(500);
set_tris_a(0x00); // en_LED
set_tris_d(0x00); // data_LED
set_tris_e(0x00);
set_tris_b(0xff);
delay_us (10); // Allow time for weak pullups to rise up
temp=input_b(); // Read PortB to clear change condition edit 30-05
clear_interrupt (int_rb);
output_a(0xff);
output_d(0xff);
output_e(0xff);
while(1)
{
if(input(PIN_B0)==0)
{
while(input(PIN_B0)==0);
loop:
delay_ms(21);
if(input(PIN_B0)==1)
{
display_7segment(count);
goto loop;
}
count++;
}
display_7segment(count);
}
}
|
The code run failure, it always count up when pulse is appear on I/O pin, if true, it only count up when pulse is disappear on I/O pin.
Thanks all. _________________ Begin Begin Begin !!! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Nov 25, 2013 7:51 am |
|
|
it is because your program logic is faulty.
you need to set a time detection window to discriminate between both longer and shorter pulses than those desired
and only accept pulses timed within the detection window, IMHO
as you say you want to know it is 20ms, then say you don't need to know period or frequency.
then how do you know it is 20 MS ???
this could be better done with a timer or the CCP module,
i/o is a very poor way actually
|
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Tue Nov 26, 2013 8:46 pm |
|
|
asmboy wrote: | it is because your program logic is faulty.
you need to set a time detection window to discriminate between both longer and shorter pulses than those desired
and only accept pulses timed within the detection window, IMHO
as you say you want to know it is 20ms, then say you don't need to know period or frequency.
then how do you know it is 20 MS ???
this could be better done with a timer or the CCP module,
i/o is a very poor way actually
|
Hi.
Can you explain the failure logic in my program ?
I think it error by delay() function, I'll added led blink in this code and saw it break out when jump in some times.
Code: |
if(input(PIN_B0)==1)
{
display_7segment(count);
output_toggle(PIN_A0);
goto loop;
}
|
Because my project on class must be used IO to detect pulse, so that I can change to used CCP or Timer.
I used 2nd PIC 16F to generate pulse 20ms and checked it OK with Digital Multimeter.
In my code, when detected pulse edge is high, i delayed 21 ms and loop check again, because in times, pulse is still high, so that I loop and loop forever until the pulse is go low.
Flw u, It's right? _________________ Begin Begin Begin !!! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Nov 28, 2013 9:22 am |
|
|
use timer0 or timer1 with say a 1 ms rollover time
and increment 'tix' for each msec
1- recognize pulse begin -zero timer tix count
2- reject if pulse end happens before 19 msecs
3-reject if at 21 msec pulse is still high
4- else u have a 20 ms pulse +/- 1 msec detection window
5-clear logic of state machine and start again
that is the only "I/O" based approach that comes to mind with
NO delay_MS used ...... |
|
|
|
|
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
|