📄 comms_uart.c
字号:
//**************************************************************************
// UART commmunication for the FE427
//
// Stefan Schauer
// Texas Instruments Freising
// Date Comments
// =====================
// 07/01/03 Code Starts for FE427
// 01/30/04 JEN changed sprintf's to call l_to_a and ul_to_a
//**************************************************************************
#include "parameter.h"
#include "device.h"
#include "comms_uart.h"
#include "SendData.h"
#include "subroutines.h"
#include "display.h"
#include "uart0.h"
#include "fet4xx_rtclcd.h"
#include "emeter.h"
#include "calibration.h"
#include "SysVects.h"
#include "emeter.h"
#include <math.h>
//#include <stdio.h>
#include "NumConv.h"
#include <string.h> // Needed for strcpy()
//#include <stdint.h>
#ifdef withUARTComm
#define ldfs "%ld"
// ************************************************************
// Definitions & Declarations
// ************************************************************
unsigned char Cal_Mode = 0;
const char * txt_help = "\
UART commands:\r\
SHxx: set hour\r\
SMxx: set minutes\r\
SSxx: set seconds\r\
SDxx: set day\r\
SOxx: set month\r\
SYxx: set year\r\
SIxxxx: set calibration current\r\
SVxxxx: set calibration voltage\r\
SFxx: set line frequency\r\
\r\
Dx: set Display mode\r\
1: Off\r\
2: Time\r\
3: Date\r\
4: Voltage (V)\r\
5: Current (A)\r\
6: PeakVoltage (V)\r\
7: PeakCurrent (A)\r\
8: Frequency (Hz)\r\
9: CosPhi\r\
10: Temp\r\
11: Power (kW)\r\
12: Energy (kWh)\r\
Vx: value of single measurement\r\
1: Off\r\
2: Time\r\
3: Date\r\
4: Voltage (V)\r\
5: Current (A)\r\
6: PeakVoltage (V)\r\
7: PeakCurrent (A)\r\
8: Frequency (Hz)\r\
9: CosPhi\r\
10: Temperature\r\
11: Power (kW)\r\
12: Energy (kWh)\r\
\r\
H : show help test\r\
Tx: set test dump mode\r\
Qx: query dump\r\
R : reset system\r\
Wxx: write message to LCD display\r\
FTxxxx: set FLASH programming tag\r\
FExxxx: erase FLASH block\r\
FWxxxxyyyy: write FLASH address\r\
FRxxxx: read FLASH address\r\
FS:read FLASH tag status\r\
Mx: execute calibration measurement over x seconds\r\
I : init\r\
\r\
C0: auto calibration of U / I / P / PhaseShift\r\
C1: calibration of Interrupt Level\r\
C2: calibration of Phase correction 1\r\
C3: calibration of Phase correction 2\r\
C4: calibration of V1 Offset\r\
C5: calibration of I1 Offset\r\
C6: calibration of I2 Offset\r\
C9: save settings to flash\r\
C10: calibration of V Ratio\r\
C11: calibration of I Ratio\r\
C12: calibration of Energy Ratio\r\
+ : inc values for calibration\r\
- : dec values for calibration\r\
";
// ************************************************************
// functions forward declarations
// ************************************************************
int Digit0(unsigned char Register);
int Digit1(unsigned char Register);
int Hex2ASCII(int hex);
// ************************************************************
// functions
// ************************************************************
void InitUART(unsigned long baud)
{
//UART
// Configure USART in UART mode, 8-bit, 1-stop, no parity.
// Hold logic in reset state while configuring other registers
UCTL0 = (0*PENA) // 7 Parity Enable 0=Disabled
|(0*PEV) // 6 Parity Select 0= Odd
|(0*SPB) // 5 Stop Bit Select 0= 1 Stop bit
|(1*CHAR) // 4 Character Length 1= 8-bit data
|(0*LISTEN) // 3 Loopback control 0= Loopback disabled
|(0*SYNC) // 2 Synchronous Mode 0= UART mode
|(0*MM) // 1 Multiprocessor Mode 0= Idle-line multiproc control
|(1*SWRST); // 0 Software Reset 1= Logic Held in Reset State ...
// while configuring other registers
switch(baud)
{
case 9600:
UTCTL0 = SSEL0; // UCLK = ACLK
UBR00 = 0x03; // 32k/9600
UBR10 = 0x00; //
UMCTL0 = 0x4a; //
break;
case 19200:
UTCTL0 = SSEL1; // UCLK = SMCLK
UBR00 = 0xDA; // 4MHz/19200
UBR10 = 0x00; //
UMCTL0 = 0x55; //
break;
case 57600:
UTCTL0 = SSEL1; // UCLK = SMCLK
UBR00 = 0x48; // 4Mhz/57600
UBR10 = 0x00; //
UMCTL0 = 0x7B; //
break;
case 115200:
UTCTL0 = SSEL1; // UCLK = SMCLK
UBR00 = 0x24; // 4Mhz/115.2k
UBR10 = 0x00; //
UMCTL0 = 0x29; //
break;
default:
while(1){;}
//break;
}
U0ME |= UTXE0+URXE0 ; // Enabled USART0 TXD/RXD
P2SEL |= (BIT4 + BIT5); // P2.4,5 = USART0 TXD/RXD
P2DIR |= BIT4; // P2.4 output direction
U0IFG &= ~URXIFG0; // Clear USART0 RX interrupt flag
UCTL0 &= ~(SWRST); // 8-bit character - clr SWRST bit
// U0IE |= URXIE0+UTXIE0; // Enable USART0 RX + TX interrupt
U0IE |= URXIE0; // Enable USART0 RX interrupt
}
void SendString(const char* ARRAY)
{
int i=0;
while (ARRAY[i]) // transmit " String" via RS232
{
SendChar(ARRAY[i]);
i++;
}
}
void SendResult(unsigned char* ARRAY, unsigned int length)
{
int i;
//#ifdef withRS485
// SelectRS485Direction(RS485_TALK); // Set P2.2 & P2.3 to talk on serial output
//#endif
for (i=length-1;(i+1)>0;i--)
{ SendChar(Digit1(ARRAY[i])); // transmit digit 1
SendChar(Digit0(ARRAY[i])); // transmit digit 0
}
//#ifdef withRS485
// while ((U0IFG&UTXIFG0)==0); // wait till TX buf empty
// SelectRS485Direction(RS485_LISTEN); // Set P2.2 & P2.3 to listen to serial input
//#endif
}
void SendFloat(uint32_t lvalue, unsigned int after)
{
int i;
int digitval; // ASCII version of next character to send
int LZSflag = 1; // Leading-zero supression flag
uint8_t bcd[5];
bin2bcd32(bcd,lvalue); // Convert from 32 bit to BCD
//#ifdef withRS485
// SelectRS485Direction(RS485_TALK); // Set P2.2 & P2.3 to talk on serial output
//#endif
for (i=10; i>after; i--)
{
if ((i&1) == 0) // Test for odd or even
{
digitval = Digit1(bcd[(10-i)/2]); // Even: high digit
}
else
{
digitval = Digit0(bcd[(10-i)/2]); // Odd: low digit
}
if (digitval > '0')
{LZSflag = 0;} // Clear LZS flag when non-zero character seen
if ( (digitval != '0')
||(LZSflag == 0)
||(i == after+1) )
{
SendChar(digitval);
}
}
if(after > 0)
{
SendChar('.');
for (i=after; i>0; i--)
{
if ((i&1) == 0) // Test for odd or even
{
digitval = Digit1(bcd[(10-i)/2]); // Even: high digit
}
else
{
digitval = Digit0(bcd[(10-i)/2]); // Odd: low digit
}
SendChar(digitval);
}
}
//#ifdef withRS485
// while ((U0IFG&UTXIFG0)==0); // wait till TX buf empty
// SelectRS485Direction(RS485_LISTEN); // Set P2.2 & P2.3 to listen to serial input
//#endif
}
void SendChar(char ch)
{
#ifdef withRS485
SelectRS485Direction(RS485_TALK); // Set P2.2 & P2.3 to talk on serial output
P1OUT &= ~0x01; // Turn off P1.0 to turn on User0 LED
#endif
while ((U0IFG&UTXIFG0)==0); // wait till TX buf empty
TXBUF0=ch; // transmit ch
#ifdef withRS485
// while ((U0IFG&UTXIFG0)==0); // wait till TX buf empty
while ((U0TCTL&TXEPT)==0); // wait until TX shift register is empty
if (ch == '\r') // Only change RS485 direction if this is the end of a line
{
SelectRS485Direction(RS485_LISTEN); // Set P2.2 & P2.3 to listen to serial input
P1OUT |= 0x03; // Turn on P1.0 and P1.1 to turn off User0 and User1 LEDs
}
#endif
}
int Digit0(unsigned char Register) // return LSB nibble (digit 0)
{ int result;
result = Hex2ASCII(0x0F & Register);
return result;
}
int Digit1(unsigned char Register) // return nibble (digit 1)
{ int result;
result = Register >> 4;
result = Hex2ASCII(0x0F & result);
return result;
}
int Hex2ASCII(int hex) // hexadecimal to ASCII conversion
{ int result;
if (hex<=9)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -