Problem with switch input on 16f818


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190

    Default Problem with switch input on 16f818

    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.

    Code:
    '#   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
    Last edited by tasmod; - 27th December 2011 at 15:34.

  2. #2
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190


    Did you find this post helpful? Yes | No

    Default Re: Problem with switch input on 16f628

    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.

  3. #3
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190


    Did you find this post helpful? Yes | No

    Default Re: Problem with switch input on 16f628

    Doh, and why when you post the code do you also spot the obvious mistake. The 1 sec pause in the loop !!

    Sorry.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Problem with switch input on 16f628

    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.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190


    Did you find this post helpful? Yes | No

    Default Re: Problem with switch input on 16f628

    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.

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Problem with switch input on 16f628

    Quote Originally Posted by tasmod View Post
    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. 16f818 from 16f628a
    By andybarrett1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th August 2014, 07:31
  2. Replies: 6
    Last Post: - 9th January 2011, 15:26
  3. Problem getting switch info into FOR..NEXT Start
    By Roddy Wayne in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th October 2010, 23:02
  4. Using input from Switch to control a loop
    By MrRoboto in forum mel PIC BASIC
    Replies: 9
    Last Post: - 2nd February 2009, 05:02
  5. Replies: 1
    Last Post: - 7th November 2007, 20:36

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts