PDA

View Full Version : Bits & Bytes



electronicsuk
- 26th December 2003, 10:35
Is it possible to act on a single bit within a byte, depending upon the value of a counter? For example, a simple chaser could be made using the code:
HIGH portb.0
HIGH portb.1
HIGH portb.2
HIGH portb.3

However, it would be much better if this could be done with a counter. Something along the lines of:
For counter = 0 to 3
HIGH portb.counter
NEXT

PBP wont compile this. What would the code to do this look like? I was thinking of replacing .counter with (counter) but as far as I am aware, this only works with arrays. I tried it anyway, and it compiled, but as expected it didn't work.

Ideas?

Many thanks,
Matthew

george
- 27th December 2003, 01:15
You could try using array as follows:

LED var byte

(so on...)

for LED = 0 to 7

portb.0[LED] = 1
pause 500
portb.0[LED]=0

next

will scroll a string of leds. You can also assign a value on the every bit of port b using this method.

Tim B
- 27th December 2003, 11:51
The simplest method to do a single LED chaser is

temp = 1
for a = 0 to 7
portb = temp
temp = temp >> 1
next

takes only 14 words

or if you want to be really tight in your code

temp.0 = 1
repeat
portb = temp
temp = temp >> 1
until temp.7 = 1

takes 8 words

Tim

electronicsuk
- 29th December 2003, 11:46
Thanks for the ideas. The chaser was only an example, and isn't the actual intended use. I still haven't found exactly what I'm looking for, but the ideas were a great help.

Matthew

Melanie
- 29th December 2003, 13:44
I think what you're looking for is in section '4.11 Pins ' of the PBP manual - specifically look at the table addressing pins on different PIC's... so if you're using an 18-pin PIC and your original example...

For CounterA=0 to 7
High CounterA
Pause 100
Low CounterA
Pause 100
Next CounterA

will sequentially toggle LED's across PortB.

Do a search in the MeLabs archives for "PICBASIC-L Using a variable to determine a Port pin - Melanie?" of 25 April 2003 wherein I gave a "Bouncing Ball" example... actually that whole thread is probably what you're after.

Melanie

electronicsuk
- 29th December 2003, 18:01
Perfect! Thanks Melanie.

Matthew

Pat-UK
- 12th April 2006, 10:58
I know this is an old thread, sorry to dig it up being my first post and all

But how could I make this start only on the press of a button and then stop with say LED1 on and wait till the button was pressed again.

I'm am totally new to pic programming and have until half way through May to get a project finished.

I will be using a 12F675 and only need to chase through 5 LEDs

Many Thanks

Melanie
- 12th April 2006, 12:52
Pushbutton operated LED chase...


@ DEVICE pic12F675, INTRC_OSC_NOCLKOUT
' System Clock Options (Internal)
@ DEVICE pic12F675, WDT_ON
' Watchdog Timer
@ DEVICE pic12F675, PWRT_ON
' Power-On Timer
@ DEVICE pic12F675, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic12F675, BOD_ON
' Brown-Out Detect
@ DEVICE pic12F675, CPD_OFF
' Data Memory Code Protect
@ DEVICE pic12F675, PROTECT_OFF
' Program Code Protection

'
' Hardware Defines
' ----------------
PressButton var GPIO.3
LED1 var GPIO.0
LED2 var GPIO.1
LED3 var GPIO.2
LED4 var GPIO.4
LED5 var GPIO.5

'
' Software Defines
' ----------------
CounterA var BYTE

'
' Initialise PIC
' --------------
TRISIO=%00001000
CMCON=%00000111
ANSEL=%00000000

'
' Main Program Starts Here
' ------------------------
CounterA=0
Gosub LightLEDs
Loop:
If PressButton=1 then goto Loop
CounterA=CounterA+1
If CounterA>4 then CounterA=0
Gosub LightLEDs
Pause 50 ' Debounce Delay
While PressButton=0:Wend ' Wait for finger OFF the Button
Goto Loop

'
' Subroutine Lights the LEDs
' --------------------------
LightLEDs:
High LED1
High LED2
High LED3
High LED4
High LED5
If CounterA=0 then Low LED1
If CounterA=1 then Low LED2
If CounterA=2 then Low LED3
If CounterA=3 then Low LED4
If CounterA=4 then Low LED5
Return

End

Pressbutton on GPIO.3 connected between PIC pin and Vss. Resistor (10K) goes between PIC pin and Vdd.

Each LED connected between Vdd and pic pin via 330R Resistor.

Not tested (what do you expect for free and less than five minutes!!!) but should work. You can work out what the code does yourself from the PBP Manual and the PICs Datasheet.