250 reg4 = 0x00
500 reg4 = 0x10
2000 reg4 = 0x20 or 0x30
why
250 reg4 = 0x00
500 reg4 = 0x10
2000 reg4 = 0x20 หรือ 0x30
this code
#include "stm32f10x_lib.h"
#include "stdio.h"
void delay(unsigned long ms);
void RCC_setup(void);
//void GPIO_setup(void);
void USART1_setup(void);
void USART1_PutC (unsigned char c);
void USART1_PutString (unsigned char *String);
int fputc(int ch, FILE * f) ; //user with printf
int USART1_Get(void);
void I2C_setup(void);
void Delay_us(unsigned int n);
void I2C_write(int address,int register_write,int data);
u8 I2C_read(int address,int register_read);
void Gyro_setup(void);
void get_gyro(void);
u8 Gyro_status(void);
void delay(unsigned long ms) // delay 1 ms per count @ Crystal 8.0 MHz and PLL9x or SYSCLK = 72 MHz
{
volatile unsigned long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 5525; j++ );
}
void Delay_us(unsigned int n)
{
u32 i;
while(n--)
{
i=2;
while(i--);
}
}
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function RCC Configuration ------------------------------------------//
//----------------------------------------------------------------------------------------------------//
void RCC_setup(void)
{
ErrorStatus HSEStartUpStatus; // Keep error status
RCC_DeInit(); // RCC system reset(for debug purpose)
RCC_HSEConfig(RCC_HSE_ON); // Enable HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2
FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable Prefetch Buffer
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz
RCC_PLLCmd(ENABLE); // Enable PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait until PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock source
while(RCC_GetSYSCLKSource() != 0x08); // Wait till PLL is used as system clock source
}
}
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function GPIO Configuration -----------------------------------------//
//----------------------------------------------------------------------------------------------------//
/*void GPIO_setup(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |
GPIO_Pin_9 |
GPIO_Pin_10|
GPIO_Pin_11|
GPIO_Pin_12|
GPIO_Pin_13|
GPIO_Pin_14|
GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
} */
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function USART Configuration -----------------------------------------//
//----------------------------------------------------------------------------------------------------//
void USART1_setup(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); // Enable USART1
}
void USART1_PutC (unsigned char c)
{
while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == 0);
USART_SendData (USART1,(int)c);
}
void USART1_PutString (unsigned char *String)
{
while (*String != 0)
{
USART1_PutC(*String++);
}
}
int USART1_Get(void)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == 0);
return(USART_ReceiveData(USART1));
}
int fputc(int ch, FILE * f) //user with printf
{
USART_SendData(USART1, (u8) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function I2C Configuration -----------------------------------------//
//----------------------------------------------------------------------------------------------------//
void I2C_setup(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
//I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C2, &I2C_InitStructure);
I2C_Cmd(I2C2, ENABLE);
}
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function I2C Configuration -----------------------------------------//
//----------------------------------------------------------------------------------------------------//
void I2C_write(int address,int register_write,int data)
{
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C2, address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C2,register_write);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C2,data);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C2, ENABLE);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
}
u8 I2C_read(int address,int register_read)
{
u8 datax;
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C2, address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C2,register_read);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C2,address, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_GenerateSTOP(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));
datax = I2C_ReceiveData(I2C2);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
return datax;
}
//----------------------------------------------------------------------------------------------------//
//------------------------------ Function Gyro Configuration -----------------------------------------//
//----------------------------------------------------------------------------------------------------//
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
int L3G4200D_Address = 0xD0; //I2C address of the L3G4200D
unsigned int xl,xh,yl,yh,zl,zh;
s16 x,y,z;
u8 Gyro_status(void)
{
u8 status;
status = I2C_read(L3G4200D_Address,0x27);
return status;
}
void Gyro_setup(void)
{
I2C_write(L3G4200D_Address,CTRL_REG1,0x0F);delay(10);
I2C_write(L3G4200D_Address,CTRL_REG2,0x00);delay(10);
I2C_write(L3G4200D_Address,CTRL_REG3,0x00);delay(10);
//I2C_write(L3G4200D_Address,CTRL_REG4,0x00);delay(10); //<< 250dps
//I2C_write(L3G4200D_Address,CTRL_REG4,0x10);delay(10); //<< 500dps
I2C_write(L3G4200D_Address,CTRL_REG4,0x20);delay(10); //<< 2000dps
I2C_write(L3G4200D_Address,CTRL_REG5,0x00);delay(10);
delay(1000);
}
void get_gyro(void)
{
xl = I2C_read(L3G4200D_Address, 0x28);
xh = I2C_read(L3G4200D_Address, 0x29);
x = (xh<<8) | xl;
yl = I2C_read(L3G4200D_Address, 0x2a);
yh = I2C_read(L3G4200D_Address, 0x2b);
y = (yh<<8) | yl;
zl = I2C_read(L3G4200D_Address, 0x2c);
zh = I2C_read(L3G4200D_Address, 0x2d);
z = (zh<<8) | zl;
}
//----------------------------------------------------------------------------------------------------//
//------------------------------------ Main Program --------------------------------------------------//
//----------------------------------------------------------------------------------------------------//
int main()
{
RCC_setup();
USART1_setup();
I2C_setup();
Gyro_setup();
printf("REG1 = %x \r\n",I2C_read(L3G4200D_Address, CTRL_REG1));
printf("REG2 = %x \r\n",I2C_read(L3G4200D_Address, CTRL_REG2));
printf("REG3 = %x \r\n",I2C_read(L3G4200D_Address, CTRL_REG3));
printf("REG4 = %x \r\n",I2C_read(L3G4200D_Address, CTRL_REG4));
printf("REG5 = %x \r\n",I2C_read(L3G4200D_Address, CTRL_REG5));
while(1)
{
get_gyro();
printf(" x = %d y = %d z = %d \r\n",x,y,z);
delay(1000);
}
}