⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iic.c

📁 感光芯片ISL9001控制程序源码
💻 C
字号:
#include "iic.h"void udelay(int time){	int i;	for(i=0;i<time;i++)	{;}}static void setsda(int Type){	UINT32 mask=0x2;    GPIO->pbddr |= mask;	if(Type)	        GPIO->pbdr |= mask;	else	        GPIO->pbdr &= ~ mask;	udelay(20);}static void setscl(int Type){	UINT32 mask=0x4;    GPIO->pbddr |= mask;	if(Type)	        GPIO->pbdr |= mask;	else	        GPIO->pbdr &= ~ mask;	udelay(20);}static int getsda(){	UINT32 mask=0x2;	udelay(20);    GPIO->pbddr &= ~ mask;		if(GPIO->pbdr & mask) 			return 1;	else 					return 0;	}/*error message:return 0:ok,start success!return 1:can't setsda=1,can't start!return 2:can't setsda=0,can't start!*/int SendStart(){	setscl(1);	/**/	if(!getsda()) /*if sda=0*/	{		setsda(1);		if(!getsda()) return 1;	}	/*sda=1*/	setsda(0);	if(getsda()) return 2;	return 0;}/*error message:return 0:ok,stop success!return 1:can't setsda=1,can't stop!return 2:can't setsda=0,can't stop!*/int SendStop(){	setscl(1);	/**/	if(getsda()) /*if sda=1*/	{		setsda(0);		if(getsda()) return 2;	}	/*sda=0*/	setsda(1);	if(!getsda()) return 1;	return 0;}/*error message:return 0:ok, success!return 1:no ack!*/int WriteData(char data){	char i,data1[8]= {0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};	int ret=0,timeout=0;	setscl(0);	for(i=0;i<8;i++)	{		if(data & data1[i])	setsda(1);		else 			setsda(0);		setscl(1);		setscl(0);	}	setscl(1);	while(getsda())	{		timeout++;		if(timeout>0x10){ SendStop(); /*printf("have no ack!\n");*/return 1; }	}	setscl(0); 	return 0;}int ReadData(){	char i,data1[8]= {0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};	int ret = 0,timeout= 0;	setscl(0); 	for(i=0;i<8;i++)	{		if(getsda())	ret|= data1[i];		setscl(1);			setscl(0); 	}		setsda(0);	setscl(1);		setscl(0); 		return ret;}/**/int ReadLightSensorValue(char *ptr,int count){	int i;	if(count>4) count=4;	if(count<1) count=1;	SendStart();	WriteData(0x89);/*0x44<<1+1);*/	for(i=0;i<4;i++)	{		ptr[i] = ReadData();		if(i==count-1) break;	}	SendStop();}int SetLightSensor(ISL_COMMAND command){	int ret;	ret = SendStart();/**/	WriteData(0x88);/*0x44*0x10) the first write addr*/	WriteData(command);	ret = SendStop();}int Read_LightSensor(){	char Temp[4];	UINT32 Value;	ReadLightSensorValue(Temp,4);	Value = Temp[1]*0xff+Temp[0];	Value = Value*10000/32768;	return Value;}/*set iic bus and initial the light sensor*/ void Init_iic(){	UINT32 mask=0x8,mask1 = 0x20;	GPIO->pbdr &= ~mask; /*initial the key backlight control signal*/	GPIO->pinmux &= ~mask;  /*set the UART3CON =0 ,gpio function*/    GPIO->pbddr |= mask1; /*set PB5 is GPIO_OUTPUT*/	GPIO->pbdr &= ~mask1; /*enable ISL9001*/    SetLightSensor(ISL_COMMAND_RST);    SetLightSensor(ISL_COMMAND_CVT1_INT);}/*Set ISL9001 Power down on or off*/ void setLightSensorOn(unsigned char flag){	UINT32 mask1 = 0x20;    GPIO->pbddr |= mask1; /*set PB5 is GPIO_OUTPUT*/   if(flag)   {		GPIO->pbdr &= ~mask1; /*enable ISL9001*/   		SetLightSensor(ISL_COMMAND_RST);		SetLightSensor(ISL_COMMAND_CVT1_INT);	   }   else   {		SetLightSensor(ISL_COMMAND_PWD);		GPIO->pbdr |= mask1; /*enable ISL9001*/      }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -