PDA

View Full Version : Problem with switch input on 16f818



tasmod
- 27th December 2011, 15:25
Hi everyone,

Newby to posting forum, but been a lurker for quite some time.

I have a problem using a 16f628A and a switch input. This is for a chicken coop motorised sliding door with a LDR as input.

I have a microswitch fed +5v via a 10k resistor to ground, the mid junction of which is connected to PortB.6. Typical manual type stuff.

There is only one microswitch which is fixed position, the door has two switch stops fitted so the switch is monitored in seperate up/down routines.

This switch state is monitored in a loop as it is a limit switch for the motor. However it's action is very slow in operation, taking a few iterations of the loop to act, causing the motor to overrun.

My question is should the INTCON be set as 000000 to prevent the interupt feature on this pin and make the pin a digital input, or am I missing something?

At the moment the project is built and installed but now running the motor on a time setting rather than a limit switch.




'# Automatic chicken coop sliding door. #

'# #

'# 12 Volt Battery powered. #

'# Open when light, close when dark. #

'# Sleep approx. 4 hours between to conserve #

'# power and to prevent retriggering. #

'# #

'# Uses single microswitch as feed back. #

'# #

'# Pic 16f818 --- Rob Lane - Dec 2011 #

'###############################################



OSCCON=$60 'set 4mhz internal oscillator

'(Set INT_OSC in programmer fuses)



' Define ADCIN parameters

Define ADC_BITS 10 'Set number of bits in result

Define ADC_SAMPLEUS 50 'Set sampling time in uS



Photo var word 'Create 'photo' to store result of ldr input

Counter1 var byte 'flip flop counter variable



'PIN ASSIGNMENTS

LDR var porta.0

MotorUp var portb.4

MotorDown var portb.5

Switch var portb.6



Counter1=1 'Set counter for power up, door must

'be closed in daylight for first powerup.



TRISA=%00000001 ' Set PORTA

TRISB=%01000000 ' Set PORTB

input switch

'INTCON=%00000000



Main:

ADCIN ldr, photo 'read ldr adc value

IF Photo <20000 Then checkup 'daylight

IF Photo >30000 Then checkdown 'darkness

'sleep 300 'about 5 minutes low power

goto Main



checkup:

if counter1=1 then up

goto main



up:

High MotorUp 'start motor up

pause 1000 'wait 1 sec so micro switch opens

if switch = 1 then pressedup 'check switch state

goto up 'check again



pressedup:

low motorup 'stop motor

Counter1=2 'flip flop counter for up/down of door

'sleep 14400 'sleep 4 hours

goto main



checkdown:

if counter1=2 then down

goto main

down:

high MotorDown 'start motor down

pause 1000

if switch = 1 then presseddown 'check again

goto down



presseddown:

low MotorDown

Counter1=1

'sleep 14400

goto main



end

tasmod
- 27th December 2011, 15:39
As the 10 minute edit window has passed I am unable to change title but it's a 16f818.

Don't know why I put 628, maybe because i have one on bench in front of me.

tasmod
- 27th December 2011, 15:42
Doh, and why when you post the code do you also spot the obvious mistake. The 1 sec pause in the loop !!

Sorry.

mackrackit
- 28th December 2011, 10:27
Good job on catching the mistake.

Pretty interesting. A member here has PICs in space and now we have one using PICs with chickens... Very versatile.
It would have been nice to have had PICs back in the day. The "waffer" temperature controllers ( I hated those things) could have been replaced with a PIC.

tasmod
- 28th December 2011, 14:52
The chickens are real characters. I have 4 who are completely free range and quite tame, that doesn't mean they are not wily though. They do 'go in' each night.

I have to leave the system to work on 'timed' right now as I won't be about to check it for a few days, however last night they were locked out due to their greed.

I went round to check at dusk but they saw me, cue chickens thinking late feed, so they stayed out hanging around me till it was quite dark. Obviously the door had closed and left them out !

I don't think this will be a problem in normal use, no-one is about at dusk. I've adjusted the LDR read, to close the door at a darker setting.

When I get back I'm going to complete the limit switch again and test it when I'm around.

The whole reason for this is so I can go away for a few days and the door is taken care of, else Mr Fox would be in !!

I dabbled with Pics back in the 90's but then moved out of electronics.

Archangel
- 20th December 2014, 09:26
As the 10 minute edit window has passed I am unable to change title but it's a 16f818.

Don't know why I put 628, maybe because i have one on bench in front of me. Fixed 3 years later. Contact any admin or moderator to fix these issues