Advertisement
Guest User

Untitled

a guest
May 21st, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /*
  2. Temperaturfuehler PORT A0/AD0 0V=0 Grad C
  3. 5V=100 Grad C
  4. Drucksensor PORT A1/AD1 0V= 0 hPa
  5. 5V = 0,25 hPa
  6. Heizung Heizmittel PORT C0 Wenn 1, dann EIN
  7. Heizung ok Meldung PORT C1 Wenn 1, dann Heizmitteltemperatur ok
  8. Ausgang A0.3 (Pumpe P3) PORT A2 Wenn 1, dann soll die Heizung geregelt werden,
  9. Wenn 0, dann Heizung aus
  10.  
  11. Fluegelradzaehler Band 1 T0 10 Impulse pro l
  12. Fluegelradzaehler Band 2 T1 10 Impulse pro l
  13. Fluegelradzaehler Band 3 INT0 10 Impulse pro l
  14.  
  15. Vollmeldung Band 1 PORT C2 nach E0.4. der SPS
  16. Vollmeldung Band 2 PORT C3 nach E0.5 der SPS
  17. Vollmeldung Band 3 PORT C4 nach E0.6 der SPS
  18.  
  19. ACHTUNG: Project -> Config. Options -> Libraries -> libm.a hinzufuegen (fuer log())
  20. */
  21.  
  22. #include <avr/io.h>
  23. #include <avr/interrupt.h>
  24. #include <math.h>
  25.  
  26. uint16_t getAD(uint8_t);
  27. void IOinit();
  28.  
  29. volatile uint8_t anzahlT0;
  30. volatile uint16_t anzahlint0;
  31.  
  32.  
  33. int main(void)
  34. {
  35. float druck, solltemp, isttemp;
  36.  
  37. IOinit();
  38.  
  39. while(1) {
  40. druck = (float)getAD(1)/1023.0*0.25; // Druck in hPa
  41.  
  42. solltemp = 243.12/(17.62/log(druck/6.112)-1.0) + 5.0; // Solltemperatur in Celsius
  43.  
  44. isttemp = (float)getAD(0)/1023.0*100.0; // Isttemperatur in Celsius
  45.  
  46. // Zweipunktregelung
  47. if((PINA & (1 << PA2)) != 0) {
  48. if(isttemp < (solltemp - 2.0)) {
  49. PORTC |= (1 << PC0); // Heizug ein
  50. }
  51.  
  52. if(isttemp > (solltemp + 2.0)) {
  53. PORTC &= ~(1 << PC0); // Heizung aus
  54. }
  55. } else {
  56. PORTC &= ~(1 << PC0);
  57. }
  58.  
  59. if((isttemp < (solltemp + 2.0)) && (isttemp > (solltemp - 2.0))) {
  60. PORTC |= (1 << PC1); // wenn Temperatur ok
  61. }
  62. }
  63. return 0;
  64. }
  65.  
  66. ISR(TIMER0_COMP_vect)
  67. {
  68. if(anzahlT0 < 5) {
  69. anzahlT0++;
  70. } else {
  71. PORTC &= ~(1 << PC2); // SPS soll Ventil Y1 fuer Band 1 schliessen
  72. while((PORTC & (1 << PC5)) == 0); // Warten auf 1 von SPS
  73. anzahlT0=0;
  74. }
  75. }
  76.  
  77. ISR(TIMER1_COMPA_vect)
  78. {
  79. PORTC &= ~(1 << PC3); // SPS soll Ventil Y2 fuer Band 2 schliessen
  80. while((PORTC & (1 << PC6)) == 0); // Warten auf 1 von SPS
  81. }
  82.  
  83. ISR(INT0_vect)
  84. {
  85. if(anzahlint0 < 1000) {
  86. anzahlint0++;
  87. } else {
  88. PORTC &= ~(1 << PC4); // SPS soll Ventil Y3 fuer Band 3 schliessen
  89. while((PORTC & (1 << PC7)) == 0); // Warten auf 1 von SPS
  90. anzahlint0 = 0;
  91. }
  92. }
  93.  
  94. void IOinit() {
  95.  
  96. // Timer 0 Behaelter voll nach 5 Interrupts zu je 200 Impulsen (10 Impulse/l)
  97. TCNT0 = 0; // Zaehlerstand Null
  98. TCCR0 = (1<< WGM01) | (1 << CS00) | (1 << CS01) | (1 << CS02); // CTC Mode. positive Flanke
  99. // an T0 zaehlt aufwaerts
  100. OCR0=200; // nach 5 Interrupts werden die 100l erreich
  101. TIMSK |= (1 << OCIE0); // Intterupt fuer Timer0 wird eingeschaltet
  102. anzahlT0 = 0;
  103.  
  104. // Timer1 Interrupt bei 1000 Impulsen
  105. TCNT1 = 0;
  106. TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS11) | (1 << CS10); // CTC-Mode Zaehler zaehlt
  107. // per positiver Flanke an T1
  108. OCR1A = 1000;
  109. TIMSK |= (1 << OCIE1A); // Interrupt frei
  110.  
  111. // INT0 Interrupt bei 1000 Impulse
  112. MCUCR = (1 << ISC01) | (1 << ISC00); // positive Flanke an INT0 erzeugt Interrupt
  113. anzahlint0 = 0;
  114.  
  115. DDRC = (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4); // 5 Bits sind Ausgang
  116.  
  117. DDRD = 0x00;
  118. DDRB = 0x00;
  119. DDRA = 0x00;
  120. PORTB = 0xFF;
  121. PORTD = 0xFF;
  122.  
  123. sei(); // Interrupts global erlauben
  124. }
  125.  
  126. uint16_t getAD(uint8_t kanal)
  127. {
  128. unsigned int retwert;
  129.  
  130. ADMUX = kanal & 0x07;
  131. ADCSRA |= (1 << ADEN) // ADC enabled
  132. | (1 << ADSC) // Start conversion
  133. | (1 << ADPS2)
  134. | (1 << ADPS1); // Teilfaktor 64: Wandelfrequenz von 125kHz bei 8 MHZ
  135.  
  136. while((ADCSRA & (1 << ADSC)) != 0); // warte auf End of Conversion
  137.  
  138. retwert = ADCW;
  139.  
  140. return retwert; // Zurueck mit den 10 Bit
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement