Code.c


1
/************************************************/
2
/*                         */
3
/*       C-Code Feuchtemesser Schaltung      */
4
/*                        */
5
/************************************************/
6
7
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#include <avr/wdt.h>
11
#include <stdint.h>
12
#include <math.h>
13
#include <avr/sleep.h>
14
15
#define LED_TOGGLE_CYCLE 1000
16
17
int main(void)
18
{
19
  unsigned long PassCount = 0;  // für Blinkfunktion
20
  unsigned char byToggle = 0;  
21
22
  /* Initialisierung Port */
23
  DDRA |= (1<<PA1);  // Pins PA1, PA2, PA3, PB1 auf Ausgang gesetzt
24
  DDRA |= (1<<PA2);  // PB2 für on/off Taster auf Eingang mit internem
25
  DDRA |= (1<<PA3);  // Pull-up Widerstand
26
  PORTA = (1<<PA1)|(1<<PA2)|(1<<PA3);   // alle LEDs aus
27
  DDRB &= ~(1<<PB2);  // LEDs auf GROUND daher Low-active
28
  DDRB |= (1<<PB1);  // LED für Batterieüberwachung
29
  PORTB = (1<<PORTB1)|(1<<PORTB2);  // Taster an PB2 mit Pull-UP, Bat-LED aus
30
  //PORTB = 0xFE;    // b 1111.1110;
31
32
  /* Watchdog Timer */
33
  wdt_disable();    // Watchdog Timer deaktiviert
34
35
  /* Initialisierung ADC */
36
  ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADATE);
37
  //ADCSRA = 0xE0;     // b 1110.0000
38
  /* ADC enable(1);ADSC (1);ADATE auto trigger(1);ADIF (0); ADIE no Interrupt(0);
39
  Division factor 2(000)*/
40
  ADCSRB &= ~((1<<ADLAR)|(1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0));
41
  // ADCL 8 Bits, ADCH 2 Bits; Free Running Mode
42
  //ADCSRB = 0x00;
43
  //ADMUX = 0x00;  // b 0000.0000; VCC als Referenz; Input ADC0 (PA0)
44
  
45
  /* internal masterclock (Zielwert: 1 MHz)*/
46
  // CLKPR = (1<<CLKPCE) | (1<<CLKPS1) | (1<<CLKPS0);
47
  // CLKPR = 0x83;    // b 1000.0011
48
  // Benutzter Teiler 8. defaulteinstellung 8 MHz.
49
  // nicht nötig da CKDIV8 gesetzt. ATtiny 24V läuft bereits mit 1 MHz
50
  
51
  /* Interrupts */
52
  sei();  // alle Interrupts möglich
53
54
  
55
  while(1)
56
57
  {
58
    PassCount ++;
59
    
60
    /* Spannungsmessung an VCC zur Batterieüberwachung */
61
    ADMUX = (1<<MUX5)|(1<<MUX0);  // MUX5...0 auf 1,1 V ext. Referenz
62
                    // Referenzspg. = VCC
63
    if(ADC <= 0x1D6)  /* 1D6h = 470d; bei VCC 2,4V entspricht 1,1V der 
64
              externen Referenz 470 "Stufen" im 10Bit ADC*/  
65
    {
66
      PORTB &= ~(1<<PB1);    // PB1 auf 0 - LED leuchtet
67
    }
68
    ADMUX &= ~((1<<MUX5)|(1<<MUX0));  // für Sensorwandlung auf ADC0(PA0)
69
70
71
    /* Tasterabfrage für Power-Down-Modus */
72
    // Aufruf über eigene Funktion, noch nicht realisiert
73
    // Aufwecken mit externem Interrupt
74
75
    //conversion start
76
    while(ADCSRA == 0xFF);
77
78
    if(ADC >= 0x96)    // rote LED
79
    // 96h = 150d
80
      {    
81
        if(PassCount % LED_TOGGLE_CYCLE == 0)
82
        {
83
          if(byToggle == 0)
84
          {
85
            // PORTA |= (1<<PA1);      
86
            PORTA = 0xFC;  // b 1111.1100
87
            byToggle = 1;
88
          }
89
          else
90
          {
91
            // PORTA &= ~(1<<PA1);
92
            PORTA = 0xFE;   // b 1111.1110
93
            byToggle = 0;
94
          }
95
        }
96
                  
97
      }
98
99
    else if(ADC > 0x32 && ADC < 0x96)  // gelbe LED
100
    // 96h = 150d, 32h = 50d     (ADC > 0x48 && ADC < 0xC8)
101
      {
102
        // PORTA &= ~(1<<PA2);
103
        PORTA = 0xFA;        // b 1111.1010
104
      }
105
106
    else if(ADC <= 0x32)        // grüne LED
107
    // 32h = 50d
108
      {
109
        // PORTA &= ~(1<<PA3);
110
        PORTA = 0xF6;        // b 1111.0110
111
      }
112
  
113
  }
114
115
  return 0;
116
}