#i nclude
IC.h>
#i nclude
#define uchar unsigned char
#define uint unsigned int
static volatile unsigned char aCC @ 0x20; //发收数据暂存器
static volatile bit acc0 @ (unsigned)&acc*8+0; //接收
static volatile bit acc7 @ (unsigned)&acc*8+7; //发送
static volatile unsigned char err @ 0x21; //
static volatile bit err0 @ (unsigned)&err*8+0;
static volatile bit err3 @ (unsigned)&err*8+3;
static volatile bit err6 @ (unsigned)&err*8+6;
static volatile bit err7 @ (unsigned)&err*8+7; //应答位暂存位
void Cack(); //应答位检查
void Mnack(); //发送非应答位
void Mack(); //发送应答位
void Stop(); //停止IC总线
void Sta(); //启动IC总线
void Wrbyt (uchar x) ; //写一字节
uchar Rdbyt( ); //读一字节
void Rdnbyt (); //读n个数据
void Wrnbyt(uchar SLAwtemp ); //写n个数据
void initial(); //I/O初始化子程序
void LED (); //
void delay(uint a);
void delay1();
#define SCL RB4 //4 ;24C01-6
#define SDA RB5 //5 ;24C01-5
#define SCLIO TRISB4 //4
#define SDAIO TRISB5 //5
#define SLAW 0x0A0//寻址字节写
#define SLAR 0x0A1//寻址字节读
uchar numer=1; //数码管数据暂存区
unsigned char sun[10] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//////////////24C01/////////////////
//--------应答位检查---------
void Cack()
{
SCL=1;
SDAIO=1;//输入1
SCLIO=0;
delay1();
err7=SDA;//c=1错误ERR
SCL=0;
}
///--------发送非应答位----------
void Mnack()
{
SDA=1;
SCLIO=0;
SDAIO=0;//输出
SCL=1;
delay1();
SCL=0;
SDA=0;
}
//-------发送应答位---------------
void Mack()
{
SDA=0;
SCLIO=0;
SDAIO=0;//输出
SCL=1;
delay1();
SCL=0;
SDA=1;
}
//--------停止IC总线---------------
void Stop()
{
SDAIO=0;//输出
SCLIO=0;
SDA=0;
delay1();
SCL=1;
delay1();
SDA=1;
delay1();
}
//--------;启动IC总线-------------
void Sta()
{
SDAIO=0;//输出
SCLIO=0;
SDA=1;
delay1();
SCL=1;
delay1();
SDA=0;
delay1();
SCL=0;
}
//--------写一字节------------------------
void Wrbyt(uchar x)
{
uchar i;
acc=x;
SCLIO=0;
SDAIO=0;
for(i=8;i>0;i--)
{
SDA=acc7;
SCL=1;
delay1();
SCL=0;
SDA=0;
acc=acc<<1;
}
}
//---------读一字节-----------------
uchar Rdbyt( )
{
uchar i;
SCLIO=0;
SDAIO=1;//;输入
for(i=8;i>0;i--)
{
acc=acc<<1;//接收数据左移寄存器
SCL=1;
delay1();
acc0=SDA;
SCL=0;
delay1();
}
return(acc);
}
////---------读n个数据----------------
void Rdnbyt () //传送数据个
{
Sta(); //启动IC总线
delay1();
numer = Rdbyt();///读一字节
delay1();
Stop(); //停止IC总线
}
//---------写n个数据---------------
void Wrnbyt(uchar SLAwtemp )
{
err7=1;
if(err7==1)///c=1E0,ERR
{
Sta(); //启动IC总线
delay1();
Wrbyt(SLAwtemp); //写一字节
delay1();
Cack();//应答位检查-
}
delay1();
while(err7==1);
Stop(); //停止IC总线
}
//*************************************//
//系统 各I/O初始化子程序
void initial()
{
TRISC=0;
TRISA=0;
TRISB1=1;
TRISB2=1;
}
//LED
void LED ()
{
PORTA=0X1f;
PORTC=sun[numer];
}
void delay(uint a)
{
uint b=100;
while(a--)
{
while(b--);
}
}
void delay1()
{
NOP();
NOP();
NOP();
NOP();
}
void main()
{
initial();//系统 各I/O初始化子程序
LED();
delay(100);
Wrnbyt (SLAW);//寻址字节写#0A0
delay1();
Wrnbyt (0x05);//写地址0x05
delay1();
Wrnbyt (0x06);//写数据0x06
delay1();
Wrnbyt (SLAR);//寻址字节读#0A1
delay1();
Wrnbyt (0x05);//读地址0x05
delay1();
Rdnbyt (); //读数据
delay(5);
while(1)
{LED();}
}
上一篇:两机串口通讯程序
下一篇:877A软件模拟SPI通信读写93C46
推荐阅读最新更新时间:2024-03-16 15:49