//*********************************
//±????ò??SCCB???????????ú??
//??×÷??????2014.07.22
//********************
#include "SCCB.h"
//********************************************
//????×÷??????????SCCB??I/O????
//????????void SCCB_Init(void)
//·?????????
//********************************************
void SCCB_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);//????I2C0???±??
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//????I2C0???????????±??
GPIOPinConfigure(GPIO_PB2_I2C0SCL);//????PB2??SCL
GPIOPinConfigure(GPIO_PB3_I2C0SDA);//????PB3??SDL
//GPIOPinTypeI2C(GPIO_PORTB_BASE,GPIO_PIN_2|GPIO_PIN_3);//????PB2??PB3???????à??
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_PIN_2);//????????
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_PIN_3);//
SCCB_SDA_OUT();//?è??SDA??????????
}
//**********************************************
//SCCB????????
//?±?±?????????±?ò,??????????????,??SCCB????????
//???¤??×?????,SDA??SCL?ù????????
//**********************************************
void SCCB_Start(void)
{
SCCB_SDA1; //????????????
SCCB_SCL1; //???±?????????±?ò??????????????
delay_Nus(50);
SCCB_SDA0;
delay_Nus(50);
SCCB_SCL0; //??????????????????????×÷????±???
}
//***********************************************
//SCCB????????
//?±?±?????????±?ò,??????????????,??SCCB????????
//????×?????,SDA,SCL?ù????????
//***********************************************
void SCCB_Stop(void)
{
SCCB_SDA0;
delay_Nus(50);
SCCB_SCL1;
delay_Nus(50);
SCCB_SDA1;
delay_Nus(50);
}
//**********************************************
//?ú?úNA????
//**********************************************
void SCCB_No_Ack(void)
{
delay_Nus(50);
SCCB_SDA1;
SCCB_SCL1;
delay_Nus(50);
SCCB_SCL0;
delay_Nus(50);
SCCB_SDA0;
delay_Nus(50);
}
void SCCB_Ack(void)
{
SCCB_SDA_OUT();
SCCB_SDA1;
delay_Nus(50);
SCCB_SCL1;
delay_Nus(50);
SCCB_SCL0;
delay_Nus(50);
SCCB_SDA1;
}
//*********************************************
//SCCB,????????×???
//·?????:0,????;1,?§°?.
//*********************************************
uint8_t SCCB_WR_Byte(uint8_t dat)
{
uint8_t j,res;
for(j=0;j<8;j++) //???·8??·???????
{
if(dat&0x80)SCCB_SDA1;
else SCCB_SDA0;
dat<<=1;
delay_Nus(50);
SCCB_SCL1;
delay_Nus(50);
SCCB_SCL0;
}
SCCB_SDA_IN(); //?è??SDA??????
delay_Nus(50);
SCCB_SCL1; //??????????,????????·?·???????
delay_Nus(50);
if(SCCB_READ_SDA)res=1; //SDA=1·????§°???·???1
else res=0; //SDA=0·?????????·???0
SCCB_SCL0;
SCCB_SDA_OUT(); //?è??SDA??????
return res;
}
//****************************************************
//SCCB ????????×???
//??SCL????????,????????
//·?????:??????????
//****************************************************
uint8_t SCCB_RD_Byte(void)
{
uint8_t temp=0,j;
SCCB_SDA_IN(); //?è??SDA??????
for(j=8;j>0;j--) //???·8??????????
{
delay_Nus(50);
SCCB_SCL1;
temp=temp<<1;
if(SCCB_READ_SDA)temp++;
delay_Nus(50);
SCCB_SCL0;
}
SCCB_SDA_OUT(); //?è??SDA??????
return temp;
}
/***************************************************************************
?? ????unsigned char WrCmos7670(unsigned char regID, unsigned char regDat)
?? ?????òCMOS???¨?????÷??????
??????????regID ???¨?????÷???·
regDat ?????????¨?????÷????
??????????1 ????????
0 ?????§°?
?? ?÷??
?÷??·?·¨??m=WrCmos7670(0x3a,0x04);
***************************************************************************/
uint8_t WrCmos7670(uint8_t reg, uint8_t data)
{
uint8_t res=0;
SCCB_Start(); //????SCCB????
if(SCCB_WR_Byte(SCCB_ID))res=1; //???÷??ID
delay_Nus(100);
if(SCCB_WR_Byte(reg))res=1; //???????÷???·
delay_Nus(100);
if(SCCB_WR_Byte(data))res=1; //??????
SCCB_Stop();
return res;
}
uint8_t rdCmos7670Reg(uint8_t reg)//???????÷
{
uint8_t val=0;
SCCB_Start(); //????SCCB????
SCCB_WR_Byte(SCCB_ID); //???÷??ID
delay_Nus(100);
SCCB_WR_Byte(reg); //???????÷???·
delay_Nus(100);
SCCB_Stop();
delay_Nus(100);
//?è???????÷???·?ó????????
SCCB_Start();
SCCB_WR_Byte(SCCB_ID|0X01); //·??????ü??
delay_Nus(100);
val=SCCB_RD_Byte(); //????????
SCCB_No_Ack();
SCCB_Stop();
return val;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>