Working PIC16F88 using sensors and switches


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    3

    Default Working PIC16F88 using sensors and switches

    Hello all,

    I am programming in PicBasic Pro. We are using two switches (connected at A0 and A3) and two sensors (connected at A1 and A2) as the input and two LEds (connected at B0 and B1) as outputs. The following code is supposed to turn on the LED for a fixed time when the switch is pressed. It does the same when the sensors detect anything. It works fine if the switches are pressed separately. However, if they are pressed together, one of the LEd starts blinking while the other stays on.


    ADCON0 = 0
    ADCON1=0
    CMCON = 7
    ANSEL = 0

    DEFINE OSC 20

    'OSCTUNE = %011110
    'T1CON = %01111001

    ' Simple LED Test

    'TRISB = %00000000 'setup PORTB; 0 for output, 1 for input
    'TRISB=0 'alternative way of writing above line

    TRISB.1=0
    TRISA.0=1
    TRISA.1=1

    TRISB.0=0
    TRISA.2=1
    TRISA.3=1

    i VAR BYTE
    i=100


    j VAR BYTE
    j=100

    loop:


    PORTB.1=PORTA.1
    PORTB.0=PORTA.2


    'first switch
    IF PORTA.0 = 1 Then
    i = 0
    ENDIF

    if (i < 100) then
    PORTB.1=1 'light LED
    PAUSE 50 'pause
    i = i+1
    ELSE
    PORTB.1=0 'turn off LED
    i=100
    ENDIF



    'second switch
    IF PORTA.3 = 1 Then
    j=0
    ENDIF

    if(j < 100) THEN
    PORTB.0=1 'light LED
    PAUSE 50
    j = j+1
    ELSE
    PORTB.0=0 'turn off LED
    j=100
    ENDIF



    GOTO loop




    end

    '''''''''''''''''''''''''''''''''''''''''''

    When successful with the above code, I plan to implement it further to use 4 inputs and 4 outputs. Any help in this direction will be greatly appreciated.

    Thanks.

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


    Did you find this post helpful? Yes | No

    Default

    What do you want it to do if both switches are pushed at the same time?

    Keep in mind that it is nearly impossible to push them both at the same time, one will always be first.

    And are these momentary switches? And do you have pull up resistors on the switch pins (maybe a 10K going to 0 volts)?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You are also aware that multiple pauses (one in each channel) will affect the time delay in the other channels? Therefore if one channel is on, the loop will have a 50mS delay... if four channels are on then your loop time will be 200mS and your delay will be a lot longer than expected. It would be easier to stick to one delay which is either ON or OFF depending if it is needed...

    There is still one dissadvantage here... if a momentary pulse arrives during the 50mS Pause, then it would be missed... but then you can increase the Count time and proportionately decrease the Pause (eg count to 1000 and Pause for 5mS, or Count to 5000 and Pause for 1mS etc etc) - not forgetting to change your variables to WORDS from BYTES if you're going to do this...

    Also, if you alias the PORTS as I have done, it makes the program more understandable, and if you have to change I/O's (usually at PCB layout time!!!) then it is done just ONCE in your program, not a dozen times throughout your code where it could easily be missed.


    Code:
    	SwitchA var PortA.0
    	SwitchB var PortA.3
    	SensorA var PortA.1
    	SensorB var PortA.2
    	LEDA var PortB.2
    	LEDB var PortB.0
    
    	TRISA=%11111111
    	TRISB=%00000000
    
    	Low LEDA
    	Low LEDB
    
    	i=100
    	j=100
    
    Loop: 
    	'
    	'	Read Inputs
    	'	-----------
    	If SwitchA=1 then i=0
    	If SensorA=1 then i=0
    	If SwitchB=1 then j=0
    	If SensorB=1 then j=0
    
    	'
    	'	Process Outputs
    	'	---------------
    	If i < 100 then 
    		High LEDA
    		else
    		Low LEDA
    		endif
    	If j < 100 then 
    		High LEDB
    		else
    		Low LEDB
    		endif
    
    	'
    	'	Update Counters & Timer
    	'	-----------------------
    	If i < 100 then i=i+1
    	If j < 100 then j=j+1
    	If ((i+j)<200) then Pause 50
    	Goto Loop

Members who have read this thread : 0

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