CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Read external 10-bit ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

Read external 10-bit ADC
PostPosted: Tue Jun 07, 2005 10:55 am     Reply with quote

Hi

I have a TLC1541 (texas Instruments) 12 channel/10-bit ADC. Interface serial

http://www.lierda.com/upfile/1074129861.pdf


Im using 16F84a, PIN_A4 is pull-up(ed). I want read channnel 0....my rutine to read 10 bit is:

Main.c

Code:

#include <16f84a.h>
#use delay(clock=4000000)
#fuses XT,NOWDT,NOPROTECT
#use RS232(BAUD=9600, BITS=8, PARITY=N, XMIT=PIN_A2, RCV=PIN_A4, RESTART_WDT)
#include "TLC1541.c"

float valor;
long  adc_result;
void main() {
  for(;;) {
   adc_init();
   set_adc(0);
   read_adc();
   printf("A/D Conversion using TLC1541\n\r");
   value=(float)(adc_result*25)/(float)1024;           // 0-25 Volt measure
   putc(13);
   printf("HEX Value of adc_result=%lX\n\r",adc_result);
   printf("Channel 0 Voltage= %2.2f Volts.\n\r",value);
   }
}



TLC1541.c

Code:



void  delay() {            // Delay.
      delay_us(10);
}

void  Clock() {            // Clock signal generation
      output_high(AD_CK);   
      delay();                     
      output_low(AD_CK);
}

void adc_init() {               
      output_high(AD_CS);   // Be sure TLC1541 is not activated
      delay_us(10);
      output_low(AD_CS);    //Activate TLC1541, CS=0.
      delay();
      Clock();              //Valid (High to LOW) transition of CS
      Clock();              //need 2 "rising edge" of AD_CK
                            //then 1 "alling edge".
      delay();
                                 
}


void  set_adc(int ch) {     //Set ch selection
      int i;                // AIN0-10
      for (i=0;i<4;i++) {   // 4 "rising edges" of I/O CLOCK. MSB first
      output_bit(AD_DI,shift_left(ch,1,0));
      Clock();
      delay();
      }

      for (i=0;i<5;i++) {   //Start sample cycle. The 10° clock is
                            //controled by read_adc();
      Clock();
      delay();              // to A/D conversion star at any time
      }                     
}

void read_adc() {
      int i;
      Clock();                //10° Clock
      delay();
      output_high(AD_CS);     //CS=1 when I/O and System clock
                              //is same pin.
      for(i=0;i<44;i++) {     //of datasheet, 44 clock cycle for
                              //finished conversion
      Clock();
      delay();
      }
      output_low(AD_CS);      //Activate TLC1541 again to read
                              //10 bit result stream.

      for(i=0;i<9;i++) {      //Start adc reading, 9 clocks
                              //(10 bits). Read MSB first
      shift_left(&adc_result,2,input(AD_DO));
      Clock();
      delay();
      }
      output_high(AD_CS);     //TLC1541 is no active. End od reading.
}



I canīt read the 10 bit result...adc_result= 0x0000 forever......why???

Help me please


Thank you[/url]


Last edited by densimitre on Tue Jun 07, 2005 2:34 pm; edited 4 times in total
bfemmel



Joined: 18 Jul 2004
Posts: 40
Location: San Carlos, CA.

View user's profile Send private message Visit poster's website

PostPosted: Tue Jun 07, 2005 11:31 am     Reply with quote

densimitre,

You certainly don't give us much to go on. I need to make a lot of assumptions about what I think Clock() and delay() do in the code. One thing I see is that you are raising AD_CS high after 10 clock cycles. First of all it is active low so you should be setting it low and secondly it should be before the clock cycles and the four bit address of the channel should be on the bus at that time which since you want channel zero might be OK anyway. Just make sure that the adress input is low during this time.

If you can give us more of your program, such as the missing routines and the main body of the program we can probably make some more observations.

Good Luck
- Bruce
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Tue Jun 07, 2005 11:17 pm     Reply with quote

Hi

I made a change on TLC1541.c

Code:

void  set_adc(int ch) {     
      int i;               
      for (i=0;i<4;i++) {         
      if((ch&0x8)==0)     
      output_low(AD_DI);   
      else                 
      output_high(AD_DI);   
      ch <<= 1;           

      Clock();
      delay();
      }
}

 



Now I can store adc_result...and in my protoboard i had tested it, now its working.....

WOW


Thank you ...


Bye
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group