| |
---|---|
(点击题目可以在互联网中搜索该题目的相关内容) 日期:2007-11-18 18:34:46 来源: 作者: 点击: | |
//THIS CODE WAS DESIGNED TO DEMONSTRATE HOW THE FM24C04/FM24C04A SERIAL FRAM COULD //BE INTERFACED TO THE 8051 MICROCONTROLLER. THE INTERFACE USES 2 LINES //FROM PORT 2 (P2.7 AND P2.6) TO COMMUNICATE. //THE CODE SHOWN DEMONSTRATES A 'RANDOM READ' AND 'BYTE WRITE'. THE OTHER * //MODES OF OPERATION CAN BE CREATED BY EXPANDING UPON THESE ROUTINES. //深圳市华胄科技有限公司 //the first date writed:2003-11-20 //============================= #include #include #define uchar unsigned char #define uint unsigned int #define IIC_READ 0XA1 //定义读指令 #define IIC_WRITE 0XA0 //定义写指令 #define NOP _nop_() //#define SDA INT0 //#define SCL T0 //============================== uchar idata buff[32]; //save the byte read out form iic device in test operation uchar idata readbuff[32];//测试数组,暂存写入读出数据 //uchar writebuff[32]; //=============================== sbit SDA=P1^4;//P2^7; //THE SDA BIT IS PORT 2 BIT 7 sbit SCL=P1^7;//P2^6; //THE SCL BIT IS PORT 2 BIT 6 //sbit WP=P2^5; //sbit test=P1^0; //=============================== //define a bit_operation byte to use in shift operation //use this mode can achieve high operation speed uchar bdata bbyte;//定义位操作用数组,采用此方法可提高位操作速度 sbit a0=bbyte^0; sbit a1=bbyte^1; sbit a2=bbyte^2; sbit a3=bbyte^3; sbit a4=bbyte^4; sbit a5=bbyte^5; sbit a6=bbyte^6; sbit a7=bbyte^7; //======================================== bit IFACK; //record the SDA state to confirn if ACK has happened bit NO_ACK; //no ack flag bit BUS_FAULT; //bus fault flag //======================================== //FUNCTION:ROUTES TO PROVIDE A START SIGNAL void start(void) { SCL=0;SDA=1;SCL=1;SDA=0;SCL=0; } //======================================= //FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL void stop(void) { SCL=0;SDA=0;SCL=1;SDA=1;SCL=0; } //===================================== //FUNCTION:ROUTES TO PROVIDE ACK SINGAL void ack(void) { SCL=0;SDA=0;SCL=1;SCL=0; } //===================================== //FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL // OR TO PROVIDE A NO_ACK SIGNAL //type=1等待应答信号 //type=0 产生无应答信号 void nack(uchar type) { SCL=0;SDA=1;SCL=1;IFACK=SDA; SCL=0; if(type) { if(IFACK)//如果无应答信号,则置标志位NO_ACK,程序中止 { NO_ACK=1;//用户可以加入自己的异常处理程序 //while(1); } else NO_ACK=0; } } //======================================================= //FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE TO THE MASTER uchar inbyte(void) {//从IIC器件中读出数据 SDA=1; SCL=1;a7=SDA;SCL=0; SCL=1;a6=SDA;SCL=0; SCL=1;a5=SDA;SCL=0; SCL=1;a4=SDA;SCL=0; SCL=1;a3=SDA;SCL=0; SCL=1;a2=SDA;SCL=0; SCL=1;a1=SDA;SCL=0; SCL=1;a0=SDA;SCL=0; return(bbyte); } //======================================================= //FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER void outbyte(uchar outdata) {//将数据写放IIC器件 bbyte=outdata; SDA=a7;SCL=1;SCL=0; SDA=a6;SCL=1;SCL=0; SDA=a5;SCL=1;SCL=0; SDA=a4;SCL=1;SCL=0; SDA=a3;SCL=1;SCL=0; SDA=a2;SCL=1;SCL=0; SDA=a1;SCL=1;SCL=0; SDA=a0;SCL=1;SCL=0; } //====================================================== //FUNCTION:BYTE WRITE. 'add' THE WRITE ADDRESS, 'wbyte' THE DATA WANT TO WRITE 本新闻共2页,当前在第1页1 2 |
|
上一篇: SC-501/DE系列PLC以太网远程设备监控系统 下一篇: 移动视频传输解决方案 |