Advertisement
hugol

Untitled

Dec 13th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Ac6
  5.   * @version V1.0
  6.   * @date    01-December-2013
  7.   * @brief   Default main function.
  8.   ******************************************************************************
  9. */
  10.  
  11.  
  12. #include "stm32f4xx.h"
  13. #include "stm32f429i_discovery.h"
  14. #include "tm_stm32f4_ili9341.h"
  15. #include "tm_stm32f4_fonts.h"
  16. #include "tm_stm32f4_delay.h"
  17. #include "tm_stm32f4_spi.h"
  18.  
  19. #include "tm_stm32f4_i2c.h"
  20.  
  21. #include "tm_stm32f4_delay.h"
  22. #include <stdio.h>
  23.  
  24.  
  25. #define TM_SPIx_PRESCALER    SPI_BaudRatePrescaler_32
  26. //Specify datasize
  27. #define TM_SPIx_DATASIZE     SPI_DataSize_8b
  28. //Specify which bit is first
  29. #define TM_SPIx_FIRSTBIT     SPI_FirstBit_MSB
  30. //Mode, master or slave
  31. #define TM_SPIx_MASTERSLAVE SPI_Mode_Master
  32. //Specify mode of operation, clock polarity and clock phase
  33. //Modes 0 to 3 are possible
  34. #define TM_SPIx_MODE        TM_SPI_Mode_0
  35.  
  36.  
  37. int L3G4200D_Address = 210;
  38.  
  39. #define CTRL_REG1 0x20
  40. #define CTRL_REG2 0x21
  41. #define CTRL_REG3 0x22
  42. #define CTRL_REG4 0x23
  43. #define CTRL_REG5 0x24
  44.  
  45. void initGYRO(int scale)
  46. {
  47.      //From  Jim Lindblom of Sparkfun's code
  48.  
  49.       // Enable x, y, z and turn off power down:
  50.     TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG1, 0b00001111);
  51.  
  52.       // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  53.     TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG2, 0b00000000);
  54.  
  55.       // Configure CTRL_REG3 to generate data ready interrupt on INT2
  56.       // No interrupts used on INT1, if you'd like to configure INT1
  57.       // or INT2 otherwise, consult the datasheet:
  58.     TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG3, 0b00001000);
  59.  
  60.       // CTRL_REG4 controls the full-scale range, among other things:
  61.  
  62.       if(scale == 250){
  63.           TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG4, 0b00000000);
  64.       }else if(scale == 500){
  65.           TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG4, 0b00010000);
  66.       }else{
  67.           TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG4, 0b00110000);
  68.       }
  69.  
  70.       // CTRL_REG5 controls high-pass filtering of outputs, use it
  71.       // if you'd like:
  72.       TM_I2C_Write(I2C1, L3G4200D_Address, CTRL_REG5, 0b00000000);
  73. }
  74.  
  75. int gyroX, gyroY, gyroZ;
  76. void readGyro(){
  77.     char xMSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x29);
  78.     char xLSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x28);
  79.     gyroX = ((xMSB << 8) | xLSB);
  80.  
  81.     char yMSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x2B);
  82.     char yLSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x2A);
  83.     gyroY = ((yMSB << 8) | yLSB);
  84.  
  85.     char zMSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x2D);
  86.     char zLSB = TM_I2C_Read(I2C1, L3G4200D_Address, 0x2C);
  87.     gyroZ = ((zMSB << 8) | zLSB);
  88.  
  89. }
  90.  
  91. void clearbuffer(char * buff, int len){
  92.      int _i =0;
  93.             for(_i=0; _i<len; _i++){
  94.                 buff[_i] = ' ';
  95.             }
  96.  
  97.             buff[len] = 0;
  98. }
  99.  
  100.  
  101.  
  102. int main(void) {
  103.     /* Initialize system */
  104.     SystemInit();
  105.  
  106.     /* Initialize delay */
  107.     TM_DELAY_Init();
  108.  
  109.     /* Initialize ILI9341 */
  110.     TM_ILI9341_Init();
  111.  
  112.     /* Rotate LCD for 90 degrees */
  113.     TM_ILI9341_Rotate(TM_ILI9341_Orientation_Landscape_2);
  114.  
  115.     /* Fill lcd with color */
  116.     TM_ILI9341_Fill(ILI9341_COLOR_MAGENTA);
  117.     /* Draw white circle */
  118.     TM_ILI9341_DrawCircle(60, 60, 40, ILI9341_COLOR_GREEN);
  119.     /* Draw red filled circle */
  120.     TM_ILI9341_DrawFilledCircle(60, 60, 35, ILI9341_COLOR_RED);
  121.     /* Draw blue rectangle */
  122.     TM_ILI9341_DrawRectangle(120, 20, 220, 100, ILI9341_COLOR_BLUE);
  123.     /* Draw black filled circle */
  124.     TM_ILI9341_DrawFilledRectangle(130, 30, 210, 90, ILI9341_COLOR_BLACK);
  125.     /* Draw line with custom color 0x0005 */
  126.     TM_ILI9341_DrawLine(10, 120, 310, 120, 0x0005);
  127.  
  128.  
  129.     /* Put string with black foreground color and blue background with 11x18px font */
  130.     TM_ILI9341_Puts(65, 130, "STM32F4 Discovery", &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_BLUE2);
  131.     /* Put string with black foreground color and blue background with 11x18px font */
  132.     TM_ILI9341_Puts(60, 150, "ILI9341 LCD Module", &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_BLUE2);
  133.     /* Put string with black foreground color and red background with 11x18px font */
  134.  
  135.  
  136.     TM_I2C_Init(I2C1, TM_I2C_PinsPack_2, 50000);
  137.  
  138.     initGYRO(2000);
  139.  
  140.     /* While loop */
  141.     while (1) {
  142.         char buff[256];
  143.  
  144.         readGyro();
  145.  
  146.         sprintf(buff, "%10hd; %10hd; %10hd;", gyroX, gyroY, gyroZ);
  147.  
  148.         TM_ILI9341_Puts(45, 230, buff, &TM_Font_7x10, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE);
  149.  
  150.         // delay
  151.  
  152.         int i;
  153.         for (i = 0; i < 500000; i++);
  154.         for (i = 0; i < 500000; i++);
  155.         for (i = 0; i < 500000; i++);
  156.         for (i = 0; i < 500000; i++);
  157.         for (i = 0; i < 500000; i++);
  158.         for (i = 0; i < 500000; i++);
  159.         for (i = 0; i < 500000; i++);
  160.         for (i = 0; i < 500000; i++);
  161.  
  162.  
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement