PDA

View Full Version : 16F630 and MCP3221 I2C



badcock
- 17th August 2005, 09:37
Hi,

Has anyone tried interfacing to the MCP3221 ADC via I2C ?

I have the PicKit1 and the MCP3221 Pictail board that puts a direct connection between the RC0, RC1 and SCA SCL. I wanted to test out PicBasic I2C routines linking the 16F630 to the MCP3221.

Problem is that I do not seem to be getting any reading from the ADC. Can anyone suggest where I have gone wrong:

include "modedefs.bas"

' PIC Defines
' -----------

@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN & _CP_OFF & _CPD_OFF

' PIC16F630
' PIN NAME USE/CONNECTION
' 1 Vdd +5VDC
' 2 RA.5 NC
' 3 RA.4 NC
' 4 RA.3 NC
' 5 RC.5 NC
' 6 RC.4 NC
' 7 RC.3 NC
' 8 RC.2 NC
' 9 RC.1 SCL
' 10 RC.0 SCA
' 11 RA.2 NC
' 12 RA.1 LED1 K, LED2 A
' 13 RA.0 LED1 A, LED2 K
' 14 Vss GND

Define OSCCAL_1K 1 ' Set OSCCAL for 1K
DEFINE OSC 4 ' 4 MHz OSC


CMCON = 7 ' PortA = digital I/O
VRCON = 0 ' Voltage reference disabled
TRISA = %00000000' PortA.0-4 = outputs
TRISC = %00000001' PortC.1-5 = outputs PORTC.0 = input

SDA var PORTC.0
SCL var PORTC.1

B0 var byte
B1 var byte
B2 var byte
cont con %10011011
loop:

I2CREAD SDA,SCL,cont,[B1,B2] 'Read 2 locations in a row
if B1 > 2 then
HIGH PORTA.1
LOW PORTA.2
ELSE
LOW PORTA.1
HIGH PORTA.2
Endif

PAUSE 500
Goto loop ' Go back to loop and blink LED forever
End

'goto loop


Thanks,

Rod

Darrel Taylor
- 18th August 2005, 09:26
badcock,

The datasheet for the MCP3221 is a bit ambiguous. While the graphic in figure 5-2 shows the device code as 1001. The text in section 5.2 says that it's 1010.

I've never used that part before, so I don't know which one it really is, but you might try ...

cont con %10101010

Also, the first byte sent (B1) is the highbyte. So the A/D value will have to be greater than 1280 (1.56V) to satisfy the B1 > 2 condition.

And, don't forget the pull-up resistors on SDA and SCL.<br><br>

badcock
- 18th August 2005, 10:21
The datasheet for the MCP3221 is a bit ambiguous. While the graphic in figure 5-2 shows the device code as 1001. The text in section 5.2 says that it's 1010.
Tell me about the ambiguity! One of the reasons I aksed the question is that the AppNote (AN845) and the datasheet are confusing. My problems could stem from this.


I've never used that part before, so I don't know which one it really is, but you might try ...

cont con %10101010
I'll give a few of these variants a go - worth a try for a small amount of effort.


Also, the first byte sent (B1) is the highbyte. So the A/D value will have to be greater than 1280 (1.56V) to satisfy the B1 > 2 condition.
Exactly as it is meant to - I have a pot and reference voltage attached; I should be able to swap the lit LED by turning the pot (nice easy check).


And, don't forget the pull-up resistors on SDA and SCL.<br><br>
Already fitted on the Pictail board - one of the advantages of using two of the Microchip development boards together is that the hardware is definitely correct - pity their software data is so ambiguous!

Many thanks Darrel - I was starting to question whether I had missed some other device specific code. It's nice to have some confirmation that it ma only be the address / control code. Can anyone else confirm the correct MCP3221 control/address byte?

Rod

badcock
- 18th August 2005, 10:35
I just tracked some information down on the Microchip website:

Description: Section 5.2 of the datasheet states '1010' is the 4-bit device code.
However, all of the diagrams indicate '1001' is the device code.
Which is it?

Resolution: The paragraph is incorrect.
The device code is '1001' as stated in the diagrams.

I'll give the other code a try!

Rod

badcock
- 18th August 2005, 10:39
I just tracked some information down on the Microchip website:


I'll give the other code a try!

Rod

But the code I had originally used was correct - could there be something else I am missing?

Rod

Darrel Taylor
- 18th August 2005, 13:59
Rod,

Your comments show that the LED's are on PORTA.0 and PORTA.1

But, PORTA.1 and PORTA.2 are being toggled after reading the ADC<br><br>

badcock
- 18th August 2005, 17:36
Rod,

Your comments show that the LED's are on PORTA.0 and PORTA.1

But, PORTA.1 and PORTA.2 are being toggled after reading the ADC<br><br>
Not a problem - 16F630 is in the PicKit1 board - there are LEDS on RA1, RA2, RA4 and RA5. My comments are incorrect!

Rod

Darrel Taylor
- 18th August 2005, 18:56
Is there a jumper installed in JP1 on the PICtail board?

It connects the pot to the ADC.<br><br>

badcock
- 18th August 2005, 23:06
Is there a jumper installed in JP1 on the PICtail board?

It connects the pot to the ADC.<br><br>

Yep - and confirmed through DataView

Thanks,

Rod

Darrel Taylor
- 19th August 2005, 06:31
Well that's good news. If you can get dataview to work with it, at least we know the Pictail is working.

So here's another question that you've probably already checked.

When programming the F630 on the Pickit, did you check the "Device Power" checkbox?

Without it, the PIC in the "Evaluation Socket", and the MCP3221 won't get power.

Other than that, I think it's down to checking all the basic troubleshooting items, such as.

Check continuity, from F630 pins 9 and 10 to the MCP3221 pins 4 and 5.

Is there power getting to all the chips involved.

Does Pin1 match up on the connectors between the 2 boards

As you can tell, I'm focusing on the hardware, because the program looks fine.<br><br>

badcock
- 19th August 2005, 06:56
When programming the F630 on the Pickit, did you check the "Device Power" checkbox?

Without it, the PIC in the "Evaluation Socket", and the MCP3221 won't get power.

Yep, the reason I know the reading is not being made is because the LED's are not changing (toggling)


Other than that, I think it's down to checking all the basic troubleshooting items, such as.

Check continuity, from F630 pins 9 and 10 to the MCP3221 pins 4 and 5.

Is there power getting to all the chips involved.

Does Pin1 match up on the connectors between the 2 boards

As you can tell, I'm focusing on the hardware, because the program looks fine.<br><br>
OK ... I'll do a hardware check on the i2c lines + the power lines from the PicKit - from what you are saying it does not look like software!

Thanks,

Rod

badcock
- 19th August 2005, 07:20
OK

I've done the idiot checks on the hardware:

1. +5 from PicKit is going through to the VDD on the PicTail
2. SDA and SCL are connected to pins 5 and 4 of the MCP3221 and have continuity through to pin 10 and pin 9 of the 16F630.

Looks like hardware connections are fine!

Rod

P.S. there isn't any chance of the on-board (PicTail) 16c745 keeping the SCL and SDA lines low or otherwise interfering is there?

Darrel Taylor
- 22nd August 2005, 07:16
>> P.S. there isn't any chance of the on-board (PicTail) 16c745 keeping the
>> SCL and SDA lines low or otherwise interfering is there?

That's a good possibility. One way to find out is to just ground the MCLR pin on the 16C745. It should put all the pins into high impeadance state. And, see what happens.<br><br>