/*
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"
#include "tim.h"
#include "Set_Value.h"
/* USER CODE BEGIN Includes */
#include "bsp-lcd1602.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart1;
static void MX_USART1_UART_Init(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t rxData[100];//定义接收数组
uint8_t data[5]={10,10,10,10,10};
/* USER CODE END PV */
u8 status = 0;
/* 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 */
u8 min = 1,sec = 0,num = 0;
u8 start = 0,Tnum=0;
u8 JS_N = 0;
u8 Switch = 0;
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
// u8 key=0,state=0;
u8 key=0,mode = 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();
HAL_UART_Receive_IT(&huart1,rxData,1);// 设置中断接收
/* USER CODE BEGIN 2 */
LCD1602_Init();
HAL_Delay(10); //延时一段时间
MX_TIM3_Init();
/* Initialize interrupts */
MX_NVIC_Init();
HAL_TIM_Base_Start_IT(&htim3);
// LCDDispString(0,1," WIFI Warehouse ");
// LCDDispString(0,2,"Detection System");
HAL_Delay(1000);
LCD1602_WriteCmd( 0x01);//清屏
/* USER CODE END 2 */
printf("流水线产品计数\r\n");
// printf("回复1代表启动计时器\r\n");
// printf("回复2代表暂停计时器\r\n");
// printf("回复3代表终止计时器\r\n");
// printf("Light=%d\r\n",Light);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(mode==0)
key=KEY_Scan(0);
else
key=KEY_Scan(1);
if(mode==0)
{
LCDDispString(0,1," CHAN PIN JI SHU ");
if(Switch==0) //暂停 LED灯常亮
{
HAL_GPIO_WritePin(GPIOA, Relay_Pin, GPIO_PIN_SET);
LCDDispString(13,2,"OFF");
}
else if(Switch==1) //终止计时 LED灯常灭
{
HAL_GPIO_WritePin(GPIOA, Relay_Pin, GPIO_PIN_RESET);
LCDDispString(13,2," ON");
}
LCDDispNum(0,2,JS_N/100);
LCDDispNum(1,2,JS_N/10%10);
LCDDispNum(2,2,JS_N%10);
}
if(key==KEY1_PRESS)
{
key = KEY_NO;
if(Switch)
Switch = 0;
else
Switch = 1;
//LCDClear();
}
if(key==KEY2_PRESS)
{
key = KEY_NO;
if(Switch==1)
{
JS_N++;
}
}
if(key==KEY3_PRESS)
{
key = KEY_NO;
JS_N = 0;
}
if(key==KEY4_PRESS)
{
key = KEY_NO;
if(Switch==1)
{
JS_N++;
}
}
if(start)
{
printf("NUM:%d \r\n",JS_N);
start = 0;
}
//printf("humidity:%d \r\n",humidity);
// printf("Light=%d\r\n",Light);
}
/* USER CODE END 3 */
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart1,rxData,1,100);// 发送接收到的字符
HAL_UART_Receive_IT(&huart1,rxData,1); //再次设置
if(rxData[0] == '0') { printf("NUM:%d \r\n",JS_N); }
if(rxData[0] == '1') {status = 1;HAL_TIM_Base_Start_IT(&htim3);}
if(rxData[0] == '2') {status = 0;HAL_TIM_Base_Stop_IT(&htim3);}
if(rxData[0] == '3') {status = 2;HAL_TIM_Base_Stop_IT(&htim3);min = 0;sec = 0;num = 0;}
if(rxData[0] == '4') data[0]=4;
if(rxData[0] == '5') data[0]=5;
if(rxData[0] == '6') data[0]=6;
if(rxData[0] == '7') data[0]=7;
if(rxData[0] == '8') data[0]=8;
if(rxData[0] == '9') data[0]=9;
}
static void MX_NVIC_Init(void)
{
/* TIM6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(TIM3_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
Tnum++;
if(Tnum>=20)
{
Tnum = 0;
if(start==0)
{
start = 1;
}
}
if(num==0)
{
if(sec==0)
{
if(min==0)
{
}
else
{
num = 9;
sec = 59;
min--;
}
}
else
{
num = 9;
sec--;
}
}
else
{
num--;
}
}
/** 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_1;
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_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, IO_DHT11_Pin|Relay_Pin, GPIO_PIN_SET);
/*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|RUN_Pin
|ALARM_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, RUN_Pin|ALARM_Pin, GPIO_PIN_SET);
/*Configure GPIO pins : IO_DHT11_Pin Relay_Pin */
GPIO_InitStruct.Pin = IO_DHT11_Pin|Relay_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &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 RUN_Pin
ALARM_Pin */
GPIO_InitStruct.Pin = LCD1602_E_Pin|LCD1602_RW_Pin|LCD1602_RS_Pin|RUN_Pin
|ALARM_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : KEY1_Pin KEY2_Pin KEY3_Pin */
GPIO_InitStruct.Pin = KEY1_Pin|KEY2_Pin|KEY3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* 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
/**
* @}
*/
/**
* @}
*/
原理图: 无
仿真:
仿真.rar
(217.93 KB, 下载次数: 12)
代码: 无 |