Sophomore, predecessors to answer, please let us have your specific point, basic knowledge is weak, there may be some places don't know much about, the following is a code
# include "ioCC2530. H"
# include & lt; String. H>
# define UART0_RX 1
# define UART0_TX 2
Unsigned char dataRecv;
Unsigned char Flag=0;
Char UartState;//serial current state
Typedef unsigned char uchar;
Typedef unsigned int uint;
Unsigned char Tx_Buffer [8].
Unsigned char Rx_Buffer [40].
Unsigned char read_enable, receive_finished receive_number;
Unsigned long Voltage_data Current_data, Power_data Energy_data, Pf_data, CO2_data;
Unsigned char Read_ID=0 x01;
/* * * * * * * * * * * * * * * * * * * * delay function * * * * * * * * * * * * * * * * * * * * * * * * */
Void DelayMS (uint msec)
{
Uint I, j;
for (i=0; i
}
/* * * * * * * * * * * * * * * * * * * set the system clock source * * * * * * * * * * * * * * * * * * * * */
Void InitCLK ()
{
CLKCONCMD & amp;=~ 0 x40;//set the system clock source for 32 MHZ crystals
While (CLKCONSTA & amp; 0 x40);//wait for stable crystals to 32 m
CLKCONCMD & amp;=~ 0 x47;//set the system master clock frequency is 32 MHZ
}
/* * * * * * * * * * * * * * * * * * a serial port initialization function * * * * * * * * * * * * * * * * * * * * * */
Void Init_Uart0 ()
{
PERCFG=0 x00;//UART0 select position 0 [email protected] [email protected]
P0SEL |=0 x0c;//P0.3 P0.2 choose a peripheral function
U0CSR |=0 xc0;//UART mode receiver can make
P2DIR & amp;=~ 0 xc0;//P0 priority as UART0
IEN1 |=0 x20;
P0IEN |=0 x08;
U0GCR |=7.//and look-up table to obtain U0GCR U0BAUD
U0BAUD=59;//4800
URX0IF=1;//that can receive interrupt IEN0 @ BIT2
EA=1;
}
/* * * * * * * * * * * * * * * * * * UR0 receiving interrupt service function * * * * * * * * * * * * * * * */
# pragma vector=URX0_VECTOR
__interrupt void UR0_RecvInt ()
{
DataRecv=U0DBUF;//the data from the receive buffer read
URX0IF=0;//remove RX receives the interrupt flag
Flag=1;//set to receive instruction sign
}
/* * * * * * * * * * * * * * * * calculated CRC check code, don't have to figure out how to calculate the * * * * * * * * * * * * * * * * * * * */
Unsigned int calccrc (unsigned char crcbuf, unsigned int CRC)
{
unsigned char i;
Unsigned char CHK;
CRC=CRC ^ crcbuf;//is different or the same zero
for(i=0; i<8; I++) {
CHK=(unsigned char) (crc& 1);
CRC=crc> 1;
CRC=crc& 0 x7fff;//the supreme position zero
If (CHK==1)
CRC=CRC ^ 0 xa001;//take exclusive or, the same zero
CRC=crc& 0 XFFFF;//pay themselves, fixed value 0 xa001
}
The return of CRC;
}
Unsigned int CHKCRC (unsigned char * buf, unsigned char len)
{
Unsigned char the hi and lo;
Unsigned int I;
Unsigned int CRC;
CRC=0 XFFFF;
for(i=0; i
Buf++;
}
Hi=(unsigned char) (CRC % 256);//low eight
Lo=(unsigned char) (CRC/256);//high eight
CRC=(((unsigned int) (hi) & lt; <8) | lo;//high eight low eight swap places
The return of CRC;
}
/* * * * * * * * * * * * * * * * * * UR0 send string function * * * * * * * * * * * * * * * * */
Void UartSend (unsigned char * Data, int len)//send function
{
Uint I;
//U0CSR & amp;=~ 0 x40;//prohibited receiving
for(i=0; i
U0DBUF=* Data++;
While (UTX0IF==0);//the interrupt flag bit is 0
UTX0IF=0;
}
//U0CSR |=0 x40;//allow receiving
}
/* * * * * * * * * * * * * * * * * * * * * * * send 'total' function * * * * * * * * * * * * * * * * * * * * * * * */
Void the read_data (void) {//read operation
The union crcdata {
Unsigned int word16;
Unsigned char byte [2].
} crcnow;
If (read_enable==1)//to read time copying module, copy to read two seconds (or other)
{
Read_enable=0;
Tx_Buffer [0]=Read_ID;//module ID number, the default ID 0 x01
Tx_Buffer [1]=0 x03;
Tx_Buffer [2]=0 x00;
Tx_Buffer [3]=0 x48;
Tx_Buffer [4]=0 x00;
Tx_Buffer [5]=0 x08;
Crcnow. Word16=CHKCRC (Tx_Buffer, 6);
Tx_Buffer [6]=crcnow. Byte [1].//CRC efficacy low byte before
Tx_Buffer. [7]=crcnow byte [0];
UartSend (Tx_Buffer, 8);//send eight data, please yourself depending on the type of microcontroller programming
}
}
/* * * * * * * * * * * * * * * * * * * verify whether there are any errors, manufacturers have to * * * * * * * * * * * * * * * * * * * * */
Void Analysis_data (void) {
The union crcdata {
Unsigned int word16;
Unsigned char byte [2].
} crcnow;
If (receive_finished==1)//receiving complete
{
Receive_finished=0;
If (
Rx_Buffer [0]==Read_ID)//confirmation ID right
{
Crcnow. Word16=CHKCRC (Rx_Buffer receive_number - 2); The total length//receive_numbe is receiving data
If ((crcnow byte [0]==Rx_Buffer [] receive_number - 1) & amp; & (crcnow. Byte [1]==Rx_Buffer [receive_number - 2]))
//confirm the correct CRC check
{
Voltage_data=https://bbs.csdn.net/topics/(((unsigned long) (Rx_Buffer [3])) <24) | (((unsigned long) (Rx_Buffer [4])) <16) | (((unsigned long) (Rx_Buffer [5])) <8) | Rx_Buffer [6].
Current_data=https://bbs.csdn.net/topics/(((unsigned long) (Rx_Buffer [7])) <24) | (((unsigned long) (Rx_Buffer [8])) <16) | (((unsigned long) (Rx_Buffer [9])) <8) | Rx_Buffer [10].
Power_data=https://bbs.csdn.net/topics/(((unsigned long) (Rx_Buffer [11])) <24) | (((unsigned long) (Rx_Buffer [12])) <16) | (((unsigned long) (Rx_Buffer [13])) <8) | Rx_Buffer [14].
Energy_data=https://bbs.csdn.net/topics/(((unsigned long) (Rx_Buffer [15])) <24) | (((unsigned long) (Rx_Buffer [16])) <16) | (((unsigned long) (Rx_Buffer [17])) <8) | Rx_Buffer [18].
Pf_data=https://bbs.csdn.net/topics/(((unsigned long) (Rx_Buffer [19])) <24) | (((unsigned long) (Rx_Buffer [20])) <16) | (((unsigned long) (Rx_Buffer [21])) <8) | Rx_Buffer [22].
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull