哪問高手能幫我解答一下ㄋ???急救
|
||||
---|---|---|---|---|
新會員
|
APP001 18F452 MPLAB 7.5
請問我要將8筆資料寫入 EEPROM內 ,但每次都寫完第二筆後都會卡再這一行 while(SSPCON2bits.ACKSTAT); 我是照著範例程式W401 ex6.2改ㄉ 謝謝!!! #include <p18f452.h> #include <delays.h> #include <timers.h> #include <i2c.h> void Initialize_I2C_Master(void); void EEPROM_Write(unsigned char,unsigned char,unsigned char); unsigned char EEPROM_Read(unsigned char,unsigned char); void EEPROM_ACK(unsigned char); void I2C_Done(void); unsigned char I2C_Read_Buffer [32]; /* define following variable in data memory at Access Bank */ #pragma udata access My_RAM near unsigned char Debounce; //near unsigned char Send_Addr; //near unsigned char Send_Data; near unsigned char Send_Length; near unsigned char Read_Data; #pragma udata #define EE_CMD 0xA0 //*************************************** //* Program Main ( ) * //*************************************** void main(void) { TRISD=0x00; // Set PortD for Output PORTD=0x00; // Initila LED display = 0x80 ADCON1=0b00000110; Initialize_I2C_Master( ); PORTD=0x01; EEPROM_Write(EE_CMD,0x00,0x9C); PORTD=0x02; EEPROM_Write(EE_CMD,0x01,0x04); PORTD=0x04; EEPROM_Write(EE_CMD,0x02,0xAD); PORTD=0x08; EEPROM_Write(EE_CMD,0x03,0x7E); PORTD=0x10; EEPROM_Write(EE_CMD,0x04,0xD4); PORTD=0x20; EEPROM_Write(EE_CMD,0x05,0x23); PORTD=0x40; EEPROM_Write(EE_CMD,0x06,0x00); PORTD=0x80; EEPROM_Write(EE_CMD,0x07,0x00); } //************************************************ //* Initial I2C Master Mode with 7 bits Address * //* Clock Speed : 100KHz @16MHz * //************************************************ void Initialize_I2C_Master(void) { OpenI2C(MASTER,SLEW_ON); SSPADD= 28; } //*********************************************** //* Random Read a Byte from EEPROM * //* - ctrl : Control Byte of EEPROM (Write) * //* (Ctrl +1 ) : Read Command * //* - addr : Address Byte of EEPROM * //* - Return : Read Data from EEPROM * //*********************************************** unsigned char EEPROM_Read(unsigned char ctrl,unsigned char addr) { unsigned char f; IdleI2C(); // ensure module is idle StartI2C(); // Start condition I2C_Done(); // Wait Start condition completed WriteI2C(ctrl); // Write Control to EEPROM while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received I2C_Done(); // Clear SSPIF flag WriteI2C(addr); // Write Address to EEPROM while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received I2C_Done(); // Clear SSPIF flag RestartI2C(); // initiate Restart condition I2C_Done(); WriteI2C(ctrl+1); // Write Control to EEPROM while(SSPCON2bits.ACKSTAT); // test for ACK condition, if received I2C_Done(); // Clear SSPIF flag f=ReadI2C(); // Enable I2C Receiver & wait BF=1 until received data I2C_Done(); // Clear SSPIF flag NotAckI2C(); // Genarate Non_Acknowledge to EEPROM I2C_Done(); StopI2C(); // send STOP condition I2C_Done(); // wait until stop condition is over return(f); // Return Data from EEPROM } //*********************************************** //* Write a Byte to EEPROM * //* - ctrl : Control Byte of EEPROM * //* - addr : Location of EEPROM * //* - data : Data Byte of EEPROM * //*********************************************** void EEPROM_Write(unsigned char ctrl,unsigned char addr,unsigned char data) { IdleI2C(); // ensure module is idle StartI2C(); // Start condition I2C_Done(); // Wait Start condition completed and clear SSPIF flag WriteI2C(ctrl); // Write Control+Write to EEPROM & Check BF flag while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM I2C_Done(); // Clear SSPIF flag WriteI2C(addr); // Write Address to EEPROM WriteI2C(addr); // Write Address to EEPROM Delay10KTCYx(10); while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM I2C_Done(); WriteI2C(data); // Write Data to EEPROM while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM I2C_Done(); StopI2C(); // Stop condition I2C_Done(); // Wait the Stop condition completed } //*********************************************** //* EEPROM Acknowledge Polling * //* -- The routine will polling the ACK * //* response from EEPROM * //* -- ACK=0 return * //* -- ACK=1 send Restart & loop check * //*********************************************** void EEPROM_ACK(unsigned char ctrl) { unsigned char i; IdleI2C(); // ensure module is idle StartI2C(); // Start condition I2C_Done(); // Wait Start condition completed WriteI2C(ctrl); // Write Control to EEPROM (WRITE) I2C_Done(); // Clear SSPIF flag while (SSPCON2bits.ACKSTAT) // test for Acknowledge from EEPROM { for (i=0;i<100;i++); // Delay for next Repet-Start RestartI2C(); // initiate Repet-Start condition I2C_Done(); // Wait Repet-Start condition completed WriteI2C(ctrl); // Write Control to EEPROM (WRITE) } I2C_Done(); // Clear SSPIF flag *** StopI2C(); // send STOP condition I2C_Done(); // wait until stop condition is over } //*********************************************** //* Check I2C action that is completed * //*********************************************** void I2C_Done(void) { while (!PIR1bits.SSPIF); // Completed the action when the SSPIF is Hi. PIR1bits.SSPIF=0; // Clear SSPIF }
發表於: 2007/5/8 18:38
|
|||
|