本帖最后由 cmc1205 于 2014-3-11 14:35 编辑
在PIC官网上找的示例程序 然后改了改 就是一个简单的发送 想用示波器测出波形看对不对 但是一直测不出来 SDA口电压只有0.45V(示例里也有示波器的波形 最高是5V的) 波形也不对 请各位大神帮忙看看
SDA是RC4 SCL是RC3
另外 I2C是需要上拉电阻的吧? 我I2C通信的另一端是AD7745 测电容的ADC AD7745的手册上写的是加10K的上拉电阻 我加了测电压还是0.45V 求解释 上拉电阻会提高电压吗?
#include<p18f45k20.h>
#include<i2c.h>
/** C O N F I G U R A T I O N B I T S ******************************/
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30 // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H
unsigned char LDByteWriteI2C( unsigned char, unsigned char, unsigned char );
void InitPIC(void);
void delay(unsigned int x)
{
unsigned int a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
unsigned char ControlByte; //Control Byte
unsigned char HighAdd; //High Order Address Byte
unsigned char LowAdd; //Low Order Address Byte
unsigned char Data; //Data Byte
unsigned char Length; //Length of bytes to read
unsigned int PageSize; //Page size in bytes
unsigned char PageString[64]; // Holds the device page data to/from EEPROM
void main(void)
{
unsigned int n;
TRISD = 0x00;
PORTD=0xaa;
//delay(200);
ControlByte = 0xA0; //Control is always A0 for reads (& with 0x01 for writes)
HighAdd = 0x5A; //HighAdd is first byte transmitted for High Density devices
LowAdd = 0x51; //LowAdd is only Address byte for Low Density, second byte to HD
Data = 0xAA; //Data can be whatever user would like
// Length = 0x01; //Length param set to 1 for single byte read
// PageSize = 0x10; //PageSize is Physical page boundry from Datasheet. (0x10 = 16, 0x20 = 32)
// for(n = 0; n < PageSize; n++)
// {
// PageString[n] = Data;
// }
InitPIC();
//PORTD=0x01;
//delay(200);
while(1){
LDByteWriteI2C(ControlByte, LowAdd, Data );
//PORTD=0x02;
//delay(200);
} // Loop here forever
} //Low Density Byte Write
void InitPIC(void)
{
TRISC=0x0f; //Configure SCL
//as Input
//Configure SDA
//as Input
SSPSTAT = 0x80; //Disable SMBus &
//Slew Rate Control
SSPCON1 = 0x28; //Enable MSSP Master
SSPADD = 0x27; //Should be 0x27
//for 100kHz 16MHz
SSPCON2 = 0x00; //Clear MSSP Conrol Bits
}
unsigned char LDByteWriteI2C( unsigned char ControlByte, unsigned char LowAdd, unsigned char data )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
WriteI2C( ControlByte ); // write 1 byte - R/W bit should be 0
IdleI2C(); // ensure module is idle
WriteI2C( LowAdd ); // write address byte to EEPROM
IdleI2C(); // ensure module is idle
WriteI2C ( data ); // Write data byte to EEPROM
IdleI2C(); // ensure module is idle
StopI2C();
// send STOP condition
//while ( SSPCON2bits.PEN ); // wait until stop condition is over
//while (EEAckPolling(ControlByte)); //Wait for write cycle to complete
//return ( 0 ); // return with no error
}
先谢谢各位了~ |