NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Switching from ADMUX=0 to ADMUX=5

April 17, 2012
by jasongillikin
jasongillikin's Avatar

So, I am making a differential temperature controller, and I have one temp sensor set to ADMUX=5 and the other to ADMUX=0. I get the ADC reading from both ports, but then I take just one of the values and convert it into degrees, then display the degrees in binary. just to test and make sure each sensor is working independantly.

However, it seems to be reading from both ADC ports... because even though I pass just one of the sensor's readings into the method to turn on the lights, it reads from both

How do I switch from port to port.

here is my code,

thanks,

jason

// tempsensor.c
// for NerdKits with ATmega168
// mrobbins@mit.edu

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
//
// PC0 -- temperature sensor analog input

//Motor Stuff
void motorOn(){
//Pin =PB5
//
DDRB|= (1<<DDB5);
PORTB|= (1<<PB5);
//DELAY 1 MINUTE (60000 MICROSECONDS)
delay_ms (120000L);
PORTB &= (0<<PB5);

}
// convert steps to degrees
int16_t convertDegreeF(int16_t step){
 int16_t degF= (step-524)*.9;

 return degF;
    // turn all ports to outpu on d and b

}

void LEDTest(){
    DDRD=255;
    DDRB=255;
    int i=0;
    for(i=0;i<2;i++){
    //turn on all lights for .25 secs

    PORTB|=(1<<PB4);
      delay_ms (50L);
       PORTB&=(0<<PB4);

    PORTB|=(1<<PB3);
      delay_ms(50L);
       PORTB&=(0<<PB3);

    PORTD|=(1<<PD2);
      delay_ms (50L);
       PORTD&=(0<<PD2);

    PORTD|=(1<<PD3);
      delay_ms (50L);
       PORTD&=(0<<PD3);

    PORTD|=(1<<PD4);
      delay_ms (50L);
       PORTD&=(0<<PD4);

    PORTD|=(1<<PD5);
      delay_ms (50L);
       PORTD&=(0<<PD5);

    PORTD|=(1<<PD6);
      delay_ms (50L);
       PORTD&=(0<<PD6);
    }
DDRD=0;
    DDRB=0;
}

void lightsOn(int16_t   temp){

 // max temp= 127
 // ports to use
/*
    bool L0=false;
     bool L1=false;
      bool L2=false;
       bool L3=false;
        bool L4=false;
         bool L5=false;
         bool L6=false;
bool mybool;
mybool=false;

*/
int16_t temp2=temp;

PORTD=0;
PORTB=0;
    DDRD=255;
DDRB=255;
         if((temp2-64)>0){
           PORTB|=(1<<PB4);
            temp2=temp2-64;}

        if((temp2-32)>0){
             PORTB|=(1<<PB3);;temp2=temp2-32;}

        if((temp2-16)>0){
           PORTD|=(1<<PD2);;temp2=temp2-16;}

        if((temp2-8)>0){
            PORTD|=(1<<PD3);;temp2=temp2-8;}

        if((temp2-4)>0){
            PORTD|=(1<<PD4);;temp2=temp2-4;}

        if((temp2-2)>0){
           PORTD|=(1<<PD5);;temp2=temp2-2;}

        if((temp2-1)>0){
             PORTD|=(1<<PD6);;temp2=temp2-1;}

              // turn all ports to output on d and b

delay_ms (50L);

// turn off lights
PORTD=0;
PORTB=0;
delay_ms (10L);

}
//COLLECTOR STUFF

 void collectorAdcInit() {
//set ADC for external reference (5v) single ended...
//ADLAR =1 sets 8Bit read ADCH.
//MUX=0 means ADC0 pin is on
//MUX=1 means ADC1 pin is on
ADMUX=5;

// set ADC to be enable with a clock prescale of 1/128
// so the ADC clock runs @ 115.2kHz
ADCSRA|= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1)  | (1<<ADPS0);
ADCSRA |= (1<<ADSC);

}

uint16_t collectorAdc_Read() {
//read from ADC, waiting for conversion to finish , and wait
//for it to be cleared
ADMUX=5;
delay_ms(50L);
int i=0;
uint16_t collectorResult=0;
uint16_t collectorTemp=0;
uint16_t accumulator=0;
// read ADCL and ADCH Because we are using 10 Bit ADC
// must shift ADCH to left 8 bit so it doesn't write over
// ADCL.
// Also, ADCL Must be read first (see pg 259 of datasheet)
for (i=0;i<100;i++)
{
while(ADCSRA & (1<<ADSC)) {
    // do nothing... just hold your breath.
  };
collectorResult=ADCL;
collectorTemp=ADCH;
collectorResult= (collectorResult+(collectorTemp<<8));

accumulator=(accumulator+collectorResult);

}

collectorResult= accumulator/i;

//test//

ADMUX=0;

return collectorResult;
}

//
//
// Storage
//
//

void storageAdcInit() {
//set ADC for external reference (5v) single ended...
//ADLAR =1 sets 8Bit read ADCH.
//MUX=0 means ADC0 pin is on
//MUX=1 means ADC1 pin is on
ADMUX =0;

// set ADC to be enable with a clock prescale of 1/128
// so the ADC clock runs @ 115.2kHz
ADCSRA|= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1)  | (1<<ADPS0);
ADCSRA |= (1<<ADSC);

}

int16_t storageAdc_Read() {
//read from ADC, waiting for conversion to finish , and wait
//for it to be cleared

ADMUX=0;
delay_ms(200L);
// read ADCL and ADCH Because we are using 10 Bit ADC
// must shift ADCH to left 8 bit so it doesn't write over
// ADCL.
// Also, ADCL Must be read first (see pg 259 of datasheet)
int i=0;
uint16_t storageResult=0;
uint16_t storageTemp=0;
uint16_t accumulator=0;
for(i=0;i<100;i++)
{
    while(ADCSRA & (1<<ADSC)) {
    // do nothing... just hold your breath.
  };
 storageResult=ADCL;
 storageTemp=ADCH;
storageResult= (storageResult+(storageTemp<<8));
accumulator= (storageResult+accumulator);

}

storageResult= (accumulator/i);
//farenheit conversion

ADMUX=5;

// RETURNS VALUES IN DEGREES... 5000MV/1024STEPS/10MV PER DEG
return storageResult;}

 main() {

// for testing temp Probes turn OFF motorOn and turn ON convertDegF and Lighton

//TEST LEDs

LEDTest();

for(;;)
{
//
// INITIALIZE AND READ COLLECTOR AND STORAGE ADCs
int16_t collectorResult=0;
int16_t storageResult=0;

//collector reading

collectorAdcInit();
collectorResult= collectorAdc_Read();

//delay just to make sure no reading overlap

//storage reading
storageAdcInit();
storageResult= storageAdc_Read ();
//delay just to make sure no overlap

//
// COMPARE TWO VALUES IN DEGREES

int16_t tempDiff=0;

tempDiff=(collectorResult-storageResult);

  // turn motor on and wait 1 minute
   // if (tempDiff>12 && storageResult<700){

   //  motorOn();

    // }

     //convert temp and turn on indicator lights

     int16_t convTemp=convertDegreeF(collectorResult);
     lightsOn(convTemp);

     }}
April 19, 2012
by pcbolt
pcbolt's Avatar

Jason -

How do you know it's reading from both sensors?

April 20, 2012
by Ralphxyz
Ralphxyz's Avatar

Jason, you can search the forum for "multi sensors" to see some great discussions/instructions about using multi sensors!

Ralph

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...