HomeTOOLSEXAMPLESEXPLORE EMBEDDEDE CSERVICESECU SAMPLESRegistration
Embedded C programming Tutorial , Keil C ide , microsoftware.gr
Keil CRC and CAN BUS codes.
1. Shift led left
2.It's time for DAVE! <7/6/13>
3.Capture/Compare unit 6
4.ASC0-GPT1-MACROS
5.ASC0-FIFO-PEC
6.Analog converter
7.Memory manipulation routines
8. Recursion
9.Understanding interrupt priorities using CAPCOM2 module
10. POINTERS TO FUNCTION <4/7/13>,<4/28/13>
11.Memory models, memory types
12. The heap , part 1
13. The heap , part 2
14. The heap , part 3
15. Structure example
16. Nested structures, Array of structures.
17. Passing array of structures to function using pointers.<1/5/13>
18. Self Referential Structures
19. BITFIELDS
20. Linked list example
21. Circular linked list
22. Union example
23. Enumeration example
24. Watchdog timer example
25. Void pointer example <7/4/13>
26. The sieve of Eratosthenes
27. The stack
28. Union and bitfields as flags example. <6/23/13>
29. Look up table example. <8/11/13>
30. Seven segment display multiplexing -four digits with dot- example
31. LCD character display example - JHD162A
32. Hash table introduction example <8/27/14>
33. Array of Linked Lists example
34. Array of Linked lists-more functions included.
35. Hash table construction,searching and printing.
36. Fininte state machines- a first approach.
37. Finite state machines- two events example.
38. SPI port and an AT25128 serial eeprom hardware.
39. CRC CHECK
40. Definite Integral Calculator for Scientists, Engineers...
41 .Hamming distance of a CRC polynomial
42. Linux play starting.
43. Galois GF(2^4) Finite Field
44. Construct your own time triggered real time operating system.
45. CANBUS C CODE EXAMPLE.
40. Definite Integral Calculator for Scientists, Engineers...

Definite Integral Calculator using Simpson's rule of Numerical Analysis. 

f(x) = x^3, where x is [0,1], with 100 approximations. The exact result is 1/4, or 0.25.


f(x) = 1/x, where x is [1,100], with 1,000 approximations. The exact result is the natural log of 100, or about 4.605170


f(x) = x, where x is [0,5000], with 20,000 approximations. The exact result is 12,500,000
 

 f(x) = x, where x is [0,6000], with 20,000 approximations. The exact result is 18,000,000
 
 
The program is a good exercise about the use of array of functions .

 typedef double (*pfunc)(double, double, double, double (*)());

pfunc  f[5] = {
       int_leftrect, int_rightrect,
       int_midrect,  int_trapezium,
       int_simpson
     }; 

 The C code and result at Hyperterminal.


Simpson's rule:

http://pages.pacificcoast.net/~cazelais/187/simpson.pdf

Exercise: Calculate the definite integral of the function f(x)=sqrt(1+x*x*x),
x in [1,4] with 100 approximations as in the Simpson's pdf file above.

     Answer and result at Hyperterminal.            

//****************************************************************************
// @Module        Project Settings
// @Filename      MAIN.C
// @Project       R.dav
//----------------------------------------------------------------------------
// @Controller    Infineon XC164CM-8F40
//
// @Compiler      Keil
//
// @Codegenerator 1.0
//
// @Description   This file contains the project initialization function.
//
//----------------------------------------------------------------------------
// @Date          4/11/2012 8:08:26 πμ
//
//****************************************************************************
// USER CODE BEGIN (MAIN_General,1)
// USER CODE END
 
//****************************************************************************
// @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

 

 
 
 
       
   
 
  
 
 
 
 
 
 
 
 
 
 
 
                                
     
 
    
  
 
 
 
    
 
    
  
 
 
 
 
 
 

 

 

Home|TOOLS|EXAMPLES|EXPLORE EMBEDDEDE C|SERVICES|ECU SAMPLES|Registration