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

[S O L V E D] About max1039 multi channel 8bit ADC

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



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

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

[S O L V E D] About max1039 multi channel 8bit ADC
PostPosted: Wed Dec 21, 2016 9:25 am     Reply with quote

Here is my code driver I wrote down.
I can read from channel 0 but can't get any value from other channels.
Please any help will be appreciated.
Code:

#define max1039_addr 0xCA
#define max1039_write_addr 0xCA
#define max1039_read_addr 0xCB
// Configuration byte  config bit 1, no scan 11, cs3 cs2 cs1 cs0 single ended 1
#define configbyte 0b11100001
// Setup Byte
// setupbit 1, sel2 sel 1 sel0 0 010 external ref,CLk int 0, UNIpolar 0, RST 1, X
#define setupbyte  0b00100010

unsigned int8 adcval=0;


Code:

#include "elf-max1039.h"

BOOLEAN max1039_ready()
{
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(max1039_write_addr);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void max1039_test(void)
{

do
 {
 }while(!max1039_ready());

}

void max1039_write(char deviceAddress,char val)
{
   i2c_start();               // start transmission to device
   i2c_write(deviceAddress);  // send device address
   i2c_write(val);            // send value to write
   i2c_stop(); 
}

char max1039_read(int8 deviceAddress,char AN_channel)
{
   unsigned int8 configbyte_,adcvalue;
   configbyte_=configbyte|(AN_channel<<1);
   i2c_start();                 
   i2c_write(deviceAddress);   
   i2c_write(configbyte_);           
   i2c_start();                 
   i2c_write(deviceAddress + 1);
   delay_us(10);
   adcvalue = i2c_read(0);
   i2c_stop();                   
   return adcvalue;
}

void max1039_init(void)
{
max1039_write(max1039_write_addr,configbyte);
max1039_write(max1039_write_addr,setupbyte);
}


and my main()
Code:

max1039_test();

for(i=0;i<11;++i)
{
   adcval=max1039_read(max1039_addr,1);
   printf("ADC channel-> %u |  adc value-> %u\r\n",i,adcval);
   delay_ms(500);
}

What could be the problem?
All my hardware setting are correct.
I already get the channel 0 values and have other i2c devices functioning correctly like eeprom reading correctly, etc. and TCA9535 functioning like a charm.
I just put the code section for the max1039.
cheers
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.


Last edited by scanan on Thu Dec 29, 2016 6:07 am; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Dec 21, 2016 10:33 am     Reply with quote

It's behaving as if it is in the default scan mode. So returning AN0 as the first value read.
Try sending the setup byte first. Though they say the order does not matter. the RST bit will be 0, at start, which could then reset things.
If you are intending to read multiple channels, why not let it scan, and just read the results from all the channels?.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 21, 2016 10:36 am     Reply with quote

You need to 'play computer'... cut extra code to display(send to PC or LCD) the variable 'configbyte_'.
That way you can actually SEE what it is.
Actually dumping it in binary is the best way, so you can easily check that the bits are correct.

You could have a hardware problem, grounded ADC input pins, bad cable, etc. but you still need to confirm the software !

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 21, 2016 1:18 pm     Reply with quote

You have this in your program:
Quote:
#define configbyte 0b11100001
// Setup Byte
// setupbit 1, sel2 sel 1 sel0 0 010 external ref,CLk int 0, UNIpolar 0, RST 1, X
#define setupbyte 0b00100010

But in the max1039 data sheet, in Table 1 or Table 2, it says the opposite
of what you are doing. It says this:
Quote:

Bit 7 - Register bit. 1 = Setup Byte , 0 = Configuration Byte.

Notice in your #define statements, your 'configbyte' has bit 7 = 1
and 'setupbyte' has bit 7 = 0. That's not correct.

Also, I don't like the way you write code. In the C language, there is
a convention that #define'd constants should be in upper case.
2nd thing. Adding an underscore to a similar varibale name is very error
prone. These two things you are doing make it harder to see errors by
just glancing at your code. Your goal should be to write code in a style
that minimizes errors.
scanan



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

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

PostPosted: Thu Dec 22, 2016 7:22 am     Reply with quote

At the moment I solved the problem.
It was from the setup and conf bytes 7th bit.
Many thanks for your help.
The driver is working well for all channels.

Here is the corrected code:
*.h file
Code:

#define MAX1039_ADDR 0xCA
#define MAX1039_WRITE_ADDR 0xCA
#define MAX1039_READ_ADDR  0xCB

// Configuration byte  configbit 0, no scan 11, cs3 cs2 cs1 cs0, single ended 1
#define CONFIGURATION_BYTE 0b01100001

// Setup Byte
// setupbit 1, sel2 sel 1 sel0 010 (external ref),CLK 0 (internal), UNIpolar 0, RST 1, X
#define SETUP_BYTE  0b10100010

unsigned int8 adcval=0;


max1039.c
Code:

BOOLEAN max1039_ready()
{
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(MAX1039_ADDR);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void max1039_test(void)
{
do
{
  printf("no connection\r\n");
}while(!max1039_ready());
if(max1039_ready())
printf(" max1039 connect\r\n");
//delay_ms(500);
}
void max1039_write(char deviceAddress,char val)
{
   i2c_start();               // start transmission to device
   i2c_write(deviceAddress);  // send device address
   i2c_write(val);            // send value to write
   i2c_stop(); 
}
unsigned int8 max1039_read(unsigned int8 deviceAddress,unsigned int8 AN_Channel)
{
unsigned int8 newConfigbyte,adcValue=0;
newConfigbyte=CONFIGURATION_BYTE|(AN_Channel<<1);
//printf("CONFIGURATION_BYTE: %u \r\n",CONFIGURATION_BYTE);
//printf("newConfigbyte: %u \r\n",newConfigbyte);
   i2c_start();                // start transmission to device
   i2c_write(deviceAddress);
   i2c_write(newConfigbyte);
   i2c_start();                // restart - the bus is now set to _read_
   i2c_write(deviceAddress+1); // now turn the bus round
   adcvalue = i2c_read(0);
   i2c_stop();
   return adcValue;
}

void max1039_init(void)
{
   max1039_write(max1039_write_addr,SETUP_BYTE);
   max1039_write(max1039_write_addr,CONFIGURATIOn_BYTE);
}


main.c
Code:

void main(void)
{
unsigned int i;

max1039_init();
max1039_test();

do{
   for(i=0;i<11;++i)
   {
   adcval=max1039_read(MAX1039_ADDR,i);
   printf("ADC channel-> %u | ADC value-> %u\r\n",i,adcval);
   delay_ms(3000);
   }
   printf("\r\n \r\n \r\n");
}while(1==1);
}



You may use this driver at your own risk.

Many thanks
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.
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