[SOLVED] Can I write a 8 bit or 16 bit Data to the outport of the STM32 directly

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Activity points
10,615
0

I am using STM32F103 ,CUBE IDE with HAL library , I am trying to write the outport as giving a 4 bit value to the entire port (16bit ) By using a shift bit operator . Can I directly apply that data to the port as follows. GPIO_PORTA |= 0x0ff; Like wise Or how to write it
 

Hello!

Not sure of what you want to know, I don't understand thee 4 bits and shift question. Are you asking if you can write a 16-bit number directly at the output? If you are looking for this, then you have this instruction for instance if you want to write 0xABCD to the output.

GPIOA->ODR = 0xABCD;

ODR stands for Output Data Register.
Similarily, you have an IDR for input. But before that, be sure that the direction is properly configured.

NB: writing to GPIO_PORTA will (probably) not do anything good. It's the base structure to the port registers, so you have to name which field of PORTA you want to change.

Dora.


 
Thanks

(I am learning STM32 based programming with HAL )
By using HAL Library ,
How to put data as follows
GPIOA->ODR = 0xABCD;
what is the procedure
writing 16 bit data to the whole port expect individual bit ?
--- Updated ---

@doraemon Thanks​

It workded
 
Last edited:

Hi,

HAL is a library. Each HAL command will be processed by a function. Each function may consist of a bunch of machine instructions.

There are instructions to SET dedicated bits (OR style)
There are instructions to CLEAR dedicated bits (AND style)

Using both these instructions in a function you are able to "write" any combination to the output:
Each bit in a 16 bit word can be
* unchanged
* HIGH
* LOW

*****
You may do the same functionality with different instructions. Like:
* read port pins into a variable
* perform OR on the variable
* perform AND on the variable
* write variable to the port

***
Maybe there are single instructions that perform both masking and updating in one. For this you need to read the instruction set.

***
You should provide an example of what exactly you want to achieve

Klaus
 

By using HAL Library ,
How to put data as follows
GPIOA->ODR = 0xABCD;
There's no HAL_GPIO function to manipulate multiple pins at once. Instead you'll use GPIO register read and write instructions, as suggested.

You don't necessarily manually code the and/or operations to write part of a register. After defining a respective structure, the compiler can take care of it.
 

Then GPIOA->ODR = 0xABCD; is it wrong idea ,May i need to write each bitfield one by one ?
--- Updated ---

I can touch the whole port as said @doraemon .whch can be make flexxible to me to write code (I dont know it is good )
I feel it is good to touch the port to direct .please comment my queries





 
Last edited:

I still didn't hear clearly what you want to achieve.
Then GPIOA->ODR = 0xABCD; is it wrong idea ,May i need to write each bitfield one by one ?
For writing 16 bit of data to GPIOA->ODR at once, the instruction is correct, because GPIOx_ODR register uses only 16 bit and requires the upper word to be set to 0. But you have been asking how to write 4/8/16 bit data, so it's not the general solution.
 

Sir I am here writing 4 bit data to the 16bit port by the above said way ,
Is it correct ,its for sending 4 bita data to the lcd for 4 bit mode
is it correct .
 

The command sequence is only setting but not resetting GPIO bits, thus not achieving what you want.

You can either define respective bit fields or use and/or to mask previous register value.
Code:
 GPIO->ODR = (GPIO->ODR & 0x0fff) | (cmd & 0xf000);
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating
After writing to the PORT and strobe ,may i need it to reset sir can you elaborate .
May I get the code snipet full for the function like you said above ?


or as follows
C:
void LCD_cmd(__uint16_t cmd)
{       HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_RESET);    
        cmd = (cmd << 8) ;
        GPIOB->ODR =  0xf000 & cmd;
        LCD_Strobe();
        GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (cmd & 0xf000);
         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11, GPIO_PIN_RESET);
        cmd = (cmd << 4) ;
        GPIOB->ODR =  0xf000 & cmd;
        LCD_Strobe();
        GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (cmd & 0xf000);

}

}



sir Please correct if wrong
 
Last edited:

Don't know how pin11 comes into play. It's not used in your previous code.
Code:
void LCD_cmd(__uint16_t cmd)
{ 
        cmd = (cmd << 8) ;
        GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (cmd & 0xf000);
        LCD_Strobe();
        cmd = (cmd << 4) ;
        GPIOB->ODR = (GPIOB->ODR & 0x0fff) | (cmd & 0xf000);
        LCD_Strobe();
}
 
I just noticed by looking at your parallel same topic thread that pin 11 is not register select, it's enable pin. Really bad to post code without specifying the hardware.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…