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

📄 util.c

📁 Analog 公司 ADE7169 SOC 电表方案DEMO程序
💻 C
字号:
/*
-02/24/2006: Petre M.
  -this file contains functions that may be charaterized as utilities
*/

#include "ioADE7169F16.h"
#include "extern_declarations.h"

//this function sets the watchdog period
//PRE=0, 1,2,...,7 = properly bits PRE of WDCON
//WDCON can only be modified in a sequence of 2 instructions. For this reason,
//the optimization is turned off for this function
#pragma optimize=none
void Setup_Watchdog(char PRE) {

  //WDE=bit1=1
  //WDWR=bit0=1
  PRE = (PRE<<4) | 0x03;

  WDCON_bit.WDWR = 1;
  WDCON = PRE;

  return;
}

//this function prevents watchdog from timeout
void Refresh_Watchdog(void) {

  IE_bit.EA = 0;//disable all interrupts
  WDCON_bit.WDWR = 1;
  WDCON_bit.WDE = 1;
  IE_bit.EA = 1;//enable all interrupts

  return;
}

//this function tests if the reset has been provoked by a watchdog
//and displays error message
void Test_Watchdog(void) {
  char i, a;

  //only if the watchdog is enabled (Bit1), test WDS bit (Bit2) and see if the
  //watchdog reset happened
  if ((WDCON & 0x06)==0x06) {
      //disable all interrupts
      IE_bit.EA = 0;

      Disable_Watchdog();

      Setup_LCD();

      //LCD_buffer is turned completely on and blinks with 1Hz frequency
      //BLKFREQ bits of LCDCLK SFR are 0
      Blink_LCD(Blink_1Hz);

      LCDCONY_bit.REFRESH = 1;  //LCD data memory is updated, so bit0=1
      a = 0x8e;
      for (i=0; i<15; i++) {
        LCDDAT = 0xff;
        LCDPTR = a;
        a--;
      }
      LCDCONY_bit.REFRESH = 0;  //updating LCD data memory has ended, so bit0=0

      while(1);//stay here
  }
  return;
}

void Disable_Watchdog(void) {

  WDCON_bit.WDWR = 1;
  WDCON_bit.WDE = 0;


  return;
}

//this function clears the bit identified by !NBit
void Clear_flag(char NBit, char __idata  *iram_ptr) {

  char a;
  //we compute this here to avoid spending to much time with interrupts disabled
  a = *iram_ptr & NBit;

  IE_bit.EA = 0;//disable all interrupts
    *iram_ptr = a;
  IE_bit.EA = 1;//enable all interrupts
  return;
}

//this function clears two bits identified by !NBit1 and !NBit2
void Clear_2_flags(char NBit_1, char __idata  *iram_ptr1, char NBit_2, char __idata  *iram_ptr2 ) {

  char a,b;
  a = *iram_ptr1 & NBit_1;
  b = *iram_ptr2 & NBit_2;
  IE_bit.EA = 0;//disable all interrupts
    *iram_ptr1 = a;
    *iram_ptr2 = b;
  IE_bit.EA = 1;//enable all interrupts
  return;
}


//this function sets a bit identified by Bit
void Set_flag(char Bit, char __idata  *iram_ptr) {

  char a;

  a = *iram_ptr | Bit;
  IE_bit.EA = 0;//disable all interrupts
    *iram_ptr = a;
  IE_bit.EA = 1;//enable all interrupts
  return;
}



//this function initializes variables that have common values for PSM0 and PSM1.
//We want to avoid the compiler doing it
//every time the part is reset or is woken up from PSM2 mode
void Init_variables(void) {
  char i;

  //LCD_buffer is initialized here
  for(i=0;i<15;i++) {
    LCD_buffer[i] = 0;
  }

  Low_Priority_Command[0] = 0;

  Command_Status[0] = 0;
  I2C_Status = 0;

  //the pointer that manages the Low Priority Tasks is initialized
  LP_Task = 0x00;

  return;
}

//this function initializes variables special for PSM0
void Init_variables_PSM0(void) {

  Meter_Errors[0] = 0;

  return;
}


//this function loads the ADE Register with the content of ByteH,ByteM,ByteL
void Write_ADE_SFR(char ByteM, char ByteL, char Register) {

///////  MDATH = 0x00;
  MDATM = ByteM;
  MDATL = ByteL;
  MADDPT = 0x80 | Register;

  return;
}

//this function reads the ADE Register and puts the result into ByteH, ByteM, ByteL
void Read_ADE_SFR(unsigned char __idata *Pointer, char Register) {
  char ByteH, ByteM, ByteL;

  MADDPT = Register;
  ByteH = MDATH;
  ByteM = MDATM;
  ByteL = MDATL;

  *Pointer = ByteL;
  *(Pointer+1) = ByteM;
  *(Pointer+2) = ByteH;

  return;
}

//this function is always the last function that is written in the
//Table_Low_Priority_Func table. It ensures that Bit0 of Low_Priority_Command[0]
void End_Low_Priority_Tasks(void) {

  //the pointer that manages the Low Priority Tasks is initialized back to 0
  LP_Task = -1;

  Clear_flag(NBit0, &Low_Priority_Command[0]);

  return;
}

⌨️ 快捷键说明

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