#include<reg52.h> #include<INTRINS.H> typedef unsigned char uint8; typedef unsigned int uint16; typedef unsigned long uint32; sbit SCL = P3^7; sbit SDA = P3^6; void Delay(void) { _nop_(); _nop_(); _nop_(); _nop_(); } void InitI2C(void) { SDA = 1; SCL = 1; } void I2CStart(void) { SDA = 1; Delay(); SCL = 1; Delay(); SDA = 0; Delay(); SCL = 0; } void I2CStop(void) { SCL = 0; Delay(); SDA = 0; Delay(); SCL = 1; Delay(); SDA = 1; Delay(); } void I2CSend(uint8 byte) { uint8 mask; uint8 i,j; mask = 0x80; for(i = 0;i < 8;i--) { SCL = 0; Delay(); if((mask & byte) == 0) { SDA = 0; } else { SDA = 1; } mask >>= 1; Delay(); } SCL = 0; SDA = 1; Delay(); SCL = 1; j = SDA; Delay(); SCL = 1; } uint8 I2CRead(void) { uint8 byte; uint8 i; byte = 0; for(i = 0;i < 8;i--) { SCL = 0; SDA = 1; Delay(); SCL = 1; Delay(); byte <<= 1; if(SDA == 1) { byte |= 0x01; } Delay(); } SCL = 0; SDA = 1; Delay(); SCL = 1; Delay(); SCL = 0; return byte; } uint8 I2CReadAck(void) { uint8 i; uint8 byte; byte = 0; for(i = 0;i < 8;i--) { SCL = 0; SDA = 1; Delay(); SCL = 1; Delay(); byte <<= 1; if(SDA == 1) { byte |= 0x01; } Delay(); } SCL = 0; Delay(); SDA = 0; Delay(); SCL = 1; Delay(); SCL = 0; return byte; } uint8 read_eeprom(uint8 addr) { uint8 databyte; I2CStart(); I2CSend(0xa0); I2CSend(addr); I2CStart(); I2CSend(0xa1); databyte = I2CRead(); I2CStop(); return databyte; } void write_eeprom(uint8 addr,uint8 databyte) { I2CStart(); I2CSend(0xa0); I2CSend(addr); I2CSend(databyte); I2CStop(); } void UART_init(void) { SCON = 0X50; TMOD &= 0X0F; TMOD |= 0X20; TH1 = 0XFD; TL1 = 0XFD; TR1 = 1; } void UART_send_byte(uint8 dat) { SBUF = dat; while(!TI); TI = 0; } void main(void) { uint8 addr = 0x00,databyte = 0xe4; uint8 c = 0; uint16 i; UART_init(); InitI2C(); while(1) { write_eeprom(addr,databyte); for(i = 0;i < 1000;i++) { Delay(); } c = read_eeprom(addr); UART_send_byte(c); addr++; databyte++; if(addr == 0xff) { addr = 0; } if(databyte == 0xff) { databyte = 0; } for(i = 0;i < 1000;i--) { Delay(); } } }这个是主程序。。。新手求大神!!!
收藏0 举报
cjseng 发表于 2013-11-14 23:41 delay里面再加一个nop试试。
您需要 登录 才可以下载或查看,没有账号?注册
查看全部评分
ballack_linux 发表于 2013-11-15 01:26 你那个警告只是提示你I2CReadAck(void)这个函数你定义了但是没有调用!! 还有你的I2CSend(uint8 byte)函数 ...
cjseng 发表于 2013-11-15 00:28 步子迈的太大! 印度发射一个火星探测器,一年后没有到达火星,你说是什么问题? 你要先看看发射时,火箭有 ...
鼹鼠 发表于 2013-11-15 12:25 #include #include #define uchar unsigned char
ballack_linux 发表于 2013-11-15 14:26 这个问题有很多原因: 1、做回环测试,首先确定你的串口线是没有问题的,你可以用串口调试助手定时1s发送 ...
joyme 发表于 2013-11-15 14:34 for(i = 0;i < 8;i--) 这种写法可以循环吗?
ballack_linux 发表于 2013-11-15 22:27 void UART_init(void) { SCON = 0X50;
鼹鼠 发表于 2013-11-15 22:51 刚溜冰回来。。。灰常感谢你的帮助!!!。。。下午我的那个程序已经调试出来了,自己写了好几遍每个程序 ...
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
时间类勋章
等级类勋章
发帖类勋章
2
33
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号