⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comms_uart.c

📁 MSP430 examples or customized
💻 C
📖 第 1 页 / 共 2 页
字号:
//**************************************************************************
//    UART commmunication for the FE427
//
//    Stefan Schauer
//    Texas Instruments Freising
//    Date        Comments
//    =====================
//    07/01/03    Code Starts for FE427
//                Added 'SP' Command to read/modify ESP430 Parameters
//
//    Version 1.0 first release for Application Report
//    06/14/04
//    Version 1.1
//    08/19/04  added calibration of POFFSETx
//    Version 1.2
//    12/23/04  changed calibration function (+/-) to use values instead of
//                steps
//
//**************************************************************************

/* ***********************************************************
* THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
* REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
* INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
* COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
* TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
* POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
* INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
* YOUR USE OF THE PROGRAM.
*
* IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
* THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
* OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
* EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
* REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
* OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
* USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
* AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
* YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
* (U.S.$500).
*
* Unless otherwise stated, the Program written and copyrighted
* by Texas Instruments is distributed as "freeware".  You may,
* only under TI's copyright in the Program, use and modify the
* Program without any charge or restriction.  You may
* distribute to third parties, provided that you transfer a
* copy of this license to the third party and the third party
* agrees to these terms by its first use of the Program. You
* must reproduce the copyright notice and any other legend of
* ownership on each copy or partial copy, of the Program.
*
* You acknowledge and agree that the Program contains
* copyrighted material, trade secrets and other TI proprietary
* information and is protected by copyright laws,
* international copyright treaties, and trade secret laws, as
* well as other intellectual property laws.  To protect TI's
* rights in the Program, you agree not to decompile, reverse
* engineer, disassemble or otherwise translate any object code
* versions of the Program to a human-readable form.  You agree
* that in no event will you alter, remove or destroy any
* copyright notice included in the Program.  TI reserves all
* rights not specifically granted under this license. Except
* as specifically provided herein, nothing in this agreement
* shall be construed as conferring by implication, estoppel,
* or otherwise, upon you, any license or other right under any
* TI patents, copyrights or trade secrets.
*
* You may not use the Program in non-TI devices.
* ********************************************************* */

#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 <math.h>
#include <stdio.h>
#include <string.h>      // Needed for strcpy()

#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\
 SIxx: set calibration current\r\
 SVxx: set calibration voltage\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: Temp\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\
 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\
  C7: calibration of Gain Correction 1\r\
  C8: calibration of Gain Correction 2\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\
 C13: calibration of Power1 Offset\r\
 C14: calibration of Power2 Offset\r\
 +xxx : inc values for calibration\r\
 -xxx : 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;
  case 230400:
    UTCTL0 = SSEL1;                       // UCLK = SMCLK
    UBR00=0x12;
    UBR10=0x00;
    UMCTL0=0x84; /* uart0 4194304Hz 230456bps */
    break;

  default:
    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 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

   for (i=10; i>after; i--)
   {
      if ((i&1) == 0)      // Test for odd or even
      {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -