zoukankan      html  css  js  c++  java
  • I2C模块 外接EEPROM和相关知识点

    /*
    * File: main.c
    * Author: Colder
    *
    * Created on 2016-11-17,5:02
    *
    */


    #include <stdio.h>
    #include <stdlib.h>
    #include "p30f6014.h"


    unsigned long int i;
    unsigned int WriteTable[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
    unsigned int ReadTable[16];

    unsigned int ControlByteW = 0xa0;
    unsigned int ControlByteR = 0xa1;

    unsigned int AddressH = 0x00;
    unsigned int AddressL = 0x00;

    void ConfigeI2CEEPROM(){

    SRbits.IPL = 7;//禁用中断
    TRISG = TRISG&0xfeff;
    I2CCONbits.I2CEN = 1;//s使能I2C模块
    I2CBRG = 0xFA;
    }

    void PageWrite(){

    I2CCONbits.SEN = 1;
    while(I2CCONbits.SEN == 1);
    I2CTRN = ControlByteW;
    while(I2CSTATbits.TRSTAT == 1);
    I2CTRN = AddressH;
    while(I2CSTATbits.TRSTAT == 1);
    I2CTRN = AddressL;
    while(I2CSTATbits.TRSTAT == 1);
    for(i = 0;i<16;i++){

    I2CTRN = WriteTable[i];
    while(I2CSTATbits.TRSTAT == 1);
    }
    I2CCONbits.PEN = 1;
    while(I2CSTATbits.TRSTAT == 1);
    }

    void PageRead(){

    I2CCONbits.SEN = 1;
    while(I2CCONbits.SEN == 1);
    I2CTRN = ControlByteW;
    while(I2CSTATbits.TRSTAT == 1);
    I2CTRN = AddressH;
    while(I2CSTATbits.TRSTAT == 1);
    I2CTRN = AddressL;
    while(I2CSTATbits.TRSTAT == 1);
    I2CCONbits.RSEN = 1;
    while(I2CCONbits.RSEN == 1);
    I2CTRN = ControlByteR;
    while(I2CSTATbits.TRSTAT == 1);
    for(i=0;i<16;i++){

    I2CCONbits.RCEN = 1;
    while(I2CSTATbits.RBF == 0);
    ReadTable[i] = I2CRCV;
    I2CCONbits.ACKDT = 0;
    if(i==15)
    I2CCONbits.ACKDT = 1;
    I2CCONbits.ACKEN = 1;
    while(I2CCONbits.ACKEN == 1);
    }
    I2CCONbits.PEN = 1;
    while(I2CCONbits.PEN == 1);
    }
    int main(){

    ConfigeI2CEEPROM();
    PORTGbits.RG8 = 0;
    PageWrite();
    for(i=0;i<650000;i++);
    PageRead();
    while(1);
    }


    //***********************************
    // I2CRCV 接收寄存器
    // I2CTRN 发送寄存器
    // I2CBRG 波特率发生器
    // I2CCON 控制寄存器
    // I2CSTAT 状态寄存器
    // I2CADD 地址寄存器

    //************************************
    // I2CCON下的重要标志位:
    // ACKDT 应答数据位(工作于主控器件模式,适用于主控器件的接受过程),当软件起动应答序列时将发送该值
    // ACKEN 应答序列使能位(工作于主控器件,适用于主控器件接受过程).
    // RCEN 接收使能位(作为主控器件工作时)
    // PEN 停止条件使能位(作为主控器件工作时)
    // SEN 起动条件使能位-(起动后序列结束时,由硬件清0)


    //************************************
    // I2CSTAT下的重要标志位:
    // TRSTAT 发送状态位 (1-主控器件正在发送过程中)
    // RBF 接收缓冲器满状态位(1-接收完成,I2CRCV满/0-接收未完成,I2CRCV空)

    //***********************************

    // SI2CIF为从动器件中断,在检测到地址为从动器件地址时激活中断.下列事件会产生SI2CIF中断:

    //1.检测到有效器件地址(包括广播呼叫方式的地址)

    //2.发送数据的请求

    //3.接收到数据

  • 相关阅读:
    代表行为已成为习惯的信号有哪些?
    Java使用JDBC连接Oracle数据库
    JS正则表达式
    java实现内网通信
    纯前端代码实现美团外卖页面
    HTML绘制表格
    教你如何使用谷歌浏览器,亲测可用!
    Java 多线程实现多窗口同时售票简单功能
    实现获取命令行的返回结果
    HTML模仿实现京东登录页面
  • 原文地址:https://www.cnblogs.com/chenbokai/p/6076281.html
Copyright © 2011-2022 走看看