/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"
#include "crc.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
void PLC_Data_Wr2HMI(uint16_t HMI_start_addr,uint16_t PLC_return_data_start_addr,uint8_t len);
void WRITE_HMI(uint16_t addr,uint8_t data1,uint8_t data2,uint8_t data3,uint8_t data4,uint8_t len);
void READ_HMI(uint16_t addr,uint8_t num);
void CLOSE_OPEN_IO(uint8_t hmi_key_value);
void SCAN_KEY(uint8_t io_key_value);
void delay_ms(uint16_t nms);
extern uint16_t USART_Calc_CRC16(uint8_t *Ptr, uint8_t u8BuffLen);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
uint8_t HMI_SEND_BUF[20] = {0};
uint8_t usart1_counter = 0;
uint8_t usart1_data;
uint8_t usart1_rxBuf[50];
uint8_t usart1_txBuf[50];
uint8_t HMI_HANDLEFLAG;
uint8_t LED_FLAG[4] = {0};
uint8_t HMI_FLAG[5] = {0};
uint8_t TEMP = 25;//éÏμçζè
uint8_t VOLUME = 50;//éÏμçòôá¿
uint8_t UP_POWER_FLAG=0;
int main(void)
{
uint8_t i;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while (1)
{
HAL_UART_Receive_IT(&huart1,&usart1_data,1);
if(UP_POWER_FLAG == 0)//éÏμç3õê¼»ˉ′¥ÃtÆá
{
UP_POWER_FLAG = 1;
HMI_SEND_BUF[0] = TEMP;
PLC_Data_Wr2HMI(0X1110,0,1);
delay_ms(100);
for(i=0;i<4;i++)
{
HMI_SEND_BUF[i] = 0;
}
for(i=0;i<20;i++)
{
usart1_rxBuf[i] = 0x00;
}
HMI_HANDLEFLAG = 0;
}
SCAN_KEY(0);//°′¼üé¨Ãè
if(HMI_HANDLEFLAG)//½óêÕ′¥ÃtÆáêy¾Y′|àí
{
HMI_HANDLEFLAG = 0;
CLOSE_OPEN_IO(0x01);
CLOSE_OPEN_IO(0x02);
CLOSE_OPEN_IO(0x03);
CLOSE_OPEN_IO(0x04);
for(i=0;i<50;i++)
{
usart1_rxBuf[i] = 0x00;
}
usart1_counter = 0;
}
}
}
uint16_t USART_Calc_CRC16(uint8_t *Ptr, uint8_t u8BuffLen)
{
uint8_t u8CRCHi = 0xFF ;
uint8_t u8CRCLo = 0xFF ;
uint16_t u16Index ;
while (u8BuffLen--)
{
u16Index = u8CRCLo ^ *Ptr++ ;
u8CRCLo = u8CRCHi ^ u8CRCHTbl[u16Index] ;
u8CRCHi = u8CRCLTbl[u16Index] ;
}
return (uint16_t)(u8CRCHi << 8) + u8CRCLo;
}
void PLC_Data_Wr2HMI(uint16_t HMI_start_addr,uint16_t PLC_return_data_start_addr,uint8_t len)//êy¾YD′èë′¥ÃtÆá±äῼÄ′æÆ÷
{
uint8_t i;
uint16_t CRC_tmp;
uint8_t BUFF_ptr = 0;
usart1_txBuf[0]=0x5a;
usart1_txBuf[1]=0xa5;
// usart1_txBuf[2]=len*2+3;
usart1_txBuf[3]=0x82;
usart1_txBuf[4]=((uint8_t)(HMI_start_addr>>8))&0XFF;
usart1_txBuf[5]=(uint8_t)(HMI_start_addr&0XFF);
BUFF_ptr = 6;
for(i=0;i<len;i++)
{
usart1_txBuf[BUFF_ptr++]=((HMI_SEND_BUF[i+PLC_return_data_start_addr] >>8)&0xff);
usart1_txBuf[BUFF_ptr++]=(HMI_SEND_BUF[i+PLC_return_data_start_addr] &0xff);
}
usart1_txBuf[2]=BUFF_ptr-1;
CRC_tmp = USART_Calc_CRC16(usart1_txBuf+3,BUFF_ptr-3);
usart1_txBuf[BUFF_ptr++]=(uint8_t)(CRC_tmp&0xff);
usart1_txBuf[BUFF_ptr++]=(uint8_t)((CRC_tmp>>8)&0xff);
HAL_UART_Transmit(&huart1, usart1_txBuf, BUFF_ptr, 10);//′®¿ú·¢Ëí
}
void WRITE_HMI(uint16_t addr,uint8_t data1,uint8_t data2,uint8_t data3,uint8_t data4,uint8_t len)//êy¾YD′èë′¥ÃtÆá±äῼÄ′æÆ÷
{
uint16_t CRC_tmp;
usart1_txBuf[0]=0x5a;
usart1_txBuf[1]=0xa5;
usart1_txBuf[2]=len*2+3;
usart1_txBuf[3]=0x82;
usart1_txBuf[4]=(uint8_t)((addr>>8)&0xff);
usart1_txBuf[5]=(uint8_t)(addr&0xff);
usart1_txBuf[6]=data1;
usart1_txBuf[7]=data2;
usart1_txBuf[8]=data3;
usart1_txBuf[9]=data4;
CRC_tmp = USART_Calc_CRC16(usart1_txBuf+3,7);
usart1_txBuf[10]=(uint8_t)(CRC_tmp&0xff);
usart1_txBuf[11]=(uint8_t)((CRC_tmp>>8)&0xff);
HAL_UART_Transmit(&huart1, usart1_txBuf, 12 , 20);
}
void READ_HMI(uint16_t addr,uint8_t num)//¶á′¥ÃtÆá¼Ä′æÆ÷êy¾Y
{
uint16_t CRC_tmp;
usart1_txBuf[0]=0x5a;
usart1_txBuf[1]=0xa5;
usart1_txBuf[2]=0x04;
usart1_txBuf[3]=0x83;
usart1_txBuf[4]=(uint8_t)((addr>>8)&0xff);
usart1_txBuf[5]=(uint8_t)(addr&0xff);
usart1_txBuf[6]=num;
CRC_tmp = USART_Calc_CRC16(usart1_txBuf+3,4);
usart1_txBuf[7]=(uint8_t)(CRC_tmp&0xff);
usart1_txBuf[8]=(uint8_t)((CRC_tmp>>8)&0xff);
HAL_UART_Transmit(&huart1, usart1_txBuf, 9 , 20);
}
void CLOSE_OPEN_IO(uint8_t hmi_key_value) //¿a1ØLED
{
uint8_t i;
if(usart1_rxBuf[7] == 0x00&&usart1_rxBuf[8] == hmi_key_value)//LED1
{
if(LED_FLAG[hmi_key_value-1] == 0)
{
if(hmi_key_value == 0x01)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);//¿aLED1
}
if(hmi_key_value == 0x02)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); //¿aLED2
}
if(hmi_key_value == 0x03)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); //¿aLED3
}
if(hmi_key_value == 0x04)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET); //¿aLED4
}
}
else if(LED_FLAG[hmi_key_value-1] == 1)
{
if(hmi_key_value == 0x01)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);//1ØLED1
}
if(hmi_key_value == 0x02)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);//1ØLED2
}
if(hmi_key_value == 0x03)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);//1ØLED3
}
if(hmi_key_value == 0x04)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);//1ØLED4
}
LED_FLAG[hmi_key_value-1] = 0;
}
for(i=0;i<9;i++)
{
usart1_rxBuf[i] = 0xff;
}
for(i=0;i<4;i++)
{
HMI_SEND_BUF[i] = LED_FLAG[i];
}
PLC_Data_Wr2HMI(0X1000,0,5);//·¢ËíLED×′ì¬μ½′¥ÃtÆᣬ¸Ä±äLEDÏÔê¾í¼±ê£»
delay_ms(50);
}
}
void SCAN_KEY(uint8_t io_key_value)
{
uint8_t i;
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_11)==GPIO_PIN_RESET)//KEY1,ÆáÄ»¿a1Ø
{
delay_ms(150);//襶¶¶ˉ
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_11)==GPIO_PIN_RESET)//KEY1,ÆáÄ»¿a1Ø
{
if(HMI_FLAG[0] == 0)
{
WRITE_HMI(0X82,0x00,0x00,0,0,1);//1ØÆáÄ»
HMI_FLAG[0] = 1;
}else if(HMI_FLAG[0] == 1)
{
WRITE_HMI(0X82,0X64,0x00,0,0,1);//¿aÆáÄ»
HMI_FLAG[0] = 0;
}
}
for(i=0;i<50;i++)
{
usart1_rxBuf[i] = 0x00;
}
usart1_counter = 0;
}
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)==GPIO_PIN_RESET)//KEY2£¬Î¶è+
{
delay_ms(150);//襶¶¶ˉ
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)==GPIO_PIN_RESET)
{
TEMP += 1;
HMI_SEND_BUF[0] = TEMP;
PLC_Data_Wr2HMI(0X1110,0,1);
}
for(i=0;i<50;i++)
{
usart1_rxBuf[i] = 0x00;
}
usart1_counter = 0;
}
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET)//KEY3£¬Î¶è-
{
delay_ms(150);//襶¶¶ˉ
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET)
{
TEMP -= 1;
HMI_SEND_BUF[0] = TEMP;
PLC_Data_Wr2HMI(0X1110,0,1);
}
for(i=0;i<50;i++)
{
usart1_rxBuf[i] = 0x00;
}
usart1_counter = 0;
}
// for(i=0;i<50;i++)
// {
// usart1_rxBuf[i] = 0x00;
// }
// usart1_counter = 0;
}
|