|
|
View previous topic :: View next topic |
Author |
Message |
assawapong
Joined: 11 Jun 2017 Posts: 2 Location: Thailand
|
How to config I2C in PIC24F08KA101 |
Posted: Sun Jun 11, 2017 11:04 pm |
|
|
Dear Sir,
How to config I2C in PIC24F08KA101. I have already work find in PIC16F887. Please advise me. Thank you.
:PCWHD Compiler, PCD Version 5.015
:MPLAB X IDE v3.51
:PICkit 3
Code: |
#include <24F08KA101.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#FUSES XT,PR_PLL,NOPROTECT,NOWDT,NOOSCIO
#use delay (clock = 8M)
#define TxD PIN_B7
#define RxD PIN_B2
#use rs232(baud=9600,xmit=TxD,rcv=RxD)
#use i2c(Master, fast, i2c1)
float measure=0;
void MCP3422_Write (int Data)
{
i2c_start();
i2c_write(0xD0);
i2c_write(Data);
i2c_stop();
delay_ms(40);
}
void MCP3422_ini(void)
{
MCP3422_Write(0xB0); //0xb0
}
float MCP3422_Read (void)
{
float volt;
int data=0,sign=0;
i2c_start();
i2c_write(0xD1);
data = i2c_read();
sign=data;
data&=0x07;
data=data<<8;
data = data | i2c_read();
i2c_read();
i2c_read();
i2c_stop();
delay_ms(1);
if((sign&0x08)!=0)
volt=(0x07FF-data)*-0.001;
else
volt=(0.001)*data;
return(volt);
}
void main()
{
setup_oscillator(OSC_INTERNAL);
MCP3422_ini();
while(TRUE)
{
measure = MCP3422_Read ();
}
} |
_________________ Jack |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 12, 2017 1:00 am |
|
|
CCS has a driver file for the MCP3422. It's called mcp342x.c and it's
in your CCS \Drivers folder. Here is the first part of the driver file:
Quote: |
////////////// Driver for MCP342X Delta-Sigma A/D Converter
//// Driver for Microchip's MCP3421, MCP3422, MCP3423, MCP3424,
//// MCP3425, MCP3426, MCP3427 and MCP3428 Delta-Sigma
//// A/D Converters.
|
|
|
|
assawapong
Joined: 11 Jun 2017 Posts: 2 Location: Thailand
|
|
Posted: Mon Jun 12, 2017 1:33 am |
|
|
Thank you for your suggestion.
I would like to ask in the header of code. Can I config the I2C of PIC24F?
(#use i2c(Master, fast, i2c1)). Could you give me the example of code that use in I2C of PIC24F? _________________ Jack |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19620
|
|
Posted: Mon Jun 12, 2017 2:11 am |
|
|
The only time it is different from other PIC's, is on the more complex PIC24/30/33's, where you have to configure the peripheral pins first with #PIN_SELECT (this applies to some of the newer PIC18's and 16's as well).
Otherwise it behaves the same as for PIC12/16/18's.
If you look at the I2C slave example, the setup is identical for the PIC24.
Similarly ex_bootloder_to_i2c.c, shows an I2C master. Again the same.
Other things you have to consider (again applies on all the more modern chips in particular), is making sure the other peripherals on the same pins are disabled, and also that it is a separate peripheral (on the smaller PIC's, it is combined with the SPI to form the MSSP peripheral).
Much more important, are you sure your chip clock settings are right?. You have it trying to boot off an XT oscillator, with the PLL, then switching to the internal oscillator after booting. If the XT oscillator is not there (and depending on what the failsafe monitor is defaulting to), the chip may not even boot. If you want to use the internal oscillator, then just use:
Code: |
#use delay(internal=8M)
#FUSES INTRC_IO, NOPROTECT,NOWDT,NOOSCIO
|
and get rid of the setup oscillator line.
Your code throws away the LSB of the data from the chip, and does an extra unneeded read (the fourth byte from the chip is the configuration and doesn't have to be read).
Remember also an 'int' in a PIC24, is a signed int16 by default. Your rotation and data assembly may not work as you expect. This si why I always say _be explicit_. Don't use 'int', but use int8, int16 etc.. |
|
|
|
|
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
|