root/flukso/trunk/uc/calibration.c

Revision 7, 4.4 KB (checked in by icarus75, 3 years ago)

all: cleaned-up $Id$ keywords

  • Property svn:keywords set to Id
Line 
1// Copyright (c) 2008 jokamajo.org
2// $Id$
3
4// define section, move to main.h later on
5
6// macro's
7#ifndef __AVR_ATmega168__
8        #define __AVR_ATmega168__
9#endif
10
11#define METER0 "cccccccccccccccccccccccccccccccc"
12#define METER1 "dddddddddddddddddddddddddddddddd"
13#define METER2 "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
14#define METER3 "ffffffffffffffffffffffffffffffff"
15
16#define START 0
17#define END3 0xffffffff
18#define END2 0xeeeeeeee
19#define END1 0xdddddddd
20#define END0 0xcccccccc
21
22// pin definitions
23#define METER0PIN 2
24#define METER1PIN 3
25#define METER2PIN 4
26#define METER3PIN 9
27
28#define POTPIN0 6
29#define POTPIN1 7
30#define POTPIN2 8
31
32#define LEDPIN 13
33
34// end of define
35
36
37// pin/register/ISR definitions
38#include <avr/interrupt.h>
39
40// eeprom library
41#include <avr/eeprom.h>
42
43// watchdog timer library
44#include <avr/wdt.h>
45
46// variable declarations
47uint16_t i;
48
49typedef struct {
50  boolean pulse0;
51  boolean toggle0;
52  boolean pulse1;
53  boolean toggle1;
54  boolean pulse2;
55  boolean toggle2;
56  boolean pulse3;
57  boolean toggle3;
58} struct_aux;
59
60volatile struct_aux aux = {false, false, false, false, false, false, false, false};
61
62
63typedef struct {
64 char meter[513]; //don't forget to reserve a byte for a terminating NULL
65} struct_meas;
66
67volatile struct_meas EEMEM EEPROM_measurements;
68volatile struct_meas measurements;
69
70
71// interrupt service routine for analog comparator
72ISR(ANALOG_COMP_vect) {
73  digitalWrite(LEDPIN, HIGH);   // sets the LED on
74  UCSR0B &= ~((1<<RXEN0) | (1<<TXEN0)); //disable UART Tx and Rx
75  ADCSRA &= ~(1<<ADEN); //disable ADC
76  ACSR |= (1<<ACD); //disable AC
77
78//  PRR |= (1<<PRUSART0) | (1<<PRADC);
79  eeprom_write_block((const void*)&measurements, (void*)&EEPROM_measurements, sizeof(measurements));
80}
81
82// interrupt service routine for watchdog timeout
83ISR(WDT_vect) {
84}
85
86// disable WDT
87void WDT_off(void) {
88  cli();
89  wdt_reset();
90  // clear the WDT reset flag in the status register
91  MCUSR &= ~(1<<WDRF);
92  // timed sequence to be able to change the WDT settings afterwards
93  WDTCSR |= (1<<WDCE) | (1<<WDE);
94  // disable WDT
95  WDTCSR = 0x00;
96}
97
98// enable WDT
99void WDT_on(void) {
100  // enable the watchdog timer (1s)
101  wdt_enable(WDTO_1S);
102  // set watchdog interrupt enable flag
103  WDTCSR |= (1<<WDIE);
104}
105
106void setup()
107{
108  // WDT_off(); -> moved the call to this function to start of the main loop, before init
109
110  // clock settings: divide by 8 to get a 1Mhz clock, allows us to set the BOD level to 1.8V (DS p.37)
111  CLKPR = (1<<CLKPCE);
112  CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
113
114  // load meterid's and metervalues from EEPROM
115//  eeprom_read_block((void*)&measurements, (const void*)&EEPROM_measurements, sizeof(measurements));
116
117  // init serial port
118  Serial.begin(4800);
119  delay(100);
120
121  pinMode(LEDPIN, OUTPUT);
122
123  // DP2=INT0 configuration
124  pinMode(METER0PIN, INPUT);
125  // turn on the internal 20k pull-up resistor
126  digitalWrite(METER0PIN, HIGH);
127  // set INT0 (=METER0PIN) active LOW interrupt
128
129  // DP3=INT1 configuration
130  pinMode(METER1PIN, INPUT);
131  // turn on the internal 20k pull-up resistor
132  digitalWrite(METER1PIN, HIGH);
133  // set INT1 (=METER1PIN) active LOW interrupt
134
135  // PD4=PCINT20 configuration
136  pinMode(METER2PIN, INPUT);
137  // turn on the internal 20k pull-up resistor
138  digitalWrite(METER2PIN, HIGH);
139  //enable pin change interrupt on PCINT20
140  PCMSK2 |= (1<<PCINT20);
141  //pin change interrupt enable 2
142  PCICR |= (1<<PCIE2);
143
144  // PB1=PCINT1 configuration
145  pinMode(METER3PIN, INPUT);
146  // turn on the internal 20k pull-up resistor
147  digitalWrite(METER3PIN, HIGH);
148  //enable pin change interrupt on PCINT1
149  PCMSK0 |= (1<<PCINT1);
150  //pin change interrupt enable 0
151  PCICR |= (1<<PCIE0);
152
153  // analog comparator setup for brown-out detection
154  // DP6=Vcc+R20k configuration
155  pinMode(POTPIN0, INPUT);
156  // turn on the internal 20k pull-up resistor
157  digitalWrite(POTPIN0, HIGH);
158  // DP7=AIN1 just configure as input to obtain high impedance
159  pinMode(POTPIN1, INPUT);
160  // DP8=GND configuration + connect the DP8 pin to GND
161  pinMode(POTPIN2, INPUT);
162  // comparing AIN1 (Vcc/4.4) to bandgap reference (1.1V)
163  // bandgap select | AC interrupt enable | AC interrupt on rising edge (DS p.243)
164  ACSR |= (1<<ACBG) | (1<<ACIE) | (1<<ACIS1) | (1<<ACIS0);
165
166  WDT_on();
167
168  //set global interrupt enable in SREG to 1 (DS p.12)
169  sei();
170
171  for(i=0; i<sizeof(measurements.meter); i++)
172    measurements.meter[i] = 0x12;
173}
174
175
176void loop()
177{
178  // reset the watchdog timer
179  wdt_reset();
180  Serial.println("msg testing the integrity of the UART interface");
181  delay(500);
182}
Note: See TracBrowser for help on using the browser.