《匠人手记》推荐网上购书渠道:
EDN网(ednchina)购书入口   >>>
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历

百宝专栏

  • 首页 相册 标签
  • 电脑应用(65)
  • 供需信息(22)
  • 写书近况(82)
  • 匠人文集(115)
  • 硬件技术(171)
  • 匠人公告(86)
  • 与非门专栏(545)
  • 匠人笔记(115)
  • 团队撰写(96)
  • 汽车电子(52)
  • 编程技巧(465)
  • 程序宝典(476)
  • 网络酷文(472)
  • 开发工具(19)
  • 资料宝藏(274)
  • 项目管理(11)
  • 藏经宝阁(42)
  • 趣味设计(5)
  • 社区热贴(2)
  • 比尔盖茨熊专栏(0) 
  • 百宝信息

    载入中...

    百宝流量

    (2006-07-01开始)



    匠人手记

    PIC:以C 语言完成读写24LCxx系列的EEPROM的实例.c
    程序匠人 发表于 2005-6-3 21:44:00  阅读全文 | 回复(0) | 引用通告 | 编辑

    //************************************************************************ 
    //*            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  */

    #i nclude <p18f452.h>
    #i nclude <i2c.h>   // Load I2C Header file from defult direct
    #i nclude <timers.h>
    #i nclude "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;
    }

     

     

    看《匠人手记》,与匠人同行!北航出版,正在热卖!

    发表评论:
    载入中...

    芯片专题

    器件专题

    软件专题

    硬件专题

    综合专题

    项目专题

    原创专题

    器件检测
    LCD LED
    按键 触摸键
    E2PROM
    电池 电机
    电阻 电容 电感

    指令系统
    软件算法
    编程规范
    滤波算法
    串行通讯

    PCB设计
    I2C PWM
    红外遥控
    充电技术
    中断 ADC 

    匠人手记
    匠人夜话
    网络心路
    一周热点串烧
    从零开始玩PIC
    DIY旋转时钟

    广告5号位 [投放]


    学习板、开发板、编程器、下载器、仿真器(查看详情……)

    站内搜索


    站外搜索


    百度  google
    mp3  歌词 
    图片  FLASH 
    知道  文档
    新闻  词典 
    地图  mp3 
    软件  天网 
    雅虎  爱问 
    搜狗  讯雷 
    网讯  华军 
    天空 

    21IC器件搜索
    百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     
     

    新鲜货色

    匠人手记

    近期动态

    载入中...

      《匠人手记》购书全攻略 
     书友近况:淘书手记答疑与讨论:什么是散转程序 
     《匠人手记》新书艳照
     EDN《匠人手记》签名售书优惠活动开始报名啦!
     欢迎加入《匠人手记》EDN书友会
     欢迎加入《匠人手记》书友会Q群
     《匠人手记》终稿目录
     《匠人手记》封面,请大家先睹为快
     上周六收到了北航寄来的《匠人手记》清样,让大家先睹为快

    匠人原创

    粉丝评论

    往日酷贴

    载入中...

    载入中...



     网络酷文:博客,改变的不仅仅是图书 
     网络酷文:C语言宏定义技巧C语言 条件编译详解

      21IC上海2008-04聚会报名进行中。。。 
     两分钟让你明白什么是ERP![转]
      神奇的Duff's Device 算法
      实用一线通讯电路及软件设计方法
      程序员的“七年之痒”
      史上最短但最精彩的武侠小说
      网络无厘头文学《缺钙水浒》(爆笑)

     你的博客还能持续多久(转贴)
     电动车无刷电机控制器软件设计要点(作者:谢渊斌)

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!