Adding Ports status to a variable


Closed Thread
Results 1 to 13 of 13
  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: 588
Size:  71.4 KB

    Thanks for your help

    Mike

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


    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

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Hi,
    I'd probably use a LDO regulator for the PIC and run the motor directly off the battery. You can always limit the dutycycle of the PWM to ~75% so that the motor never sees more than 4.5V average if that's critical. Running the motor directly off the battery would allow the motor to operate closer to its top speed even when the battery voltage has dropped a bit, (but then you obviously need to go above 75% dutycycle). If you use a 3.3V regulator for Vdd and as reference for the ADC you could monitor the battery voltage thru a voltage divider and use the result to compensate the PWM dutycycle as the battery voltage drops.

    If you really don't want to use a regulator I guess you could use a diode or two but that would make monitoring the battery voltage a lot trickier.

    /Henrik.

  9. #9
    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

    As the circuit will not work under 4V I guess I would need to supply 5V so I will be using a 6V SLA battery. I think I will stick with a couple diodes. About reducing duty cycle I was under the impression that although the pulses are a % of the time, each pulse is still at full voltage but if reducing duty cycle is ok then I can do this for the motor.

    Thank you for the information.

    Mike

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Hi,
    I can't see why the circuit wouldn't work below 4V. If the LED is a white one then I understand but if it's a normal red or green with ~2V forward drop then you should be fine on 3.3V. Besides, the LED is to be replaced by the transistor, right?

    Yes, the pulses are still at the full voltage and you are correct that it's something you should take into consideration however I don't think it would be a problem in this case. It's quite normal to run hava a higher power supply voltage in order to compensate for the I*R-drop in the windings etc, I don't think 25% "overvoltage" would be a problem. Do you have a datasheet on the motor?

    /Henrik.

  11. #11
    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 actually have a blue LED 3.5V forward, I just tried with a red LED 1.9V forward. The whe I hit 3.9V the PWM doesn't work, the LEDs remain at full intensity (Dimmed by the lower voltage but no difference from changing the POT).
    Here is the datasheet for the motor (model 2295)

    Mike

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: Adding Ports status to a variable

    Hi,
    Personally I wouldn't worry about running the motor off a 6V supply.
    As for the circuit not operating properly below ~4V the only think I can think of is the POT command not doing the business. The manual says that the Pot/resistor should be 5-50k, you have 100k. It says to adjust Scale contstant to a lower value for large RC constants, you have the scale set to its maximum. Perhaps that's got anything to do with it.

    /Henrik.

  13. #13
    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

    Since my first post, I have reduced the POT to 50K and I've used the code example given in the PBP book to adjust the scale and my value is now set to 30 so this is a mystery but I am not so concerned.
    I will use 2 diodes to supply the PIC and run the motor off the 6V, anyway the goal is to reduce the motor speed so it will be set to probably half duty-cycle. I didn't receive the motor yet but fingers crossed.

    Thank you Henrik for your help.

    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