View previous topic :: View next topic |
Author |
Message |
hussong1555
Joined: 03 Feb 2010 Posts: 2
|
Digital input monitoring |
Posted: Wed Feb 03, 2010 2:48 pm |
|
|
I am working on a project where I have a relay hooked up to a PLC that is then sending a pulse to RA0 and RA1 alternately at a given interval on a Pickit 3 demo board. How do I setup RA0 and RA1 as inputs, then monitor them to cause a sub-routine when one or the other pulses high?
I am a total newbie so any help would be very appreciated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 04, 2010 1:30 am |
|
|
Quote: |
I have a relay hooked up to a PLC that is then sending a pulse to RA0
and RA1 alternately at a given interval.
|
What PIC is on the demo board ?
What is the pulse duration ?
What is the interval between pulses ?
What is the Vdd volage of the PIC, and what are the voltage levels
of the pulses put out by the PLC ? |
|
|
hussong1555
Joined: 03 Feb 2010 Posts: 2
|
|
Posted: Thu Feb 04, 2010 7:04 am |
|
|
Quote: |
What PIC is on the demo board ?
|
18F45K20
Quote: |
What is the pulse duration ?
What is the interval between pulses ?
|
The duration I am not sure of, but I can adjust. The interval will be variable depending on the relay, but the range could be anywhere from 60 seconds to 5 minutes.
Quote: |
What is the Vdd volage of the PIC, and what are the voltage levels
of the pulses put out by the PLC ?
|
5v |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 04, 2010 2:57 pm |
|
|
Here is a basic framework for software edge detection of your incoming
pulses (I just did one pulse in this example). Also, I have a 1 ms
debounce period in the polling loop. Your positive pulse duration should
be at least 1.1 ms for this to work. If not, then you need to reduce the
debounce period so it's less than the pulse duration. Also, I didn't put
in any code to turn the relay off at some point. You can do that.
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define PULSE0_PIN PIN_A0
#define PULSE1_PIN PIN_A1
#define RELAY0_PIN PIN_B0
#define RELAY1_PIN PIN_B1
//======================================
void main()
{
int8 current_state0;
int8 old_state0;
old_state0 = 0; // Assumes Pulse0 idle state is a low level
output_low(RELAY0_PIN); // Start with Relay0 "off"
// Poll the pulse0 pin. Wait for a rising edge to occur.
// Then turn on Relay0.
while(1)
{
current_state0 = input(PULSE0_PIN); // Read Pulse0 pin
// Check if new state is 1 and old state was 0.
// This means we have detected the rising edge
// of the pulse. If so, turn on the Relay.
if((current_state0 == 1) && (old_state0 == 0))
{
output_high(RELAY0_PIN); // Turn on Relay0
}
old_state0 = current_state0; // Save state for next pass
delay_ms(1); // Wait for debounce period
}
while(1);
} |
|
|
|
|