Get some bit from different places and put them as one byte!

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

Hi,
I think this problem is very difficult to explain, but I try my best!
For some days ago I write my own library for LCD (HD44780)
Then I used prototype board, and one Atmega8.
I used DDRD and all PORTD for the LCD,
PORTD.0 = R/S , PORTD.1 = Enable, PORTD.2 "“ 5 for DATA
everything works fine and I write everything I wanted on the screen.

But now I have design more complicated circuit, with PushButton, ADC and more..
I use now different PORTx and DDRx.
I use PORTB.0 = R/S , PORTB.1 = Enable
and PORTC.2 "“ 5 for DATA
But I use PORTC.0 "“ 1 as ADC input..

And in my LCD driver, I get bit from R/S, Enable, D4 "“ 7 and do some operation on them
and write it back, but NOW the bit is from different places!
So how can I read from different places in ONE (just one) byte?
And write from byte to different places?

So let say 1 byte have 8bit,
and they start from index 0 to 7,
So in Index 0 put value from PORTB.0, Index 1 put value from PORTB.1
and Index 2 put value from PORTC.2
And the same for write back?

I give you my code from LCD library, as you can see, here we just read from PORTD,
but in my new design PORTD do some other things..
So how can I write this code for this problem?
(In my new design I use Atmega32)

Thank you

void LCD_send(char data){
	char _rs = PORTD;
	_rs &= 4;

	//Send some "HIGH"
	char _temp = data;
	_temp &= 0xF0;
	_temp |= _rs;
	PORTD = _temp;

	//PULSE
	PORTD |= (1<<EN);
	PORTD &= ~(1<<EN)

	//Send some "LOW"
	_temp = data;
	_temp &= 0x0F;
	_temp *= 16;
	_temp |= _rs;
	PORTD = _temp;

	//PULSE
	PORTD |= (1<<EN);
	PORTD &= ~(1<<EN)

	//1mS delay
	_delay_ms(1);

}

Attachment(s): 

Last Edited: Tue. Aug 23, 2011 - 03:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See Fleury's LCD code as an example that allows the bits to be spread across ports.

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

clawson wrote:
See Fleury's LCD code as an example that allows the bits to be spread across ports.

Do you have some link? (Yes I google it, but not sure I found the currect one or not)
And do you have some good tips??

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

Peter Fleury's code is on this page:

http://homepage.hispeed.ch/peter...

Note in lcd.h:

#define LCD_PORT         PORTA        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
#define LCD_RS_PIN       4            /**< pin  for RS line         */
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
#define LCD_RW_PIN       5            /**< pin  for RW line         */
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
#define LCD_E_PIN        6            /**< pin  for Enable line     */

Then follow through the code to see how these are used - in effect allowing every control signal and every one of the data bits to be defined on different ports if you want.

That is all the above don't have to be "LCD_PORT" (which is set to PORTA in this instance).

=============================================================================

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

Thanks, I tryed to use Peter Fleury's code, I know is very famous..
But I don't know why I have so much problem whit it..
I found some other library and now its works fine.

https://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_type=project&item_id=2167
writen by: alank2

Takes me 5min, just to setup (very easy and a lot of command inside for better help)
and now works perfect..

I just miss some function to write double value.. (23.41)
But I write some function and put it into library by my self, if somebody nead it so here is the function:

/*************************************************************************
Display Double
Input:    Dobuel value
Returns:  none
*************************************************************************/
void lcd_putd(double value){
	char buffer[6];
	dtostrf(value, 2, 2, buffer);
	for(int i = 0; i < (sizeof(buffer) - 1); i++){ 
		if(buffer != 0){
			lcd_putc(buffer[i]);
		}
	}
}

Thanks to [i]clawson and alank2

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

Quote:

char buffer[6];

How sure are you that all possible outputs of dtostrf(), including a terminating NUL will fit in a 6 byte buffer?

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

clawson wrote:
Quote:

char buffer[6];

How sure are you that all possible outputs of dtostrf(), including a terminating NUL will fit in a 6 byte buffer?

Yes, works fine..
The thing is here I want two digit before "." and two digit after..
altso xx.xx and and I know my value dont need to be more..
then I have 5 characters.. 4digit and one dot.
And I use one more place for null.. so 6 at the end..
If I want more decimal or less so I must change it...

I write this self for my old LCD driver..
I wanted to put double into the screen, and this code works fine :=)