⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iar-

📁 IAR_example_EasyARM8962.zip
💻
字号:
/****************************************Copyright (c)*****************************************************
**                               Guangzhou ZHIYUAN electronics Co.,LTD.
**                                     
**                                 http:// www.embedtools.com 
**
**--------------File Info----------------------------------------------------------------------------------
** File Name:           I2C_24C02.c
** Last modified Date:  2007.9.21 
** Last Version:        v1.0
** Description:         Stellaris系列单片机模拟I2C主方式读写CAT24C02。
** 
**---------------------------------------------------------------------------------------------------------
** Created By:          kang qinhua
** Created date:        2007.9.21 
** Version:             v1.0
** Descriptions:		初始版本
**
**---------------------------------------------------------------------------------------------------------
** Modified by:         Kang qinhua
** Modified date:       2008.01.13
** Version:             v1.1
** Description:
**
**********************************************************************************************************/
#include "VI2C_LM3S101.H"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"

#ifndef uchar 
#define uchar     unsigned char
#endif
 
/**********************************************************************************************************
  I2C引脚的定义  
**********************************************************************************************************/                                                                        
#define SDA       GPIO_PIN_3                                            /*  模拟I2C数据传送位            */
#define SCL       GPIO_PIN_2                                            /*  模拟I2C时钟控制位            */
 
#define BEEP      GPIO_PIN_7                                            /*  数据判断的BEEP蜂鸣器指示     */


#define CSI24c02  0xA0                                                  /*  从机地址,注意需将原从机地址
                                                                            左移一位                     */
#define writeaddr 0x00                                                  /*  对24c02操作的子地址          */
#define readaddr  0x00

void  Delays(uchar nom);                                                /*  延时函数声明                 */

/**********************************************************************************************************
** Function name:         main	   
** Descriptions:          Main function of the project 工程的主函数
**                        通过模拟I2C主方式读写CAT24C02,先发送5个数据,然后再读取这5个数据,
**                        验证读回的数据是否与写入的一致,一致则蜂鸣器鸣叫一声,出错则长鸣。
** input parameters:      无
** output parameters:     无      
** Returned value:        无	 
**********************************************************************************************************/
int main(void)
{
    uchar   WDATA[5] = {0xAA, 0x55, 0xAA, 0x55, 0xAA};                  /*  主机向24C64写入的数据        */
    uchar   RDATA[5];                                                   /*  主机从24C64读出的数据        */
    unsigned  long  ulIdx = 0x00;
    
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);                                   /*  设置晶振为系统时钟           */
     
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);                        /*  使能本例所使用的外设         */
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    GPIODirModeSet(GPIO_PORTB_BASE, SDA | SCL, GPIO_DIR_MODE_OUT);      /*  配置相关引脚为I2C操作        */
                                                            
    GPIODirModeSet(GPIO_PORTA_BASE, BEEP, GPIO_DIR_MODE_OUT);           /*  配置蜂鸣器BEEP为输出         */

    GPIOPadConfigSet(GPIO_PORTB_BASE, SDA | SCL,			            /*  配置I2C的端口模式            */
                              GPIO_STRENGTH_4MA,
                              GPIO_PIN_TYPE_STD);
    
    GPIOPadConfigSet(GPIO_PORTA_BASE, BEEP,                             /*  配置蜂鸣器的端口模式         */                            
                         GPIO_STRENGTH_4MA,
                         GPIO_PIN_TYPE_STD);

    while(1) {                                                          /*  将一串数据写入EEPROM的前8个
                                                                            字节                         */
	    ISendStr(CSI24c02, writeaddr, WDATA, 5);                        /*  向24C02中写入数据            */
	    Delays(5);                                                      /*  等待主机向24C02中写入数据延时*/
	    IRcvStr(CSI24c02, readaddr, RDATA, 5);                          /*  从24C02中读出数据            */
	    Delays(5);                                                      /*  等待主机从24C02中读出数据延时*/
        for(ulIdx = 0; ulIdx < 5; ulIdx++) {
            if(WDATA[ulIdx] != RDATA[ulIdx]) {                          /*  判断接收到的数据是否正确     */
                GPIOPinWrite(GPIO_PORTA_BASE, BEEP, ~BEEP);             /*  如果接收的数据出错,则蜂鸣器
                                                                            长鸣                         */
                while(1) {
				    ;
				}
            }
        }
        GPIOPinWrite(GPIO_PORTA_BASE, BEEP, ~BEEP);                     /*  如果接收到的数据正确,则蜂鸣器
                                                                            鸣叫一声                     */
        Delays(3);  
        GPIOPinWrite(GPIO_PORTA_BASE, BEEP, BEEP);
        while(1) {
		    ;
		}
    }
}

/*********************************************************************************************************
** Function name:      Delays
** Descriptions:       延时子程序,当nom的值越大延时的时间越长
** input parameters:   nom: 延时参数
** output parameters:  无
** Returned value:     无   
*********************************************************************************************************/
void Delays(uchar nom)
{ 
    int i, j;
    for( ; nom > 0; nom--) { 
        for(i = 0; i < 150; i++) {
            for(j = 0; j < 255; j++);
	   	}
    }
}
/**********************************************************************************************************
 END FILE
**********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -