Beruflich Dokumente
Kultur Dokumente
h>
#include <stdio.h>
char addr;
void delay1(void );
void delay(void );
int atoh(int ch);
void serial_tx(int ch);
void string_tx(char *a);
void UARTInit();
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/************************************ Macros
*******************************************/
#define UP
1<<20
#define INCR
1<<21
#define DECR
1<<22
#define MAX
10
#define
AA
#define
SI
#define
STO
#define
STA
#define
I2EN 6
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Declarations
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void I2C_ISR(void)__irq;
char ReLoad[MAX] =
{0x00/*Address Low Bits*/,0x00/*Address Low
Bits*/,'L','P','C','-','2','1','4','8'};
char Buff[MAX]
=
{0x00/*Address Low Bits*/,0x00/*Address Low
Bits*/,'L','P','C','-','2','1','4','8'};
{"NO-DATA!"};
0;
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Delay(void)
{
unsigned int i,j;
for(i=0;i<150;i++)
for(j=0;j<900;j++);
}
0x00050050;
I2C0SCLH
150;
I2C0SCLL
30MHz
150;
1 << I2EN;
// Enable I2C 0
I2C0CONSET =
{
I2C0CONCLR =
1 << STO;
I2C0CONSET =
1 << AA;
I2C0CONSET =
1 << STA;
return 0;
}
void delay1(void )
{
volatile int i,j;
for (i=0;i<5;i++)
for (j=0;j<50;j++);
}
void delay(void )
{
volatile int i,j;
for (i=0;i<100;i++)
for (j=0;j<500;j++);
}
int atoh(int ch)
{
if(ch<=0x09)
ch = ch + 0x30;
else
ch = ch + 0x37;
return(ch);
}
/****************** serial transmission routine ***************/
void serial_tx(int ch)
{
int i;
U1THR = ch;for(i=0;i<15000;i++);
}
void string_tx(char *a)
{
int i;
while(*a!='\0')
{
// while((U1LSR&0X20)!=0X20);
U1THR=*a;for(i=0;i<15000;i++);
a++;
}
int main(void)
{
int i;
PINSEL0 |= 0x00050050; // Enable rx,tx
U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit
U1DLM = 0x00;
U1DLL = 0x5D;//U1DLL = u1dl & 0xFF;
U1LCR = 0x00000003; // DLAB =0
VICIntSelect = 0<<9;
VICVectCntl0
= 0x020 | 9 ;
VICVectAddr0
= (unsigned long)I2C_ISR;
VICIntEnable
= 1<<9;
I2C0
/*
*/
I2C_Init();
//UART0_PutS ("[2J\0");
// Clear Screen
string_tx ("[~] Press INCR ON Key Erase data from EEPROM \n\r");
while (1)
{
if ((IOPIN1 & UP) == 0)
Default Data to the EEPROM ...*/
{
ii = 0;
while (ii < MAX)
Default Data to Buff
//Re-load
{
Buff [ii]
ReLoad [ii];
ii++;
}
Wait (5000);
flag
'W';
I2C_Start (0x70);
Erase = 0;
for (i=0;i<30;i++) Wait(10000);
I2C0CONCLR =
1 << SI;
flag
'R';
I2C_Start (0x70);
I2C0CONCLR =
1 << SI;
if (Ready == 'T')
//Check if
Ready = TRUE
{
ii=0;
string_tx("\n\r The Read Data are: \t");
while (ii<MAX)
{
//U0THR
'\n';
Rec[ii];
Wait (1000);
U1THR
Wait (1000);
ii++;
}
serial_tx (0x0d);
serial_tx (0x0e);
Wait (1000);
Ready
'F';
}
while (!(IOPIN1 & INCR));
Wait (5000);Wait (5000);Wait (5000);Wait (5000);
}
/*To
{
ii = 2;
while (ii < MAX)
{
Buff[ii]
0xFF;
0xFF to EEPROM
ii++;
}
flag
'W';
Erase = 1;
I2C_Start (0x70);
I2C0CONCLR =
1 << SI;
//Load
switch (I2C0STAT)
{
case (0x08) :/*... A START condition has been Transmitted...*/
if (flag == 'W')
{
I2C0CONCLR =
1 << STO;
I2C0CONCLR =
1 << STA;
I2C0DAT
I2C0CONCLR =
1 << SI;
}
else if (flag == 'R')
{
//I2C0CONCLR
STO;
1 <<
I2C0DAT
0xA0;
1 << SI;
}
index =
0;
break;
case (0x10) :
Transmitted...*/
I2C0CONCLR =
1 << STA;
if (flag == 'W')
I2C0DAT
0xA0;
1 << STA;
I2C0CONCLR =
1 << STO;
I2C0DAT
I2C0CONCLR =
1 << SI;
0xA1;
index = 0;
}
break;
case (0x18) :
been received....*/
I2C0CONCLR =
0x20;
I2C0CONCLR =
0x38;
I2C0DAT
index++;
break;
case (0x20) :
has been received....*/
I2C0CONCLR =
0x20;
I2C0CONCLR =
0x38;
I2C0DAT
index++;
break;
case (0x28) :
ACK has been received....*/
if (flag == 'W')
{
I2C0DAT
I2C0CONCLR =
0x20;
I2C0CONCLR =
0x38;
Buff[index];
//Clear START Bit
//Clear all flags (STA, STO, SI)
}
'R')
0x18;
I2C0CONSET =
1 <<
STA;
}
else
// Send Upper Nibble of Adress to Read the Data...
{
I2C0DAT
I2C0CONCLR =
0x20;
I2C0CONCLR =
0x38;
Buff[index];
//Clear START Bit
//Clear all flags (STA, STO, SI)
index++;
}
else
//Stop Transmission to EEPROM.... Data Count Reset..
{
index = 0;
flag = 'R';
I2C0CONSET =
1 << STO;
}
break;
I2C0CONCLR =
1 << STA;
I2C0CONCLR =
1 << SI;
case (0x30) :
/*... Data byte in I2DAT has been
transmitted; NOT ACK has been received ...*/
I2C0CONCLR =
0x20;
Buff[index];
index++;
}
else
{
index = 0;
flag = 0;
I2C0CONSET =
0x10;
I2C0CONCLR =
1 << SI;
}
break;
case (0x38) :
bytes ....*/
I2C0CONSET =
0x20;
break;
case (0x40) :
been received.*/
I2C0CONSET =
1 << AA;
I2C0CONCLR =
1 << STA;
I2C0CONCLR =
1 << STO;
I2C0CONCLR =
1 << SI;
break;
case (0x48) :
has been received ...*/
I2C0CONSET =
1 << STA;
//Repeated Start
break;
case (0x50) :
I2C0CONSET =
if (index < MAX)
{
1 << AA;
Rec [index] =
I2C0DAT;
index++;
}
else
{
I2C0CONSET =
1 << STO;
I2C0CONCLR =
1 << SI;
index = 0;
Ready =
'T';
case (0x58) :
has been returned ....*/
Rec [index] =
I2C0DAT;
I2C0CONCLR =
1 << STA;
I2C0CONCLR =
1 << STO;
break;
}
}
I2C0CONCLR =
1 << SI;
VICVectAddr =
0x00;