Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

to densimitre: can you please teanslate it?

Status
Not open for further replies.

kate

New Member
hi! ive seen your code about 16f84a working with ADC. can u please translate it in english so i can understand your code because your comments are written in a different language.
 
kate said:
hi! ive seen your code about 16f84a working with ADC. can u please translate it in english so i can understand your code because your comments are written in a different language.


Hi again.

This is a reloaded version of ADC0832. Provides Single-Ended and Differential Mode.

adc0832.c

Code:
#define AD_CK pin_A2    // Clock
#define AD_DO pin_A3    // Data Out
#define AD_DI pin_A4    // Data In (MUX Channel Selector). Use a PULL-UP resistor
#define AD_CS pin_A7    // Chip Select
 

int adc_result;
int MUX;

void  Clock() {
      output_high(AD_CK);
      output_low(AD_CK);
}

void set_adc(short ch, short mode)  {   //SGL:1/0 ==>Single-Ended/Differential Mode
      int i;
       if(mode==1 & ch==0)    {
       MUX=2;
       }
       if(mode==1 & ch==1)    {
       MUX=3;
       }
       if(mode==0 & ch==0)    {
       MUX=0;
       }
       if(mode==0 & ch==1)    {
       MUX=1;
       }
       output_high(AD_CS);          //CS=1, disable ADC0832
       output_low(AD_CS);           //CS=0, enable ADC0832
       output_high(AD_DI);          //Start BIT
       Clock();

       for (i=0;i<2;i++) {          //Send MUX word
       if((MUX & 0x2)==0)  {        //
         output_low(AD_DI);         //
         }
         else {                     //
         output_high(AD_DI);        //
         }
         MUX<<= 1;
         Clock();

         }
}


void read_adc() {
    int i;
    for (i=0;i<8;i++) {          
         Clock();                
         shift_left(&adc_result, 1, input(AD_DO));
         }
         output_high(AD_CS);     
}


main.c
Code:
//use following code to read ADC0832

set_adc(0,1);  //set channel 0, in Single_Ended mode. I can be Differential mode too.
read_adc();


For reading a LM35 output, read this

LM35DZ(2-100ºC) ==>0 volt for 2ºC, 10 mV/ºC....This mean 100-2 steps, 98 steps...and 2ºC of offset. Max output voltage for 100ºC is 98*10mV=0,98 volts

Well, AC0832 does not have a external Vref, because Vref is internally conected to VCC=5 Volts.

5 volts /255 steps=0,02 aprox...it would be like 20mV/ºC...in fact, the output of LM35 muts be multiplied by a factor of 2. Use an LM358 OA. in mode amplifier......

Here we start to read LM35 output on ADC0832...

0,02 => 1ºC
----- ------ ==>D ºC=Xvolt/0,02
Xvolts D ºC


and

255 => 5
----- ----- ==> Xvolt=count/51
count Xvolts


So, DºC=count/51/0,02 = count/1,02. But there is a 2ºC offset, this mean than DºC=count/1,02 + 2.


So, to measure LM35 Temperature with ADC0832 use a code like this


Code:
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc,"Tempe:");
lcd_gotoxy(12,1);
printf(lcd_putc,"°C");

   while(true) {
   float T;
   set_adc(0,1);                      // Choice CH & Single_Ended mode
   read_adc();
   T=(adc_result/1.02)+2;      //Vref is 5 Volts
   lcd_gotoxy(7,1);
   printf(lcd_putc,"%3.2f",T);
   delay_ms(100);
   }


If you use a different LM35 package, make an suitable modifications in output voltage range calculations

Enjoy

Bye
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top