📄 comm.c
字号:
#include "SysDef.h"
//RxTx Buffer
#define CONFIG_TX_BUFSIZE 64
#define CONFIG_RX_BUFSIZE 64
unsigned char ConfigTXBuffer[CONFIG_TX_BUFSIZE];
static unsigned char ucConfigTXReadIndex;
static unsigned char ucConfigTXCharCount;
unsigned char ConfigRXBuffer[CONFIG_RX_BUFSIZE];
static unsigned char ucConfigRXCharCount;
//static unsigned char ucConfigFrameError;
#define C_WAIT_FOR_START 0
#define C_RCV_DATA 1
#define C_WAIT_FOR_END 2
#define C_RCV_REST_CHAR 3
#define EVENT_CONFIG_MSGRCV 0x04
unsigned char ucConfigRCVState;
unsigned int g_Event;
unsigned char uiRXTXData;
unsigned char ucTComMode;
unsigned char ucBitCnt;
U8 ReciveData;
union
{
float Flt;
uchar Bt[4];
}unionFlt;
//----------------------------------------------------------
// Frame method: mostly like modbus ascii mode.
// start: 0xFF
// Fuction: 1byte(1-4)
// Data: up to 19 byte (relate to the config uart buffer)
// parity: OXR(1byte)
// end: 0xFE,0xFD
//----------------------------------------------------------
//----------------------------------------------------------
// Commad:
// 0:
// 1: 读取温度湿度露点和温湿度回路电流
// 2: 读取温湿度量程 或 设置温湿度量程
// 3: 读取温湿度零点 或 设置温湿度零点
// 4: write modbus relation information
// 5:
// 6:
//----------------------------------------------------------
void ProcessConfigCmd00();
void ProcessConfigCmd01();
void ProcessConfigCmd02();
void ProcessConfigCmd03();
void ProcessConfigCmd04();
void ProcessConfigCmd05();
void (* const ProcessConfigCmd[])() = {
ProcessConfigCmd00,
ProcessConfigCmd01,
ProcessConfigCmd02,
ProcessConfigCmd03,
ProcessConfigCmd04,
ProcessConfigCmd05
};
void CnvtFlt()
{
ConfigTXBuffer[ucConfigTXCharCount++] = unionFlt.Bt[3];
ConfigTXBuffer[ucConfigTXCharCount++] = unionFlt.Bt[2];
ConfigTXBuffer[ucConfigTXCharCount++] = unionFlt.Bt[1];
ConfigTXBuffer[ucConfigTXCharCount++] = unionFlt.Bt[0];
}
void GetFlt(unsigned int Count)
{
unionFlt.Bt[3] = ConfigRXBuffer[Count++];
unionFlt.Bt[2] = ConfigRXBuffer[Count++];
unionFlt.Bt[1] = ConfigRXBuffer[Count++];
unionFlt.Bt[0] = ConfigRXBuffer[Count++];
}
void InitComm0(void)
{
P3SEL = 0x30; // P3.3,4 = USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; // Enabled USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character, SWRST=1
//串口通讯选择32K时钟源
U0TCTL |= SSEL1; //SSEL0 // UCLK = SMCLK = 460.8kHz
//9600 0x30 19200 0x18 115200 0x04 576000 08
UBR10 = 0x00; // 9600
UBR00 = 0x60; // 9600
UMCTL0 = 0x00; // 9600
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0 ; //+ UTXIE0 // Enable USART0 RX/TX interrupt
//IE1 |= URXIE0; // Enable USART0 RX interrupt
IFG1 &= ~UTXIFG0; // Clear inital flag on POR
ucConfigRCVState = C_WAIT_FOR_START;
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
//ReciveData = RXBUF0;
unsigned int i;
unsigned char ucParity = 0x00;
uiRXTXData = RXBUF0;
switch(ucConfigRCVState)
{
case C_WAIT_FOR_START:
if(uiRXTXData == 0xFF)
{
ucConfigRCVState = C_RCV_DATA;
ucConfigRXCharCount = 0;
}
break;
case C_RCV_DATA:
if(uiRXTXData == 0xFF)
{
ucConfigRXCharCount = 0;
break;
}
if(ucConfigRXCharCount < CONFIG_RX_BUFSIZE)
{
if(uiRXTXData == 0xFE)
{
ucConfigRCVState = C_WAIT_FOR_END;
}
else
{
ConfigRXBuffer[ucConfigRXCharCount++] = uiRXTXData;
}
}
else
{
ucConfigRCVState = C_WAIT_FOR_START;
}
break;
case C_WAIT_FOR_END:
ucConfigRCVState = C_WAIT_FOR_START;
if(uiRXTXData == 0xFD)
{
if(ConfigRXBuffer[0] != 0x03)
{
ConfigRXBuffer[0] = 0X03;
break;
}
for(i=0; i<ucConfigRXCharCount-2; i++)
{
ucParity ^= ConfigRXBuffer[i];
}
if(ucParity == ((ConfigRXBuffer[ucConfigRXCharCount-2]<<4) + (ConfigRXBuffer[ucConfigRXCharCount-1]&0x0F)))
{
g_Event |= EVENT_CONFIG_MSGRCV;
//收到完整数据,关闭接收中断
IE1 &=~ URXIE0;
}
}
else
{
if(uiRXTXData == 0xFF)
{
ucConfigRCVState = C_RCV_DATA;
ucConfigRXCharCount = 0;
}
}
break;
default:
break;
}
}
void MakeConfigParity()
{
unsigned int i;
unsigned char ucParity = 0x00;
for(i=1; i<ucConfigTXCharCount; i++)
{
ucParity ^= ConfigTXBuffer[i];
}
ConfigTXBuffer[ucConfigTXCharCount++] = ucParity >>4;
ConfigTXBuffer[ucConfigTXCharCount++] = ucParity & 0x0F;
ConfigTXBuffer[ucConfigTXCharCount++] = 0xFE;
ConfigTXBuffer[ucConfigTXCharCount++] = 0xFD;
}
void StartConfigTX()
{
ucConfigTXReadIndex = 0;
TXBUF0 = 0xFF;
while(ucConfigTXReadIndex < ucConfigTXCharCount)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = ConfigTXBuffer[ucConfigTXReadIndex++];
};
IE1 |= URXIE0;
}
#pragma vector=UART0TX_VECTOR
__interrupt void usart0_tx (void)
{
if(ucConfigTXReadIndex < ucConfigTXCharCount)
{
TXBUF0 = ConfigTXBuffer[ucConfigTXReadIndex++];
}
else //发送完数据打开接收中断
{
IE1 |= URXIE0;
}
}
void SendVari(S16 variables)
{
U16 SendData;
U8 SendH,SendL;
if(variables > 13000 || variables < -4000)
{
variables = 9999;
}
if(variables >= 0)
{
ConfigTXBuffer[ucConfigTXCharCount++] = 0x01;
}
else
{
ConfigTXBuffer[ucConfigTXCharCount++] = 0x00;
}
SendData = variables ;
SendH = SendData/100;
SendL = SendData%100;
ConfigTXBuffer[ucConfigTXCharCount++] = SendH/10*16 + SendH%10;
ConfigTXBuffer[ucConfigTXCharCount++] = SendL/10*16 + SendL%10;
}
void ProcessConfigCmd00()
{
}
//读取 温度 湿度 露点
void ProcessConfigCmd01()
{
ucConfigTXCharCount = 0;
ConfigTXBuffer[ucConfigTXCharCount++] = 0xFF;
ConfigTXBuffer[ucConfigTXCharCount++] = 0x03;
ConfigTXBuffer[ucConfigTXCharCount++] = 0x01;
//发送温度
unionFlt.Flt = envionment_variables.temperature;
CnvtFlt();
//发送湿度
unionFlt.Flt = envionment_variables.humidity;
CnvtFlt();
//发送露点
unionFlt.Flt = envionment_variables.dew_point;
CnvtFlt();
MakeConfigParity();
StartConfigTX();
}
// 读取温湿度的 量程,零点,电流输出类型
void ProcessConfigCmd02()
{
U8 GetNum;
ucConfigTXCharCount = 0;
ConfigTXBuffer[ucConfigTXCharCount++] = 0xFF;
ConfigTXBuffer[ucConfigTXCharCount++] = 0x03;
ConfigTXBuffer[ucConfigTXCharCount++] = 0x02;
if(ConfigRXBuffer[2] == 0x55) //读取温湿度的量程
{
// ReadSystemVar();
ConfigTXBuffer[ucConfigTXCharCount++] = ConfigRXBuffer[2];
//发送温度量程
unionFlt.Flt = envionment_variables.Mini_temperature;
CnvtFlt();
unionFlt.Flt = envionment_variables.Max_temperature;
CnvtFlt();
//发送湿度量程
unionFlt.Flt = envionment_variables.Mini_humidity;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -