Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Bits, Bytes and registers

Status
Not open for further replies.

SwingeyP

Member
apart from 8 bits - byte and all that I have to admit that I am still a little cold on this subject of .hb and .lb

Can anyone help me with this please.

I am editing the example for the 7SEG display. I want to read a code from PORT D on 16F877A.

It could be 01111101 = 0x7d- Great I thought. So now lets look at the .hb and the .lb

.hb = 0111 .lb = 1101 right?

SO whay can't I do this ....

Dim digit As Byte 'input variable for GETMASK subroutine
Dim digit1 As Byte 'current high digit
Dim digit2 As Byte 'current low digit

blah, blah , blah - as per example

and then this .....

loop:
For i = 0 To 99
digit1 = PORTD.LB 'get lowbyte
digit2 = PORTD.HB 'get highbyte
TMR0 = 0 'reset Timer0 to prevent its interrupt before both masks are determined
digit = digit1
Gosub getmask 'get mask for high digit
mask1 = mask
digit = digit2
Gosub getmask 'get mask for low digit
mask2 = mask
Gosub show1 'display new mask
Gosub show2 'display new mask
WaitUs 500 'delay interval suitable for simulation
'use large delay for the real device, say WAITMS 500
Next i
Goto loop
End

Hmm something needs to be a word here I think.

I am trying to read the .lb and .hb to drive two 7 seg displays that will display whats is on the DPORT. not really bothered about the other code (timer etc) I was just editing the example.

Regards - Paul
 
Well, blah, blah, blah may be relevant or not but the three subroutines called are definitely relevant. Post complete code and you may get help. When you post code, type [code] before it and [/code] after it so that it keeps it's formatting.

Mike.
 
Last edited by a moderator:
I think your a little confused.

01111101 = 0x7d

0111 = 7..... This is the higher NIBBLE of a byte
1101 = d... . This is the lower NIBBLE of a byte

If you had 0000011100001101 = 0x070d

High byte = 7
Low byte = d

You need to be clear on this.
 
Here is the complete code. Its really jst the example form the OSHON site

Code:
Dim digit1 As Byte  'current high digit
Dim digit2 As Byte  'current low digit
Dim mask As Byte  'output variable from GETMASK subroutine
Dim mask1 As Byte  'current high digit mask
Dim mask2 As Byte  'current low digit mask
Dim i As Byte
Dim phase As Bit

Symbol d1enable = PORTC.0  'enable line for higher 7-segment display
Symbol d2enable = PORTC.1  'enable line for lower 7-segment display

TRISB = %00000000  'set PORTB pins as outputs
TRISC.0 = 0  'set RC0 pin as output
TRISC.1 = 0  'set RC1 pin as output
TRISD = %11111111  'Set port D to input

d1enable = False
d2enable = False
mask1 = 0
mask2 = 0
phase = 0
INTCON.T0IE = 1  'enable Timer0 interrupts
INTCON.GIE = 1  'enable all un-masked interrupts
OPTION_REG.T0CS = 0  'set Timer0 clock source to internal instruction cycle clock

loop:
For i = 0 To 99
	digit1 = PORTD.LB  'get current high digit
	digit2 = PORTD.HB  'get current low digit
	TMR0 = 0  'reset Timer0 to prevent its interrupt before both masks are determined
	digit = digit1
	Gosub getmask  'get mask for high digit
	mask1 = mask
	digit = digit2
	Gosub getmask  'get mask for low digit
	mask2 = mask
	Gosub show1  'display new mask
	Gosub show2  'display new mask
	WaitUs 500  'delay interval suitable for simulation
	'use large delay for the real device, say WAITMS 500
Next i
Goto loop
End                                               

On Interrupt  'Timer0 interrupt routine
'continuously switch between high and low digit displays
If phase = 0 Then
phase = 1
Gosub show1
Else
phase = 0
Gosub show2
Endif
INTCON.T0IF = 0  'enable new TMR0 interrupts
Resume                                            

getmask:  'get appropriate 7-segment mask for input digit
mask = LookUp(0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71), digit
Return                                            

show1:  'show high digit on its display
d2enable = False
PORTB = mask1
d1enable = True
Return                                            

show2:  'show low digit on its display
d1enable = False
PORTB = mask2
d2enable = True
Return

The original example just made two 7 segment displays count. I used this code and added the bits for a,b,c,d,e,f on the lookup.
Much of the code I wont use like the timer etc. I just want to read 8 bits and display the value on the two 7segment displays. so if I get 11100110 = E6 is displayed.

Hope that makes sense.

Regards - Paul
 
Hi Thanks for that. Im still not really getting this. I now have.
Code:
Dim digit As Byte  'input variable for GETMASK subroutine
Dim digit1 As Byte  'current high digit
Dim digit2 As Byte  'current low digit
Dim mask As Byte  'output variable from GETMASK subroutine
Dim mask1 As Byte  'current high digit mask
Dim mask2 As Byte  'current low digit mask
Dim i As Byte

Symbol d1enable = PORTC.0  'enable line for higher 7-segment display
Symbol d2enable = PORTC.1  'enable line for lower 7-segment display

TRISB = %00000000  'set PORTB pins as outputs
TRISC.0 = 0  'set RC0 pin as output
TRISC.1 = 0  'set RC1 pin as output
TRISD = %11111111  'Set port D to input

d1enable = True
d2enable = True
mask1 = 0
mask2 = 0

loop:
For i = 0 To 255
	digit1 = PORTD  'get current high digit
	digit2 = ShiftLeft(PORTD, 4)  'get current low digit
	
	digit = digit1
	Gosub getmask  'get mask for high digit
	mask1 = mask
	
	digit = digit2
	Gosub getmask  'get mask for low digit
	mask2 = mask
	Gosub show1  'display new mask
	Gosub show2  'display new mask
	WaitUs 500  'delay interval suitable for simulation
	'use large delay for the real device, say WAITMS 500
Next i
Goto loop
End                                               

getmask:  'get appropriate 7-segment mask for input digit
mask = LookUp(0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71), digit
Return                                            

show1:  'show high digit on its display
PORTB = mask1
Return                                            

show2:  'show low digit on its display
PORTB = mask2
Return

I have stripped quite a bit out. The code compiles and runs but when I watch the variables they are always zero and of course 0 is always displayed on the displays.

I still don't understand whay I can't just do this ....

digit1 = PORTD.HB
digit2 = PORTD.LB

This would surely solve the problem but it doesn't compile like that.

Regards - Paul
 
Forgot to mention that when I run the current code above I am setting the PORTD values on the simulator. Nothing is happening :-(
 
Aha! - Found a few things out. First off I need to click on the pins of the Microcontroller view NOT the DPORT as shown in the simulator. I have also just realised what it is I am trying to do. It is of course a 'nibble' I think. 1/2 a byte. So I need to address the high nibble and the low nibble. I guess this is where the shiftleft comes in. I have never really used this so please bear with me whilst I get my head round it.

The code I posted earlier does compile and does work but the shiftleft is reading the same value for digit1 and digit2. How do I address just the high nibble and the low nibble?

Regards - Paul
 
This is just so frustrating. - Can anyone tell me how to assign the high nibble to a variable and the low nibble to a variable. Surely there must be a way to do this?

Everytime I try something the compiler tells me something about incorrect byte variable or bit variable ........ I can't even add PORTD.1 to PORTD.2 (a= portd.1+portd.2) that may have been a solution.

CAN ANYONE PLEASE HELP ME?

Regards - Paul
 
This is just so frustrating. - Can anyone tell me how to assign the high nibble to a variable and the low nibble to a variable. Surely there must be a way to do this?
...
Regards - Paul

Ian Rogers showed you how to do this exact thing in post #6.

I still don't understand whay I can't just do this ....

digit1 = PORTD.HB
digit2 = PORTD.LB

PORTD is 8 bits or 1 byte. There is no HB or LB
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top