howfast.c


1
#include <stdlib.h>
2
#include <stdint.h>
3
#include <math.h>
4
#include <avr/io.h>
5
6
#define LEDPORT      PORTB
7
#define LEDDDR      DDRB
8
#define LEDGRUEN    0   
9
#define LEDTOGGLE(x)  (LEDPORT ^= (x))            
10
11
#define PWMDDR      DDRB 
12
#define PWMPIN      3   
13
#define PWMVALUE     OCR1B
14
15
// Test Parameter
16
17
// Die Anzahl der Funktionsaufrufe
18
#define COUNT_INIT     100  
19
20
// Der Teiler bzw. Shift-Wert
21
#define TEILER      32
22
#define SHIFT       5  // = log2(TEILER)
23
24
// der zu unteruchende Datentyp
25
#define test_t       int32_t  
26
27
// Die testfunktion (1=Dummy, 2=Division, 3=Shift)
28
#define TESTFUN     2
29
30
31
#if (TESTFUN==1) 
32
test_t scale(test_t x, uint8_t y){ 
33
  
34
  return(x + y);     
35
}
36
#endif
37
38
#if (TESTFUN==2) 
39
test_t scale(test_t x, uint8_t y){ 
40
  
41
  return(x / (test_t) TEILER + y);     
42
}
43
#endif
44
45
#if (TESTFUN==3) 
46
test_t scale(test_t x, uint8_t y){ 
47
  
48
  if (x < 0)
49
     x += TEILER - 1;
50
  
51
  return((x >> (test_t) SHIFT) + y);
52
}
53
#endif
54
55
56
 
57
void init(){
58
59
  PWMDDR |= (1 << PWMPIN);          // Ausgangspins einschalten
60
  LEDDDR |= (1 << LEDGRUEN);
61
62
  while (!(PLLCSR & (1 << PLOCK)));       // Warten bis PLL gelockt
63
  PLLCSR |= (1 << PCKE);             // PLL-CK schalten 
64
  TCCR1B |= (1 << CS10);             // PCK ohne Presacler
65
  TCCR1A = ((1 << COM1B1) | (1 << PWM1B));   // PWM1B, OC1B cleared on CM, nOC1B n.c.
66
  OCR1C = 0xfa;                 // Timer immer voll durchlaufen lassen
67
}
68
69
70
int main(void){
71
      
72
  uint8_t counter = COUNT_INIT;
73
74
  // irgendein Long-Wert, erzeugt bei kleineren test-Variablen einen Compiler-Error  
75
  test_t temp   = 0xABCDEF01;        
76
77
  init();
78
79
  // loop forever
80
  for (;;) {                             
81
82
    counter--;
83
    temp = scale( temp , counter );         
84
    if ( counter == 0 ) {
85
      
86
      LEDTOGGLE( 1 << LEDGRUEN ); 
87
        counter    = COUNT_INIT;    
88
      
89
      // Ausgabe verhindert wegoptimieren durch den Compilers...
90
      PWMVALUE   = (uint8_t) temp;    
91
    }
92
  }     //End of loop forever  
93
}    // End of main