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 support@ccsinfo.com

MCP3221

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



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

MCP3221
PostPosted: Mon Sep 04, 2006 4:21 am     Reply with quote

Hi All, i am trying to write a driver for the MCP3221 ADC. I have tried to search for one on the web, but have found nothing. I did manage to find some ASM code from a microchip App Note. I am not very good with ASM, but it looks very simple.

Code:

call WrtStop ; Send Stop Bit to Reset
call check_idle
call WrtStart ; Start Bit
call check_idle
call SendWrtAddr ; Send Address Byte
call check_idle
call StartReadDataHigh ; Get High Byte of A/D Conversion
call check_idle
call SendReadAck ; Acknowledge Low Byte
call check_idle
call StartReadDataLow ; Get Low Byte of A/D Conversion
call check_idle
call SendReadNack ; Non-Acknowledge to signal single conversion
call check_idle
call WrtStop ; Send Stop Bit


So what i can see from that, the c code should look sothing like this

Code:

#include <18F4550.h>

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOBROWNOUT
#use delay(clock=48000000)

#use i2c(MASTER, SDA=PIN_B0, SCL=PIN_B1)

#define USB_CON_SENSE_PIN             PIN_E1            

#include <usb_cdc.h>

int MSB, LSB;
int16 ADC;
void main()
   {
      usb_init();

      printf(usb_cdc_putc,"system ok\n\r");

   while(1)
      {
         delay_ms(1000);
         printf(usb_cdc_putc,"Getting Reading\n\r");
      i2c_stop(); //I have tried with and without this
         i2c_start();
         i2c_write(0xAB);                     // Device address 10101011
         delay_us(5);
         MSB = i2c_read(1);
         LSB = i2c_read(0);
         i2c_stop();
         ADC = make16(MSB,LSB);
         printf(usb_cdc_putc,"Reading = %LU\n\r",ADC);
         
      }
}


When i run the code, the olny reading i get reading is 65535, even if i remove the MCP3221. I know the I2C bus is OK, as i have other devices attached to it that wok OK. I have removed them from the code for testing. I have checked Vdd and Vss, and both are OK. I have read the datasheet many, many times, but it looks like what i am doing should work.

Can anybody help??

Thanks, Mark
Ttelmah
Guest







PostPosted: Mon Sep 04, 2006 5:11 am     Reply with quote

Are you sure you have the address right?.
I have used this chip, and it needed address 0x9B, not 0xAB. This is for the 'A5T' version. If you have any other number, you need to recalculate the second byte.

The sequence I used, was:
Code:

I2C_START();
if (I2C_WRITE(0x9A)==0) {
   //Here the device is present
   I2C_START();
   //Trigger the conversion
   I2C_WRITE(0x9B);
   delay_us(10);
   //In single acquire mode,you need to wait for Tacq(1.12uSec)
   //plus Tconv(8.96uSec).
   MSB=I2C_READ();
   LSB=I2C_READ(0);
}
I2C_STOP();


Best Wishes
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Thu Sep 14, 2006 5:46 am     Reply with quote

Thanks Ttelmah, i finaly got some time to test this out. I have now tested each of the 8 addresses that the chip could be, but i dont get any response from any off them. If i try a conversion, the output will always be 65535. I dont have anymore MCP3221's to try to see it is just a faulty chip, but i will order some more today.

Also, do you, or anyone here, know of any other 10, 11 or 12 bit ADCs that are I2C? I have tried to find some, bu they are all SPI.

Thanks, Mark
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Fri Sep 15, 2006 5:57 am     Reply with quote

Hi all, just to finnish this post off, the ADC was faulty, and the code works OK

Thanks, Mark
Ttelmah
Guest







PostPosted: Fri Sep 15, 2006 7:31 am     Reply with quote

Well done.
I think you had pretty much decided that the software _ought_ to work, so it is nice to find that you were right!. Smile

Best Wishes
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