Reading a single bit.

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, friends, thanks for your time!

What I'm doing is reading the high nibble of a port, the low nibble its rotating, so the port its working as an input and as an output.

The code:

#include 
#include 

int main(void)
{
	//DDRB = 0X0F;
	DDRB = 0x0f;
	volatile uint8_t FlagA = 0;
	PORTB = 0x01;
    while(1)
    {
                 if(FlagA == 0)
		 {
			PORTB = PORTB;
			FlagA = 1;
		 }
		 else
		 {
			 PORTB = (PORTB << 1);
		 }
		 
		 _delay_ms(100);
		 if (PORTB == 0x08)
		 {
			 PORTB = 0x01;
			 FlagA = 0;
		 }
		 
		 
	         switch(PINB)
		 {
			 case 0b0001xxxx:
										
				break;
		 }		 
			 
    }
}
 

using STK600 and a Atmega2560.

I want to know if there is any changes in high nibble.

I know that the xxxx dont go there, what I mean with the Xs its that I dont care the low nibble, I just wrote it that way so I can better explain the case.

Thanks again!

Hope you have some ideas!

Alex.

Last Edited: Sat. Apr 28, 2012 - 01:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have the idea that you should post in the correct forum. :-) I'll move the thread.

Also see the "bit manipulation" tutorial in the tutorial forum.

edit I see you are already aware of the tutorial https://www.avrfreaks.net/index.p...

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
uint8_t value = PINB & 0xf0;
switch (value)
    ...

But unless all 16 variations of the upper nibble have unique meanings, using a switch is probably no the correct way of doing this.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alexotano wrote:

switch(PINB)
{
    case 0b0001xxxx:
        
        break;
}

I want to know if there is any changes in high nibble.


Try this:
if (PINB & 0xF0)
{
    
}

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about

unsigned char HiNibThisPass,HiNibLastPass;
unsigned char HiNibChanged;

HiNibThisPass=(PINB & 0xf0);
HiNibChanged=HiNibThisPass != HiNibLastPass;
HiNibLastPass=HiNibThisPass;

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
I have the idea that you should post in the correct forum. :-) I'll move the thread.

Also see the "bit manipulation" tutorial in the tutorial forum.

edit I see you are already aware of the tutorial https://www.avrfreaks.net/index.p...

Sorry about that! thanks for correcting me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now go edit the misspelled thread title for the benefit of other folks?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well Thanks to all the answers! at the end I use a mask PORTB & 0xf0, that to ensure the value of the low nibble.

And it is working just fine..

Now, is it possible to make a pointer to the port register and move it bit per bit?

My friend had that idea, but I insist that a memory address has 1 byte (8 bits).

what he proposes is that if the port register is in the address 0x90, then you'll have the 8 bits of the port from 0x90 to 0x97... is it possible? how can I access memory like that.

I'll finish the code and I'll post it, it is to read a 4x4 matrix keyboard, using a single port.

if any one has an idea to read bit per bit without using masking..

Last Edited: Sat. Apr 28, 2012 - 02:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Now go edit the misspelled thread title for the benefit of other folks?

Correct!! never saw it.. sorry about my english, every marked error is apreciated!
you mean the "single" right??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alexotano wrote:
I friend had that idea, but I insist that a memory address has 1 byte (8 bits).

what he proposes is that if the port register is in the address 0x90, then you'll have the 8 bits of the port from 0x90 to 0x97... is it possible? how can I access memory like that.

Your friend is wrong and you are right. Even if you could access each bit like that, you wouldn't want to.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read that bit manipulation 101 thread as far as page 6 and "danni" (Peter Danneger) posts his sbit.h which casts a bitfield onto any memory or IO location you choose so that you can treat the individual bits as if they were separate - which may be what you are trying to achieve.

You can do things like:

#include "sbit.h"

#define RED_LED PORT_B5
#define GREEN_LED PORT_B7

int main(void) {
  DDR_B5 = 1;
  DDR_B7 = 1;
  while(1) {
    RED_LED = 1;
    GREEN_LED = 0;
    _delay_ms(200);
    RED_LED = 0;
    GREEN_LED = 1;
    _delay_ms(200);
  }
}

Where RED_LED and GREEN_LED are symbolic names for the individual bits PORTB.5 and PORTB.7

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Read that bit manipulation 101 thread as far as page 6 and "danni" (Peter Danneger) posts his sbit.h which casts a bitfield onto any memory or IO location you choose so that you can treat the individual bits as if they were separate - which may be what you are trying to achieve.

Great to know.. I didn't saw that .h file, I just downloaded and I'll try it later and comment the results.

Thanks for the info!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well at the end I didnt use the sbit.h lib instead I use masking, but here is the end code if some one has the seam problem from reading a matrix keyboard. Sorry about the dealy, a lot of work from school and costumers.

int main(void)
{
	//TODO:: config ports. 0-3 output  - 4-7 input
	DDRB = 0x0f;
	DDRD = 0xff;
	uint8_t a= 0,b= 0, c = 0;
	uint8_t PTOB = 0,PNB = 0;
	uint8_t tabble[4][4]={0x06,0x5b,0x4f,0x77,0x66,0x6d,0x7d,0x7c,0x07,0x7f,0x6f,0x39,0x76,0x3f,0xf6,0x5e};
	PORTB = 0x01;

	while (1)
	{
		//TODO :: making a rotabit once at the time and in each move check if any key is press
		if (c==0)
		{
			c = 1;
		} 
		else
		{
			PORTB = (PORTB << 1);
			a++;
		}
		_delay_ms(10);
		
		PTOB = (PINB & 0x0f);
		if (PTOB == 0x08)
		{
			PORTB = 1;
			c = 0;
			a=0;
		}
		
		// TODO :: check for changes in colums
		PNB = (PINB & 0xf0);
		if (PNB != 0)
		{

			switch(PNB)
			{
				case 0x10:

					while(PNB == (PINB & 0xf0))
					{
						PORTD = ~tabble[a][0];
					}
					break;
				case 0x20:

					while(PNB == (PINB & 0xf0))
					{
						PORTD = ~tabble[a][1];
					}
					break;
				case 0x40:

					while(PNB == (PINB & 0xf0))
					{
						PORTD = ~tabble[a][2];
					}
					break;
				case 0x80:

					while(PNB == (PINB & 0xf0))
					{
						PORTD = ~tabble[a][3];
					}
					break;
				default:
					while(PNB == (PINB & 0xf0))
					{
						PORTD = 0x86; // in case of two keys been press we write a E in the 7 segs display
					}
					break;
			}
			
			PORTD = 0xbf;
		} 
		else
		{
			//TODO ::  leave a - in the display
			PORTD = 0xbf;
		}
	}
		
}

if any questions I'll do my best to answer
Thanks to all answers!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
      PTOB = (PINB & 0x0f);
      if (PTOB == 0x08)

? ;-)