STM32F103仿真驱动点亮LCD1602
一、开发环境介绍
proteus 8.8 版本+ STM32CubeMX 4.18.0
程序基于keil集成开发环境编写,基于ST官方的HAL库。
STM32CubeMX pack版本
STM32CubeMX的版本
二、项目功能介绍
该项目程序中实现了
1、程序实现了LCD1602的点亮显示
2、程序中实现了STM32的AD采样
3、程序中实现了串口通信
4、程序中实现了按键控制操作
通过滑动变阻器加放大器改变输入值。后边加上lcd1602,初始化显示“光节点检测系统”
然后加个按键按下开始 显示“光功率值:(多少)dbm 这里的范围就0-100
通过调节前边电位器每次只变化一下, 输出某个数。
按键控制串口发送
三、实验效果
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- #include "bsp-lcd1602.h"
- #include "delay.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc1;
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- #define KEY0 HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_0) //KEY0按键PC5
- #define KEY1 HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_1) //KEY1按键PA15
- #define KEY0_PRESS 1
- #define KEY1_PRESS 2
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void Error_Handler(void);
- static void MX_GPIO_Init(void);
- static void MX_ADC1_Init(void);
- static void MX_USART1_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- int fputc(int ch, FILE *f)
- {
- HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
- return ch;
- }
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- void PrintFloat(float value)
- {
- int tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
- tmp = (int)value;
- tmp1=(int)((value-tmp)*10)%10;
- tmp2=(int)((value-tmp)*100)%10;
- tmp3=(int)((value-tmp)*1000)%10;
- tmp4=(int)((value-tmp)*10000)%10;
- tmp5=(int)((value-tmp)*100000)%10;
- tmp6=(int)((value-tmp)*1000000)%10;
- printf("%d.%d%d%d%d%d%d\r\n",tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);
- }
- u8 KEY_Scan(u8 mode)
- {
- static u8 key_up=1;//按键按松开标志
- if(mode)key_up=1; //支持连按
- if(key_up&&(KEY0==0||KEY1==0))
- {
- HAL_Delay(10);//去抖动
- key_up=0;
- if(KEY0==0)return KEY0_PRESS;
- else if(KEY1==0)return KEY1_PRESS;
- }else if(KEY0==1&&KEY1==1)key_up=1;
- return 0;// 无按键按下
- }
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- u8 t=0,display_flag=0;
- u8 a,b,c,d;
- unsigned int value=0;
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_ADC1_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- LCD1602_Init();
- HAL_Delay(10); //延时一段时间
-
- LCD1602_ShowStr(2,0,"Optical node",12);
- LCD1602_ShowStr(0,1,"detection system",16);
-
- HAL_Delay(1000);
-
- LCD1602_WriteCmd( 0x01);//清屏
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(HAL_ADC_Start(&hadc1)!=HAL_OK) //启动ADC1
- {
- Error_Handler();
- }
-
- HAL_ADC_PollForConversion(&hadc1, 300) ;// 开始转换
- value=HAL_ADC_GetValue(&hadc1); //获取转换的结果
-
- if(value==4095)
- value=100;
- else if(value==0)
- value=0;
- else value=value/41;
-
- a=value/1000;
- b=value%1000/100;
- c=value%100/10;
- d=value%100%10;
-
- t=KEY_Scan(0);
-
- if(t==KEY0_PRESS)
- {
- display_flag=1;
- LCD1602_ShowStr(0,0,"Optical Power",13);
- LCD1602_ShowStr(0,1,"Value=",6);
- }
-
- if(display_flag)
- {
- if(a!=0)
- LCD_ShowNum(6,1,a);
- else LCD1602_ShowStr(6,1," ",1); //显示空白
- if(b!=0)
- LCD_ShowNum(7,1,b);
- else LCD1602_ShowStr(7,1," ",1);//显示空白
- LCD_ShowNum(8,1,c);
- LCD_ShowNum(9,1,d);
- LCD1602_ShowStr(10,1,"dbm ",6);
- }
-
- if(t==KEY1_PRESS)
- printf("%d\r\n",value); //输出采样值
-
-
- // HAL_Delay(10);//延时10ms 输入的正弦波信号的频率为100Hz,波形的周期为10ms, 根据自己的需求,可适当增加延时
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* ADC1 init function */
- static void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Common config
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc1.Init.ContinuousConvMode = DISABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 1;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_2;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USART1 init function */
- static void MX_USART1_UART_Init(void)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 9600;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pins : PC0 PC1 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pins : DB0_Pin DB1_Pin DB2_Pin DB3_Pin
- DB4_Pin DB5_Pin DB6_Pin DB7_Pin */
- GPIO_InitStruct.Pin = DB0_Pin|DB1_Pin|DB2_Pin|DB3_Pin
- |DB4_Pin|DB5_Pin|DB6_Pin|DB7_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pins : LCD1602_E_Pin LCD1602_RW_Pin LCD1602_RS_Pin */
- GPIO_InitStruct.Pin = LCD1602_E_Pin|LCD1602_RW_Pin|LCD1602_RS_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, DB0_Pin|DB1_Pin|DB2_Pin|LCD1602_E_Pin
- |LCD1602_RW_Pin|LCD1602_RS_Pin|DB3_Pin|DB4_Pin
- |DB5_Pin|DB6_Pin|DB7_Pin, GPIO_PIN_RESET);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
AD_ex.7z
(1.17 MB, 下载次数: 344)
|