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

📄 main.c

📁 Freescale Code Warrior中C的编程
💻 C
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

#define SINE_FLOAT
//#define SINE_FIXED

#ifdef SINE_FLOAT
#include <math.h> /* include peripheral declarations */
double sin (double x);
#endif
#ifdef SINE_FIXED
#define LOAD_CARRY __asm("TPA\nSTA test")
unsigned int mul161616(unsigned int, unsigned int);
unsigned int sin(unsigned int);
#endif


void MCU_init(void); /* Device initialization function declaration */

void main(void) {
  #ifdef SINE_FLOAT
    double sine = 0.78539;
    double result;
  #endif
  #ifdef SINE_FIXED
    unsigned int sine = 0x640A;
    unsigned int result;
  #endif
  /* Uncomment this function call after using Device Initialization
     to use the generated code */
  /* MCU_init(); */

  EnableInterrupts; /* enable interrupts */

  /* include your code here */

  for(;;) {

  result = sin(sine);

    __RESET_WATCHDOG(); /* feeds the dog */
  } /* loop forever */
  /* please make sure that you never leave this function */
}

#ifdef SINE_FIXED
unsigned int mul161616(unsigned int a, unsigned int b)
{
  typedef union {
  char bPart[2];
  unsigned int iPart;
  } char_or_int;
  volatile char_or_int uA,uB, result;
  volatile unsigned int TempUp,TempDown;
  char test;
  
  uA.iPart = a;
  uB.iPart = b;
  
  TempUp = (char)((uA.bPart[1]*uB.bPart[1])>>8);
  TempDown = (unsigned int)(uA.bPart[1]*uB.bPart[0]);
  result.iPart = (unsigned int)(uA.bPart[1]*uB.bPart[0]);
  result.iPart += (TempDown+TempUp);
  TempUp = result.bPart[0];
  LOAD_CARRY;
  if(test & 1) {
    TempUp += 0x0100;
  }
  result.iPart = (unsigned int)(uA.bPart[0]*uB.bPart[0]);
  result.iPart += (TempUp);
  return result.iPart;
}

unsigned int sin(unsigned int x) 
{
  unsigned int iTemp,t3,t5,t7,sineint;

  sineint = x;
  
  iTemp = (unsigned int)mul161616(x,x);
  
  t3 = (unsigned int)(iTemp<<1);
  iTemp = (unsigned int)mul161616(t3,x);
  t3 = (unsigned int)(iTemp<<2)/3;
  iTemp = (unsigned int)mul161616(t3,x);
  t5 = (unsigned int)(iTemp<<2)/4;
  iTemp = (unsigned int)mul161616(t5,x);
  t5 = (unsigned int)(iTemp<<2)/5;
  iTemp = (unsigned int)mul161616(t5,x);
  t7 = (unsigned int)(iTemp<<2)/6;
  iTemp = (unsigned int)mul161616(t7,x);
  t7 = (unsigned int)(iTemp<<2)/7;
  
  //iTemp = (unsigned int)mul161616(t7,x);
  //t9 = (unsigned int)(iTemp<<2)/8;
  //iTemp = (unsigned int)mul161616(t7,x);
  //t9 = (unsigned int)(iTemp<<2)/9;
  
  sineint = x-t3+t5-t7;//*/ + t9;
  return sineint;
  
}
#endif

⌨️ 快捷键说明

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