Advertisement
Guest User

ATtiny45 I2C Summensignal

a guest
Jun 19th, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 18.06.2011
  5.  *      Author: peka
  6.  */
  7.  
  8. //#define F_CPU = 20MHz //(ext Crystal)
  9.  
  10. // Defines
  11. #define setBit(ADDRESS, BIT)    (ADDRESS |=  (1<<BIT))
  12. #define clrBit(ADDRESS, BIT)    (ADDRESS &=  ~(1<<BIT))
  13. #define tglBit(ADDRESS, BIT)    (ADDRESS ^=  (1<<BIT))
  14. #define getBit(ADDRESS, BIT)    ((ADDRESS & (1<<BIT))?1:0)
  15. #define lowByte(WORD)           ((uint8_t)(WORD))
  16. #define highByte(WORD)          ((uint8_t)(((uint16_t)(WORD)) >> 8))
  17. #define getWord(HIGH,LOW)       (((uint16_t)HIGH << 8) | LOW)
  18.  
  19. #define TWI_ADDR                20 <<1    // --> Address 20
  20. // Includes
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include "usiTwiSlave.h" //I2C Lib
  24. // Global Vars
  25. volatile uint16_t timerCounter = 0;
  26. volatile uint16_t pulseTimer = 0;
  27. volatile uint8_t servoIndex = 0;
  28. volatile uint16_t servoPos0 = 0;
  29. volatile uint16_t servoPos1 = 0;
  30. volatile uint16_t servoPos2 = 0;
  31. volatile uint16_t servoPos3 = 0;
  32. volatile uint16_t servoPos4 = 0;
  33. volatile uint16_t servoPos5 = 0;
  34. volatile uint16_t servoPos6 = 0;
  35. volatile uint16_t servoPos7 = 0;
  36. volatile uint16_t syncTime = 0;
  37. volatile uint8_t updateFlag = 0;
  38.  
  39. // Main Program
  40. int main(void)
  41. {
  42.     // Timer 1
  43.     setBit(TCCR1, CS10); // Prescaler 16 - 800ns Auflösung
  44.     setBit(TCCR1, CS12);
  45.     setBit(TIMSK, OCIE1A); // Timer/Counter1, Output Compare A Match Interrupt Enable
  46.     OCR1A = 250; // 200µs
  47.  
  48.     // RCRx Interrupt
  49.     clrBit(DDRB, PB1); //Input
  50.     setBit(PORTB, PB1); //int. Pullup
  51.     setBit(GIMSK, PCIE); //Pin Change Interrupt Enable
  52.     PCMSK = 0x00;
  53.     setBit(PCMSK, PCINT1); //Enable PCINT only on PB1
  54.  
  55.     // TWI
  56.     usiTwiSlaveInit(TWI_ADDR); // TWI starten
  57.     for (int i = 0; i < 24; i++) // Buffer füllen
  58.         txbuffer[i] = 0xFF;
  59.  
  60.     // Interrupts aktivieren
  61.     sei();
  62.  
  63.     // Hauptprogrammschleife
  64.     while (1)
  65.     {
  66.         while (updateFlag == 0)
  67.         {
  68.             ; //warte auf neue servoPosition
  69.         }
  70.  
  71.         txbuffer[0] = 0; //dataReady Flag
  72.         txbuffer[1] = highByte(servoPos0);
  73.         txbuffer[2] = lowByte(servoPos0);
  74.         txbuffer[3] = highByte(servoPos1);
  75.         txbuffer[4] = lowByte(servoPos1);
  76.         txbuffer[5] = highByte(servoPos2);
  77.         txbuffer[6] = lowByte(servoPos2);
  78.         txbuffer[7] = highByte(servoPos3);
  79.         txbuffer[8] = lowByte(servoPos3);
  80.         txbuffer[9] = highByte(servoPos4);
  81.         txbuffer[10] = lowByte(servoPos4);
  82.         txbuffer[11] = highByte(servoPos5);
  83.         txbuffer[12] = lowByte(servoPos5);
  84.         txbuffer[13] = highByte(servoPos6);
  85.         txbuffer[14] = lowByte(servoPos6);
  86.         txbuffer[15] = highByte(servoPos7);
  87.         txbuffer[16] = lowByte(servoPos7);
  88.         txbuffer[17] = highByte(syncTime);
  89.         txbuffer[18] = lowByte(syncTime);
  90.         txbuffer[0] = 1; //dataReady Flag
  91.  
  92.         updateFlag = 0;
  93.     }
  94. }
  95.  
  96. ISR( TIMER1_COMPA_vect )
  97. {
  98.     TCNT1 = 0;
  99.     pulseTimer++; // 1 tick = 200µs
  100. }
  101.  
  102. ISR( PCINT0_vect )
  103. {
  104.  
  105.     if (getBit(PINB, PB1) == 0) //fallende Flanke
  106.     {
  107.         uint16_t pulseTimerFraction = 5 * TCNT1 / 4;
  108.         uint16_t pulseTime = pulseTimer * 200 + pulseTimerFraction; // 1 tick = 1µs
  109.  
  110.         TCNT1 = 0; // timer zurücksetzen
  111.         pulseTimer = 0;
  112.  
  113.         if (pulseTime > 3000) //Pulspause > 3000µs --> Sync
  114.         {
  115.             servoIndex = 0; //nächster Puls wird wieder die länge von Servo0 angeben
  116.             syncTime = pulseTime;
  117.             updateFlag = 1; //servowerte dürfen in der hauptschleife ins i2c buffer geschrieben werden
  118.         }
  119.         else // if (pulseTime > 500) //filtere mini-spikes
  120.         {
  121.             if (servoIndex == 0)
  122.                 servoPos0 = pulseTime;
  123.             else if (servoIndex == 1)
  124.                 servoPos1 = pulseTime;
  125.             else if (servoIndex == 2)
  126.                 servoPos2 = pulseTime;
  127.             else if (servoIndex == 3)
  128.                 servoPos3 = pulseTime;
  129.             else if (servoIndex == 4)
  130.                 servoPos4 = pulseTime;
  131.             else if (servoIndex == 5)
  132.                 servoPos5 = pulseTime;
  133.             else if (servoIndex == 6)
  134.                 servoPos6 = pulseTime;
  135.             else if (servoIndex == 7)
  136.                 servoPos7 = pulseTime;
  137.  
  138.             servoIndex++; //nächster Puls wird nächsten Servowert eben oder sync sein.
  139.         }
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement