首页 > 代码库 > stm32f030 模拟IIC
stm32f030 模拟IIC
#define read_SDA (GPIOA->IDR&GPIO_Pin_10) >>10 //PA10 SDA
#define set_SDA GPIO_SetBits(GPIOA, GPIO_Pin_10) //PA10 SDA
#define clr_SDA GPIO_ResetBits(GPIOA, GPIO_Pin_10) //PA10 SDA
#define set_SCL GPIO_SetBits(GPIOA, GPIO_Pin_9) //PA9 SCL
#define clr_SCL GPIO_ResetBits(GPIOA, GPIO_Pin_9) //PA9 SCL
/*******************************************************************************
** IO?ú????
*******************************************************************************/
void IO_Confing(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;//òy??????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ê?3??£ê?
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//í?íìê?3??£ê?
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//′?é?à-μ?×è
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;//ê?3??ù?è
GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_9|GPIO_Pin_10);
}
/*******************************************************************************
** SDA?ú????
*******************************************************************************/
void SDA_MODE_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//òy??????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//ê?è??£ê?
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ????
}
void SDA_MODE_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//òy??????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ê?3??£ê?
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//2?′?é?à-μ?×è
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;//ê?3??ù?è
GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ????
}
//*******************************************************************
// ?e?ˉ×ü??oˉêy
//oˉêy?-Dí: void Start_I2c();
//1|?ü: ???ˉI2C×ü??,?′·¢?íI2C?eê?ì??t.
//*******************************************************************
void Start_I2c(void)
{
SDA_MODE_OUT();
clr_SDA; //·¢?í?eê?D?o?
delay_us(2); //?óê±2us
clr_SCL; //?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
delay_us(2); //?óê±2us
}
//*******************************************************************
// ?áê?×ü??oˉêy
//oˉêy?-Dí: void Stop_I2c();
//1|?ü: ?áê?I2C×ü??,?′·¢?íI2C?áê?ì??t.
//*******************************************************************
void Stop_I2c(void)
{
SDA_MODE_OUT();
clr_SDA; //·¢?í?eê?D?o?
delay_us(2); //?óê±2us
set_SCL;
delay_us(2); //?óê±2us
set_SDA; //·¢?íI2C×ü???áê?D?o?
delay_us(5); //?óê±5us
}
//********************************************************************
// ó|′e×óoˉêy
//oˉêy?-Dí: void Ack_I2c(bit a);
//1|?ü: ?÷???÷??DDó|′eD?o?(?éò?ê?ó|′e?ò·?ó|′eD?o?£?óé??2?êya???¨)
//********************************************************************
void Ack_I2c(u8 a) //ó|′e?ò??2?ó|′e SDA=0ó|′e£?SDA=1·?ó|′e
{
SDA_MODE_OUT();
clr_SCL; //??ê±?ó??£??ˉ×?I2C×ü??ò?±??ìD??óê?
delay_us(2); //?óê±2us
if(a==0)
clr_SDA; //?ú′?·¢3?ó|′e?ò·?ó|′eD?o?
else
set_SDA;
delay_us(2); //?óê±2us
set_SCL;
delay_us(2); //?óê±2us
clr_SCL; //??ê±?ó??£??ˉ×?I2C×ü??ò?±??ìD??óê?
delay_us(2); //?óê±2us
clr_SDA; //·¢?í?áê?ì??tμ?êy?YD?o?
delay_us(2); //?óê±2us
}
//*******************************************************************
// ×??úêy?Y·¢?íoˉêy
//1|?ü: ??êy?Yc·¢?í3?è¥,?éò?ê?μ??·,ò2?éò?ê?êy?Y,·¢íêoóμè′yó|′e,2¢??
// ′?×′ì?????DD2ù×÷.(2?ó|′e?ò·?ó|′e??ê1ack=0)
// ·¢?íêy?Y?y3££?ack=1; ack=0±íê?±????÷?Tó|′e?ò?e?μ?£
//*******************************************************************
void SendByte(unsigned char dat)
{
unsigned char BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) //òa′??íμ?êy?Y3¤?è?a8??
{
if((dat<<BitCnt)&0x80)
set_SDA; //?D??·¢?í??
else
clr_SDA;
delay_us(2); //?óê±2us
set_SCL; //??ê±?ó???a??£?í¨?a±????÷?aê??óê?êy?Y??
delay_us(2); //?óê±3us
clr_SCL;
delay_us(2); //?óê±1us
}
SDA_MODE_IN();
set_SCL;
delay_us(2); //?óê±2us
if(read_SDA==1)
ack=0;
else
ack=1; //?D??ê?·??óê?μ?ó|′eD?o?
clr_SCL;
delay_us(2); //?óê±2us
SDA_MODE_OUT();
delay_us(2); //?óê±2us
clr_SDA;
}
//*******************************************************************
// ×??úêy?Y?óê?oˉêy
//oˉêy?-Dí: UCHAR RcvByte();
//1|?ü: ó?à′?óê?′ó?÷?t′?à′μ?êy?Y,2¢?D??×ü??′í?ó(2?·¢ó|′eD?o?)£?
// ·¢íêoó??ó?ó|′eoˉêyó|′e′ó?ú?£
//*******************************************************************
unsigned char RcvByte(void)
{
unsigned char retc=0;
unsigned char BitCnt;
SDA_MODE_IN();
delay_us(1);
for(BitCnt=0;BitCnt<8;BitCnt++)
{
clr_SCL; //??ê±?ó???aμí£?×?±??óê?êy?Y??
delay_us(2); //?óê±5us
set_SCL; //??ê±?ó???a??ê1êy?Y??é?êy?YóDD§
delay_us(2); //?óê±2us
retc=retc<<1;
if(read_SDA==1)
retc=retc+1; //?áêy?Y??,?óê?μ?êy?Y??·?è?retc?D
delay_us(2); //?óê±2us
}
clr_SCL;
delay_us(2); //?óê±2us
return(retc);
}
//*******************************************************************
// ?òóD×óμ??·?÷?t?áè??à×??úêy?Yoˉêy
//oˉêy?-Dí: bit RecndStr(UCHAR sla,UCHAR suba,ucahr *s,UCHAR no);
//1|?ü: ′ó???ˉ×ü??μ?·¢?íμ??·£?×óμ??·,?áêy?Y£??áê?×ü??μ?è?1y3ì,′ó?÷?t
// μ??·sla£?×óμ??·suba£??á3?μ??úèY·?è?s???òμ?′?′¢??£??áno??×??ú?£
// è?1?·μ??1±íê?2ù×÷3é1|£?·??ò2ù×÷óD?ó?£
//×¢òa£o ê1ó??°±?D?ò??áê?×ü???£
//********************************************************************
u8 IRcvStr(unsigned char sla,unsigned char *s,unsigned char no)
{
unsigned char i;
Start_I2c(); //???ˉ×ü??
SendByte(sla); //·¢?í?÷?tμ??·
if(ack==0)//?Tó|′e
{
Stop_I2c();
return 0;
}
for(i=0;i<no-1;i++)
{
*s=RcvByte(); //
Ack_I2c(0); //?óê??í′e??
s++;
}
*s=RcvByte();
Ack_I2c(1); //·¢?í·?ó|??
Stop_I2c(); //?áê?×ü??
return 1;
}
stm32f030 模拟IIC