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

📄 uart.c

📁 Analog 公司 ADE7169 SOC 电表方案DEMO程序
💻 C
字号:
/*
-08/15/2006: Petre M.
  -this file contains all the functions used in UART communications

*/
#include "ioADE7169F16.h"
#include "extern_declarations.h"
#include "EEPROM_locations.h"


void Setup_UART(void) {
   /*01=Mode 1=8 bit UART, variable rate
        0=RI is set as soon as the data byte is received
         1=serial port reception enabled
          0=this bit represents the 9th data bit transmitted in Modes 2 and 3
           0=this bit represents the stop bit received in Mode 1 case
            0=Tx INT flag
             0=Rx INT flag
    */
  SCON = (SCON & 0x00) | 0x50;

  //for 9600baud@4.096MHz, DIV=4 and SBAUDF=0xab, real freq=9570baud
  SBAUDT = 0x04;
  SBAUDF = 0xab;

  //initialize the pointer at the beginning of UART_Rx buffer
  UART_ptr = &UART_Rx[0];

  return;
}


void Manage_UART_Rx(void) {

  //if one byte has been received
  if (SCON_bit.RI) {
    //store SBUF in the buffer indicated by UART_ptr
    *UART_ptr=SBUF;
    UART_ptr++;
    SCON_bit.RI=0;

    //if 11 bytes have been received
    if (UART_ptr==(&UART_Rx[10]+1)) {
      //set UART_ptr at the beginning of the buffer to be ready
      //for a new message
      UART_ptr=&UART_Rx[0];
      UART_func_ptr =  (void (*) (void))Table_UART[UART_Rx[6]];

      //execute the command
      (*UART_func_ptr)();

      //send back the message
      SBUF = *UART_ptr;
      UART_ptr++;
    }
  }

  if (SCON_bit.TI) {
    SCON_bit.TI = 0;
    if (UART_ptr<(&UART_Rx[10]+1)) {
      SBUF = *UART_ptr;
      UART_ptr++;
    }
    else
      UART_ptr = &UART_Rx[0];
  }
  return;
}

void IRAM_read(void) {
  char __idata *iram_ptr;
  char a;

  iram_ptr = (char __idata *) UART_Rx[4];
  a = *iram_ptr;

  End_message_bytes(a, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void IRAM_write(void) {
  char __idata *iram_ptr;
  iram_ptr = (char __idata *) UART_Rx[4];
  *iram_ptr = UART_Rx[0];

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);
  return;
}

//this function reads one ADE internal register and then prepares the
//buffer that will be sent back to PC
void ADE_reg_read(void) {

  Read_ADE_SFR(&UART_Rx[7], UART_Rx[4]);

  UART_Rx[10] = 0xaa;
  return;
}

//this function writes one ADE internal register and then prepares the
//buffer that will be sent back to PC
void ADE_reg_write(void) {

  Write_ADE_SFR(UART_Rx[1], UART_Rx[0], UART_Rx[4]);

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void INTPR_read(void) {
  char a;
  a = INTPR;

  End_message_bytes(a, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void INTPR_write(void) {

  INTPR = UART_Rx[0];

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void RTCCOMP_read(void) {
  char a;
  a = RTCCOMP;

  End_message_bytes(a, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void RTCCOMP_write(void) {
  RTCCOMP = UART_Rx[0];

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}


void TEMPCAL_read(void) {
  char a;
  a = TEMPCAL;

  End_message_bytes(a, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void TEMPCAL_write(void) {
  TEMPCAL = UART_Rx[0];

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

void VRMS_read(void) {
  char a,b,c;

  c = VRMSH;
  b = VRMSM;
  a = VRMSL;
  End_message_bytes(a, b, c, End_Byte_10);

  return;
}


void IRMS_read(void) {
  char a,b,c;

  c = IRMSH;
  b = IRMSM;
  a = IRMSL;
  End_message_bytes(a, b, c, End_Byte_10);

  return;
}

void Time_read(void) {

  Store_time((char __idata *)&UART_Rx[7]);
  return;
}

//this function writes the RTC SFRs
void Time_write(void) {

  HTHSEC = UART_Rx[0];
  SEC = UART_Rx[1];
  MIN = UART_Rx[2];
  HOUR = UART_Rx[3];
  if (HTHSEC!=UART_Rx[0]) {
    HTHSEC = UART_Rx[0];
    SEC = UART_Rx[1];
    MIN = UART_Rx[2];
    HOUR = UART_Rx[3];
  }

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);

  return;
}

//this function reads one byte from the EEPROM
void EEPROM_read(void) {
  int __idata * iram_ptr;

  iram_ptr = (int __idata *)&UART_Rx[4];

  Tx_CTRL_byte(Nr_Bytes_1, *iram_ptr,(unsigned char __idata  *)&UART_Rx[7]);

  UART_Rx[8] = End_Byte_8;
  UART_Rx[9] = End_Byte_9;
  UART_Rx[10]= End_Byte_10;

  return;
}

//this function writes a byte into the EEPROM
void EEPROM_write(void) {
  int __idata * iram_ptr;

  iram_ptr = (int __idata *)&UART_Rx[4];

  Tx_byte(Nr_Bytes_1, *iram_ptr, (unsigned char __idata  *)&UART_Rx[0]);

  End_message_bytes(End_Byte_7, End_Byte_8, End_Byte_9, End_Byte_10);
  return;
}


void End_message_bytes(char a, char b, char c, char d) {

  UART_Rx[7]=a;
  UART_Rx[8]=b;
  UART_Rx[9]=c;
  UART_Rx[10] = d;
  return;
}

⌨️ 快捷键说明

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