- #include
- #include "2440addr.h"
- #include "2440lib.h"
- #include "Option.h"
- #include "def.h"
- int flag; //interrupt flag (cleared in the interrupt subroutine, i.e. flag=1 before interruption, flag=0 after interruption)
- void __irq IicInt(void);
- void Wr24C02(U32 slvAddr,U32 addr,U8 data);
- void Rd24C02(U32 slvAddr,U32 addr,U8 *data);
- void Main(void)
- {
- unsigned int i,j;
- static U8 data[256]; //Used to store the data read out by AT24C02
- SelectFclk(2); //Set the system clock to 400M
- ChangeClockDivider(2, 1); //Set the frequency division to 1:4:8
- CalcBusClk(); //Calculate bus frequency
- rGPHCON &=~((3<<4)|(3<<6));
- rGPHCON |=(2<<4)|(2<<6); //GPH2--TXD[0];GPH3--RXD[0]
- rGPHUP=0x00; //Enable pull-up function
- Uart_Init(0,115200);
- Uart_Select(0);
- Uart_Printf("[ IIC Test(Polling) using AT24C020 ]\n");
- rGPEUP |= 0xc000; //Close the pull-up
- rGPECON &= ~0xf0000000;
- rGPECON |= 0xa0000000; //GPE15:IICSDA , GPE14:IICSCL
- pISR_IIC=(unsigned)IicInt; //Set the interrupt program address
- rINTMSK &=~(BIT_IIC); //Open interrupt source
- //Enable response, IIC bus clock IICCLK=PCLK/16, enable interrupt, send clock IICCLK/16
- rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);
- rIICADD = 0x10; //2440 slave address = [7:1]
- rIICSTAT = 0x10; //IIC bus data output enable (Rx/Tx)
- Uart_Printf("Write test data into AT24C02\n");
- for(i=0;i<256;i++)
- Wr24C02(0xa0,(U8)i,i); //Write data to AT24C02
- for(i=0;i<256;i++) // array data clear
- data[i] = 0;
- Uart_Printf("Read test data from AT24C02\n");
- for(i=0;i<256;i++)
- Rd24C02(0xa0,(U8)i,&(data[i])); //Read the data from AT24C02 and put it into the data array
- for(i=0;i<16;i++)
- {
- for(j=0;j<16;j++)
- Uart_Printf("%2x ",data[i*16+j]); //Print the data read from AT24C02
- Uart_Printf("\n");
- }
- }
- void Wr24C02(U32 slvAddr, U32 addr, U8 data)
- {
- flag=1;
- rIICDS = slvAddr; //Write data shift register from device address
- rIICSTAT=0xf0; //Master mode, generate start signal, enable Rx/Tx
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- flag=1;
- rIICDS = addr; //Write the address of the storage byte to the data shift register
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- flag=1;
- rIICDS = data; //Write the data to be sent into the data shift register
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- rIICSTAT = 0xd0; // Disable Rx/Tx
- rIICCON = 0xaf; //Resumes IIC operation.
- Delay(1);
- }
- void Rd24C02(U32 slvAddr, U32 addr, U8 *data)
- {
- U8 temp;
- flag=1;
- rIICDS = slvAddr; //Write data shift register from device address
- rIICSTAT=0xf0; //Master mode, generate start signal, enable Rx/Tx
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- flag=1;
- rIICDS = addr; //Write the address of the storage byte to the data shift register
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- flag=1;
- rIICDS = slvAddr;
- rIICSTAT=0xb0; //Master receiving mode, enable Rx/Tx
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1) //Wait for sending to complete
- Delay(1);
- //Note: Reading the following byte must be done because after sending the slave device address with the read command,
- //AT24C02A will return a slave device address or slave device memory address as a response, so
- // Be sure to discard the byte after reading it, because it is not the information we want to read;
- flag=1;
- temp=rIICDS;
- rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag
- while(flag==1)
- Delay(1);
- rIICCON=0x2f; //Resumes IIC operation. Disable response
- *data=rIICDS;
- Delay(1);
- rIICSTAT = 0x90; //Stop MasRx condition
- rIICCON = 0xaf; //Resumes IIC operation.
- Delay(1); //Wait until stop condtion is in effect.
- }
- void __irq IicInt(void)
- {
- ClearPending(BIT_IIC);
- flag=0;
- }
Previous article:Some details of the differences between s3c2410 and s3c2440
Next article:S3C2440 IIC bus interface
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
Guess you like
- [Silicon Labs Development Kit Review] TensorFlow Application Code Analysis Based on Deep Learning
- Direction of electrical energy transfer in a circuit
- Share a reference implementation of path planning, the key is that there are animated pictures!
- TMS320C6678 chip TI original board development
- How to configure practical Euler angles for sensortile box?
- Participate in the survey and get a 50-yuan JD card. The number of places is limited and will be given away while stocks last!
- How to Design an RTC Circuit
- Xunwei IMX6 development board QT system Sqlite3 transplantation and use
- DSP Transplantation and Optimization of H.264 Encoder
- Solutions to reduce noise in op amp circuits