#include "24cxx.h"
#include "delay.h"
#include "delay.h"
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC->APB2ENR|=1<<4;//��ʹ������IO PORTCʱ��
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //�������
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
IIC_SCL=1;
IIC_SDA=1;
}
//����IIC��ʼ�ź�
void IIC_Start(void)
{
SDA_OUT(); //sda�����
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//ǯסI2C���ߣ������ͻ��������
}
//����IICֹͣ�ź�
void IIC_Stop(void)
{
SDA_OUT();//sda�����
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1;//����I2C���߽����ź�
delay_us(4);
}
//�ȴ�Ӧ���źŵ���
//����ֵ��1������Ӧ��ʧ��
// 0������Ӧ��ɹ�
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA��������
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//ʱ�����0
return 0;
}
//����ACKӦ��
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//������ACKӦ��
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//IIC����һ���ֽ�
//���شӻ�����Ӧ��
//1����Ӧ��
//0����Ӧ��
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//����ʱ�ӿ�ʼ���ݴ���
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //��TEA5767��������ʱ���DZ����
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//��1���ֽڣ�ack=1ʱ������ACK��ack=0������nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA��������
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();//����nACK
else
IIC_Ack(); //����ACK
return receive;
}
void AT24CXX_Init(void)
{
IIC_Init();
}
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //�������
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8);//���ߵ�ַ
IIC_Wait_Ack();
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //����������ַ0XA0,д����
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //���͵͵�ַ
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1); //�������ģʽ
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//����һ��ֹͣ����
return temp;
}
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //�������
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8);//���ߵ�ַ
}else
{
IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //����������ַ0XA0,д����
}
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //���͵͵�ַ
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //�����ֽ�
IIC_Wait_Ack();
IIC_Stop();//����һ��ֹͣ����
delay_ms(10);
}
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
{
u8 t;
for(t=0;t<Len;t++)
{
AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
}
}
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len)
{
u8 t;
u32 temp=0;
for(t=0;t<Len;t++)
{
temp<<=8;
temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);
}
return temp;
}
u8 AT24CXX_Check(void)
{
u8 temp;
temp=AT24CXX_ReadOneByte(255);//����ÿ�ο�����дAT24CXX
if(temp==0X55)return 0;
else//�ų���һ�γ�ʼ�������
{
AT24CXX_WriteOneByte(255,0X55);
temp=AT24CXX_ReadOneByte(255);
if(temp==0X55)return 0;
}
return 1;
}
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
{
while(NumToRead)
{
*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}
24cxx.h
#ifndef __24CXX_H
#define __24CXX_H
#include "myiic.h"
#include "sys.h"
#define AT24C01 127
#define AT24C02 255
#define AT24C04 511
#define AT24C08 1023
#define AT24C16 2047
#define AT24C32 4095
#define AT24C64 8191
#define AT24C128 16383
#define AT24C256 32767
#define SDA_IN() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=8<<12;}
#define SDA_OUT() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=3<<12;}
#define IIC_SCL PCout(12) //SCL
#define IIC_SDA PCout(11) //SDA
#define READ_SDA PCin(11) //����SDA
#define EE_TYPE AT24C02
void IIC_Init(void); //��ʼ��IIC��IO��
void IIC_Start(void); //����IIC��ʼ�ź�
void IIC_Stop(void); //����IICֹͣ�ź�
void IIC_Send_Byte(u8 txd); //IIC����һ���ֽ�
u8 IIC_Read_Byte(unsigned char ack);//IIC��ȡһ���ֽ�
u8 IIC_Wait_Ack(void); //IIC�ȴ�ACK�ź�
void IIC_Ack(void); //IIC����ACK�ź�
void IIC_NAck(void); //IIC������ACK�ź�
void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC_Read_One_Byte(u8 daddr,u8 addr);
u8 AT24CXX_ReadOneByte(u16 ReadAddr); //ָ����ַ��ȡһ���ֽ�
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite); //ָ����ַд��һ���ֽ�
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len);//ָ����ַ��ʼд��ָ�����ȵ�����
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len); //ָ����ַ��ʼ��ȡָ����������
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite); //��ָ����ַ��ʼд��ָ�����ȵ�����
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead); //��ָ����ַ��ʼ����ָ�����ȵ�����
u8 AT24CXX_Check(void); //�������
void AT24CXX_Init(void); //��ʼ��IIC
#endif
主函数调用
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "key.h"
#include "24cxx.h"
const u8 TEXT_Buffer[]="Kafen Wong EDIL";
#define SIZE sizeof(TEXT_Buffer)
int main(void)
{
delay_init();
OLED_Init();//显示屏初始化
u8 datatemp[SIZE];
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
KEY_Init(); //按键初始化
AT24CXX_Init(); //芯片初始化
while(AT24CXX_Check())//检测不到24c02
{
OLED_Refresh();
OLED_ShowString(0,0,"CANT OPEN",16,1);
}
OLED_ShowString(0,0,"OK!",16,1);
static int ii=0;
while(1)
{
OLED_Refresh();
if(KEY_Scan())//WK_UP 按下,写入24C02
{
ii++;
if(ii%2){
OLED_Clear();
OLED_Refresh();
OLED_ShowString(0,0,"Start Write",16,1);
AT24CXX_Write(0,(u8*)TEXT_Buffer,SIZE);
OLED_ShowString(0,16,"Finished",16,1);
}
else{
OLED_Clear();
OLED_Refresh();
OLED_ShowString(0,0,"Start Read",16,1);
AT24CXX_Read(0,datatemp,SIZE);
OLED_ShowString(0,16,"Data is",16,1);
OLED_ShowString(0,32,datatemp,16,1);
}
}
}
}
作业
- 尝试把库中IIC的部分独立出来,并且将OLED库的IIC函数删去,使用独立出来的IIC库
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)