?? comms_uart.c
字號:
//**************************************************************************
// UART commmunication for the FE427
//
// Stefan Schauer
// Texas Instruments Freising
// Date Comments
// =====================
// 07/01/03 Code Starts for FE427
//**************************************************************************
#include "device.h"
#include "comms_uart.h"
#include "subroutines.h"
#include "display.h"
#include "uart0.h"
#include "fet4xx_rtclcd.h"
#include "emeter.h"
#include "calibration.h"
#include "parameter.h"
#include <math.h>
#include <stdio.h>
#ifdef withUARTComm
// ************************************************************
// Definitions & Declarations
// ************************************************************
const char * txt_help = "\
UART commands:\r\
shxx: set hour\r\
smxx: set minutes\r\
ssxx: set seconds\r\
xsdxx: set day\r\
xsoxx: set month\r\
xsyxx: set year\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\
A: Temp\r\
B: Power (kW)\r\
C: Energy (kWh)\r\
\r\
h : show help test\r\
tx: set test dump mode\r\
r : reset system\r\
mx: execute calibration measuerment over x*50 cycles\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\
ca: calibration of V Ratio\r\
cb: calibration of I Ratio\r\
cc: 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
UCTL0 = CHAR+SWRST; // 8-bit character
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;
for (i=length-1;(i+1)>0;i--)
{ SendChar(Digit1(ARRAY[i])); // transmit digit 1
SendChar(Digit0(ARRAY[i])); // transmit digit 0
}
}
void SendChar(char ch)
{
while ((U0IFG&UTXIFG0)==0);// wait till TX buf empty
TXBUF0=ch; // transmit ch
}
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)
{ result=hex+'0'; } // convert number
else
{ result=hex+('A'-10); } // convert letter
return result;
}
//;--------------------------------------------------------------
//; This routine processes a received UART Command
void Process_UART(void)
{
if (UART_Status & LineReceived)
{
//UARTSet
switch (UART_RX_Buffer[0])
{
case 's':
//SetClock
switch (UART_RX_Buffer[1])
{
case 'h':
HOUR = GetNumber(&UART_RX_Buffer[2]);
break;
case 'm':
MIN = GetNumber(&UART_RX_Buffer[2]);
break;
case 's':
SEC = GetNumber(&UART_RX_Buffer[2]);
break;
default:
break;
}
break;
#ifdef withDisplay
case 'd':
//SetDisplay_Mode
Display_Mode = GetNumber(&UART_RX_Buffer[1]);
break;
#endif // withDisplay
case 'h':
//send help text
SendString(txt_help);
break;
case 't':
//SetTX_Mode
TX_Mode = GetNumber(&UART_RX_Buffer[1]);
break;
case 'r':
WDTCTL =0; // Generate WDT violation Reset
break;
case 'm':
//SetMeasureMode
TX_Mode = 0; // ; Clear running TX_Mode
OP_Mode = request_cal;
CalCyclCnt = 50*GetNumber(&UART_RX_Buffer[1]); // Set Cycles
break;
case 'i':
//Init
_DINT(); // Disable Interrupts
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
// Init Digital Hardware
init_system();
// Init. FLL and clocks
init_fll(10, defSystemFreq, 32);
// Init. analog front-end (sigma-delta ADC)
init_analog_front_end();
// Init. Embedded Signal Processing parameters
init_esp_parameter(1);
// Init. event generation and start measurement
start_measurement();
_EINT(); // Enable Interrupts
break;
#ifdef withCalibration
case '+':
CalPlus();
break;
case '-':
CalMinus();
break;
case 'c':
//Set Calibratoin mode
{
unsigned char Cal_Mode;
static char strbuf[20];
Cal_Mode = GetNumber(&UART_RX_Buffer[1]);
if (Cal_Mode == 0)
{
SendString("Start Calibration : expecting 230V / 10A / cosPhi = 0.5: \r");
SendString("Old Data : \r");
sprintf(strbuf, "%ld",(long) (s_parameters.VRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) (s_parameters.IRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) (s_parameters.EnergyRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) s_parameters.pSET_PHASECORR1); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) s_parameters.pSET_PHASECORR2); SendString(strbuf); SendChar('\r');
//host_decu32((long) (s_parameters.VRatio*1000)); SendChar('\r');
//host_decu32((long) (s_parameters.IRatio*1000)); SendChar('\r');
//host_decu32((long) (s_parameters.EnergieRatio*1000)); SendChar('\r');
//host_decu32((long) s_parameters.pSET_PHASECORR1); SendChar('\r');
//host_decu32((long) s_parameters.pSET_PHASECORR2); SendChar('\r');
}
CalMode(Cal_Mode);
if (Cal_Mode == 0)
{
SendString("Calibration Done\r");
SendString("New Data : \r");
sprintf(strbuf, "%ld",(long) (s_parameters.VRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) (s_parameters.IRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) (s_parameters.EnergyRatio*1000)); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) s_parameters.pSET_PHASECORR1); SendString(strbuf); SendChar('\r');
sprintf(strbuf, "%ld",(long) s_parameters.pSET_PHASECORR2); SendString(strbuf); SendChar('\r');
//host_decu32((long) (s_parameters.VRatio*1000)); SendChar('\r');
//host_decu32((long) (s_parameters.IRatio*1000)); SendChar('\r');
//host_decu32((long) (s_parameters.EnergieRatio*1000)); SendChar('\r');
//host_decu32((long) s_parameters.pSET_PHASECORR1); SendChar('\r');
//host_decu32((long) s_parameters.pSET_PHASECORR2); SendChar('\r');
}
if (Cal_Mode == 4) { SendString("Parameters saved\r"); }
}
break;
#endif // withCalibration
}
UART_Status &= ~LineReceived;
UART_RX_Bytes = 0;
}
}
//No_Process_Uart
// ret
#endif // withUARTComm
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -