找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3047|回复: 2
收起左侧

STM32F030以SPI方式驱动OLED(HAL库版)

[复制链接]
ID:466381 发表于 2021-2-28 15:58 | 显示全部楼层 |阅读模式
运用HAL库驱动的硬件SPI OLED程序,用起来爽歪歪,快得很

单片机源程序如下:

  1. /* USER CODE END Header */
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "spi.h"
  5. #include "gpio.h"
  6. #include "oledspi.h"
  7. /* Private includes ----------------------------------------------------------*/
  8. /* USER CODE BEGIN Includes */
  9. #include "bmp.h"
  10. /* USER CODE END Includes */

  11. /* Private typedef -----------------------------------------------------------*/
  12. /* USER CODE BEGIN PTD */

  13. /* USER CODE END PTD */

  14. /* Private define ------------------------------------------------------------*/
  15. /* USER CODE BEGIN PD */
  16. /* USER CODE END PD */

  17. /* Private macro -------------------------------------------------------------*/
  18. /* USER CODE BEGIN PM */

  19. /* USER CODE END PM */

  20. /* Private variables ---------------------------------------------------------*/

  21. /* USER CODE BEGIN PV */

  22. /* USER CODE END PV */

  23. /* Private function prototypes -----------------------------------------------*/
  24. void SystemClock_Config(void);
  25. /* USER CODE BEGIN PFP */

  26. /* USER CODE END PFP */

  27. /* Private user code ---------------------------------------------------------*/
  28. /* USER CODE BEGIN 0 */

  29. /* USER CODE END 0 */

  30. /**
  31.   * @brief  The application entry point.
  32.   * @retval int
  33.   */
  34. int main(void)
  35. {
  36.   /* USER CODE BEGIN 1 */
  37.         uint8_t t;
  38.   /* USER CODE END 1 */

  39.   /* MCU Configuration--------------------------------------------------------*/

  40.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  41.   HAL_Init();

  42.   /* USER CODE BEGIN Init */

  43.   /* USER CODE END Init */

  44.   /* Configure the system clock */
  45.   SystemClock_Config();

  46.   /* USER CODE BEGIN SysInit */

  47.   /* USER CODE END SysInit */

  48.   /* Initialize all configured peripherals */
  49.   MX_GPIO_Init();
  50.   MX_SPI1_Init();
  51.   /* USER CODE BEGIN 2 */
  52.         OLED_Init();                        //初始化OLED  
  53.         OLED_Clear()          ;
  54.   /* USER CODE END 2 */

  55.   /* Infinite loop */
  56.   /* USER CODE BEGIN WHILE */
  57.   while (1)
  58.   {
  59.     /* USER CODE END WHILE */               
  60.     /* USER CODE BEGIN 3 */
  61.                 OLED_Clear();
  62.                 OLED_ShowCHinese(0,0,0);//晶
  63.                 OLED_ShowCHinese(18,0,1);//蓝
  64.                 OLED_ShowCHinese(36,0,2);//电
  65.                 OLED_ShowCHinese(54,0,3);//子
  66.                 OLED_ShowCHinese(72,0,4);//科
  67.                 OLED_ShowCHinese(90,0,5);//技
  68.                 OLED_ShowString(0,2,"0.96 OLED TEST");
  69.                  OLED_ShowString(20,4,"2020/02/28");  
  70.                 OLED_ShowString(0,6,"ASCII:");  
  71.                 OLED_ShowString(63,6,"CODE:");  
  72.                 OLED_ShowChar(48,6,t);//显示ASCII字符          
  73.                 t++;
  74.                 if(t>'~')t=' ';
  75.                 OLED_ShowNum(103,6,t,3,16);//显示ASCII字符的码值        
  76.                        
  77.                
  78.                 HAL_Delay(500);
  79.                 OLED_Clear();
  80.                 HAL_Delay(500);
  81.                 OLED_DrawBMP(0,0,128,8,(uint8_t*)BMP1);  //图片显示(图片显示慎用,生成的字表较大,会占用较多空间,FLASH空间8K以下慎用)
  82.                 HAL_Delay(500);
  83.                 OLED_DrawBMP(0,0,128,8,(uint8_t*)BMP2);
  84.                 HAL_Delay(500);
  85.   }
  86.   /* USER CODE END 3 */
  87. }

  88. /**
  89.   * @brief System Clock Configuration
  90.   * @retval None
  91.   */
  92. void SystemClock_Config(void)
  93. {
  94.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  95.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  96.   /** Initializes the RCC Oscillators according to the specified parameters
  97.   * in the RCC_OscInitTypeDef structure.
  98.   */
  99.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  100.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  101.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  102.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  103.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  104.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  105.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  106.   {
  107.     Error_Handler();
  108.   }
  109.   /** Initializes the CPU, AHB and APB buses clocks
  110.   */
  111.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  112.                               |RCC_CLOCKTYPE_PCLK1;
  113.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  114.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  115.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  116.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  117.   {
  118.     Error_Handler();
  119.   }
  120.   /** Enables the Clock Security System
  121.   */
  122.   HAL_RCC_EnableCSS();
  123. }

  124. /* USER CODE BEGIN 4 */

  125. /* USER CODE END 4 */

  126. /**
  127.   * @brief  This function is executed in case of error occurrence.
  128.   * @retval None
  129.   */
  130. void Error_Handler(void)
  131. {
  132.   /* USER CODE BEGIN Error_Handler_Debug */
  133.   /* User can add his own implementation to report the HAL error return state */
  134.   __disable_irq();
  135.   while (1)
  136.   {
  137.   }
  138.   /* USER CODE END Error_Handler_Debug */
  139. }

  140. #ifdef  USE_FULL_ASSERT
  141. /**
  142.   * @brief  Reports the name of the source file and the source line number
  143.   *         where the assert_param error has occurred.
  144.   * @param  file: pointer to the source file name
  145.   * @param  line: assert_param error line source number
  146.   * @retval None
  147.   */
  148. void assert_failed(uint8_t *file, uint32_t line)
  149. {
  150.   /* USER CODE BEGIN 6 */
  151.   /* User can add his own implementation to report the file name and line number,
  152.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  153.   /* USER CODE END 6 */
  154. }
  155. #endif /* USE_FULL_ASSERT */

  156. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有程序51hei提供下载:
OLED_SPI.7z (5.11 MB, 下载次数: 87)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:735675 发表于 2021-7-9 00:41 | 显示全部楼层
兄弟,不对呀,你这不是硬件SPI啊
回复

使用道具 举报

ID:81196 发表于 2021-7-9 09:12 | 显示全部楼层
还是不带字库的,这个开发起来未免有些麻烦吧,,直接使用带字库的多方便
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表