共3条
1/1 1 跳转至页
iccavr 谁有iccavr读写外部存储器的程序,请赐教
问
谢谢先
答 1:
我也想要
答 2:
想当年呀,想当年我也和你们一样在这里求要I2C的程序,可是,却没有人会理会你的,我也说不出来是什么原因,在这里交流的人总是想得到别人的东西,却很少有人提供什么东西,这迫使你自己去写,自己去看datasheet,也许有利于一个人的经验值增长,但是对整个IC界来说,这种藏私的现象却让它发展慢之又慢。
还是给你们发一下吧:
//*** Functions declare for I2C operations ***//
void twi_init (void);
void I2C_SendByte (unsigned char Byte );
void I2C_ReceiveByte(unsigned char SetACK);
unsigned char DataBuffer[100];
unsigned char ReceiveBuffer[100];
unsigned char I2C_Wait (void);
unsigned char I2C_CurRead(
unsigned char DeviceAddr);
unsigned char I2C_WriteByte(
unsigned char H_Addr,
unsigned int D_Addr,
unsigned char Data);
unsigned char I2C_WriteBytes(
unsigned char DeviceAddr,
unsigned int DataAddr,
unsigned char *Buffer,
unsigned char Length);
unsigned char I2C_ReadBytes(
unsigned char DeviceAddr,
unsigned int D_Addr,
unsigned char *Buffer ,
unsigned char Length);
//defines for I2C operations
#define I2C_START TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
#define I2C_STOP TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
//以下的错误是自定义的
#define START_ERROR 0xAA
#define RESTART_ERROR 0x9F
#define SLA_W_ERROR 0xAB
#define SLA_R_ERROR 0xAC
#define DATA_W_ERROR 0xAD
#define DATA_R_ERROR 0xAE
#define TIMEOUT 0xAF
#define Ok 0x01
#define Error 0x00
//*************************************************************************//
//*** 以下进行I2C总线的操作,包括启动停止应答发送接收数据等所有的功能 ****//
//*************************************************************************//
void twi_init(void)
{
PORTD|=0x01;
PORTD|=0x02;
TWBR = 0x48;
TWAR = 0x00;
TWSR = 0x00;
TWCR = 0xC4;
}
unsigned char I2C_Wait(void)
{
unsigned int timeout=10000;
while(!(TWCR&(1<<TWINT))&&(timeout>0))
timeout--;
if(!timeout)
return Error;
return Ok;
}
void I2C_SendByte(unsigned char Byte) //发送一个字节到I2C Bus上
{
unsigned int c;
TWDR=Byte;
TWCR=(1<<TWINT)|(1<<TWEN);
for ( c=0;c<1000;c++ );
}
void I2C_ReceiveByte(unsigned char SetACK) //接收应答
{
if(SetACK)
TWCR = (1<<TWINT) | (1 << TWEN ) | (1 << TWEA);
else
TWCR = (1<<TWINT) | (1 << TWEN );
}
unsigned char I2C_CurRead(unsigned char DeviceAddr)//读当前位置
{
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
I2C_SendByte(DeviceAddr | 0x01);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x40)
{
I2C_STOP;
return SLA_R_ERROR;
}
I2C_ReceiveByte(0); //Receive current byte. NACK, master requests no more data
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
ReceiveBuffer[0]=TWDR;
if ((TWSR&0xF8)!=0x58)
{
I2C_STOP;
return DATA_R_ERROR;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_ReadBytes(unsigned char DeviceAddr, unsigned int D_Addr,unsigned char *Buffer, unsigned char Length)
{
unsigned char i;
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
I2C_SendByte(DeviceAddr & 0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return SLA_W_ERROR;
}
I2C_SendByte(D_Addr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(D_Addr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x10)
{
I2C_STOP;
return RESTART_ERROR;
}
I2C_SendByte(DeviceAddr | 0x01);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x40)
{
I2C_STOP;
return SLA_R_ERROR;
}
if (Length>1)
{
for (i=0;i<Length-1;i++)
{
I2C_ReceiveByte(1);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
*Buffer=TWDR;
Buffer++;
}
}
I2C_ReceiveByte(0); //Receive last or one byte. NACK, master requests no more data
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
*Buffer=TWDR;
if ((TWSR&0xF8)!=0x58)
{
I2C_STOP;
return DATA_R_ERROR;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_WriteByte(unsigned char H_Addr,unsigned int D_Addr,unsigned char Data)
{
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return Error;
}
I2C_SendByte(H_Addr&0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return Error;
}
I2C_SendByte(D_Addr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(D_Addr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(Data);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x28)
{
I2C_STOP;
return Error;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_WriteBytes(unsigned char DeviceAddr, unsigned int DataAddr, unsigned char *Buffer, unsigned char Length)
{
unsigned char i;
// Send start condition
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
// Send device adress + W
I2C_SendByte(DeviceAddr & 0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return SLA_W_ERROR;
}
// Send data adress in device
I2C_SendByte(DataAddr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(DataAddr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
// Send data in buffer
while((Length--)>0)
{
I2C_SendByte(*(Buffer++));
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
}
if ((TWSR&0xF8)!=0x28)
{
I2C_STOP;
return DATA_W_ERROR;
}
I2C_STOP;
return Ok;
}
//************************The End********************************//
以上的程序是经过烧写成功运行的
外接存储器如At24cXXX都可以正常使用,像M16和M128只能接四片,具体接线方法请自已参照datasheet,不能什么都要我说出来,那就真的什么都学不到的
还是给你们发一下吧:
//*** Functions declare for I2C operations ***//
void twi_init (void);
void I2C_SendByte (unsigned char Byte );
void I2C_ReceiveByte(unsigned char SetACK);
unsigned char DataBuffer[100];
unsigned char ReceiveBuffer[100];
unsigned char I2C_Wait (void);
unsigned char I2C_CurRead(
unsigned char DeviceAddr);
unsigned char I2C_WriteByte(
unsigned char H_Addr,
unsigned int D_Addr,
unsigned char Data);
unsigned char I2C_WriteBytes(
unsigned char DeviceAddr,
unsigned int DataAddr,
unsigned char *Buffer,
unsigned char Length);
unsigned char I2C_ReadBytes(
unsigned char DeviceAddr,
unsigned int D_Addr,
unsigned char *Buffer ,
unsigned char Length);
//defines for I2C operations
#define I2C_START TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
#define I2C_STOP TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
//以下的错误是自定义的
#define START_ERROR 0xAA
#define RESTART_ERROR 0x9F
#define SLA_W_ERROR 0xAB
#define SLA_R_ERROR 0xAC
#define DATA_W_ERROR 0xAD
#define DATA_R_ERROR 0xAE
#define TIMEOUT 0xAF
#define Ok 0x01
#define Error 0x00
//*************************************************************************//
//*** 以下进行I2C总线的操作,包括启动停止应答发送接收数据等所有的功能 ****//
//*************************************************************************//
void twi_init(void)
{
PORTD|=0x01;
PORTD|=0x02;
TWBR = 0x48;
TWAR = 0x00;
TWSR = 0x00;
TWCR = 0xC4;
}
unsigned char I2C_Wait(void)
{
unsigned int timeout=10000;
while(!(TWCR&(1<<TWINT))&&(timeout>0))
timeout--;
if(!timeout)
return Error;
return Ok;
}
void I2C_SendByte(unsigned char Byte) //发送一个字节到I2C Bus上
{
unsigned int c;
TWDR=Byte;
TWCR=(1<<TWINT)|(1<<TWEN);
for ( c=0;c<1000;c++ );
}
void I2C_ReceiveByte(unsigned char SetACK) //接收应答
{
if(SetACK)
TWCR = (1<<TWINT) | (1 << TWEN ) | (1 << TWEA);
else
TWCR = (1<<TWINT) | (1 << TWEN );
}
unsigned char I2C_CurRead(unsigned char DeviceAddr)//读当前位置
{
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
I2C_SendByte(DeviceAddr | 0x01);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x40)
{
I2C_STOP;
return SLA_R_ERROR;
}
I2C_ReceiveByte(0); //Receive current byte. NACK, master requests no more data
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
ReceiveBuffer[0]=TWDR;
if ((TWSR&0xF8)!=0x58)
{
I2C_STOP;
return DATA_R_ERROR;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_ReadBytes(unsigned char DeviceAddr, unsigned int D_Addr,unsigned char *Buffer, unsigned char Length)
{
unsigned char i;
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
I2C_SendByte(DeviceAddr & 0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return SLA_W_ERROR;
}
I2C_SendByte(D_Addr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(D_Addr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x10)
{
I2C_STOP;
return RESTART_ERROR;
}
I2C_SendByte(DeviceAddr | 0x01);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x40)
{
I2C_STOP;
return SLA_R_ERROR;
}
if (Length>1)
{
for (i=0;i<Length-1;i++)
{
I2C_ReceiveByte(1);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
*Buffer=TWDR;
Buffer++;
}
}
I2C_ReceiveByte(0); //Receive last or one byte. NACK, master requests no more data
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
*Buffer=TWDR;
if ((TWSR&0xF8)!=0x58)
{
I2C_STOP;
return DATA_R_ERROR;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_WriteByte(unsigned char H_Addr,unsigned int D_Addr,unsigned char Data)
{
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return Error;
}
I2C_SendByte(H_Addr&0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return Error;
}
I2C_SendByte(D_Addr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(D_Addr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(Data);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if((TWSR&0xF8)!=0x28)
{
I2C_STOP;
return Error;
}
I2C_STOP;
return Ok;
}
unsigned char I2C_WriteBytes(unsigned char DeviceAddr, unsigned int DataAddr, unsigned char *Buffer, unsigned char Length)
{
unsigned char i;
// Send start condition
I2C_START;
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x08)
{
I2C_STOP;
return START_ERROR;
}
// Send device adress + W
I2C_SendByte(DeviceAddr & 0xFE);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
if ((TWSR&0xF8)!=0x18)
{
I2C_STOP;
return SLA_W_ERROR;
}
// Send data adress in device
I2C_SendByte(DataAddr>>8);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
I2C_SendByte(DataAddr&0xFF);
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
// Send data in buffer
while((Length--)>0)
{
I2C_SendByte(*(Buffer++));
if(!I2C_Wait())
{
I2C_STOP;
return TIMEOUT;
}
}
if ((TWSR&0xF8)!=0x28)
{
I2C_STOP;
return DATA_W_ERROR;
}
I2C_STOP;
return Ok;
}
//************************The End********************************//
以上的程序是经过烧写成功运行的
外接存储器如At24cXXX都可以正常使用,像M16和M128只能接四片,具体接线方法请自已参照datasheet,不能什么都要我说出来,那就真的什么都学不到的
共3条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【笔记】生成报错synthdesignERROR被打赏50分 | |
【STM32H7S78-DK评测】LTDC+DMA2D驱动RGBLCD屏幕被打赏50分 | |
【STM32H7S78-DK评测】Coremark基准测试被打赏50分 | |
【STM32H7S78-DK评测】浮点数计算性能测试被打赏50分 | |
【STM32H7S78-DK评测】Execute in place(XIP)模式学习笔记被打赏50分 | |
每周了解几个硬件知识+buckboost电路(五)被打赏10分 | |
【换取逻辑分析仪】RA8 PMU 模块功能寄存器功能说明被打赏20分 | |
野火启明6M5适配SPI被打赏20分 | |
NUCLEO-U083RC学习历程2-串口输出测试被打赏20分 | |
【笔记】STM32CUBEIDE的Noruletomaketarget编译问题被打赏50分 |