Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////
- //
- // AS-MLV_P VOC Sensor
- //
- // Test program
- //
- // (c) 2014 by Koepi
- //
- ///////////////////////////////////////////////////////
- #include <stdio.h>
- #define heater 0b00000010 // Heater VCC on A1 (PinC1)
- #define sensor 0b00000001 // Sensor VCC/Input on A0 (PinC0)
- #define GREEN 0b00001000 // RGB LED green
- #define BLUE 0b00100000 // RGB LED blue
- #define RED 0b00010000 // RGB LED red
- volatile uint16_t sensor_val = 0;
- uint32_t sensor_ovs = 0;
- volatile uint32_t timer = 0;
- volatile uint16_t milliseconds = 0;
- volatile bool sent = false;
- volatile bool measured = false;
- volatile int temperature=0;
- volatile uint8_t ms_old = 0;
- ISR(TIMER0_COMPA_vect)
- {
- milliseconds++;
- if (milliseconds == 1001) {
- milliseconds = 0;
- timer++;
- }
- if (milliseconds & 0x04) { // PWM duty 12,5% for LED - switch on every 4th uneven ms.
- if (sensor_val<200) {
- PORTD |= GREEN;
- } else if (sensor_val<500) {
- PORTD |= BLUE;
- } else if (sensor_val>=500) {
- PORTD |= RED;
- }
- } else PORTD&=~(RED|GREEN|BLUE); // switch all LEDs off again. It's PWM! ;)
- }
- void setup() {
- // setup the sensor
- DDRC |= heater; // Heater is OUTPUT
- PORTC &= ~(heater); // Heater is OFF
- DDRC &= ~(sensor); // Sensor is INPUT
- PORTC &= ~(sensor); // Disable PullUp
- // ADC setup
- ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //Prescaler at 128 -> 125Khz ADC clock source
- ADMUX |= (1<<REFS1); // Avcc(+5v) as voltage reference
- ADMUX |= 0; // Pin A0 -> No change in MUX register...
- ADCSRA |= (1<<ADEN); //Power up the ADC
- ADCSRA |= (1<<ADSC); //Start converting
- DIDR0 = (1 << ADC0D) | (1 << ADC1D); // Disable Digital Input Buffer on Pin C0/C1
- // setup the LED
- DDRD |= RED | GREEN | BLUE; // RGB LED is OUTPUT
- // setup timer for LED PWM
- cli(); // disable global interrupts
- TCNT0 = 0; // reset Timer2 counter value
- TCCR0A = (1 << WGM01); // CTC mode
- TCCR0B = (1 << CS02) | (1 << CS00); // Prescaler 1024
- OCR0A = 15; // 1000 Hz, Prescaler 1024 -> F_CPU / 1024 (Prescaler) /1000 (Hz) -1 -> 15,625Hz
- TIMSK0 = (1<<OCIE0A); // enable comapre-match OCR2A interrupt
- sei(); // start global interrupts
- // for monitoring
- Serial.begin(57600);
- }
- void loop() {
- char text[16];
- if ((milliseconds >= 100) && (milliseconds <=(127 - (temperature/4)))) { // heat up the sensor / temperature compensated
- if (milliseconds & 0x01) PORTC |= heater; // turn on heating
- else PORTC &= ~(heater); // turn off heating
- }
- if (milliseconds == 130) PORTC &= ~(heater); // turn off heating for sure
- if (milliseconds == 200) { // measure the sensor
- for (int i=0; i<4; i++) { // averaging
- ADCSRA |= (1<<ADSC); // Start a new conversion
- while(ADCSRA & (1<<ADSC)); // Wait for the conversion to finish
- sensor_ovs += ADC ;
- }
- sensor_val = (sensor_ovs >> 2);
- sensor_ovs=0;
- sent = false;
- }
- if (milliseconds == 250) { // print out results via serial console
- if (!sent) {
- sprintf(text, "$1;1;");
- Serial.print(text);
- sprintf(text, "%u;",timer);
- Serial.print(text);
- sprintf(text, "%u;",sensor_val);
- Serial.print(text);
- sprintf(text, "%u;",temperature);
- Serial.print(text);
- sprintf(text, "0");
- Serial.println(text);
- sent = true;
- }
- }
- if (milliseconds == 690) { // switch ADC Mux to Pin A1 & Heater -> INPUT; stabelizing for some ms
- if (!measured) { // only run once per second ;)
- measured=true;
- // make heater INPUT
- DDRC &= ~(heater); // Heater is INPUT
- PORTC |= (heater); // Heater uses PullUp for measurement
- // switch ADC Mux to Pin A1 and measure
- ADMUX |= (1 << MUX0); // Pin A1
- ADCSRA |= (1<<ADSC); //Start converting
- } // of if measured
- }
- if (milliseconds==699) measured=false;
- if (milliseconds == 700) { // measure resistance of heater
- if (!measured) { // only run once per second ;)
- measured=true;
- for (int i=0; i<4; i++) { // averaging
- ADCSRA |= (1<<ADSC); //Start converting
- while(ADCSRA & (1<<ADSC)); // Wait for the conversion to finish
- temperature+=(ADC-62);
- }
- temperature >>= 2;
- } // of if measured
- }
- if (milliseconds==702) measured=false;
- if (milliseconds==703) { // switch back to Heater mode and Pin C0 measurement
- if (!measured) { // only run once per second ;)
- measured=true;
- // switch ADC Mux back to Pin A0
- ADMUX &= ~(1 << MUX0); // Disable Pin C1, return to Pin C0
- ADCSRA |= (1<<ADSC); //Start converting
- // restore heater as OUTPUT
- PORTC &= ~(heater); // Heater disable PullUp
- DDRC |= heater; // Heater is OUTPUT
- PORTC &= ~(heater); // Heater is OFF
- } // of if measured
- }
- if (milliseconds==705) measured=false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement