S3C2440IIC interrupt mode

Publisher:QianfengLatest update time:2016-04-18 Source: eefocusKeywords:S3C2440  IIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. #include   
  2. #include "2440addr.h"  
  3. #include "2440lib.h"  
  4. #include "Option.h"  
  5. #include "def.h"  
  6.   
  7. int flag; //interrupt flag (cleared in the interrupt subroutine, i.e. flag=1 before interruption, flag=0 after interruption)  
  8.   
  9. void __irq IicInt(void);   
  10. void Wr24C02(U32 slvAddr,U32 addr,U8 data);       
  11. void Rd24C02(U32 slvAddr,U32 addr,U8 *data);      
  12.   
  13. void Main(void)  
  14. {  
  15.     unsigned int i,j;  
  16.     static U8 data[256]; //Used to store the data read out by AT24C02  
  17.       
  18.     SelectFclk(2); //Set the system clock to 400M       
  19.     ChangeClockDivider(2, 1); //Set the frequency division to 1:4:8  
  20.     CalcBusClk(); //Calculate bus frequency  
  21.       
  22.     rGPHCON &=~((3<<4)|(3<<6));     
  23.     rGPHCON |=(2<<4)|(2<<6);    //GPH2--TXD[0];GPH3--RXD[0]     
  24.         
  25.     rGPHUP=0x00; //Enable pull-up function  
  26.           
  27.     Uart_Init(0,115200);  
  28.     Uart_Select(0);  
  29.       
  30.     Uart_Printf("[ IIC Test(Polling) using AT24C020 ]\n");  
  31.   
  32.     rGPEUP |= 0xc000; //Close the pull-up  
  33.       
  34.     rGPECON &= ~0xf0000000;  
  35.     rGPECON |= 0xa0000000;       //GPE15:IICSDA , GPE14:IICSCL    
  36.       
  37.     pISR_IIC=(unsigned)IicInt; //Set the interrupt program address  
  38.     rINTMSK &=~(BIT_IIC); //Open interrupt source    
  39.   
  40.     //Enable response, IIC bus clock IICCLK=PCLK/16, enable interrupt, send clock IICCLK/16  
  41.     rIICCON  = (1<<7) | (0<<6) | (1<<5) | (0xf);  
  42.   
  43.     rIICADD = 0x10; //2440 slave address = [7:1]  
  44.     rIICSTAT = 0x10; //IIC bus data output enable (Rx/Tx)  
  45.       
  46.     Uart_Printf("Write test data into AT24C02\n");  
  47.   
  48.     for(i=0;i<256;i++)  
  49.         Wr24C02(0xa0,(U8)i,i); //Write data to AT24C02  
  50.           
  51.     for(i=0;i<256;i++) // array data clear  
  52.         data[i] = 0;  
  53.   
  54.     Uart_Printf("Read test data from AT24C02\n");  
  55.     for(i=0;i<256;i++)  
  56.         Rd24C02(0xa0,(U8)i,&(data[i])); //Read the data from AT24C02 and put it into the data array  
  57.   
  58.     for(i=0;i<16;i++)  
  59.     {  
  60.         for(j=0;j<16;j++)  
  61.             Uart_Printf("%2x ",data[i*16+j]); //Print the data read from AT24C02  
  62.         Uart_Printf("\n");  
  63.     }     
  64. }  
  65.   
  66. void Wr24C02(U32 slvAddr, U32 addr, U8 data)  
  67. {  
  68.     flag=1;   
  69.     rIICDS = slvAddr; //Write data shift register from device address  
  70.     rIICSTAT=0xf0; //Master mode, generate start signal, enable Rx/Tx  
  71.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  72.     while(flag==1) //Wait for sending to complete  
  73.     Delay(1);  
  74.           
  75.     flag=1;  
  76.     rIICDS = addr; //Write the address of the storage byte to the data shift register  
  77.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  78.     while(flag==1) //Wait for sending to complete  
  79.     Delay(1);  
  80.       
  81.     flag=1;  
  82.     rIICDS = data; //Write the data to be sent into the data shift register  
  83.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  84.     while(flag==1) //Wait for sending to complete  
  85.     Delay(1);  
  86.           
  87.     rIICSTAT = 0xd0; // Disable Rx/Tx  
  88.     rIICCON = 0xaf;     //Resumes IIC operation.   
  89.     Delay(1);  
  90. }  
  91.   
  92. void Rd24C02(U32 slvAddr, U32 addr, U8 *data)  
  93. {  
  94.     U8 temp;  
  95.     flag=1;  
  96.     rIICDS = slvAddr; //Write data shift register from device address  
  97.     rIICSTAT=0xf0; //Master mode, generate start signal, enable Rx/Tx  
  98.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  99.     while(flag==1) //Wait for sending to complete  
  100.     Delay(1);  
  101.       
  102.     flag=1;  
  103.     rIICDS = addr; //Write the address of the storage byte to the data shift register  
  104.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  105.     while(flag==1) //Wait for sending to complete  
  106.     Delay(1);  
  107.       
  108.     flag=1;  
  109.     rIICDS = slvAddr;     
  110.     rIICSTAT=0xb0; //Master receiving mode, enable Rx/Tx  
  111.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  112.     while(flag==1) //Wait for sending to complete  
  113.     Delay(1);  
  114.       
  115.      //Note: Reading the following byte must be done because after sending the slave device address with the read command,     
  116.     //AT24C02A will return a slave device address or slave device memory address as a response, so  
  117.     // Be sure to discard the byte after reading it, because it is not the information we want to read;  
  118.     flag=1;  
  119.     temp=rIICDS;  
  120.     rIICCON &=~0x10; //Clear Tx/Rx interrupt pending flag  
  121.     while(flag==1)  
  122.     Delay(1);  
  123.       
  124.     rIICCON=0x2f; //Resumes IIC operation. Disable response  
  125.     *data=rIICDS;  
  126.     Delay(1);  
  127.       
  128.     rIICSTAT = 0x90;                //Stop MasRx condition      
  129.     rIICCON  = 0xaf;                //Resumes IIC operation.     
  130.     Delay(1);                       //Wait until stop condtion is in effect.  
  131.  }  
  132.    
  133. void __irq IicInt(void)  
  134. {  
  135.     ClearPending(BIT_IIC);  
  136.     flag=0;  
  137. }  
Keywords:S3C2440  IIC Reference address:S3C2440IIC interrupt mode

Previous article:Some details of the differences between s3c2410 and s3c2440
Next article:S3C2440 IIC bus interface

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号