当前位置:首页 > 单片机 > 单片机
[导读]调试通过。#include #include #include #include #define uchar unsigned char#define uint unsigned int#define Fclk 11059200UL /*使用11.0592M体*/#define BitRate 9600UL /*波特率定义为9600*/#define DEBUG0sbi

调试通过。

#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define Fclk 11059200UL /*使用11.0592M体*/
#define BitRate 9600UL /*波特率定义为9600*/


#define DEBUG0

sbit sda= P1^2; //2ic串行数据
sbit scl= P1^5; //2ic串行时钟
sbit WP = P3^2; //2ic硬件写保
sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;

sbit led_g= P3^4; //red led
sbit led_r= P2^3; //green led
sbit kr = P2^1; //kr

void outChar(uchar c);
void outStr(uchar *buf,uchar len);

uchar xKey;
uint Life;
uint KB_Life;

uchar recvBuf[16+6];
uchar exeCmd;
uchar sendBuf[16+6];


unsigned char checksum,count3;
bit read_flag=0;

volatile uchar Sending;
volatile uchar Accepting;
volatile uchar len,temp,i;
volatile unsigned int j;

//延时10微秒(包括调用和返回的时间),base=12M
void Delay(void)
{
P3_5 = ~P3_5;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/*********************************************************
* AT24C01读写函数 *
**********************************************************/
void Start(void) //起始条件
{
scl=0;
sda=1;
scl=1;
sda=0;
scl=0;
}
void Stop(void) //停止条件
{
scl=0;
sda=0;
scl=1;
sda=1;
scl=0;
}
void WaitAck(void)
{
scl=0;
sda=0;
scl=1;
scl=0;
sda=1;
}
void Ack(void) //应答位
{
scl=0;
_nop_();
scl=1;
_nop_();
scl=0;
_nop_();
sda=1;
_nop_();
}
void NoAck(void) //反向应答位
{
scl=0;
sda=1;
scl=1;
scl=0;
sda=1;
}
uchar Read(void) //读一个字节的数据,并返回该字节值
{
scl=1;_nop_();_nop_();_nop_();
a7=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a6=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a5=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a4=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a3=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a2=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a1=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a0=sda;_nop_();_nop_();scl=0;_nop_();_nop_();sda=1;_nop_();_nop_();
return(ACC);
}
void Send(uchar Data) //发送数据子程序,Data为要求发送的数据
{
ACC=Data;
scl=0;
sda=a7;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a6;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a5;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a4;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a3;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a2;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a1;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a0;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
}
void RdFromROM(uchar *Data,uchar Address,uchar Num)
{
uchar i;

Start();
Send(0xa0); //读操作
Ack();
Send(Address); //读的地址
Ack();

Start();
Send(0xa1);
Ack();

for(i=0;i {
Data[i]=Read();
WaitAck();
}
NoAck();
Stop();
}
void WrToROM(uchar *Data,uchar Address,uchar Num)
{
uchar i;

Start();
Send(0xa0); //写操作
Ack();

Send(Address);//写的地址
Ack();

for(i=0;i {
Send(Data[i]);
Ack();
}
Stop();
Delay();
Delay();
Delay();
}

/***********************************************************************/
//初始化串口通讯参数
void InitialUart()
{
PCON = 0x00;
TMOD = 0x21;
TH0 = 0x52; //0x4c;
TL0 = 0x00; //0x00;
TR0 = 1; //开启定时器
ET0 = 1; //开启定时器1的中断
/*
TH1 = 0xFD; //Baud:9600 fosc=11.0592MHz
TL1= 0xFD;
TR1 = 1; //启动定时器1
SCON = 0x50; //串口工作模式为1,允许接收串行数据
ES = 1; //开启串口中断

EA = 1; //开启中断
*/
EA=0; //暂时关闭中断
TMOD&=0x0F; //定时器1模式控制在高4位
TMOD|=0x20; //定时器1工作在模式2,自动重装模式
SCON=0x50; //串口工作在模式1
TH1=256-Fclk/(BitRate*12*16); //计算定时器重装值
TL1=256-Fclk/(BitRate*12*16);
PCON|=0x80; //串口波特率加倍
ES=1; //串行中断允许
TR1=1; //启动定时器1
REN=1; //允许接收
EA=1; //允许中断
PS=1;
}

//向串口发送一字符
/*
void OutChar(uchar c)
{
SBUF=c;
while(TI==0);
TI=0;
}
*/
void OutChar(uchar c)
{
SBUF=c;
Sending=1;
while(Sending);
}

void OutStr(uchar *buf,uchar len)
{
uchar i;
uchar check = 0x00;
OutChar(buf[0]);
for(i=1;i {
OutChar(buf[i]);
check ^= buf[i];
}
OutChar(check);
}

/**************************************
*功能: 串口中断函数 *
**************************************/
void UartISR(void) interrupt 4 using 3
{
// uchar len, i;
// unsigned int j=0;
if(RI)
{
RI=0;
if(!read_flag)
{
len=SBUF;
recvBuf[0]=len;
read_flag=1;
}
else
{
++i;
recvBuf[i]=SBUF;
if(i==len)
{
exeCmd = 1;
read_flag=0;
i=0;
}

}
/*
SCON =0x40; //接收不允许
tChar(0x88); //发送数据
OutChar(len);
OutChar(recvBuf[0]);
OutChar(recvBuf[1]);
OutChar(recvBuf[2]);
OutChar(recvBuf[3]);
OutChar(0x88);
OutChar(0x68);

outStr(sendBuf,len);
SCON =0x50; //接收允许
*/
}
else
{
TI=0;
Sending=0;
}

}

/*
void COMISP(void) interrupt 4
{
uchar len, i;
unsigned int j=0;
if(RI)
{
len=SBUF;
recvBuf[0] = len;
RI=0;
for(i=0;i{
while(!RI)
{
j++;
if(j>1000) break;
}
if(j<1000)
{
recvBuf[i+1]=SBUF;
RI=0;
j=0;
}
else
{
break;
}
}
if(i==len)
{
exeCmd = 1;
}
}
}
*/

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

单片机内部有很多的特殊功能寄存器,每个寄存器在单片机内部都分配有唯一的地址,一般我们会根据寄存器功能的不同给寄存器赋予各自的名称,当我们需要在程序中操作这些特殊功能寄存器时,必须要在程序的最前面将这些名称加以声明,声明的...

关键字: C51 数据类型 扩充定义

数据元(Data Element),也称为数据元素,是用一组属性描述其定义、标识、表示和允许值的数据单元,在一定语境下,通常用于构建一个语义正确、独立且无歧义的特定概念语义的信息单元。数据元可以理解为数据的基本单元,将若...

关键字: C51 数据类型

▼点击下方名片,关注公众号▼欢迎关注【玩转单片机与嵌入式】公众号,回复关键字获取更多免费资料。回复【加群】,限时免费进入知识共享群;回复【3D封装库】,常用元器件的3D封装库;回复【电容】,获取电容、元器件选型相关的内容...

关键字: C51 MDK RealView

在Keil C51软件中51单片机的中断服务和外设驱动程序的开发

关键字: keil5 编译 C51

Intel公司1980年推出了MCS-51系列单片机:集成 8位CPU、4K字节ROM、128字节RAM、4个8位并口、1个全双工串行口、2个16位定时/计数器。寻址范围64K,并有控制功能较强的布尔处理器。 80C5...

关键字: C51 KEIL 编程

c上标3下标5怎么算用计算机,c上标3下标5怎么算

关键字: C51 KEIL

▼点击下方名片,关注公众号▼大家好,很高兴和各位一起分享我的第16篇原创文章,喜欢和支持我的工程师,一定记得给我点赞、收藏、分享。加微信[xyzn3333]与作者沟通交流,免费获取更多单片机与嵌入式的海量电子资料。很多初...

关键字: 51单片机 C51

常看见初学者要求使用_at_,这是一种谬误,把C当作ASM看待了。在C中变量的定位是编译器的事情,初学者只要定义变量和变量的作 用域,编译器就把一个固定地址给这个变量。

关键字: C51 单片机 误区 注意事项

简介:编程首要是要考虑程序的可行性,然后是可读性、可移植性、健壮性以及可测试性。这是总则。但是很多人忽略了可读性、可移植性和健壮性(可调试的方法可能歌不相同),这是不对的。

关键字: C51 编程规范 文件配置

如果你用 Keil C51 进行编译,记住一点:它不区分大小写!!!卧槽,今天编程序那个调错啊,就因为一个数组名和一个变量名完全一样,只是大小写不一样罢了,标准 C 我怎么记得这样可以啊……上网一查,卧槽,Keil C5...

关键字: C51 单片机 编程要点
关闭
关闭