串口配置如下:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Í·Îļþ¶¨ÒåÇøÓò
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "hk32f030m.h"
#include "my_struct.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
½Ó¿Ú¶¨ÒåÇø
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define USART1_TX_PORT GPIOA
#define USART1_TX_PIN GPIO_Pin_3
#define USART1_TX_IO_CLK_EN() RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE)
#define USART1_RX_PORT GPIOD
#define USART1_RX_PIN GPIO_Pin_6
#define USART1_RX_IO_CLK_EN() RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
±äÁ¿¶¨ÒåÇø
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned char Rcv_data[31];//½ÓÊÕÊý×é
unsigned char Rcv_flag=0;//½ÓÊÕ±êÖ¾
unsigned char Cnt_cart=0;//½ÓÊÕÊý¾Ý³¤¶È
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
º¯Êý¶¨ÒåÇø
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/**************************************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] usart_async_tx_no_int_rx_rxneint.c
* [url=home.php?mod=space&uid=247401]@brief[/url] Òì²½´®¿ÚͨÐÅÀý³Ì, ͨ¹ý²éѯTXE±êÖ¾·¢ËÍÊý¾Ý,ͨ¹ýRXNEÖжϽÓÊÕÊý¾Ý,µ±ÖжϽÓÊÕµ½Êý¾Ýºó»á½«
* ÊÕµ½µÄÊý¾ÝÔÑùͨ¹ýTXÒý½Å·¢ËͳöÈ¥
* [url=home.php?mod=space&uid=187600]@author[/url] veiko.c
* [url=home.php?mod=space&uid=212281]@date[/url] 20200202
**************************************************************************************************/
static void USART_NVIC_Configurature(void)
{
NVIC_SetPriority(USART1_IRQn,0);
NVIC_EnableIRQ(USART1_IRQn);
}
static void USART_GPIO_Configurature(void)
{
GPIO_InitTypeDef m_gpio;
USART1_TX_IO_CLK_EN();
USART1_RX_IO_CLK_EN();
m_gpio.GPIO_Mode = GPIO_Mode_AF;
m_gpio.GPIO_OType = GPIO_OType_PP;
m_gpio.GPIO_Pin = USART1_TX_PIN;
m_gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
m_gpio.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(USART1_TX_PORT, &m_gpio);
GPIO_PinAFConfig(USART1_TX_PORT,GPIO_PinSource3,GPIO_AF_1);
m_gpio.GPIO_Pin = USART1_RX_PIN;
GPIO_Init(USART1_RX_PORT, &m_gpio);
GPIO_PinAFConfig(USART1_RX_PORT,GPIO_PinSource6,GPIO_AF_1);
}
void USART_Configurature(void)
{
// USART_InitTypeDef USART_InitStructure;
// USART_GPIO_Configurature();//IO¿ÚÅäÖÃ
// // USART1 ÅäÖÃ
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//¿ªÆôURATʱÍâÉèʱÖÓ
// USART_InitStructure.USART_BaudRate = 9696;
// USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8λÊý¾Ýλ
// USART_InitStructure.USART_StopBits = USART_StopBits_1;//1λֹͣλ
// USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑé
// USART_InitStructure.USART_Mode = USART_Mode_Rx;//RXģʽ
// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//½ûÓÃÁ÷Á¿Ó²¼þ¿ØÖÆ
// USART_Init(USART1, &USART_InitStructure);
// USART_SWAPPinCmd(USART1, ENABLE);// ²»»¥»»TXºÍRXÒý½Å
//// USART_Cmd(USART1, DISABLE);// ʹÄÜ USART
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//´®¿Ú½ÓÊÕÍê³ÉÖжÏ
// // Enable the USARTy Interrupt
// USART_NVIC_Configurature();//ÆôÓÃUSARTyÖжÏ
USART_InitTypeDef m_usart;
USART_GPIO_Configurature();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
m_usart.USART_BaudRate = 9600;
m_usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
m_usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
m_usart.USART_Parity = USART_Parity_No;
m_usart.USART_StopBits = USART_StopBits_1;
m_usart.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &m_usart);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_NVIC_Configurature();
}
//´®¿ÚÔËÐк¯Êý
//¹¦ÄÜ ½âÎöÄæ±äÆ÷´®¿ÚÐÒé
//²ÎÊý void
//·µ»Ø void
//µ÷¶ÈÖÜÆÚ 250ms
void uart_Run(void)//Êý¾Ý´¦Àíº¯Êý
{
static unsigned int /*last_Voltage_state=0,*/last_Electricity_state=0;/*last_Byte_state=0,last_model_state=0*/;
unsigned int temp1=0,temp2=0;
unsigned long temp3=0;
if(Rcv_flag==1)
{
ERR_TIME++;
Rcv_flag = 0;
Cnt_cart = 0;
if((Rcv_data[0]==0xF0) //Êý¾ÝÍ·
&&(Rcv_data[30]==0xFA) //Êý¾Ýβ
&&(Rcv_data[1]==28)) //ÓÐЧ³¤¶È
{
//¹ÊÕÏÊý¾Ý»ñÈ¡
//err¹ÊÕÏ 0 2 4 6 8 10
err = Rcv_data[6];//´íÎ󱨾¯
//·¢µç»úÐͺŠmodel
// ·¢µç»úµçѹ Voltage
// ·¢µç»úµçÁ÷ Electricity
temp1 = Rcv_data[7];//״̬Âë
model = temp1&0X38;//µçѹ״̬
/**************************³ÌÐòÂë»ñÈ¡****************************/
temp2 = Rcv_data[8]+((unsigned int)Rcv_data[9]<<8); //³ÌÐòÂëµÍËÄλÓë×óÒÆ°ËλÊý¾Ý½øÐкϲ¢
CXM = temp2 ; //³ÌÐòÂë
/**************************µçÁ÷»ñÈ¡********************************/
temp3 = Rcv_data[22]+((unsigned int)Rcv_data[23]<<8);//µçѹµÍËÄλÓë×óÒÆ°ËλÊý¾Ý½øÐкϲ¢
Electricity = Rcv_data[24]+((unsigned int)Rcv_data[25]<<8);//µçÁ÷µÍËÄλÓë×óÒÆ°ËλÊý¾Ý½øÐкϲ¢
// ·¢µç»úƵÂÊ»ñÈ¡ Frequency
if((temp1&0X04)==0x04)
{
Frequency =600;//60HZ
}else
{
Frequency = 500;//50HZ
}
/*********************230vÄæ±äÆ÷ϵÁÐ*****************************************************/
if(model==24)//230v 011
{
//µçѹ¼ÆËã
Voltage = temp3*10/333;//µçѹ»ñÈ¡´¦Àí
//½ÓÊÕÂ˲¨´¦Àí²¿·Ö
/* if((Electricity>1020||Rcv_data[1]!=28)&&last_Voltage_state !=0)//ÅжÏÒ»Ö¡Êý¾ÝÓÐЧÐÔ
{
Voltage = last_Voltage_state;
Electricity = last_Electricity_state;
Rcv_data[1] = last_Byte_state;
model = last_model_state;
}//¸üÐÂÉϴμǼ
last_Voltage_state = Voltage;
last_Electricity_state = Electricity;
last_Byte_state = Rcv_data[1];
last_model_state = model;*/
}
/*********************120vÄæ±äÆ÷ϵÁÐ*****************************************************/
if(model==8&&temp2!=33657)//120v
{
//µçѹ¼ÆËã
Voltage = temp3*10/600;//µçѹ»ñÈ¡´¦Àí
//½ÓÊÕÂ˲¨³·À벿·Ö
/* if((Electricity>1020||Rcv_data[1]!=28)&&last_Voltage_state !=0)//ÅжÏÒ»Ö¡Êý¾ÝÓÐЧÐÔ
{
Voltage = last_Voltage_state;
Electricity = last_Electricity_state;
Rcv_data[1] = last_Byte_state;
model = last_model_state;
}//¸üÐÂÉϴμǼ
last_Voltage_state = Voltage;
last_Electricity_state = Electricity;
last_Byte_state = Rcv_data[1];
last_model_state = model;*/
}
/*********************100vÄæ±äÆ÷*****************************************************/
if(model==0&&temp2==5823)//120v
{
//µçѹ¼ÆËã
Voltage = temp3*10/615;//µçѹ»ñÈ¡´¦Àí
//½ÓÊÕÂ˲¨´¦Àí²¿·Ö
/* if((Electricity>950||Rcv_data[1]!=28)&&last_Voltage_state !=0)//ÅжÏÒ»Ö¡Êý¾ÝÓÐЧÐÔ
Voltage = last_Voltage_state;
Electricity = last_Electricity_state;
Rcv_data[1] = last_Byte_state;
model = last_model_state;
}//¸üÐÂÉϴμǼ
last_Voltage_state = Voltage;
last_Electricity_state = Electricity;
last_Byte_state = Rcv_data[1];
last_model_state = model;*/
}
/*********************3.5KW/120vÄæ±äÆ÷*****************************************************/
if(model==8 && temp2 == 33657)//120v
{
//µçѹ¼ÆËã
Voltage = temp3*10/630;//µçѹ»ñÈ¡´¦Àí
//½ÓÊÕÂ˲¨´¦Àí²¿·Ö
/* if((Electricity>1020||Rcv_data[1]!=28)&&last_Voltage_state !=0)//ÅжÏÒ»Ö¡Êý¾ÝÓÐЧÐÔ
{
Voltage = last_Voltage_state;
Electricity = last_Electricity_state;
Rcv_data[1] = last_Byte_state;
model = last_model_state;
}//¸üÐÂÉϴμǼ
last_Voltage_state = Voltage;
last_Electricity_state = Electricity;
last_Byte_state = Rcv_data[1];
last_model_state = model;*/
}
/*ͨÐÅʱ´æÔÚÊý¾Ý²¨¶¯£¬Í¨¹ýÏÞÖÆijһ¶Îʱ¼äÄÚÊý¾Ý·¢ÉúÍ»±ä¶Ô»ñÈ¡µÄÊý¾Ý½øÐжªÆú*/
if(last_Electricity_state != Electricity)//ÉÏÒ»´ÎµÄµçÁ÷ÖµÓë±¾´ÎµçÁ÷Öµ²»ÏàµÈ
{
ERR_TIME = 0; //¼ÆʱÇåÁã
last_Electricity_state = Electricity;//²»¸üÐÂÉϴμǼ
}
if(ERR_TIME >= 5)//Á¬Ðø2.5S
{
Elect = Electricity;//¸üÐÂÉϴμǼ
}
if(err != 0)
{
Voltage = 0;
Elect = 0;
Frequency = 0;
}
}
}
}
//??????????
//?? ?????????
//?? void
//?? void
//???? 250ms
void rcv_Cache(void)//??????
{
static unsigned char temp0=0,temp1=0;
temp1++;//??
if(Rcv_flag==1)
{
temp0++;////??????????
}
if(temp1==10)//???????????
{
if(err!=0)
{
Voltage = 0;//????
Electricity = 0;//????
Frequency = 0;
model = 0;
}
temp0 = 0; //??????
temp1 = 0; //??????
}
}
void USART1_IRQHandler(void)
{
if((USART1->ISR & USART_ISR_RXNE) != 0)
{
Rcv_flag=1;//½ÓÊÕ±êÖ¾1
Rcv_data[Cnt_cart]=USART1->RDR;//½ÓÊÕµÄÊý¾Ý´æÈëÊý×é
Cnt_cart++;//¼ÆÊýÖµÀÛ¼Ó
if(Cnt_cart>31)//¼ÆÊý
{
Cnt_cart=0;
}
}
}
|