STM32F4无法驱动OLED

2019-03-23 18:57发布

代码在Arduino上可以运行, 但是移植到STM32上就不行了, 求高手赐教如何解决.
Arduino.h

  1. #include "Arduino.h"

  2. int _sda, _scl;
  3. void IO_Init(void);
  4. void Set_Pin(int pin, bool status);

  5. void IIC_without_ACK::IO_Init(void)
  6. {
  7.         pinMode(_sda, OUTPUT);
  8.         pinMode(_scl, OUTPUT);
  9. }

  10. void IIC_without_ACK::Set_Pin(int pin, bool status)
  11. {
  12.         digitalWrite(pin, status);
  13. }

  14. IIC_without_ACK::IIC_without_ACK(int sda, int scl)
  15. {
  16.         _sda = sda;
  17.         _scl = scl;
  18.         IO_Init();
  19. }

  20. void IIC_without_ACK::IIC_Start(void)
  21. {
  22.         Set_Pin(_scl, HIGH);
  23.         Set_Pin(_sda, HIGH);
  24.         Set_Pin(_sda, LOW);
  25.         Set_Pin(_scl, LOW);
  26. }

  27. void IIC_without_ACK::IIC_Stop(void)
  28. {
  29.         Set_Pin(_scl, LOW);
  30.         Set_Pin(_sda, LOW);
  31.         Set_Pin(_scl, HIGH);
  32.         Set_Pin(_sda, HIGH);
  33. }

  34. void IIC_without_ACK::Write_IIC_Byte(unsigned char IIC_Byte)
  35. {
  36.         for(int i=0; i<8; i++)
  37.         {
  38.                 if((IIC_Byte << i) & 0x80) Set_Pin(_sda, HIGH);
  39.                 else Set_Pin(_sda, LOW);
  40.                 Set_Pin(_scl, HIGH);
  41.                 Set_Pin(_scl, LOW);
  42.         }
  43.         Set_Pin(_sda, HIGH);
  44.         Set_Pin(_scl, HIGH);
  45.         Set_Pin(_scl, LOW);
  46. }

  47. void IIC_without_ACK::Write_IIC_Command(unsigned char IIC_Command)
  48. {
  49.          IIC_Start();
  50.          Write_IIC_Byte(0x78);
  51.          Write_IIC_Byte(0x00);
  52.          Write_IIC_Byte(IIC_Command);
  53.          IIC_Stop();
  54. }

  55. void IIC_without_ACK::Begin_IIC_Data()
  56. {
  57.          IIC_Start();
  58.          Write_IIC_Byte(0x78);
  59.          Write_IIC_Byte(0x40);
  60. }

  61. void IIC_without_ACK::Fill_Screen(unsigned char fill_Data)
  62. {
  63.         unsigned char m,n;
  64.         for(m=0; m<8; m++)
  65.         {
  66.                 Write_IIC_Command(0xb0 + m);
  67.                 Write_IIC_Command(0x00);
  68.                 Write_IIC_Command(0x10);
  69.                 Begin_IIC_Data();
  70.                 for(n=0; n<128; n++) Write_IIC_Byte(fill_Data);
  71.                 IIC_Stop();
  72.         }
  73. }

  74. void IIC_without_ACK::Initial()
  75. {
  76.         unsigned char shellcode[] = {0xAE, 0x00, 0x10, 0x40, 0x81, 0xCF, 0xA1, 0xC8, 0xA6, 0xA8, 0x3F, 0xD3, 0x00, 0xD5, 0x80, 0xD9, 0xF1, 0xDA, 0x12, 0xDB, 0x40, 0x20, 0x02, 0x8D, 0x14, 0xA4, 0xA6, 0xAF};
  77.         for (int i=0; i<28; i++) Write_IIC_Command(shellcode[i]);
  78. }
复制代码
STM32F4-main.c
  1. #include "main.h"

  2. static void SystemClock_Config(void);
  3. static void Error_Handler(void);
  4. static void EXTILine0_Config(void);

  5. void IIC_Start(void);
  6. void IIC_Stop(void);
  7. void Write_IIC_Byte(unsigned char IIC_Byte);
  8. void Write_IIC_Command(unsigned char IIC_Command);
  9. void Begin_IIC_Data(void);
  10. void Fill_Screen(unsigned char fill_Data);
  11. void Initial(void);
  12. int _sda = 0x8080, _scl = 0x8000;
  13. int HIGH = 0x7000, LOW = 0x7070;
  14. void IO_Init(void);
  15. void Set_Pin(int pin, int status);

  16. void IIC_Init(void);
  17. void IIC_Start(void);
  18. void IIC_Stop(void);
  19. void Write_IIC_Byte(unsigned char IIC_Byte);
  20. void Write_IIC_Command(unsigned char IIC_Command);
  21. void Begin_IIC_Data(void);
  22. void IIC_SetPos(unsigned char x, unsigned char y);
  23. void Fill_Screen(unsigned char fill_Data);
  24. void SSD1306_Init(void);

  25. int main(void)
  26. {
  27.   HAL_Init();
  28.        
  29.   BSP_LED_Init(LED3);
  30.   BSP_LED_Init(LED4);
  31.        
  32.   SystemClock_Config();

  33.   EXTILine0_Config();
  34.   
  35.         IO_Init();
  36.         HAL_Delay(10);
  37.         Initial();
  38.         HAL_Delay(10);
  39.         Fill_Screen(0xff);
  40.        
  41.   while (1)
  42.         {
  43.                 HAL_Delay(500);
  44.                 BSP_LED_Toggle(LED4);
  45.         }
  46. }

  47. static void SystemClock_Config(void)
  48. {
  49.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  50.   RCC_OscInitTypeDef RCC_OscInitStruct;
  51.   __PWR_CLK_ENABLE();
  52.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  53.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  54.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  55.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  56.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  57.   RCC_OscInitStruct.PLL.PLLM = 8;
  58.   RCC_OscInitStruct.PLL.PLLN = 360;
  59.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  60.   RCC_OscInitStruct.PLL.PLLQ = 7;
  61.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) Error_Handler();
  62.   HAL_PWREx_ActivateOverDrive();
  63.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  64.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  65.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  66.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  67.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  68.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) Error_Handler();
  69. }

  70. static void EXTILine0_Config(void)
  71. {
  72.   GPIO_InitTypeDef GPIO_InitStructure;
  73.   __GPIOA_CLK_ENABLE();
  74.   GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
  75.   GPIO_InitStructure.Pull = GPIO_NOPULL;
  76.   GPIO_InitStructure.Pin = GPIO_PIN_0;
  77.         HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  78.        
  79.   HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
  80.   HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  81. }

  82. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  83. {
  84.   if(GPIO_Pin == KEY_BUTTON_PIN)
  85.   {
  86.     BSP_LED_Toggle(LED3);
  87.   }
  88. }

  89. void IO_Init(void)
  90. {
  91.         GPIO_InitTypeDef GPIO_InitStructure;
  92.         __GPIOE_CLK_ENABLE();
  93.         GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  94.         GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
  95.         GPIO_InitStructure.Pull = GPIO_PULLDOWN;
  96.         GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
  97.        
  98.         HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
  99. }

  100. void Set_Pin(int pin, int status)
  101. {
  102.         if (pin == _sda) HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, status == HIGH ? GPIO_PIN_SET : GPIO_PIN_RESET);
  103.         if (pin == _scl) HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, status == HIGH ? GPIO_PIN_SET : GPIO_PIN_RESET);
  104. }

  105. void IIC_Start(void)
  106. {
  107.         Set_Pin(_scl, HIGH);
  108.         Set_Pin(_sda, HIGH);
  109.         Set_Pin(_sda, LOW);
  110.         Set_Pin(_scl, LOW);
  111. }

  112. void IIC_Stop(void)
  113. {
  114.         Set_Pin(_scl, LOW);
  115.         Set_Pin(_sda, LOW);
  116.         Set_Pin(_scl, HIGH);
  117.         Set_Pin(_sda, HIGH);
  118. }

  119. void Write_IIC_Byte(unsigned char IIC_Byte)
  120. {
  121.         for(int i=0; i<8; i++)
  122.         {
  123.                 if((IIC_Byte << i) & 0x80) Set_Pin(_sda, HIGH);
  124.                 else Set_Pin(_sda, LOW);
  125.                 Set_Pin(_scl, HIGH);
  126.                 Set_Pin(_scl, LOW);
  127.         }
  128.         Set_Pin(_sda, HIGH);
  129.         Set_Pin(_scl, HIGH);
  130.         Set_Pin(_scl, LOW);
  131. }

  132. void Write_IIC_Command(unsigned char IIC_Command)
  133. {
  134.          IIC_Start();
  135.          Write_IIC_Byte(0x78);
  136.          Write_IIC_Byte(0x00);
  137.          Write_IIC_Byte(IIC_Command);
  138.          IIC_Stop();
  139. }

  140. void Begin_IIC_Data()
  141. {
  142.          IIC_Start();
  143.          Write_IIC_Byte(0x78);
  144.          Write_IIC_Byte(0x40);
  145. }

  146. void Fill_Screen(unsigned char fill_Data)
  147. {
  148.         unsigned char m,n;
  149.         for(m=0; m<8; m++)
  150.         {
  151.                 Write_IIC_Command(0xb0 + m);
  152.                 Write_IIC_Command(0x00);
  153.                 Write_IIC_Command(0x10);
  154.                 Begin_IIC_Data();
  155.                 for(n=0; n<128; n++) Write_IIC_Byte(fill_Data);
  156.                 IIC_Stop();
  157.         }
  158. }

  159. void Initial()
  160. {
  161.         unsigned char shellcode[] = {0xAE, 0x00, 0x10, 0x40, 0x81, 0xCF, 0xA1, 0xC8, 0xA6, 0xA8, 0x3F, 0xD3, 0x00, 0xD5, 0x80, 0xD9, 0xF1, 0xDA, 0x12, 0xDB, 0x40, 0x20, 0x02, 0x8D, 0x14, 0xA4, 0xA6, 0xAF};
  162.         for (int i=0; i<28; i++) Write_IIC_Command(shellcode[i]);
  163. }

  164. static void Error_Handler(void)
  165. {
  166.   BSP_LED_On(LED4);
  167.   while(1);
  168. }

  169. #ifdef  USE_FULL_ASSERT

  170. void assert_failed(uint8_t* file, uint32_t line)
  171. {
  172.   while (1);
  173. }
  174. #endif
复制代码 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。