f(x) = x, where x is [0,5000], with 20,000 approximations. The exact result is 12,500,000
//****************************************************************************
// @Project Includes
//****************************************************************************
#include "MAIN.H"
// USER CODE BEGIN (MAIN_General,2)
#include <stdio.H>
#include <stdlib.H>
#include <string.h>
#include <math.h>
// USER CODE END
//****************************************************************************
// @Macros
//****************************************************************************
// USER CODE BEGIN (MAIN_General,3)
int i, j;
double ic;
// USER CODE END
//****************************************************************************
// @Defines
//****************************************************************************
// USER CODE BEGIN (MAIN_General,4)
// USER CODE END
//****************************************************************************
// @Typedefs
//****************************************************************************
// USER CODE BEGIN (MAIN_General,5)
typedef double (*pfunc)(double, double, double, double (*)());
typedef double (*rfunc)(double);
#define INTG(F,A,B) (F((B))-F((A)))
// USER CODE END
//****************************************************************************
// @Imported Global Variables
//****************************************************************************
// USER CODE BEGIN (MAIN_General,6)
// USER CODE END
//****************************************************************************
// @Global Variables
//****************************************************************************
// USER CODE BEGIN (MAIN_General,7)
double int_leftrect(double from, double to, double n, double (*func)());
double int_rightrect(double from, double to, double n, double (*func)());
double int_midrect(double from, double to, double n, double (*func)());
double int_trapezium(double from, double to, double n, double (*func)());
double int_simpson(double from, double to, double n, double (*func)());
double f3(double x);
double f3a(double x);
double f2(double x);
double f2a(double x);
double f1(double x);
double f1a(double x);
double from;
// USER CODE END
//****************************************************************************
// @External Prototypes
//****************************************************************************
// USER CODE BEGIN (MAIN_General,8)
// typedef dt date;
// USER CODE END
//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************
// USER CODE BEGIN (MAIN_General,9)
pfunc f[5] = {
int_leftrect, int_rightrect,
int_midrect, int_trapezium,
int_simpson
};
const char *names[5] = {
"leftrect", "rightrect", "midrect",
"trapezium", "simpson"
};
rfunc rf[1] = { f1 };
rfunc If[1] = { f1a};
double sdata ivals[8] = {
1.0, 4.0,
1.0, 100.0,
0.0, 5000.0,
0.0, 6000.0
};
double sdata approx[4] = { 100.0, 1000.0, 20000.0, 20000.0 };
// USER CODE END
//****************************************************************************
// @Function void MAIN_vInit(void)
//
//----------------------------------------------------------------------------
// @Description This function initializes the microcontroller.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 4/11/2012
//
//****************************************************************************
// USER CODE BEGIN (Init,1)
// USER CODE END
void MAIN_vInit(void)
{
// USER CODE BEGIN (Init,2)
// USER CODE END
/// -----------------------------------------------------------------------
/// Configuration of the System Clock:
/// -----------------------------------------------------------------------
/// - VCO clock used, input clock is connected
/// - input frequency is 8 MHz
/// - VCO output frequency 150 .. 200 MHz
/// - system clock is 40 MHz
MAIN_vUnlockProtecReg(); // unlock write security
PLLCON = 0x7844; // load PLL control register
//// -----------------------------------------------------------------------
//// Begin of Important Settings for the Start-Up File
//// -----------------------------------------------------------------------
/// All following settings must be set in the start-up file. You can use
/// DAvE's project file (*.dpt) to include this register values into your
/// compiler EDE.
/// ---------------------------------------------------------------------
/// Initialization of the CPUCON1 Register:
/// ---------------------------------------------------------------------
/// - space between two vectors is 2 words
/// - DISWDT executable until End of Init
/// - segmentation is enabled
/// - switch context is interruptible
/// - branch prediction is enabled
/// - zero cycle jump function is enabled
//// this register must be set in the start-up file
//// CPUCON1 = 0x0007
/// ---------------------------------------------------------------------
/// Initialization of the VECSEG Register:
/// ---------------------------------------------------------------------
/// - start from internal program memory
//// this register must be set in the start-up file
//// VECSEG = 0x00C0
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON0 Register:
/// ---------------------------------------------------------------------
//// this register must be set in the start-up file
//// SYSCON0 = 0x0000
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON1 Register:
/// ---------------------------------------------------------------------
/// clock prescaler for system is fpll / 1
/// clock prescaler for PD+ bus is fcpu / 1
//// this register must be set in the start-up file
//// SYSCON1 = 0x0000
/// ---------------------------------------------------------------------
/// Initialization of the SYSCON3 Register:
/// ---------------------------------------------------------------------
//// this register must be set in the start-up file
//// SYSCON3 = 0x0000
//// -----------------------------------------------------------------------
//// End of Important Settings for the Start-Up File
//// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// Initialization of the Peripherals:
// -----------------------------------------------------------------------
// initializes the Asynchronous/Synchronous Serial Interface (ASC0)
ASC0_vInit();
// USER CODE BEGIN (Init,3)
// USER CODE END
PSW_IEN = 1;
} // End of function MAIN_vInit
//****************************************************************************
// @Function void MAIN_vUnlockProtecReg(void)
//
//----------------------------------------------------------------------------
// @Description This function makes it possible to write one protected
// register. After calling of this function and write on the
// protected register is the security level set to low
// protected mode.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 4/11/2012
//
//****************************************************************************
// USER CODE BEGIN (UnlockProtecReg,1)
// USER CODE END
void MAIN_vUnlockProtecReg(void)
{
uword uwPASSWORD;
if((SCUSLS & 0x1800) == 0x0800) // if low protected mode
{
uwPASSWORD = SCUSLS & 0x00FF;
uwPASSWORD = (~uwPASSWORD) & 0x00FF;
SCUSLC = 0x8E00 | uwPASSWORD; // command 4
} // end if low protected mode
if((SCUSLS & 0x1800) == 0x1800) // if write protected mode
{
SCUSLC = 0xAAAA; // command 0
SCUSLC = 0x5554; // command 1
uwPASSWORD = SCUSLS & 0x00FF;
uwPASSWORD = (~uwPASSWORD) & 0x00FF;
SCUSLC = 0x9600 | uwPASSWORD; // command 2
SCUSLC = 0x0800; // command 3; new PASSWOR is 0x00
uwPASSWORD = SCUSLS & 0x00FF;
uwPASSWORD = (~uwPASSWORD) & 0x00FF;
SCUSLC = 0x8E00 | uwPASSWORD; // command 4
} // end if write protected mode
} // End of function MAIN_vUnlockProtecReg
//****************************************************************************
// @Function void main(void)
//
//----------------------------------------------------------------------------
// @Description This is the main function.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 4/11/2012
//
//****************************************************************************
// USER CODE BEGIN (Main,1)
// USER CODE END
void main(void)
{
// USER CODE BEGIN (Main,2)
// USER CODE END
MAIN_vInit();
// USER CODE BEGIN (Main,4)
for(j=0; j < (sizeof(rf) / sizeof(rfunc)); j++)
{
for(i=0; i < 5 ; i++)
{
ic = (*f[i])(ivals[2*j], ivals[2*j+1], approx[j], rf[j]);
printf("%10s [ 0,1] num: %+lf, an: %lf \n, ",
names[i], ic, INTG((*If[j]), ivals[2*j], ivals[2*j+1]));
}
printf("\n");
}
while (1);
}
//=================================================================================
double int_leftrect(double from, double to, double n, double (*func)())
{
double h = (to-from)/n;
double sum = 0.0, x;
for(x=from; x <= (to-h); x += h)
sum += func(x);
return h*sum;
}
double int_rightrect(double from, double to, double n, double (*func)())
{
double h = (to-from)/n;
double sum = 0.0, x;
for(x=from; x <= (to-h); x += h)
sum += func(x+h);
return h*sum;
}
double int_midrect(double from, double to, double n, double (*func)())
{
double h = (to-from)/n;
double sum = 0.0, x;
for(x=from; x <= (to-h); x += h)
sum += func(x+h/2.0);
return h*sum;
}
double int_trapezium(double from, double to, double n, double (*func)())
{
double h = (to - from) / n;
double sum = func(from) + func(to);
int i;
for(i = 1;i < n;i++)
sum += 2.0*func(from + i * h);
return h * sum / 2.0;
}
double int_simpson(double from, double to, double n, double (*func)())
{
double h = (to - from) / n;
double sum1 = 0.0;
double sum2 = 0.0;
int i;
double x;
for(i = 0;i < n;i++)
sum1 += func(from + h * i + h / 2.0);
for(i = 1;i < n;i++)
sum2 += func(from + h * i);
return h / 6.0 * (func(from) + func(to) + 4.0 * sum1 + 2.0 * sum2);
}
/* test */
double f3(double x)
{
return x;
}
double f3a(double x)
{
return x*x/2.0;
}
double f2(double x)
{
return 1.0/x;
}
double f2a(double x)
{
return log(x);
}
double f1(double x)
{
return sqrt(x*x*x+1);
}
double f1a(double x)
{
return 1;
}
// USER CODE END
// End of function main
// USER CODE BEGIN (MAIN_General,10)
// USER CODE END