📄 main.c
字号:
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES. */
/* (C) Fujitsu Microelectronics Europe GmbH */
/*---------------------------------------------------------------------------
MAIN.C
- description
- See README.TXT for project description and disclaimer.
/*---------------------------------------------------------------------------*/
#include "mb90350.h"
unsigned char EEPROM_ERROR;
unsigned char EEpromErrFlag;
unsigned char I2C_Read_Reg[5];
unsigned char I2C_Write_Reg[5];
#define SCL PDR4_P45
#define SDA PDR4_P44
#define SCL_DIRE DDR4_D45
#define SDA_DIRE DDR4_D44
#define I2CBUS_TIMEOUT 3000
void UART_sendbyte (char ch);
extern struct
{
unsigned char Byte;
union
{
unsigned char item0: 1;
unsigned char item1: 1;
unsigned char item2: 1;
unsigned char item3: 1;
unsigned char item4: 1;
unsigned char item5: 1;
unsigned char item6: 1;
unsigned char item7: 1;
}bit;
}HardWareFault;
void I2C_Init(void)
{
ICCR0_EN = 0; // stop I2C interface
ICCR0_CS4 = 0; // CS4..0 : set prescaler
ICCR0_CS3 = 0;
ICCR0_CS2 = 1; //24M, 375kbps
ICCR0_CS1 = 0;
ICCR0_CS0 = 0;
ICCR0_EN = 1; // enable I2C interface
IDAR0 = 0; // clear data register
IBCR0_BER = 0; // clear bus error interrupt flag
IBCR0_BEIE = 0; // bus error interrupt disabled
IBCR0_ACK = 0; // no acknowledge generated
IBCR0_GCAA = 0; // no call acknowledge is generated
IBCR0_INTE = 0; // disable interrupt
IBCR0_INT = 0; // clear transfer interrupt request flag
}
static void I2C_Acknowlegde(void)
{
unsigned int Timeout_Counter;
Timeout_Counter=0x00;
while(IBSR0_LRB == 1)// no anwser from slave, program stucks here
{ // a timeout mechanism should be implemented here
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
}
static void I2C_Start(unsigned char slave_address)
{
unsigned int Timeout_Counter;
unsigned int ErrTimeout_Counter;
ErrTimeout_Counter=0x00;
do
{
IBCR0_BER = 0; // clear bus error interrupt flag
ICCR0_EN = 1; // enable I2C interface
IDAR0 = slave_address; // slave_address is sent out with start condition
IBCR0_MSS = 1; // set master mode and set start condition
IBCR0_INT = 0; // clear transfer end interrupt flag
Timeout_Counter=0x00;
while(IBCR0_INT == 0) // look if transfer is in process
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
ErrTimeout_Counter++;
if(ErrTimeout_Counter>=15)//10次bus-error之后直接报故障,次数需要再考虑?
{
ErrTimeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
while (IBCR0_BER == 1); // retry if Bus-Error detected
ErrTimeout_Counter=0x00;
while(IBSR0_LRB == 1) // no acknowledge means device not ready
{ // maybe last write cycle not ended yet
IBCR0_SCC = 1; // try restart (= continue)
Timeout_Counter=0x00;
while (IBCR0_INT == 0) // wait that transfer is finished
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
ErrTimeout_Counter++;
if(ErrTimeout_Counter>=300)//这个超时时间有待考虑,可能会导致较长时间等待?
{
ErrTimeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
}
static void I2C_Continue(unsigned char slave_address)
{
unsigned int Timeout_Counter;
IDAR0 = slave_address; // slave_address is sent out with start condition
IBCR0_SCC = 1; // restart (= continue)
Timeout_Counter=0x00;
while (IBCR0_INT == 0) // wait that transfer is finished
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
}
static void I2C_Stop(void)
{
unsigned int Timeout_Counter;
Timeout_Counter=0x00;
while (IBCR0_INT == 0) // wait that transfer is finished
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
IBCR0_MSS = 0; // change to slave and release stop condition
IBCR0_INT = 0;
Timeout_Counter=0x00;
while (IBSR0_BB) // wait till bus free
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
}
static void I2C_Write(unsigned char value)
{
unsigned int Timeout_Counter;
IDAR0 = value; // load data or address in to register
IBCR0_INT = 0; // clear transfer end intrerupt flag
Timeout_Counter=0x00;
while (IBCR0_INT == 0) // look if transfer is in process
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return;
}
}
I2C_Acknowlegde(); // wait for Acknowledge
}
static unsigned char I2C_Read(void)
{
unsigned int Timeout_Counter;
IBCR0_ACK = 1; // acknowledge has to be send
IBCR0_INT = 0; // clear transfer end interrupt flag
Timeout_Counter=0x00;
while (IBCR0_INT == 0) // wait that transfer is finished
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return 0xff;
}
}
return(IDAR0); // read received data out
}
static unsigned char I2C_LastRead(void)
{
unsigned int Timeout_Counter;
IBCR0_ACK = 0; // acknowledge has to not be sent after last byte
IBCR0_INT = 0; // clear transfer end interrupt flag
Timeout_Counter=0x00;
while(IBCR0_INT == 0) // wait that transfer is finished
{
Timeout_Counter++;
if(Timeout_Counter>=3000)
{
Timeout_Counter=0x00;
EEpromErrFlag = 1;
return 0xff;
}
}
return(IDAR0); // read received data out
}
void Data_Write_Single(unsigned char Add, unsigned char Data)
{
I2C_Start(0xA0);
I2C_Write(Add);
I2C_Write(Data);
I2C_Stop();
//Eeprom_WP=1;
}
void Data_Write_Conti(unsigned char Add, unsigned char Num)
{
unsigned char i;
I2C_Start(0xA0);
I2C_Write(Add);
for(i=0;i<Num;i++)
I2C_Write(I2C_Write_Reg[i]);
I2C_Stop();
}
unsigned char Data_Read_Single(unsigned int Add)
{
unsigned char Data;
I2C_Start(0xA0);
I2C_Write(Add);
I2C_Continue(0xA1);
Data=I2C_LastRead();
I2C_Stop();
return Data;
}
void Data_Read_Conti(unsigned int Add, unsigned char Num)
{
unsigned char i;
I2C_Start(0xA0);
I2C_Write(Add);
I2C_Continue(0xA1);
for(i=0;i<Num-1;i++)
I2C_Read_Reg[i]=I2C_Read();
I2C_Read_Reg[Num-1]=I2C_LastRead();
I2C_Stop();
}
/*****************************************************************************/
/* UART */
/*****************************************************************************/
void InitUart3(void)
{
// Initialize UART3 asynchronous mode
BGR3 = 1666; // 9600 Baud @ 16MHz
SCR3 = 0x17; // 8N1
SMR3 = 0x0d; // enable SOT3, Reset, normal mode
SSR3 = 0x02; // LSB first Enables Reception Interrupt
DDR1_D12 = 0; // enable SIN3 SET MOD-> IN
}//end InitUart3
void UART_sendbyte (char ch)
{
while (!SSR3_TDRE);
TDR3 = ch;
}
char UART_readbyte_wait (void)
{
while(!SSR3_RDRF); // wait, until byte is received
return (RDR3); // return received byte
}
void UART_sendstring (const char *string)
{
unsigned int i;
for (i=0; i<strlen(string); i++)
{
if (string[i] == 10)
UART_sendbyte(13);
UART_sendbyte(string[i]);
}
}
void delay(unsigned int i)
{
volatile unsigned int j;
i = i * 120;
for (j=0; j<i; j++);
}
void main(void)
{
unsigned char ch1;
unsigned char ch2;
unsigned char ch3;
unsigned char ch4;
unsigned char ch5;
unsigned char ch6;
unsigned char ch7;
unsigned char ch8;
//unsigned char i;
InitIrqLevels();
__set_il(7); // allow all IQR levels
__EI(); // globaly enable interrupts
InitUart3();
I2C_Init();
DDR1_D10 = 1;
//SCL_DIRE = 1;
//SDA_DIRE = 1;
//SCL = 0;
//SDA = 0;
PDR1_P10 = 1;
delay(1000);
PDR1_P10 = 0;
//UART_sendbyte(0x10);
/*I2C_Start(0xA0);
//UART_sendbyte(0x10);
I2C_Write(0x10);
I2C_Write(0x11);
I2C_Write(0xaa);
I2C_Write(0x55);
I2C_Stop();*/
/*I2C_Start(0xA0);
I2C_Write(0x10);
I2C_Write(0x11);
I2C_Restart(0xA1);
ch1 = I2C_Read();
//ch2 = I2C_Read();
ch3 = I2C_LastRead();
I2C_Stop();
UART_sendbyte(ch1);
//UART_sendbyte(ch2); // 将读出的数值发送出去
UART_sendbyte(ch3);
UART_sendbyte(0x10);
/*I2C_Start(0xA0);
I2C_Write(0x20);
I2C_Write(0x21);
I2C_Write(0x22);
I2C_Write(0x05);
I2C_Write(0x03);
I2C_Write(0x01);
I2C_Stop();
I2C_Start(0xA0);
I2C_Write(0x30);
I2C_Write(0x31);
I2C_Write(0x32);
I2C_Write(0x05);
I2C_Write(0x03);
I2C_Write(0x01);
I2C_Stop();*/
/*I2C_Start(0xA0);
I2C_Write(0x20);
I2C_Write(0x21);
I2C_Write(0x22);
I2C_Write(0x05);
I2C_Write(0x03);
I2C_Write(0x01);
I2C_Stop();*/
/*I2C_Start(0xA0);
I2C_Write(0x20);
I2C_Write(0x21);
I2C_Write(0x22);
//I2C_Write(0x23);
I2C_Restart(0xA1);
ch1 = I2C_Read();
ch2 = I2C_Read();
//ch3 = I2C_Read();
ch4 = I2C_LastRead();
I2C_Stop();
UART_sendbyte(ch1);
UART_sendbyte(ch2); // 将读出的数值发送出去
//UART_sendbyte(ch3);
UART_sendbyte(ch4);
UART_sendbyte(0x10);
I2C_Start(0xA0);
I2C_Write(0x30);
//I2C_Write(0x31);
//I2C_Write(0x32);
I2C_Restart(0xA1);
//ch1 = I2C_Read();
//ch2 = I2C_Read();
ch1 = I2C_LastRead();
I2C_Stop();
I2C_Start(0xA0);
//I2C_Write(0x30);
I2C_Write(0x31);
//I2C_Write(0x32);
I2C_Restart(0xA1);
//ch1 = I2C_Read();
//ch2 = I2C_Read();
ch3 = I2C_LastRead();
I2C_Stop();
UART_sendbyte(ch1);
//UART_sendbyte(ch2); //将读出的数值发送出去
UART_sendbyte(ch3);
UART_sendbyte(0x10);
UART_sendbyte(EEpromErrFlag);
/*Data_Read_Conti(0x10, 2);
for(i = 0; i < 2; i++)
{
UART_sendbyte(I2C_Read_Reg[i]);
}
Data_Read_Conti(0x20, 3);
for(i = 0; i < 3; i++)
{
UART_sendbyte(I2C_Read_Reg[i]);
}*/
Data_Write_Single(0x10,0x55);
Data_Write_Single(0x11,0xaa);
Data_Write_Single(0x20,0x03);
Data_Write_Single(0x21,0x05);
Data_Write_Single(0x22,0x01);
Data_Write_Single(0x30,0x03);
Data_Write_Single(0x31,0x05);
Data_Write_Single(0x32,0x01);
ch1 = Data_Read_Single(0x10);
ch2 = Data_Read_Single(0x11);
ch3 = Data_Read_Single(0x20);
ch4 = Data_Read_Single(0x21);
ch5 = Data_Read_Single(0x22);
ch6 = Data_Read_Single(0x30);
ch7 = Data_Read_Single(0x31);
ch8 = Data_Read_Single(0x32);
UART_sendbyte(ch1);
UART_sendbyte(ch2);
UART_sendbyte(ch3);
UART_sendbyte(ch4);
UART_sendbyte(ch5);
UART_sendbyte(ch6);
UART_sendbyte(ch7);
UART_sendbyte(ch8);
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -