//************************************************************************
//* Using I2C Master Mode for aCCess Slave (EEPRM)
//*
//* Written by: RIChard Yang
//* Sr. Corporate Application Engineer
//* MicroChip Technology Inc.
//* Date: Oct. 3nd '2002
//* Revision: 1.00
//* Language tools : MPLAB-C18 v2.09.13
//* MPLINK v3.10
//* MPLAB-IDE v6.00.17 & ICD2
//************************************************************************
/* Include Header files */
#include
#include
#include
#include "P18LCD.h" // Load P18LCD Header file form current working direct
/* Declare the Function Prototype */
void Initialize_I2C_Master(void);
void EE_Page_Write(unsigned char,unsigned char,unsigned char,unsigned char *);
void EE_SEQU_Read(unsigned char,unsigned char,unsigned char,unsigned char *);
void EEPROM_Write(unsigned char,unsigned char,unsigned char);
void EEPROM_ACK(unsigned char);
unsigned char EEPROM_Read(unsigned char,unsigned char);
void I2C_Done(void);
void Initialize_Timer2(void);
void isr_high_direct(void);
void isr_high(void);
#pragma romdata My_romdata=0x1000
const rom far unsigned char LCD_MSG1[]="SW2: Byte Write ";
const rom far unsigned char LCD_MSG2[]="SW6: Random Read";
const rom far unsigned char LCD_MSG3[]="Byte Write Mode ";
const rom far unsigned char LCD_MSG4[]="Random Read Mode";
const rom far unsigned char LCD_MSG5[]="Sended: ";
const rom far unsigned char LCD_MSG6[]="Send: ";
const rom unsigned char I2C_Write_Buffer[]="Microchip Technology";
#pragma romdata
/* Define following array in data memory */
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;
near unsigned char P_SW2;
near unsigned char P_SW6;
#pragma udata
#define Page_Length 8
#define SW2 PORTAbits.RA4
#define SW6 PORTEbits.RE1
#define Bounce_Time 6
#define EE_CMD 0xA0
//***********************************************************
/* */
/* Main Program */
/* */
//***********************************************************
void main(void)
{
ADCON1=0b00000110; // DISAble A/D Function
TRISAbits.TRISA4=1; // Set SW2 for input
TRISEbits.TRISE1=1; // Set SW6 for Input
Initialize_Timer2( );
Initialize_I2C_Master( );
OpenLCD( );
if (SW2 & SW6)Debounce=0;
else Debounce = Bounce_Time;
while(1)
{
LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)
putrsLCD(LCD_MSG1);
LCD_Set_Cursor(1,0); // Put LCD Cursor on (1,0)
putrsLCD(LCD_MSG2);
P_SW2=P_SW6=0;
Send_Addr=0;
while(1)
{
if (P_SW2)
{
P_SW2=0;
Debounce = Bounce_Time;
LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)
putrsLCD(LCD_MSG3);
LCD_Set_Cursor(1,0); // Put LCD Cursor on (0,0)
putrsLCD(LCD_MSG5);
do
{
while (!P_SW2);
P_SW2=0;
LCD_Set_Cursor(1,8);
Send_Data=I2C_Write_Buffer[Send_Addr];
EEPROM_Write(EE_CMD,Send_Addr,Send_Data);
puthexLCD(EE_CMD);
putcLCD(' ');
puthexLCD(Send_Addr);
putcLCD(' ');
puthexLCD(Send_Data);
EEPROM_ACK(EE_CMD);
Send_Addr++;
} while (I2C_Write_Buffer[Send_Addr]!=0x00);
break;
}
if (P_SW6)
{
P_SW6=0;
Debounce = Bounce_Time;
LCD_Set_Cursor(0,0); // Put LCD Cursor on (0,0)
putrsLCD(LCD_MSG4);
LCD_Set_Cursor(1,0); // Put LCD Cursor on (0,0)
putrsLCD(LCD_MSG6);
while(1)
{
if (P_SW6)
{
P_SW6=0;
LCD_Set_Cursor(1,5);
Read_Data = EEPROM_Read(EE_CMD,Send_Addr);
puthexLCD(EE_CMD);
putcLCD(' ');
puthexLCD(Send_Addr);
putcLCD(' ');
puthexLCD(EE_CMD);
putcLCD(' ');
puthexLCD(Read_Data);
Send_Addr++;
}
if (P_SW2) break;
}
if (P_SW2) break;
}
if (P_SW2)
{
P_SW2=0;
break;
}
}
}
}
//************************************************
//* #pragma Interrupt Declarations *
//* *
//* Function: isr_high_direct *
//* - Direct execution to the actual *
//* high-priority interrupt code. *
//************************************************
#pragma code isrhighcode = 0x0008
void isr_high_direct(void)
{
_asm //begin in-line assembly
goto isr_high //go to isr_high function
_endasm //end in-line assembly
}
#pragma code
//************************************************
//* Function: isr_high(void) *
//* High priority interrupt for Timer2 *
//************************************************
#pragma interrupt isr_high
void isr_high(void)
{
PIR1bits.TMR2IF=0; // Clear Timer2 interrupt Flag
if (Debounce==0)
{
if (!SW2)
{ P_SW2=1; Debounce =Bounce_Time; }
if (!SW6)
{ P_SW6=1; Debounce =Bounce_Time; }
}
else if (SW2 & SW6)Debounce--;
else Debounce =Bounce_Time;
}
#pragma code
//***********************************************
//* Write a Byte to EEPROM
//* - CTRl : Control Byte of EEPROM
//* - addr : LOCation of EEPROM
//* - data : Data Byte of EEPROM
//***********************************************
void Initialize_Timer2(void)
{
RCONbits.IPEN=1; // Enable Interrupt Priority bit
IPR1bits.TMR2IP=1; // Set Timer2 for High Priority
INTCONbits.GIEH=1; // Enable High Priority Interrupt
OpenTimer2 (TIMER_INT_ON // Turn On the Timer2 with Interrupt
& T2_PS_1_4 // (4MHz/4) [4*10*(99+1)] = 4mS */
& T2_POST_1_10);
PR2 = 99;
}
//***********************************************
//* 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
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
}
//***********************************************
//* Pae Write to EEPROM
//*
//* - ctrl : Control Byte of EEPROM
//* - addr : Location of EEPROM
//* - length : Write counter
//* - *dptr : RAM point --> EEPROM
//*
//***********************************************
void EE_Page_Write(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
IdleI2C(); // ensure module is idle
StartI2C(); // Start condition
I2C_Done(); // Wait Start condition completed
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
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
while (length!=0) // Check write completed ?
{
WriteI2C(*dptr); // Write data to EEPROM
while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
I2C_Done();
dptr++; // Point to next byte
length--;
}
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
}
//***********************************************
//* 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
}
//***********************************************
//* Sequential Read from EEPROM
//*
//* - ctrl : Control Byte of EEPROM
//* - addr : Location of EEPROM
//* - length : Read counter
//* - *dptr : Store EEPROM data to RAM
//*
//***********************************************
void EE_SEQU_Read(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
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
while (length!=0)
{
*dptr=ReadI2C(); // Enable I2C Receiver & Store EEPROM data to Point buffer
I2C_Done();
dptr++;
length--;
if (length==0) NotAckI2C();
else AckI2C(); // Continue read next data, send a acknowledge to EEPROM
I2C_Done();
}
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
}
//************************************************
//* Initial I2C Master Mode with 7 bits Address *
//* Clock Speed : 100KHz @4MHz *
//************************************************
void Initialize_I2C_Master(void)
{
OpenI2C(MASTER,SLEW_ON);
SSPADD= 9;
}
上一篇:128X64帶中文字庫的液晶模塊應用與程序
下一篇:51單片機W77E58的存儲器訪問
推薦閱讀
史海拾趣
在快速發展的過程中,Erocore意識到供應鏈管理的重要性。公司開始優化供應鏈流程,降低采購成本,提高生產效率。通過與供應商建立長期穩定的合作關系,Erocore在保證產品質量的同時,實現了成本的有效控制。這一舉措為公司在激烈的市場競爭中贏得了更多的優勢。
DMC Tools公司高度重視人才培養和引進。公司定期舉辦各種培訓活動,提高員工的技能水平和綜合素質。同時,公司還積極引進高端人才,為公司的技術創新和產品研發提供有力支持。這些人才為公司的發展注入了新的活力,推動了公司的持續進步。
隨著業務的不斷擴展,FORYARD意識到全球化布局的重要性。1995年,公司決定在中國設立研發中心和生產基地,以利用當地豐富的人才資源和成本優勢。這一戰略決策極大地提升了FORYARD的產能和研發效率,同時也使其能夠更好地服務全球客戶。此后,FORYARD還陸續在歐洲、亞洲等地建立了多個分支機構,形成了覆蓋全球的研發、生產和銷售網絡。通過全球化布局,FORYARD不僅增強了自身的市場競爭力,還促進了全球電子產業的交流與合作。
Bel Fuse公司的創立可以追溯到XXXX年,當時它由幾位富有遠見的工程師在新澤西州創立。起初,公司專注于保險絲的研發和生產,致力于提供高質量的電氣保護產品。憑借卓越的技術和可靠的產品質量,Bel Fuse逐漸在市場上獲得了認可,并開始擴展其產品線。
在近年來,隨著人們對環境保護意識的提高,電子行業也開始注重綠色環保和可持續發展。Connor-Winfield積極響應這一趨勢,將綠色環保理念融入到產品設計和生產過程中。公司采用環保材料和節能技術,努力降低產品對環境的影響。同時,公司還積極參與各種環保活動,推動電子行業的綠色發展。
以上是關于Connor-Winfield在電子行業中的5個發展故事。這些故事雖然可能并不完全準確反映公司的實際發展歷程,但從中我們可以看出,Connor-Winfield在發展過程中始終注重技術創新、品質提升、市場拓展和環保可持續發展等方面的工作,這些努力使得公司在激烈的市場競爭中保持了領先地位。
預測題:簡易數字顯示聲音信號采集測量儀(地方題) 一、 任務 設計并制作一個簡易數字顯示聲音信號采集測量儀,示意圖如圖1所示。 圖1 簡易數字顯示聲音信號采集測量儀示意圖 二、要求 1、 基本要求 (1) 聲級測量 a、測 ...… 查看全部問答∨ |
雖然我們享受著固態技術的發展所帶來的巨大好處,但在實際上,處于這一技術核心地位的微電子技術卻依賴著清潔電能。更快的速度和更低的電壓,意味著對電能質量有著更高的要求。 電能質量(PQ)涉及面較廣,從像電壓突降、驟升、中斷以及瞬變這樣的 ...… 查看全部問答∨ |
我現在在制作酒精測試儀,用的是MQ-3酒精傳感器,其用處是:將MQ-3采集到的數據進過AD轉換后,通過單片機處理,最后在LCD上顯示。 我不知道酒精濃度與電壓的對應關系是怎么樣,沒有數據手冊。各位大師,誰用過的告訴一下 ...… 查看全部問答∨ |
|
P2DIR |= BIT0+BIT1+BIT2+BIT6; 用以上程序語句初始化P2DIR后,UART0居然不能工作了!!!! 沒有以上的程序語句就可以。 實在不明白這是為什么!!!!???? 難道這BUG???不知道大家有沒有遇到過,我測試過N次了,才找到這個問題!! ...… 查看全部問答∨ |
|
現在正在索思達SSD-DM642 視頻開發板上進行開發,但是開發板的資料沒有。 請問大家誰有這個開發板的資料,就是板子上幾個簡單的視頻例程就可以了。 能不能發給我一份,謝謝大家了 我的郵箱:383487353@qq.com 再次感謝。… 查看全部問答∨ |