📄 i2c.c
字号:
#include "i2c.H"
uint8 ledseg[11]= {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0x00 };
//static uint8 ack;
/***********************************************************************************************
*启动总线函数
*函数原型:void start_i2c();
*功能:启动I2C总线,即发送I2C起始条件
***********************************************************************************************/
void start_i2c(void)
{
SDA_1;
_delay_ms(1);
SCL_1;
_delay_ms(2);
SDA_0;
_delay_ms(2);
SCL_0;
_delay_ms(1);
}
/***********************************************************************************************
*结束总线函数
*函数原型:void stop_i2c();
*功能:结束I2C总线,即发送I2C结束条件
***********************************************************************************************/
void stop_i2c(void)
{
SDA_0;
_delay_ms(1);
SCL_1;
_delay_ms(2);
SDA_1;
_delay_ms(1);
}
/***********************************************************************************************
*字节数据传送函数
*函数原型:void sendbyte(uchar c);
*功能:将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对此状态位进行操作(不应答
* 或非应答都使ack=0,表示应答失败)。发送数据正常,ack=1;ack=0表示被控器无应答或损坏
***********************************************************************************************/
void sendbyte(unsigned char c)
{
unsigned char bitcnt;
for(bitcnt=0;bitcnt<8;bitcnt++)
{
if((c<<bitcnt)&0x80)
SDA_1;
else
SDA_0;
_delay_us(20);
SCL_1;
_delay_us(20);
SCL_0;
}
_delay_us(50);
SDA_1;
_delay_us(50);
SCL_1;
_delay_us(50);
SCL_0;
_delay_us(50);
}
/***********************************************************************************************
*向有子地址器件发送多字节数据函数
*函数原型:bit Isendstr(uchar sla,uchar suba,uchar *s,uchar no);
*功能:从启动总线到发送地址、子地址、数据,直至结束总线的全过程;从器件地址sla,子地址suba,
* 发送的内容是s指向的内容,发送no个字节。如果返回1则表示操作成功;否则操作有误
***********************************************************************************************/
uint8 Isendstr(uint8 sla,uint8 suba,uint8 *s,uint8 nbyte)
{
uint8 i;
start_i2c();
sendbyte(sla);
sendbyte(suba);
for(i=0;i<nbyte;i++)
{
sendbyte(*s);
_delay_ms(2);
s++;
}
stop_i2c();
return(1);
}
void Disp_i2c_led(unsigned long show_data)
{
unsigned char Q=0,S=0,B=0,G=0;
unsigned char temp_buf[10];
unsigned long temp = 0;
temp = show_data;
temp_buf[0] = temp/10000000;
temp_buf[1] = (temp-temp_buf[0]*10000000)/1000000;
temp_buf[2] = (temp-temp_buf[0]*10000000-temp_buf[1]*1000000)/100000;
temp_buf[3] = (temp-temp_buf[0]*10000000-temp_buf[1]*1000000-temp_buf[2]*100000)/10000;
temp = temp/10000;
temp = show_data - temp*10000;
temp_buf[4] = temp/1000; //取出temp的低四位
temp_buf[5] = (temp-temp_buf[4]*1000)/100;
temp_buf[6] = (temp-temp_buf[4]*1000-temp_buf[5]*100)/10;
temp_buf[7] = (temp-temp_buf[4]*1000-temp_buf[5]*100-temp_buf[6]*10);
Q = temp_buf[4];
B = temp_buf[5];
S = temp_buf[6];
G = temp_buf[7];
temp_buf[4] = ledseg[temp_buf[0]];
temp_buf[5] = ledseg[temp_buf[1]];
temp_buf[6] = ledseg[temp_buf[2]];
temp_buf[7] = ledseg[temp_buf[3]];
temp_buf[0] = ledseg[Q];
temp_buf[1] = ledseg[B];
temp_buf[2] = ledseg[S];
temp_buf[3] = ledseg[G];
//LED1_OFF();
Isendstr(zlg7290,0x10, temp_buf,8);
}
/*int main(void)
{
uint32 data;
DDRB = (1<<0)|(1<<1);
PORTB = 0x00;
_delay_ms(50);
data = 17369284;
temp_test(data);
while(1)
{
//Isendstr(zlg7290,0x0d,0x07,1);
//temp_test(data);
//LED0_OFF();
//LED0_ON();
//_delay_us(50);
//LED0_OFF();
// _delay_us(50);
//Isendstr(zlg7290,0x10, ledseg,8);
data--;
}
return 1;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -