Adding Ports status to a variable


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118

    Default Adding Ports status to a variable

    This is probably an easy one but I have been off this stuff for a while and I am a little lost. I read the book and searched the forum but I can't find the answer.
    Using PBP 2.61C with 16F628A.

    I am trying to get the value of PortB.2 & PortB.3 & PortB.4 and assign this value to a variable but I don't want to add the 3 ports.
    Ex.: If I have PortB.2 = 1, PortB.3 = 0, PortB.4 = 1
    I want to have my variable = %101
    Maybe I am just looking at it the wrong way but the bottom line is I have 8 possibilities with the 3 ports and I want to branch to a SELECT CASE.

    Mike

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Read the port, shift right 2, and AND it with %00000111, so say port = 11110111, shift right 2 gives 00111101, logical AND it with %00000111 should give you value of %101.

  3. #3
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    you could also do it longways

    NEWBYTE VAR BYTE : NEWBYTE = 0
    NEWBYTE.0 = PORTB.0
    NEWBYTE.1 = PORTB.1
    NEWBYTE.2 = PORTB.2
    Chris


    Any man who has accomplished anything in electronics at one time or another has said... " STOP! WAIT! NOOO! Dangit.... Oh Well, Time to start over..."

  4. #4
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Thanks for your replies.
    I will need to do some extensive reading on the first option as it might be more efficient but I tried option 2 (the long way) and it works.

    Now I have something that is almost working and I am not sure if I have an issue with my code or the hardware.
    Basically I am using a pot to control the speed of a small DC motor using PWM (since I don't have the motor/transistor yet I am using LED1)
    and I have a set of 3 jumpers to control time on and time off.

    In my example let's say I adjust the pot to have the LED at half its intensity and I have all jumpers off (0V) I should get 1 sec ON + 14 sec Off and this cycle should loop forever.
    The results I am getting now is:
    LED at half intensity for 1 sec
    LED off for 14 sec
    LED at half intensity for 1 sec
    LED on (full intensity) 14 for sec

    Somehow after a while the proper sequence starts happening which is why I think it might be hardware issue as it is unstable.

    Here is my code and schematic:
    Code:
    @ __config _INTOSC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CPD_OFF & _CP_OFF
    clear
    
    TRISA = 255         'All pins are input.
    TRISB = 0           'All pins are output.
    
    '=====Timer2 settings - 8Bit ====================
    T2CON = %00000000   ' Required for accurate timing for RPM.
    INTCON.7 = 0        ' Disable interrupts.
    
    '===================PWM Part===============
    CMCON = 7           ' PortA = digital I/O
    VRCON = 0           ' Disable A/D Voltage reference.
    PR2 = 255           ' PWM frequency.
    CCP1CON = %00001100 ' PWM Mode selected.
    T2CON = 0           ' Timer2 OFF + 1:1 prescale
    CCPR1L = 0          ' Set PWM Duty-Cycle
    
    SW1 var PortA.2
    SW2 var PortA.3
    SW3 var PortA.4
    SWconfig var byte
    PotPin VAR PORTB.0
    TimeOn var word
    TimeOff var word
    
    swconfig =  0
    timeon = 0
    timeoff = 0
    
    Begin:
    
    ' get the switches value and assign to SWconfig
    swconfig.2 = sw1
    swconfig.1 = sw2
    swconfig.0 = sw3
    
    select case swconfig
    
    case 0 
        timeon = 1000
        timeoff=14000
        
    case 1 
        timeon = 2000
        timeoff=13000
        
    case 2 
        timeon = 3000
        timeoff=12000
        
    case 3 
        timeon = 4000
        timeoff=11000
        
    case 4 
        timeon = 5000
        timeoff=10000
        
    case 5 
        timeon = 6000
        timeoff= 9000
    
    case 6 
        timeon = 7000
        timeoff= 8000
    
    case 7 
        timeon = 8000
        timeoff= 7000
    
    end select
    
        POT PotPin , 255, CCPR1L ' Get POT value
        T2CON.2 = 1'start PWM
        pause timeon
        T2CON.2 = 0'stop PWM
        pause timeoff
    
    
    goto begin
    
    
    END
    Name:  pico.PNG
Views: 658
Size:  71.4 KB

    Thanks for your help

    Mike

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Hi,
    Set the dutycycle to 0 instead of stopping the PWM timer.
    If you stop the PWM timer the output pin will "get stuck" in the state it is in at the very moment you stop the timer.

    /Henrik.

  6. #6
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    When drawing the schematic I had the LED reversed.

    Setting the duty cycle to 0 instead of stopping the timer gave the following result:
    Dim for 1 sec
    Full on for 14 sec
    loop

    So I reversed the LED (as it is showing in my schematic above) and connected to ground instead of +5
    It now appear to be working well.

    Thanks.


    Mike

  7. #7
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    I am presently powering the circuit from PICKIT2 but this circuit will eventually be powered by batteries.
    The DC motor requires 1.5 to 4.5V and the datasheet for 628A indicates an operating voltage range of 2.0V to 5.5V.
    I was hoping to have 2 pairs (series) of AA in parallel to supply 3 V. but the circuit as above will not work under 4.0V (with the LED)
    I don't want to add a regulator if possible. I have in mind a 6V SLA battery and I am thinking of adding a diode in series (drop 0.7V) to supply the PIC and another diode to further drop another .7V for the motor.
    Do I have other alternatives?

    Mike

Similar Threads

  1. activating i/o ports according to variable value
    By Picman in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 29th July 2010, 13:55
  2. accessing ports pins using an index variable
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th March 2008, 20:36
  3. adding new word variable ?
    By iugmoh in forum General
    Replies: 4
    Last Post: - 21st February 2008, 00:26
  4. STATUS re-curtain W
    By zugvogel1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th February 2005, 15:21

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