Sign in to follow this  
Followers 0
biza

How to read a byte and convert him to bit

8 posts in this topic

Fellows, I have a big problem with a application i have trying to do. I need to read a characters who came in byte format and transform him in bit format I have some trouble in write the function, can anyone helpe me

Share this post


Link to post
Share on other sites

A byte (or character) is a collection of 8 bits already so no need to convert. If you need to access the individual bits then this can be done by masking or shift combined with masking.

Or do you need to parse a multibyte character string representing a single byte? If so, what is the format?

Share this post


Link to post
Share on other sites

I'm trying to connect the msp432com an lcd. so This way I have to create a routine to read one char variable like0x0A for example and write the data pins for the LCD .(lcd 16x2 4bit interface). Just for testing the port pins registers.

Share this post


Link to post
Share on other sites

Easiest solution: use an LCD driver, then you can write the characters to the LCD without bothering with pins or bits.

Somewhat more involved solution: if your LCD is connected to pins P1.0 through P1.3 (assuming you have a 4 bit LCD), you can write your byte in two phases like this

P1OUT = byte & 0xF;
// sync/strobe
P1OUT = byte >> 4;
// sync/strobe

More complicated way: if you need to bit-bang your LCD, you can address individual bits by masking them out, similar to the thing I did above, but for single bits.

B0 = byte & 0x01;
B1 = byte & 0x02;
B2 = byte & 0x04;
B3 = byte & 0x08;
B4 = byte & 0x10;
B5 = byte & 0x20;
B6 = byte & 0x40;
B7 = byte & 0x80;

Assuming B0..B7 exist.

Share this post


Link to post
Share on other sites

Roadrunner84, 

I already take the decision and made this code:

#include "lcd.h"
/*****************************************************************/
/*********                     DEFINE                    *********/
/*****************************************************************/

#define P2orP5orP6LCD  0x0200/*PORT[A] OU PORT[B] OU PORT[C]*/

 #define EN              (PP2 + BIT3)
 #define RS              (PP6 + BIT7)
 #define D4              (PP2 + BIT6)
 #define D5              (PP2 + BIT4)
 #define D6              (PP5 + BIT6)
 #define D7              (PP6 + BIT6)

/*COMMANDS*/

#define CLEAR       0x01

/*****************************************************************/
/*********                LOCAL FUNCTIONS                *********/
/*****************************************************************/

void lcdDirPinout(unsigned int pin)
{
     if(pin < P2orP5orP6LCD){
         P2DIR |= (pin & 0x00FF);
     }else {
         P5DIR |= (pin & 0x00FF);
     }
}


void lcdClrPinout(unsigned int pin)
{
     if(pin < P2orP5orP6LCD){
         P2OUT &= ~(pin & 0x00FF);
     }else{
         P5OUT &= ~(pin & 0x00FF);
     }
}

void lcdSetValue(unsigned char value)
{
    if(value & ){
        lcdDirPinout(D7);
    }else{
        lcdClrPinout(D7);
    }
    
    if(value & ){
        lcdDirPinout(D6);
    }else{
        lcdClrPinout(D6);
    }
    
    if(value & ){
        lcdDirPinout(D5);
    }else{
        lcdClrPinout(D5);
    }
    
    if(value & ){
        lcdDirPinout(D4);
    }else{
        lcdClrPinout(D4);
    }
    delay_us(5);
}

void lcdTriggerEN()
{
    lcdSetPinout(EN);
    delay_us(40);
    lcdClrPinout(EN);
}

void lcdWriteData(unsigned char data)
{
    lcdSetPinout(RS);
    lcdSetValue(data >>); /*UPPER NIBBLE*/
    lcdTriggerEN();
    lcdSetValue(data); /*LOWER NIBBLE*/
    lcdTriggerEN();
    delay_us(40);
    
}

void lcdWriteCmd(unsigned char cmd)
{
    lcdSetPinout(RS);
    lcdSetValue(cmd >>); /*UPPER NIBBLE*/
    lcdTriggerEN();
    lcdSetValue(cmd); /*LOWER NIBBLE*/
    lcdTriggerEN();
    delay_ms(2);
    
}

/*****************************************************************/
/*********               GLOBAL FUNCTIONS                *********/
/*****************************************************************/

void LCD_PortConfig(void)
{
    lcdDirPinout(D7);
    lcdDirPinout(D6);
    lcdDirPinout(D5);
    lcdDirPinout(D4);
    lcdDirPinout(EN);
    lcdDirPinout(RS);
    
}

void LCD_Initialize(void)
{
    delay_us(80);
    P6OUT = ;
    lcdTriggerEN();
    delay_ms(2);
    lcdTriggerEN();
    delay_ms(2);
    lcdTriggerEN();
    delay_ms(2);
    
    P6OUT = ; /*SWITCH TO 4-BIT MODE*/
    lcdTriggerEN();
    delay_ms(2);
    
    lcdWriteCmd(0x28);/*4 BIT'S MODE, 2 LINES, WITH MATIX 5x8*/
    lcdWriteCmd(0x08);/*DISPLAY CURSOR OFF, SHOW OFF WITHOUT CLEAR DDRAM*/
    lcdWriteCmd(0x01);/*CLEAR ALL CONTENT SCREEN AN DDRAM*/
    lcdWriteCmd(0x06);/*SEND DE PLACE TO PUT CHARACTER HIM*/
    lcdWriteCmd(0x0C);/*SHOW CURSOR OFF AND DISPLAY ON, WITHOUT BLINK*/
}

void LCD_SetText(char* text, int x, int y)
{
    unsigned int i;
    if(x < 16){
        x|= 0x80; /*WRITE THE FIRST LINE ON LCD*/
        switch(y){
            case 1:
                x|= 0x40; /*WRITE THE SECOND LINE ON LCD*/
            break;
            
            case 2:
                x|=; /*FIRST LINE WRITE REVERSE*/
            break;
            
            case 3:
                x|=; /*SECOND LINE WRITE REVERSE*/
            break;
        }
        lcdWriteCmd(x);
    }
    
    i=0;
    while(text[i]!='\0'){
        lcdWriteData(text[i]);
        i++;
    }
    
}

void LCD_SetInt(int val, int x, int y){
    char number_string[16];
    sprintf(number_string,'%d', val); /*CHARACTER INTEGER TO STRING CONVERTION*/
    LCD_SetText(number_string, x, y);
}

void LCD_Clear()
{
    lcdWriteCmd(CLEAR);
}


void LCD_SetSymbol(unsigned char symbol, unsigned char offset, unsigned char line)
{
    lcdWriteCmd(line+offset);
    
    lcdWriteData(symbol);
}

/*****************************************************************/
/*********                       END                     *********/
/*****************************************************************/

But how can i detect  the port?

I need to detect the register of the following ports

P2

P5

P6

Share this post


Link to post
Share on other sites

What do you mean by "detecting a port"? If the port exists in hardware, there will be side effects from writing to it (ie: pins change state). If the port does not exist, there is no alias to the location of the register, so you can't write to it.

 

These lines

if(value & ){

should contain a value, 1,2,4 and 8 between the and & operator and the closing bracket.
 

These lines

lcdSetValue(data >>); /*UPPER NIBBLE*/

should contain the value 4 between the shift >> operator and the closing bracket.

Share this post


Link to post
Share on other sites
typedef union
{
    struct
    {
        unsigned char bit0 : 1;
        unsigned char bit1 : 1;
        unsigned char bit2 : 1;
        unsigned char bit3 : 1;
        unsigned char bit4 : 1;
        unsigned char bit5 : 1;
        unsigned char bit6 : 1;
        unsigned char bit7 : 1;
    }bits;
    unsigned char byte;
}byte;

Some claim this is not portable, but if you know what bit significance you're using . . . who cares.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0