以前的装修的时候智能家居还没有兴起或者未成熟、价格虚高,所以家里面的供电、照明系统还有其它家用电器都不支持远程开关,出门后老是觉得灯没关,或者空调、风扇没关,网上也有报道说有人出去打工一年回来空调未关,电费大几十万。此次想法是给家里的电箱内部加一个远程开关,目前只支持单方通信,后续加入开关状态上报,切换设备故障上报,市电电压、电流、频率等上报,实时了解家里用电器负载状态。
电路设计如下:
图1:市电取电
图2电流检测
图3输出检测&继电器驱动
如上原理图系统取电来自市电降压产生,继电器控制原理图,一共接了两个继电器,一个继电器(RY2)接在常闭端,一个接在常开端(RY1), 避免由于某些原因出现继电器(RY1)粘死,输出不能有效断开情况,则动作常闭端继电器(RY2),切断输出。输出电压检测集成频率和过零检测,实现继电器零点动作,避免拉弧及大电流情况发生,电流检测方便判断家里用电器负荷状况。市电取电及继电器、采样板为制作完成,采用板上集成的3个LED作为指示调试用,PA5 绿灯表示RY2,亮表示处在常闭端,PB7蓝灯表示RY1状态,吸合时亮起,红灯告警,报继电器拉弧粘死不能断开或者损坏不能正常吸合。
外接ESP12-F WIFI模块作为远程通信,WIFI模块与demo板串口2连接,取5V电作为工作电源。
阿里云提供免费物联网公共测试平台,数据量小,完全可以使用此免费的作为自身控制云平台,DEMO版使用AT指令控制WIFI模块,需要烧录MQTT协议固件连接阿里云(见附件),手机APP也连接阿里云,阿里云平台上面设置产品流转,实现二者通信,其中如何连接阿里云,网络有教程参考,其中需要注意的几个点需要说明下。1.配置MQTT客户端ID AT+MQTTCLIENTID=0,"i7lwMWR4n.ESP8266|securemode=2\,signmethod=hmacsha256\,timestamp=16768075338|" 如上,前面需要手动加上\,另外timestamp的值刷新后会变,另外见下图passwd也会变,需要注意,所以连不上WIFI时需要修改参数重新连接.
APP借用网上有的先做测试,如下图:
后续借用APP Inventor进行开啊,减少语言障碍。
WIFI模块返回云平台通信数据如下:
测试开启正常输出时:
检测到继电器处于输出状态,而未正常输出以及继电器切断输出,而继续输出
红灯告警,切开常闭继电器Rly2和常开继电器Rly1
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim17;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
const uint8_t SendTbl[50] = {0x55,};
uint32_t AdvRegTmCnt;
uint8_t fRly1OnOff;
uint8_t RlyChkCnt = 0x00;
uint16_t LineAd;
uint16_t LineAdOld;
uint16_t FreqCnt;
uint16_t wTemp;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_ICACHE_Init(void);
static void MX_TIM17_Init(void);
static void MX_TIM1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ICACHE_Init();
MX_TIM17_Init();
MX_TIM1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
#if 1
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
HAL_Delay(2000);
printf("AT+RST\r\n");
HAL_Delay(000);
printf("AT+RESTORE\r\n");
HAL_Delay(1000);
printf("AT+CWMODE=1\r\n");
HAL_Delay(1000);
printf("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n");
HAL_Delay(2000);
printf("AT+CWJAP=\"Xiaomi 12 Pro\",\"12345678\"\r\n");
HAL_Delay(5000);
printf("AT+MQTTUSERCFG=0,1,\"NULL\",\"ESP8266&i7lwyBCAcAH\",\"86600e03e84f061e3714219ee04e72505ef186e5041aa05c00e125d4f6560905\",0,0,\"\"\r\n");
HAL_Delay(3000);
printf("AT+MQTTCLIENTID=0,\"i7lwyBCAcAH.ESP8266|securemode=2\\,signmethod=hmacsha256\\,timestamp=1679045538771|\"\r\n");
HAL_Delay(3000);
printf("AT+MQTTCONN=0,\"iot-06z00dmxlk2ol65.mqtt.iothub.aliyuncs.com\",1883,1\r\n");
HAL_Delay(3000);
printf("AT+MQTTSUB=0,\"/i7lwyBCAcAH/ESP8266/user/get\",1\r\n");
HAL_Delay(5000);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
#endif
MX_USART2_UART_Init();
huart2.Instance->CR1 |= (1<<5);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
#if 0
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == 0)
{
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET); //R
}
else
{
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET);
}
#endif
#if 1
if(fRly1OnOff)
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == 0)
{
RlyChkCnt++;
if(RlyChkCnt >= 5)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET); //B Rly1
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET); //G Rly2
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET); //R Alarm
RlyChkCnt = 0x00;
}
}
else
{ if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7) == 1) && (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_7) == 0)) //ÓÐÊä³öÇé¿öÏÂ,Rly1£ºon ÇÒ Rly2:0ffµÄÇé¿ö
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET);
RlyChkCnt = 0x00;
}
}
else
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == 1)
{
RlyChkCnt++;
if(RlyChkCnt >= 5)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET); //Rly1
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET); //Rly2
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_SET);
RlyChkCnt = 0x00;
}
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET); //Î޸澯²Åµ¯¿ªRly2
//if((HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_7) == 0) || (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_7) == 1)) //ûÓÐÊä³öÇé¿öÏÂ,Rly1£ºoff »òÕß Rly2:0nµÄÇé¿ö
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_2,GPIO_PIN_RESET);
RlyChkCnt = 0x00;
}
}
#endif
#if 0 //50usÖжÏÖ´ÐÐ
FreqCnt++; //50us++
LineAd = HAL_ADC_GetValue(&hadc4) / 4;
if(LineAd >= 511)
{
wTemp = LineAd - 511;
}
else
{
wTemp = 511 - LineAd;
}
if((wTemp <= 20) && (LineAd< LineAdOld)) // Maybe N times zerocorr detect
{
if(FreqCnt > 250) //12.5ms 80Hz²»µ½ÅжÏΪ¸ÉÈÅ
{
if(b1sFlag & 0x02) //ÉϵçÑÓʱ1s ´ýÎÈ
fbLineZerocross ^= 1;
}
FreqCnt = 0x00;
}
LineAdOld = LineAd;
#endif
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 80;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief Power Configuration
* @retval None
*/
static void SystemPower_Config(void)
{
HAL_PWREx_EnableVddIO2();
/*
* Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
*/
HAL_PWREx_DisableUCPDDeadBattery();
/*
* Switch to SMPS regulator instead of LDO
*/
if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}
/**
* @brief ICACHE Initialization Function
* @param None
* @retval None
*/
static void MX_ICACHE_Init(void)
{
/* USER CODE BEGIN ICACHE_Init 0 */
/* USER CODE END ICACHE_Init 0 */
/* USER CODE BEGIN ICACHE_Init 1 */
/* USER CODE END ICACHE_Init 1 */
/** Enable instruction cache in 1-way (direct mapped cache)
*/
if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
{
Error_Handler();
}
if (HAL_ICACHE_Enable() != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ICACHE_Init 2 */
/* USER CODE END ICACHE_Init 2 */
}
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 8000;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
/**
* @brief TIM17 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM17_Init(void)
{
/* USER CODE BEGIN TIM17_Init 0 */
/* USER CODE END TIM17_Init 0 */
/* USER CODE BEGIN TIM17_Init 1 */
/* USER CODE END TIM17_Init 1 */
htim17.Instance = TIM17;
htim17.Init.Prescaler = 160;
htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
htim17.Init.Period = 65535;
htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim17.Init.RepetitionCounter = 0;
htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim17) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM17_Init 2 */
/* USER CODE END TIM17_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
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;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PC3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PG2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pin : PC7 */
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PB7 */
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/*
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
PUTCHAR_PROTOTYPE
{
USART_SendData_8bit(CW_UART1, (uint8_t)ch);
while (USART_GetFlagStatus(CW_UART1, USART_FLAG_TXE) == RESET);
return ch;
}
*/
/*
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1,(uint8_t *)f,1,HAL_MAX_DELAY);
return ch;
}
*/
uint8_t ch;
/*
int fputc(int c, FILE* f)
{
//ch = c;
HAL_UART_Transmit(&huart1,(uint8_t*)&c,1,HAL_MAX_DELAY);
return c;
}
*/
int fputc(int c, FILE* f)
{
//ch = c;
HAL_UART_Transmit(&huart2,(uint8_t*)&c,1,HAL_MAX_DELAY);
return c;
}
/*
int fputc(int c, FILE* f)
{
ch = c;
HAL_UART_Transmit(&huart1,&ch,1,HAL_MAX_DELAY);
return c;
}
*/
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#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 /* USE_FULL_ASSERT */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32u5xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
//extern ADC_HandleTypeDef hadc4;
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define HalSetInvPwmPosDuty(Temp) TIM1->CCR1 = Temp
#define HalSetInvPwmNegDuty(Temp) TIM1->CCR2 = Temp
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t Temp;
uint8_t RxBuff[8] = {0,};
uint8_t i=0;
uint8_t DataParsingCnt = 0x00;
uint8_t DataRecCnt = 0x00;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
extern UART_HandleTypeDef huart1;
extern uint8_t fRly1OnOff;
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim1;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Prefetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32U5xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32u5xx.s). */
/******************************************************************************/
/**
* @brief This function handles TIM1 Update interrupt.
*/
void TIM1_UP_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_UP_IRQn 0 */
/* USER CODE END TIM1_UP_IRQn 0 */
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_UP_IRQn 1 */
if((TIM1->SR & TIM_FLAG_UPDATE) == TIM_FLAG_UPDATE)
{
//__HAL_TIM_CLEAR_FLAG(&htim1,TIM_FLAG_UPDATE);
TIM1->SR = ~TIM_FLAG_UPDATE;
/*
if((hadc4.Instance->ISR & (1<<2)) != 0) //EOS:3 EOC:2
{
LineAd = HAL_ADC_GetValue(&hadc4);
HAL_ADC_Start(&hadc4);
}
*/
}
/* USER CODE END TIM1_UP_IRQn 1 */
}
/**
* @brief This function handles USART2 global interrupt.
*/
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
//HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
if((huart2.Instance->ISR & UART_FLAG_RXNE) != RESET)
{
Temp = huart2.Instance->RDR; //Read RxReg,Clear RXNE
//RxBuff[i++] = Temp;
if(Temp == 'L')
{
DataParsingCnt = 1;
DataRecCnt = 0;
}
else if(Temp == 'E')
{
if((DataParsingCnt == 1) && (DataRecCnt == 1))
DataParsingCnt = 2;
}
else if(Temp == 'D')
{
if((DataParsingCnt == 2) && (DataRecCnt == 2))
DataParsingCnt = 3;
}
#if 1
if(DataParsingCnt >= 3) //½ÓÊÕon/off
{
if(Temp == 'o')
{
if(DataRecCnt < 8)
{
DataParsingCnt = 4;
}
else
{
DataParsingCnt = 0;
}
DataRecCnt = 0;
}
else if(Temp == 'n')
{
if((DataParsingCnt == 4) && (DataRecCnt == 1))
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET); //Rly1
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET); //Rly2
fRly1OnOff = 1;
}
DataParsingCnt = 0;
DataRecCnt = 0;
}
else if(Temp == 'f')
{
if((DataParsingCnt == 4) && (DataRecCnt == 1))
{
DataParsingCnt = 5;
}
else if((DataParsingCnt == 5) && (DataRecCnt == 2))
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_RESET); //Rly1
//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET); //Rly2
fRly1OnOff = 0;
}
else
{
DataParsingCnt = 0;
DataRecCnt = 0;
}
}
else if((DataRecCnt > 8) || (Temp == '}'))
{
DataParsingCnt = 0;
DataRecCnt = 0;
}
}
#endif
DataRecCnt++;
if(DataRecCnt >= 200)
DataRecCnt = 0x00;
}
/* USER CODE END USART2_IRQn 1 */
}
---------------------
作者:416775364TP
链接:https://bbs.21ic.com/icview-3290822-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。