A question regarding 'bits' that I don't understand!

Steve2381

Senior Member
Hey all

I have bit comms working with my Oled screen using the code below you kindly showed me

Code:
#Picaxe 40x2

#No_Data
setfreq m8

Symbol pinE  = pinb.7 : Symbol dirE  = dirb.7   ; 0 = Idle      1 = Active
Symbol pinRS = pinb.5 : Symbol dirRS = dirb.5   ; 0 = Command   1 = Data

Symbol pinD4 = pinb.6 : Symbol dirD4 = dirb.6   ; LCD Data Line 4
Symbol pinD5 = pinc.5 : Symbol dirD5 = dirc.5   ; LCD Data Line 5
Symbol pinD6 = pinc.4 : Symbol dirD6 = dirc.4   ; LCD Data Line 6
Symbol pinD7 = pinB.1 : Symbol dirD7 = dirB.1   ; LCD Data Line 7


PowerOnReset:

  Gosub InitialiseLcd


Displayline1:

b0 = $80 : Gosub SendCmdByte			'Move to start of line 1

  For bPtr = 0 To 19
    LookUp bPtr, ( "Line number 1       " ), b0 : Gosub SendDataByte
  Next

stop



InitialiseLcd:

  dirE  = 1             ; Set LCD control lines as outputs
  dirRS = 1
  dirD4 = 1
  dirD5 = 1
  dirD6 = 1
  dirD7 = 1

  For bPtr = 0 To 5
    LookUp bPtr, ( $33, $32, $28,$0C, $06, $01 ), b0 : Gosub SendInitCmdByte
  Next

  Return	
	
	
SendInitCmdByte:

Pause 15              ; Delay 15mS

SendCmdByte:

  pinRS = 0             ; Send to Command register

SendDataByte:

  pinD7 = bit7          ; Put MSB out first
  pinD6 = bit6
  pinD5 = bit5
  pinD4 = bit4

  pinE  = 1             ; Give a pulse on E
  pinE  = 0

  pinD7 = bit3          ; Put LSB out second
  pinD6 = bit2
  pinD5 = bit1
  pinD4 = bit0

  pinE  = 1             ; Give a pulse on E
  pinE  = 0

  pinRS = 1             ; Send to Data register next

  Return
I don't understand the bit0,bit1,bit2 etc - but hey... its works! It is sending the contents of b0 to the display as required.
I need it to send the contents of b4.... if that makes sense (LookUp bPtr, ( $33, $32, $28,$0C, $06, $01 ), b4 : Gosub SendInitCmdByte).
If I simply put b4 at the end of the Lookup statement, it doesn't work.

Brainy people required!

Edit... OK. I understand that bit0-bit7 are the contents of b0. So how do you access the 'bit' contents of b4?
 
Last edited:

lbenson

Senior Member
LookUp bPtr, ( $33, $32, $28,$0C, $06, $01 ), b0 : Gosub SendInitCmdByte

What the lookup statement does is put 6 bytes, in sequence, into b0, and then the contents of (each) b0 are sent to the display. Bit0-bit7 are the constituent bits of b0. If you want to send b4 to the display, then first move b4 to b0 and then call SendDataByte.

b0 = b4
Gosub SendDataByte

Note that b4 needs to be a displayable character (for instance, a letter of the alphabet or a numeral), not a number in binary.
 

Steve2381

Senior Member
Ah - thanks. Trouble is b0 is already used in my program and a right pain to change now - its used across all 4 slots.
Mmm... rethink time
 

westaust55

Moderator
For computers and microcontrollers (such as PICAXE) the fundamental element of data is a bit which can have a value of 0 or 1.
A Nybble comprises 4 bits of data - we often use 4-bit interfaces to LCDs as in your project.
A byte comprises 8 bits (= 2 nybbles) and a word has 16 bits = 2 bytes.
There are also long words which comprise 32 bits = 2 words.

Within the byte variables/values bit0 is the least significant bit and represents 0 or 1.
Bit0 by convention is on the right of our representations so in binary %00000001 has bit0 set (=1)
Bit7 is shown on the left and in binary for a byte value represents 0 or 128 (decimal). So %10000000 represents 128.

Computers and microcontrollers work internally in binary whereas humans may depict a value in binary, Hexadecimal, decimal, BCD or ASCII.
Thus %01000001 = $41 = 65 decimal = ASCII character "A".

The use of the bits to pins scheme as per your program example is a convenient way of allowing one to use a collection of 4 non consecutive/adjacent PICAXE IO pins from 1 or more ports for the 4-bit interface to an LCD.
 

westaust55

Moderator
Ah - thanks. Trouble is b0 is already used in my program and a right pain to change now - its used across all 4 slots.
Mmm... rethink time
You can use b1 and on most PICAXE parts b2 and/or b3 as an alternative to b0.
byte variable b1 has bit variables bit8 to bit15 and b2 plus b3 higher bit numbers as per PICAXE manual 2 (see page 11).

For the X2 parts there are also the commands PUSHRAM and POPRAM to save to and retrieve from memory the pre-defined variables.
 
Last edited:

Goeytex

Senior Member
Ah - thanks. Trouble is b0 is already used in my program and a right pain to change now - its used across all 4 slots.
Mmm... rethink time
Good golly.... all you have to do is use the search & replace feature in the editor. How hard is that?

When I am writing a program where I may need access to Bits 0 - 31, I usually start using variables at B4/W2, reserving b0 - b3 for bits or temporary/shared variables. Sometimes I start at B55 and work down.
 

premelec

Senior Member
As Goeytex sez you can search and replace - my preference is to use a Symbol foo = b0 and then just change the b0 to b4 at the front end... makes changes down the lines pretty easy... just make sure you aren't unintentionally using same variable in different places that conflict.
 

westaust55

Moderator
Good golly.... all you have to do is use the search & replace feature in the editor. How hard is that?

When I am writing a program where I may need access to Bits 0 - 31, I usually start using variables at B4/W2, reserving b0 - b3 for bits or temporary/shared variables. Sometimes I start at B55 and work down.
Personnally, I usually do the same. My Symbol assignments for bit variables may be set up akin to:
Code:
SYMBOL Reserved_Flags1 = b0    ; this byte variable used for bit 
    SYMBOL DoubleVert       = bit0    ; 1 = double height characters
    SYMBOL DoubleHoriz      = bit1    ; 1 = double width character
    SYMBOL ColourState      = bit2    ; 1 for colour / 0 for monochrome
    ; Bits 3 to 7 spare

SYMBOL LCD_Buffer        = b1    ; buffer to hold LCD nybbles for 4-bit data transfer 
    SYMBOL LCD_D4           = bit8
    SYMBOL LCD_D5           = bit9
    SYMBOL LCD_D6           = bit10
    SYMBOL LCD_D7           = bit11
    ; Bits 12 to 15 spare

SYMBOL GraphicPtr = b2 ; Pointer to EEPROM Location for font data
 
Last edited:

hippy

Technical Support
Staff member
To explicitly use 'b4' rather than 'b0' ...

Code:
SendDataByte:

  pinD7 = b4 / $80      ; Put MSB out first
  pinD6 = b4 / $40
  pinD5 = b4 / $20
  pinD4 = b4 / $10

  pinE  = 1             ; Give a pulse on E
  pinE  = 0

  pinD7 = b4 / $08      ; Put LSB out second
  pinD6 = b4 / $04
  pinD5 = b4 / $02
  pinD4 = b4

  pinE  = 1             ; Give a pulse on E
  pinE  = 0

  pinRS = 1             ; Send to Data register next

  Return
But those divisions will make it slower. On an X2 you can use ">>" shifts. Another option is simply to swap 'b4' and 'b0' on entry and again on exit.
 

AllyCat

Senior Member
Hi,

... b0 is already used in my program and a right pain to change now
Hmm, so you didn't use "meaningful" names for all your variables (which would have required just a change in the Symbol declaration) ? Tut, Tut. ;)

Personally, I reserve b0 for "Global" flag bits (others might prefer to use b1) and b1 (or b0) and w1 as "temporary" or "local" variables, such as for passing parameters to/from subroutines. Then there is always a bit-addressable register available if needed.

As others have said, the Program Editor search/replace can be your friend, or SWAP is a "quick fix" (but not very efficient in either speed or codespace).

Cheers, Alan.
 

Steve2381

Senior Member
Ok... getting a bit of a slap here then.
I do name my variables - usually. I have used up all available variables, so I will have to swap some stuff around. I spent all last night 'trimming' down my code and making things more efficient to get the programs in all 4 slots under the size limit (converting from 8 bit to 4 bit pushed the coding over the max allowed).
My code is messy... I know it is, but it works. That is why I hate posting it because usually I get more grief for the way I have coded it, than the problem I am suffering.
I taught my self this stuff, and therefore nobody ever showed me the 'right' way of doing it. I see other peoples code and try to get mine similar, but it doesn't just come naturally when you don't understand 50% of the commands.
Anyway... thanks for the help
 

JimPerry

Senior Member
There is no "Right Way" to code a program - and English (or, insert your native language here) is a very difficult second language for most programmers :cool:
 
Last edited:
Top