单片机IIC模式配置
配置单片机的IIC标准模式,速录100K,使能II2C global interrrupt
中断。
功能函数
#ifndef DEV_I2C_C
#define DEV_I2C_C
#include "dev_i2c.h"
/*******************************组件接口*********************************/
void Dev_I2c_Init(void);
void Dev_I2c_Start(void);
void Dev_I2c_Message(MSG_E msg,unsigned long para1,unsigned long para2);
void Dev_I2c_Process(void);
void OLED_Init();
/*******************************全局定义*********************************/
const SYS_ENTRY_S g_stDevEntry_I2c = {"DEV","GPIO",Dev_I2c_Init,Dev_I2c_Start,Dev_I2c_Message,Dev_I2c_Process};
/*********************************END************************************/
extern char g_uart_flag;
void Dev_I2c_Init(void)
{
MX_I2C2_Init();
return;
}
void Dev_I2c_Start(void)
{
return;
}
static UCHAR delay_cnt = 0;
static UCHAR delay_flag = 0;
void Dev_I2c_Message(MSG_E msg,unsigned long para1,unsigned long para2)
{
if(!MSG_IsValid(msg)) {
return;
}
switch(msg) {
case MSG_Tim_1s:
if (delay_flag == 0) {
delay_cnt++;
if(delay_cnt >= 2) {
delay_flag = 1;
}
}
if (g_uart_flag == 1) {
OLED_DrawBMP(0,0,128,8,BMP1);
}
break;
case MSG_Tim_100ms:
break;
case MSG_Tim_1ms:
break;
default:
break;
}
return;
}
void Dev_I2c_Process(void)
{
if (delay_flag==1) {
OLED_Init();
OLED_CLS();
delay_flag = 2;
/*for(int i=0; i<7; i++) {
OLED_ShowCN(10+i*16,1,i); //测试
}*/
OLED_DrawBMP(0,0,128,8,BMP_Charging);
}
return;
}
void I2C_WriteByte(uint8_t register_address, uint8_t data)
{
uint8_t buffer[2] = {register_address,data};
HAL_I2C_Master_Transmit(&HAL_I2C,OLED_ADDRESS,buffer,2,1000);
}
void WriteCmd(unsigned char I2C_Command)//写命令
{
I2C_WriteByte(0x00, I2C_Command);
}
void WriteDat(unsigned char I2C_Data)//写数据
{
I2C_WriteByte(0x40, I2C_Data);
}
void OLED_Fill(unsigned char fill_Data)//全屏填充
{
unsigned char m,n;
for(m=0; m<8; m++) {
WriteCmd(0xb0+m); //page0-page1
WriteCmd(0x00); //low column start address
WriteCmd(0x10); //high column start address
for(n=0; n<128; n++) {
WriteDat(fill_Data);
}
}
}
void OLED_CLS()//清屏
{
OLED_Fill(0x00);
}
void OLED_Init()
{
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
}
void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
/*
* x,y : 起始点坐标(x:0~127, y:0~7);
* ch[] :- 要显示的字符串;
* TextSize : 字符模式选择1或者2。字符大小(1:6*8 ; 2:8*16)
*/
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
unsigned char c = 0,i = 0,j = 0;
switch(TextSize) {
case 1: {
while(ch[j] != '\0') {
c = ch[j] - 32;
if(x > 126) {
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0; i<6; i++)
WriteDat(F6x8[c][i]);
x += 6;
j++;
}
}
break;
case 2: {
while(ch[j] != '\0') {
c = ch[j] - 32;
if(x > 120) {
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0; i<8; i++)
WriteDat(F8X16[c*16+i]);
OLED_SetPos(x,y+1);
for(i=0; i<8; i++)
WriteDat(F8X16[c*16+i+8]);
x += 8;
j++;
}
}
break;
}
}
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
unsigned char wm=0;
unsigned int adder=32*N;
OLED_SetPos(x , y);
for(wm = 0; wm < 16; wm++) {
WriteDat(F16x16[adder]);
adder += 1;
}
OLED_SetPos(x,y + 1);
for(wm = 0; wm < 16; wm++) {
WriteDat(F16x16[adder]);
adder += 1;
}
}
/*x0,y0 :起始点坐标(x0:0~127, y0:0~7);
* x1,y1 : 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
*/
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1, unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0)
y = y1/8;
else
y = y1/8 + 1;
for(y=y0; y<y1; y++) {
OLED_SetPos(x0,y);
for(x=x0; x<x1; x++) {
WriteDat(BMP[j++]);
}
}
}
#endif