下面是一段I2C的程序,使用的是PIC单片机内部的I2C主机模式,不知道哪里出现问题了,不管是用Protues模拟还是实物测试一直出现无应答的现象,如果去掉应答那么读出来的数据是错误的,看到这篇帖子的同志请多多指点一下。 使用的PIC16F1824芯片 和24C02芯片通信,24C02芯片已经测试过没有问题
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#define AddWr 0xae //24C02 write address
#define AddRd 0xaf //24C02 read data address
#define nop() asm("nop")
void SysInit(void)
{
OSCCON = 0B01111000 ; //disable 4XPLL 16MHZ
TRISA = 0B00001000 ;
ANSELA = 0B00000000 ;
LATA = 0B00000000 ;
TRISC = 0B00000011 ; //SDA 、SCL 设置为输入
ANSELC = 0B00000000 ;
LATC = 0B00000011 ;
GIE = 0 ;
}
void DelayMS(unsigned int Time)
{
unsigned char i , j ;
for(i=0 ; i<Time ; i++)
for(j=0 ; j<10 ; j++) ;
}
void I2CMaster_Init(void)
{
TRISCbits.TRISC0 = 1 ;
TRISCbits.TRISC1 = 1 ;
// CKE = 1 ; //禁止SMBus 使用I2C协议规范电平
// SMP = 1 ; //SSP1STAT 关闭压摆率控制
SSP1STAT = 0B11000000 ;
SSP1CON1 = 0B00111000 ; // SSP1CON1 = 0B00101000 ; //I2C主模式,时钟=Fosc/(4*(SSP1ADD+1))
SSP1ADD = 0x27 ; // 当主频为16Mhz时波特率为100kbps
SSP1IF=0; //若使用SSP1IF作为信号发送状态位则需要清空此位
BCL1IF=0; // 清空总线冲突标志位
RCEN = 0 ;
}
void I2CMaster_Start(void)
{
SEN = 1 ; //SEN置1使硬件发送起始信号,发送完毕后硬件自动把SEN清零
while(SEN) ; //SEN=1说明发送进行中,循环结束说明SEN=0,即起始信号发送完毕
//SSP1IF = 0;
}
void I2CMaster_ReStart(void)
{
RSEN = 1 ; //RSEN置1使硬件发送重启信号
while(RSEN) ; //RSEN=1说明数据发送进行中,循环结束说明RSEN=0,即发送完毕
}
void I2CMaster_Stop(void)
{
PEN = 1 ; //发送停止位
while(PEN) ; //PEN=1说明未发送完毕,循环结束说明PEN=0,即发送完毕
}
unsigned int I2CMaster_Read(unsigned char ack)
{
unsigned char I2CReadData ;
RCEN = 1 ; //启动一次读操作
while(RCEN); //RCEN=1说明数据发送进行中,循环退出说明RCEN=0,即发送完毕
I2CReadData = SSP1BUF ; //获得读到的数据 SSP1IF=0;
if(ack)
{
ACKDT = 0 ;
}
else
{
ACKDT = 1 ;
}
ACKEN = 1 ;
while(ACKEN) ; //ACKEN=1说明未发送完毕,循环结束说明ACKEN=0,即发送完毕
return (I2CReadData) ;
}
void I2CMaster_Write(unsigned char I2CWriteData)
{
SSP1BUF = I2CWriteData ;
while(SSP1CON2bits.ACKSTAT); //等待应答
while(!SSP1IF ) ; SSP1IF = 0 ;
}
void Write_Ext_EEPROM(unsigned char addr , unsigned char data)
{
I2CMaster_Start() ;
I2CMaster_Write(AddWr) ; //写器件地址
I2CMaster_Write(addr) ; //写存储地址
I2CMaster_Write(data) ; //写数据
I2CMaster_Stop() ;
DelayMS(1) ;
}
unsigned char Read_Ext_EEPROM(unsigned char addr)
{
unsigned char data ;
I2CMaster_Start() ;
I2CMaster_Write(AddWr); //A2~A0:000, R/-W=0 写器件地址
I2CMaster_Write(addr) ; //写存储地址
I2CMaster_ReStart() ;
I2CMaster_Write(AddRd) ; //A2~A0:000, R/-W=1 读器件地址
data = I2CMaster_Read(0) ; //读数据
I2CMaster_Stop() ;
return (data) ;
}
void main(void)
{
SysInit() ;
LATA = 0B11111111 ;
I2CMaster_Init() ;
Write_Ext_EEPROM(1,8) ;
DelayMS(5) ;
if(Read_Ext_EEPROM(1) > 10)
LATAbits.LATA0 = 0 ;
else
LATAbits.LATA1 = 0 ;
while(1)
{
;
}
}
转载
|