Use Stm32cubemx to get data from nunchuck

In this topic I will show how to connect nunchuck controller with stm32microcontroller by using software stm32cubemx .
Nunchuck controller is used i2c interface.

 

To set up i2c interface with stm32cubemx you can view at this topic
Nunchuck address is 0x52.Clock was set up with 100kHz.

…………………………………………………
#define nunchuck 0x52<<1

I2C_HandleTypeDef hi2c1;
…………………………………………………………….

int main(void)
{

…………………………………………………………………….

char cmd[1];
uint8_t data[6];
uint8_t  handShake[2] ;
handShake[0]=0xf0;
handShake[1]=0x55;
HAL_I2C_Master_Transmit(&hi2c1,nunchuck,handShake, 2,0×1000);
HAL_Delay(100);
handShake[0]=0xfb;
handShake[1]=0x00;
HAL_I2C_Master_Transmit(&hi2c1,nunchuck,handShake, 2,0×1000);
HAL_Delay(100);
while (1)
{
…………………………………………………………………………………..
cmd[0]=0x00;
HAL_I2C_Master_Transmit(&hi2c1,nunchuck,cmd, 1,0×1000);
HAL_Delay(1);
HAL_I2C_Master_Receive(&hi2c1, nunchuck, data,6,0×1000);
uint16_t   accelX = (data[2] << 2)|((data[5] >> 2) & 0x03) ;
uint16_t  accelY = (data[3] << 2)|((data[5] >> 4) & 0x03) ;
uint16_t  accelZ = (data[4] << 2)|((data[5] >> 6) & 0x03) ;
uint8_t buttonc=(data[5]&0x02)>>1;
uint8_t buttonz=data[5]&0x01;
…………………………………………………..
}


 ……………………………………………………………

Most views

Leave a Reply

Your email address will not be published. Required fields are marked *