PDA

View Full Version : Adding Ports status to a variable



lilimike
- 17th March 2013, 05:35
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

aerostar
- 17th March 2013, 07:36
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.

wdmagic
- 17th March 2013, 09:57
you could also do it longways

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

lilimike
- 17th March 2013, 14:44
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:

@ __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
6895

Thanks for your help

Mike

HenrikOlsson
- 17th March 2013, 15:17
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.

lilimike
- 17th March 2013, 15:45
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

lilimike
- 18th March 2013, 16:43
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

HenrikOlsson
- 18th March 2013, 18:10
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.

lilimike
- 20th March 2013, 01:28
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

HenrikOlsson
- 20th March 2013, 06:01
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.

lilimike
- 20th March 2013, 11:27
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) (http://www.pololu.com/file/0J17/re_260ra.pdf)

Mike

HenrikOlsson
- 20th March 2013, 21:13
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.

lilimike
- 20th March 2013, 22:05
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