Bits & Bytes


Closed Thread
Results 1 to 8 of 8

Thread: Bits & Bytes

  1. #1
    electronicsuk's Avatar
    electronicsuk Guest

    Default Bits & Bytes

    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
    Last edited by electronicsuk; - 26th December 2003 at 09:37.

  2. #2
    george's Avatar
    george Guest


    Did you find this post helpful? Yes | No

    Lightbulb

    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.

  3. #3
    Tim B's Avatar
    Tim B Guest


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    electronicsuk's Avatar
    electronicsuk Guest


    Did you find this post helpful? Yes | No

    Default

    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

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


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    electronicsuk's Avatar
    electronicsuk Guest


    Did you find this post helpful? Yes | No

    Default

    Perfect! Thanks Melanie.

    Matthew

  7. #7
    Pat-UK's Avatar
    Pat-UK Guest


    Did you find this post helpful? Yes | No

    Default

    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

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


    Did you find this post helpful? Yes | No

    Default

    Pushbutton operated LED chase...
    Code:
    	@ 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.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 18:04
  4. Need clever way to convert 10 bits to 8 bits
    By MikeTamu in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 2nd September 2005, 15:13
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 1

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