Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Temperaturfuehler PORT A0/AD0 0V=0 Grad C
- 5V=100 Grad C
- Drucksensor PORT A1/AD1 0V= 0 hPa
- 5V = 0,25 hPa
- Heizung Heizmittel PORT C0 Wenn 1, dann EIN
- Heizung ok Meldung PORT C1 Wenn 1, dann Heizmitteltemperatur ok
- Ausgang A0.3 (Pumpe P3) PORT A2 Wenn 1, dann soll die Heizung geregelt werden,
- Wenn 0, dann Heizung aus
- Fluegelradzaehler Band 1 T0 10 Impulse pro l
- Fluegelradzaehler Band 2 T1 10 Impulse pro l
- Fluegelradzaehler Band 3 INT0 10 Impulse pro l
- Vollmeldung Band 1 PORT C2 nach E0.4. der SPS
- Vollmeldung Band 2 PORT C3 nach E0.5 der SPS
- Vollmeldung Band 3 PORT C4 nach E0.6 der SPS
- ACHTUNG: Project -> Config. Options -> Libraries -> libm.a hinzufuegen (fuer log())
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <math.h>
- uint16_t getAD(uint8_t);
- void IOinit();
- volatile uint8_t anzahlT0;
- volatile uint16_t anzahlint0;
- int main(void)
- {
- float druck, solltemp, isttemp;
- IOinit();
- while(1) {
- druck = (float)getAD(1)/1023.0*0.25; // Druck in hPa
- solltemp = 243.12/(17.62/log(druck/6.112)-1.0) + 5.0; // Solltemperatur in Celsius
- isttemp = (float)getAD(0)/1023.0*100.0; // Isttemperatur in Celsius
- // Zweipunktregelung
- if((PINA & (1 << PA2)) != 0) {
- if(isttemp < (solltemp - 2.0)) {
- PORTC |= (1 << PC0); // Heizug ein
- }
- if(isttemp > (solltemp + 2.0)) {
- PORTC &= ~(1 << PC0); // Heizung aus
- }
- } else {
- PORTC &= ~(1 << PC0);
- }
- if((isttemp < (solltemp + 2.0)) && (isttemp > (solltemp - 2.0))) {
- PORTC |= (1 << PC1); // wenn Temperatur ok
- }
- }
- return 0;
- }
- ISR(TIMER0_COMP_vect)
- {
- if(anzahlT0 < 5) {
- anzahlT0++;
- } else {
- PORTC &= ~(1 << PC2); // SPS soll Ventil Y1 fuer Band 1 schliessen
- while((PORTC & (1 << PC5)) == 0); // Warten auf 1 von SPS
- anzahlT0=0;
- }
- }
- ISR(TIMER1_COMPA_vect)
- {
- PORTC &= ~(1 << PC3); // SPS soll Ventil Y2 fuer Band 2 schliessen
- while((PORTC & (1 << PC6)) == 0); // Warten auf 1 von SPS
- }
- ISR(INT0_vect)
- {
- if(anzahlint0 < 1000) {
- anzahlint0++;
- } else {
- PORTC &= ~(1 << PC4); // SPS soll Ventil Y3 fuer Band 3 schliessen
- while((PORTC & (1 << PC7)) == 0); // Warten auf 1 von SPS
- anzahlint0 = 0;
- }
- }
- void IOinit() {
- // Timer 0 Behaelter voll nach 5 Interrupts zu je 200 Impulsen (10 Impulse/l)
- TCNT0 = 0; // Zaehlerstand Null
- TCCR0 = (1<< WGM01) | (1 << CS00) | (1 << CS01) | (1 << CS02); // CTC Mode. positive Flanke
- // an T0 zaehlt aufwaerts
- OCR0=200; // nach 5 Interrupts werden die 100l erreich
- TIMSK |= (1 << OCIE0); // Intterupt fuer Timer0 wird eingeschaltet
- anzahlT0 = 0;
- // Timer1 Interrupt bei 1000 Impulsen
- TCNT1 = 0;
- TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS11) | (1 << CS10); // CTC-Mode Zaehler zaehlt
- // per positiver Flanke an T1
- OCR1A = 1000;
- TIMSK |= (1 << OCIE1A); // Interrupt frei
- // INT0 Interrupt bei 1000 Impulse
- MCUCR = (1 << ISC01) | (1 << ISC00); // positive Flanke an INT0 erzeugt Interrupt
- anzahlint0 = 0;
- DDRC = (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4); // 5 Bits sind Ausgang
- DDRD = 0x00;
- DDRB = 0x00;
- DDRA = 0x00;
- PORTB = 0xFF;
- PORTD = 0xFF;
- sei(); // Interrupts global erlauben
- }
- uint16_t getAD(uint8_t kanal)
- {
- unsigned int retwert;
- ADMUX = kanal & 0x07;
- ADCSRA |= (1 << ADEN) // ADC enabled
- | (1 << ADSC) // Start conversion
- | (1 << ADPS2)
- | (1 << ADPS1); // Teilfaktor 64: Wandelfrequenz von 125kHz bei 8 MHZ
- while((ADCSRA & (1 << ADSC)) != 0); // warte auf End of Conversion
- retwert = ADCW;
- return retwert; // Zurueck mit den 10 Bit
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement