pamajema  
					New member  
					 
					Registered: 2008-12-02 
					Posts: 1 
				 
			 
			
				how to use the 3D accelerometer ? 
				
					Hi, Can someone help me on this because i can't put this working !? I have done this: 
Code: /* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "stdio.h"
#include "stm32f10x_spi.h"
/* Private typedef -----------------------------------------------------------*/
    GPIO_InitTypeDef GPIO_InitStructureB, GPIO_InitStructureA, GPIO_InitStructure;
    ErrorStatus HSEStartUpStatus;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;
    
/* Private define ------------------------------------------------------------*/
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) //inclui a função
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
    u8 nCount; // variavel da função delay
    u8 cont;   // variavel usada na interrupção
   
    u8 iAcXH,iAcXL; // H - high; L - Low
    u8 iAcYH,iAcYL; // H - high; L - Low
    u8 iAcZH,iAcZL; // H - high; L - Low
    
    u16 iAcX;
    u16 iAcY;
    u16 iAcZ;
   
/* Private function prototypes -----------------------------------------------*/
    void DEF_delay(vu32 nCount);
    void DEF_rcc(void);
    void DEF_periphals(void);
    void DEF_timer(void);
    void DEF_usart();
    void DEF_nvic(void);
    void DEF_spi(void);
    void DEF_acelerometro(void);
    void READ_iAcX(void);
    void READ_iAcY(void);
    void READ_iAcZ(void);
    void DEF_configuracao(void);
/* ---> PROGRAMA PRINCIPAL <--- */
/*******************************************************************************
* Function Name  : main
* Description    : testes aos interrupts
* Input          : P0
* Output         : P8 e P9
* Return         : None
*******************************************************************************/
int main(void) {
    DEF_configuracao();
    u8 ReadValue = 1;
    GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_SET);
    GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET);
    
    printf("vou USART\n");
   while(1) 
   {
       printf("Acelerómetro LIS3LV02DL\n");
   
       do
           {           
           READ_iAcX();
           // printf("X=%d",iAcX);
           USART_SendData(USART2, iAcX);
           
           READ_iAcY();
           // printf("Y=%d",iAcY);
           USART_SendData(USART2, iAcY);
           
           READ_iAcZ();
           // printf("Z=%d",iAcZ);
           USART_SendData(USART2, iAcZ);
           }
       while (ReadValue != 0);
   }
    
}
//
/* ---> SUBROTINA: Ler conteúdo de X <--- */
void READ_iAcX(void) {
   GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
   if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
      {
      SPI_I2S_SendData(SPI2,0x68); /* Leitura (0) + Incremento (1) + Registo incial (20) */
      if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
         {
         SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
         iAcXL = SPI_I2S_ReceiveData(SPI2);
         if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE))
            {
            SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
            iAcXH = SPI_I2S_ReceiveData(SPI2);
            }
         }
      GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
      }
   iAcX = iAcXH<<8;
   iAcX = iAcX&0xFF00;
   iAcX = iAcX + iAcXL;
}
//
/* ---> SUBROTINA: Ler conteúdo de Y <--- */
void READ_iAcY(void) {
   GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
   if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
      {
      SPI_I2S_SendData(SPI2,0x6A); /* Leitura (0) + Incremento (1) + Registo incial (2A) */
      if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
         {
         SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
         iAcYL = SPI_I2S_ReceiveData(SPI2);
         if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE))
            {
            SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
            iAcYH = SPI_I2S_ReceiveData(SPI2);
            }
         }
      GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
      }
   iAcY = iAcYH<<8;
   iAcY = iAcY&0xFF00;
   iAcY = iAcY + iAcXL;
}
//
/* ---> SUBROTINA: Ler conteúdo de Z <--- */
void READ_iAcZ(void) {
   GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
   if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
      {
      SPI_I2S_SendData(SPI2,0x6C); /* Leitura (0) + Incremento (1) + Registo incial (2C) */
      if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
         {
         SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
         iAcZL = SPI_I2S_ReceiveData(SPI2);
         if(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE))
            {
            SPI_I2S_SendData(SPI2,0x55); /* sinal clock - 0 1 0 1 0 1 0 1  */
            iAcZH = SPI_I2S_ReceiveData(SPI2);
            }
         }
      GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
      }
   iAcZ = iAcZH<<8;
   iAcZ = iAcZ&0xFF00;
   iAcZ = iAcZ + iAcZL;
}
//
/* ---> SUBROTINA: chama todas as configurações <--- */
void DEF_configuracao(void) {
    
   DEF_rcc();
   DEF_timer();
   DEF_nvic();
   DEF_periphals();
   DEF_usart();
   DEF_spi();
   DEF_acelerometro();
}
//
/* ---> SUBROTINA: definição do rcc <--- */
void DEF_rcc(void) {
   RCC_DeInit();
    RCC_HSEConfig(RCC_HSE_ON);
    HSEStartUpStatus = RCC_WaitForHSEStartUp();
    if(HSEStartUpStatus == SUCCESS)
        {
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_3);
        RCC_PLLCmd(ENABLE);
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        RCC_HCLKConfig(RCC_SYSCLK_Div1); 
        RCC_PCLK2Config(RCC_HCLK_Div1);
        /* Configure PCLK1 such as PCLK1 = HCLK/12  = 18MHz*/
        RCC_PCLK1Config(RCC_HCLK_Div2);       
        }
   
   // APB1
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
   
   // APB2
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
   
} 
//
/* ---> SUBROTINA: definição do timer <--- */
void DEF_timer(void) {
    TIM_TimeBaseStructure.TIM_Period = 65535;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_PrescalerConfig(TIM2, 1, TIM_PSCReloadMode_Immediate);
    TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}
//
/* ---> SUBROTINA: definição do nvic <--- */
void DEF_nvic(void) {
    
    /* Enable the USART2 global Interrupt */
    //NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure); // instrução que recebe as configurações
}
//
/* ---> SUBROTINA: definição pinos entrada <--- */
void DEF_periphals(void) {
   // GPIO A
  //*******************************************************************************
   /* Pin_2 - USART_TX */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   /* Pin_0 - button | Pin_3 - USART_RX */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   // GPIO B
   //*******************************************************************************
    /* Pin_3 - USART_RX | Pin_8 - led green | Pin_9 - led red | Pin_13 - SPI_SCK | Pin_15 - SPI_MOSI*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_13 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
   /* Pin_14 - SPI_MISO  */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   // GPIO D
   //*******************************************************************************
   /* Pin_2 - CS_MEMS */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
   
}
//
/* ---> SUBROTINA: definição de delay <--- */
void DEF_delay(vu32 nCount) {
    for(; nCount!= 0;nCount--);
    }
//
/* ---> SUBROTINA: definição de usart <--- */
void DEF_usart() {
    USART_InitStructure.USART_BaudRate = 9600;
    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_Tx | USART_Mode_Rx;
    USART_Init(USART2, &USART_InitStructure);
    USART_Cmd(USART2, ENABLE);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
//
/* ---> PROTOTIPO: definição para usart <--- */
PUTCHAR_PROTOTYPE
{
    /* Send one HalfWord on USART2 */
    USART_SendData(USART2, (u16) ch);
    /* Send one HalfWord on USART3 */
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
    {}
    
    return ch;
   }
    
//
/* ---> SUBROTINA: definição de SPI <--- */
void DEF_spi() {
   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
   SPI_InitStructure.SPI_CRCPolynomial = 7;
   
   SPI_Init(SPI2, &SPI_InitStructure);
}
//
/* ---> SUBROTINA: definição do acelerómetro <--- */
void DEF_acelerometro(void) {
   while(! SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE))
   {
   // configurar 
   GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
   SPI_I2S_SendData(SPI2,0xE0); /* Escrita (1) + Incremento (1) + Registo incial (100000) */
   SPI_I2S_SendData(SPI2,0xC7); /* config CTRL_REG1 (20h) - 1 1 0 0 0 1 1 1  */
   SPI_I2S_SendData(SPI2,0x04); /* config CTRL_REG2 (21h) - 0 0 0 0 0 1 0 0  */
   SPI_I2S_SendData(SPI2,0x00); /* config CTRL_REG3 (22h) - 0 0 0 0 0 0 0 0  */
   GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
   }
}  
					Last edited by pamajema (2008-12-03 11:17:06) 
				 
			 
			
			
			
		 
	 
 
	
	
		
			
				
					Francis  
					Administrator  
					 
					From: France-Grenoble 
					Registered: 2007-07-09 
					Posts: 890 
				 
			 
			
				 Re: how to use the 3D accelerometer ? 
				
					The MEMS is already managed by CircleOS. If you don't want to use CircleOS, you can see how the MEMS data is read from the source files of CircleOS (available on this web site).
				 
			 
			
			
			
		 
	 
 
	
	
		
			
				
					felipe123FELI  
					New member  
					 
					Registered: 2012-04-18 
					Posts: 2 
				 
			 
			
				 Re: how to use the 3D accelerometer ? 
				
					how to use 3D accelerometer witch i2c
				 
			 
			
			
			
		 
	 
 
	
	
		
			
				
					yrt  
					Administrator  
					 
					From: Grenoble-France 
					Registered: 2008-06-11 
					Posts: 520 
					Website  
				 
			 
			
				 Re: how to use the 3D accelerometer ? 
				
					The accelerometer can be managed by SPI or I2C, depending on its CS input. But on the Primers, it is connected to a MCU SPI port...
				 
			 
			
			
			
		 
	 
 
	
	
		
			
				
					felipe123FELI  
					New member  
					 
					Registered: 2012-04-18 
					Posts: 2 
				 
			 
			
				 Re: how to use the 3D accelerometer ? 
				
					hello... The acelerometer is ST - LSM303DLHC , witch comunication I2C..!