一、I2C
I2C时序:时钟线SCL高电平下:SDA由高变低代表启动信号,开始发送数据;SCL高电平时,数据稳定,数据可以被读走,开始进行读操作,SCL低电平时,数据发生改变;每发送一个字节的数据都会产生应答信号;时钟线SCL低电平下:SDA由低变高代表传输数据结束
初始化: P6数据线(SDA) P7时钟线(SCL),选用P6和P7两根引脚,并给他们置为低电平
分别输出数据线和时钟线
开始信号:在空闲状态下先将时钟线置为低电平输出0,数据线置位高电平,再根据开始信号的触发条件:SCL高电平时,SDA由高变低,最后再拉低SCL(增加可靠性和准确行)
停止信号:SDA由低变高,原理同开始信号大差不差
ACK应答信号,假应答
发送一个字节
发送一个字节,时钟线拉低时,发送数据;拉高读取数据
void i2c_Send_Byte(char data)
{int i=0;for(i=7;i>=0;i--)//循环发送8位数据{i2c_SCL(0);//时钟线拉低i2c_SDA(data &(0x1<<i));//发送数据i2c_SCL(1);//时钟线拉高,让接受方接收数据i2c_SCL(0);//时钟线拉低}
}
发送多个字节
void i2c_Send_Bytes(int *p,int len)
{int i,j;for(j=0;j<len;j++){for(i=7;i>=0;i--){i2c_SCL(0);//时钟线拉低i2c_SDA(p[j] &(0x1<<i));i2c_SCL(1);//时钟线拉高,让接受方接收数据i2c_SCL(0);//时钟线拉低}i2c_Ack();//一个字节写完过后需要有应答信号}
}
二、oled
数据格式
在执行到Control Byte,写命令或者写数据是根据D\C(为0时命令,1时数据),根据数据格式写
oled的初始化需要用到IIC(引脚的配置),随后先将oled关闭,最后再打开,中间根据数据手册而写
清空函数:防止有外部因素的影响
void Oled_clear(void)
{int page;//页int seg;//列for(page=0;page<8;page++)//按页遍历{for(seg=0;seg<128;seg++)//每一页的每一列{Oled_Write_cmd(0xB0+page);//页地址Oled_Write_cmd(0x00+(seg & 0xf));//设置seg列的低地址Oled_Write_cmd(0x10+((seg>>4)&0xf));//设置seg列的高地址Oled_Write_data(0x0);//清屏}}
}
每一页有8行
设置行
低位与高位
显示数据:32列需要使用4页,32-64防止数据的覆盖
unsigned char buff[8][32]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x3C,0x18,0x08,0x00,0x00,0x00,0x00,0xFC,0xF8,0x08,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0x60,0x40,0x00,0x00,0x00,0x00},
{0x00,0x00,0x20,0x10,0x08,0x04,0x03,0xFF,0xFF,0x01,0x00,0x40,0x40,0x20,0x20,0x10,0xFF,0xFF,0x08,0x0C,0x06,0x02,0x03,0x01,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x4F,0x47,0x40,0x40,0x40,0x40,0x40,0x40,0xFC,0xFC,0x43,0x43,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x63,0x63,0x41,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},{0x00,0x00,0x00,0x00,0x08,0x10,0x70,0xE0,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFC,0xFC,0x48,0x40,0x40,0x40,0x40,0x40,0x60,0x60,0x40,0x00,0x00,0x00},
{0x00,0x04,0x0C,0x18,0x78,0x30,0x00,0x80,0x78,0x24,0x20,0x20,0xA2,0x22,0x22,0x22,0x22,0x22,0x3F,0x3F,0x22,0x22,0x22,0x22,0x23,0xA3,0x22,0x30,0x30,0x20,0x00,0x00},
{0x00,0x40,0x40,0xC0,0xC0,0xE0,0x7C,0x03,0x00,0x00,0x00,0x00,0xFF,0xFF,0x91,0x91,0x91,0x91,0x91,0x91,0x91,0x91,0x91,0x91,0x91,0xFF,0xFF,0x01,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x30,0x70,0x3F,0x1F,0x00,0x00,0x00,0x00,0x00}};void Oled_Display(void)
{int page;//页int seg;//列for(page=0;page<4;page++)//按页遍历{for(seg=0;seg<32;seg++)//每一页的每一列{Oled_Write_cmd(0xB0+page);//页地址Oled_Write_cmd(0x00+(seg & 0xf));//设置seg列的低地址Oled_Write_cmd(0x10+((seg>>4)&0xf));//设置seg列的高地址Oled_Write_data(buff[page][seg]);//显示数据}}for(page=0;page<4;page++)//按页遍历{for(seg=32;seg<64;seg++)//每一页的每一列{Oled_Write_cmd(0xB0+page);//页地址Oled_Write_cmd(0x00+(seg & 0xf));//设置seg列的低地址Oled_Write_cmd(0x10+((seg>>4)&0xf));//设置seg列的高地址Oled_Write_data(buff[page+4][seg-32]);//显示数据//当seg从 32 开始计数时,通过seg - 32可以将其转换为从 0 开始的索引,//这样就能正确地从buff数组中获取与当前 OLED 页面内列位置相对应的数据}}
}