• slider image 514
  • slider image 516
  • slider image 517
  • slider image 518
  • slider image 519
:::


Browsing this Thread:   1 Anonymous Users






Re: 哪問高手能幫我解答一下ㄋ???急救
#2
版主
版主


查看用戶資訊
程式還沒有時間去詳讀,先認為寫入 EEPROM 寫入時間問題。因為在規範裡 24LC04 的寫入時間最大值為 10mS 所以再做第二筆寫入時一定要Delay 10mS 或 Polling 下一筆資料的 ACK 來決定是否可以送出第二筆的寫入資料。在這裡EEPROM 回復一個 NACK 代表上一次的寫入資料還沒完成要你在等一下。

發表於: 2007/5/10 14:59
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


哪問高手能幫我解答一下ㄋ???急救
#1
新會員
新會員


查看用戶資訊
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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------