Advertisement
Guest User

Untitled

a guest
May 6th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. // Ringpuffer für den Timer Interrupt
  5. uint8_t nextPulseState[256];
  6. uint16_t ticksToNextInterrupt[256];
  7. uint8_t bufferPointer = 0;
  8.  
  9. // Variablen der 8 Kanaele
  10. uint8_t portFrequency[8];
  11.  
  12. ISR (TIMER1_COMPA_vect) {
  13.    
  14.     // Setze Ports
  15.     PORTD = nextPulseState[bufferPointer];
  16.     bufferPointer++;
  17.    
  18. }
  19.  
  20. void setBaseFrequencyDivident(uint16_t baseDivident) {
  21.     OCR1AH = (uint8_t) (baseDivident >> 8);
  22.     OCR1AL = (uint8_t) baseDivident;
  23. }
  24.  
  25. int main(void)
  26. {
  27.    
  28.     // PORTD fuer Pulse-Kanaele
  29.     DDRD = 0xFF;
  30.    
  31.     // Timer1 mit vollem CPU Takt und OCIA im CTC Modus
  32.     TCCR1B = (1 << WGM12) | (1 << CS10);
  33.     TIMSK1 = (1 << OCIE1A);
  34.     sei();
  35.    
  36.     // Setze default Frequenzbereich
  37.     setBaseFrequencyDivident(160); // 16MHz / 160 -> 100.000 Hz
  38.    
  39.     // Debuging
  40.     portFrequency[0] = 2;
  41.     portFrequency[1] = 2;
  42.     portFrequency[2] = 2;
  43.     portFrequency[3] = 2;
  44.     portFrequency[4] = 2;
  45.     portFrequency[5] = 2;
  46.    
  47.     while (1) {
  48.        
  49.         // Berechne werte fuer PROTD and the OCR
  50.         for (uint8_t i = 0; i < 256; i++) {
  51.             uint16_t bufferPosition = ((uint16_t) bufferPointer + i);
  52.             uint8_t nextState = 0;
  53.             nextState |= ((bufferPosition % portFrequency[0] == 0) << 0);
  54.             nextState |= ((bufferPosition % portFrequency[1] == 0) << 1);
  55.             nextState |= ((bufferPosition % portFrequency[2] == 0) << 2);
  56.             nextState |= ((bufferPosition % portFrequency[3] == 0) << 3);
  57.             nextState |= ((bufferPosition % portFrequency[4] == 0) << 4);
  58.             nextState |= ((bufferPosition % portFrequency[5] == 0) << 5);
  59.             nextState |= ((bufferPosition % portFrequency[6] == 0) << 6);
  60.             nextState |= ((bufferPosition % portFrequency[7] == 0) << 7);
  61.             nextPulseState[(uint8_t) bufferPosition] = nextState;
  62.         }
  63.        
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement