找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6520|回复: 7
收起左侧

STM32F401驱动0.96寸OLED屏显示0ADC采集的值,已验证测试ok

  [复制链接]
ID:169702 发表于 2017-12-10 20:53 | 显示全部楼层 |阅读模式
使用的是四接口的0.96寸OLED
//              GND    电源地
//              VCC  接5V或3.3v电源
//              SCL   PB4
//              SDA   PB5   
44.jpg

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2017 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f4xx_hal.h"

  41. /* USER CODE BEGIN Includes */
  42. #include "oled.h"
  43. #include "bmp.h"
  44. /* USER CODE END Includes */

  45. /* Private variables ---------------------------------------------------------*/
  46. ADC_HandleTypeDef hadc1;

  47. TIM_HandleTypeDef htim3;

  48. /* USER CODE BEGIN PV */
  49. /* Private variables ---------------------------------------------------------*/

  50. /* USER CODE END PV */

  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_ADC1_Init(void);
  55. static void MX_TIM3_Init(void);

  56. /* USER CODE BEGIN PFP */
  57. /* Private function prototypes -----------------------------------------------*/
  58.   u32 ADC_Value;

  59. /* USER CODE END PFP */

  60. /* USER CODE BEGIN 0 */

  61. /* USER CODE END 0 */

  62. int main(void)
  63. {

  64.   /* USER CODE BEGIN 1 */

  65.   /* USER CODE END 1 */

  66.   /* MCU Configuration----------------------------------------------------------*/

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

  69.   /* USER CODE BEGIN Init */

  70.   /* USER CODE END Init */

  71.   /* Configure the system clock */
  72.   SystemClock_Config();

  73.   /* USER CODE BEGIN SysInit */

  74.   /* USER CODE END SysInit */

  75.   /* Initialize all configured peripherals */
  76.   MX_GPIO_Init();
  77.   MX_ADC1_Init();
  78.   MX_TIM3_Init();

  79.   /* USER CODE BEGIN 2 */
  80.   OLED_Init();
  81.   OLED_Clear();

  82.   /* USER CODE END 2 */

  83.   /* Infinite loop */
  84.   /* USER CODE BEGIN WHILE */
  85.   while (1)
  86.   {
  87.   /* USER CODE END WHILE */

  88.   /* USER CODE BEGIN 3 */
  89.     /*************显示字符测试************/
  90. //    OLED_ShowString(0,0,"A");
  91. //    OLED_ShowString(0,2,"A");
  92. //    OLED_ShowString(0,4,"A");
  93. //    OLED_ShowString(0,6,"A");
  94. //   
  95. //    OLED_ShowString(8,0,"A");
  96. //    OLED_ShowString(8,2,"A");
  97. //    OLED_ShowString(8,4,"A");
  98. //    OLED_ShowString(8,6,"A");   
  99. //   
  100. //    OLED_ShowString(120,0,"A");
  101. //    OLED_ShowString(120,2,"A");
  102. //    OLED_ShowString(120,4,"A");
  103. //    OLED_ShowString(120,6,"A");
  104.     /*************显示汉字测试************/   
  105. //    OLED_ShowCHinese(0,0,0);
  106. //    OLED_ShowCHinese(0,2,0);
  107. //    OLED_ShowCHinese(0,4,0);
  108. //    OLED_ShowCHinese(0,6,0);
  109. //   
  110. //    OLED_ShowCHinese(18,0,0);
  111. //    OLED_ShowCHinese(18,2,0);
  112. //    OLED_ShowCHinese(18,4,0);
  113. //    OLED_ShowCHinese(18,6,0);
  114. //   
  115. //    OLED_ShowCHinese(108,0,0);
  116. //    OLED_ShowCHinese(108,2,0);
  117. //    OLED_ShowCHinese(108,4,0);
  118. //    OLED_ShowCHinese(108,6,0);
  119.     /*************显示图片测试************/   
  120. //      OLED_DrawBMP(0,0,128,8,BMP1);

  121.     /*************ADC1测试************/
  122.       HAL_ADC_Start(&hadc1);
  123.       HAL_ADC_PollForConversion(&hadc1,50);
  124.       
  125.       ADC_Value=HAL_ADC_GetValue(&hadc1);
  126.       
  127.       OLED_ShowString(0,0,"ADC_Value");
  128.       OLED_ShowNum(1,2,ADC_Value,4,16);   
  129.          
  130.       /*************流水灯测试************/  
  131. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET);
  132. //      HAL_Delay(1000);
  133. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_SET);
  134. //      HAL_Delay(1000);
  135. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET);
  136. //      HAL_Delay(1000);
  137. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_SET);
  138. //      HAL_Delay(1000);
  139. //        
  140. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_RESET);
  141. //      HAL_Delay(1000);
  142. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_RESET);
  143. //      HAL_Delay(1000);
  144. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_RESET);
  145. //      HAL_Delay(1000);
  146. //      HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
  147. //      HAL_Delay(1000);
  148.     /*************TIM3测试************/
  149.       HAL_TIM_Base_Start_IT(&htim3);
  150.   }
  151.   /* USER CODE END 3 */

  152. }

  153. /** System Clock Configuration
  154. */
  155. void SystemClock_Config(void)
  156. {

  157.   RCC_OscInitTypeDef RCC_OscInitStruct;
  158.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  159.     /**Configure the main internal regulator output voltage
  160.     */
  161.   __HAL_RCC_PWR_CLK_ENABLE();

  162.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  163.     /**Initializes the CPU, AHB and APB busses clocks
  164.     */
  165.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  166.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  167.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  168.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  169.   RCC_OscInitStruct.PLL.PLLM = 6;
  170.   RCC_OscInitStruct.PLL.PLLN = 168;
  171.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  172.   RCC_OscInitStruct.PLL.PLLQ = 4;
  173.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  174.   {
  175.     _Error_Handler(__FILE__, __LINE__);
  176.   }

  177.     /**Initializes the CPU, AHB and APB busses clocks
  178.     */
  179.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  180.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  181.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  182.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  183.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  184.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  185.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  186.   {
  187.     _Error_Handler(__FILE__, __LINE__);
  188.   }

  189.     /**Configure the Systick interrupt time
  190.     */
  191.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  192.     /**Configure the Systick
  193.     */
  194.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  195.   /* SysTick_IRQn interrupt configuration */
  196.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  197. }

  198. /* ADC1 init function */
  199. static void MX_ADC1_Init(void)
  200. {

  201.   ADC_ChannelConfTypeDef sConfig;

  202.     /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  203.     */
  204.   hadc1.Instance = ADC1;
  205.   hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  206.   hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  207.   hadc1.Init.ScanConvMode = DISABLE;
  208.   hadc1.Init.ContinuousConvMode = DISABLE;
  209.   hadc1.Init.DiscontinuousConvMode = DISABLE;
  210.   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  211.   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  212.   hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  213.   hadc1.Init.NbrOfConversion = 1;
  214.   hadc1.Init.DMAContinuousRequests = DISABLE;
  215.   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  216.   if (HAL_ADC_Init(&hadc1) != HAL_OK)
  217.   {
  218.     _Error_Handler(__FILE__, __LINE__);
  219.   }

  220.     /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  221.     */
  222.   sConfig.Channel = ADC_CHANNEL_0;
  223.   sConfig.Rank = 1;

  224. ……………………

  225. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
ADC.rar (11.58 MB, 下载次数: 189)

评分

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

查看全部评分

回复

使用道具 举报

ID:282283 发表于 2018-2-6 10:57 | 显示全部楼层
做个金属壳屏蔽继电器
回复

使用道具 举报

ID:283270 发表于 2018-2-9 11:08 | 显示全部楼层
这个不错。。我要正在准备弄个OLED屏玩玩。
回复

使用道具 举报

ID:289512 发表于 2018-3-31 09:59 | 显示全部楼层
好东西,有空玩玩。。
回复

使用道具 举报

ID:342071 发表于 2018-7-22 21:53 | 显示全部楼层
666666感谢楼主
回复

使用道具 举报

ID:374694 发表于 2018-7-23 07:26 | 显示全部楼层
LIHAILE厉害了
回复

使用道具 举报

ID:348540 发表于 2019-4-28 21:58 | 显示全部楼层
谢谢楼主,但里面没知道工程文件
回复

使用道具 举报

ID:52988 发表于 2020-1-19 09:26 | 显示全部楼层
这个里面不知道有没有工程文件?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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