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       uint8_t  
26
27
28
/* gewünschte Funktion auskommentieren...
29
30
31
test_t scale(test_t x, uint8_t y){ 
32
  
33
  return(x + y);     
34
}
35
36
37
test_t scale(test_t x, uint8_t y){ 
38
  
39
  return(x / (test_t) TEILER + y);     
40
}
41
42
43
test_t scale(test_t x, uint8_t y){ 
44
  
45
  x = (x >> (test_t) SHIFT);
46
47
  if (x < 0)
48
    return(++x + y);
49
  else
50
    return(x + y);
51
}
52
*/
53
54
 
55
void init(){
56
57
  PWMDDR |= (1 << PWMPIN);          // Ausgangspins einschalten
58
  LEDDDR |= (1 << LEDGRUEN);
59
60
  while (!(PLLCSR & (1 << PLOCK)));       // Warten bis PLL gelockt
61
  PLLCSR |= (1 << PCKE);             // PLL-CK schalten 
62
  TCCR1B |= (1 << CS10);             // PCK ohne Presacler
63
  TCCR1A = ((1 << COM1B1) | (1 << PWM1B));   // PWM1B, OC1B cleared on CM, nOC1B n.c.
64
  OCR1C = 0xfa;                 // Timer immer voll durchlaufen lassen
65
}
66
67
68
int main(void){
69
      
70
  uint8_t counter = COUNT_INIT;
71
72
  // irgendein Long-Wert, erzeugt bei kleineren test-Variablen einen Compiler-Error  
73
  test_t temp   = 0xABCDEF01;        
74
75
  init();
76
77
  // loop forever
78
  for (;;) {                             
79
80
    counter--;
81
    temp = scale( temp , counter );         
82
    if ( counter == 0 ) {
83
      
84
      LEDTOGGLE( 1 << LEDGRUEN ); 
85
        counter    = COUNT_INIT;    
86
      
87
      // Ausgabe verhindert wegoptimieren durch den Compilers...
88
      PWMVALUE   = (uint8_t) temp;    
89
    }
90
  }     //End of loop forever  
91
}    // End of main