[mw_shl_code=c,true]#include "myiic.h"
#include "delay.h"
//??????IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//????GPIOB?±??
//GPIOB8,B9???????è??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//???¨????????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//????
GPIO_Init(GPIOB, &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?????????±????±?????
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
IIC_SDA=1;
delay_us(2);
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 CH452_Write(unsigned int cmd) //???ü??
{
IIC_Start(); /*????×???*/
IIC_Send_Byte(((unsigned char)(cmd>>7)&CH452_I2C_MASK)|CH452_I2C_ADDR1);
IIC_Send_Byte((unsigned char)cmd); /*·???????*/
IIC_Stop();; /*?á??×???*/
}
unsigned char CH452_Read()
{
unsigned char keycode;
IIC_Start(); //Send the Start Bit
IIC_Send_Byte(0x61); //??°?±?
keycode=IIC_Read_Byte(0); //????????
IIC_Stop(); //?á??×???
return(keycode);
}
void LEDS_Intial()
{
IIC_Init();
CH452_Read();
CH452_Write(CH452_RESET);
CH452_Write(CH452_SYSON1);
CH452_Write(CH452_DIG0 | 0x80);
CH452_Write(CH452_DIG1 | 0x80);
CH452_Write(CH452_DIG2 | 0x80);
CH452_Write(CH452_DIG3 | 0x80);
CH452_Write(CH452_DIG4 | 0x80);
CH452_Write(CH452_DIG5 | 0x80);
CH452_Write(CH452_DIG6 | 0x80);
CH452_Write(CH452_DIG7 | 0x80);
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
设置模式,再查表!
一周热门 更多>