extern unsigned charF16x16[1024];
extern unsigned charBMP[2048];
extern const unsigned charF6x8[92][6];
extern const unsigned charF8X16[1600];
数据太大,不便粘贴;如有需要的朋友,可以联系我传给你;目前实验已完成,由于权限的问题,不能上传图片,敬请谅解。
个人感觉IIC通信比四线SPI(见上篇博客)简单,具体体现:硬件、软件和接线都比较少,只代表个人观点;其实如果会的话,应该都差不多。
IIC.h
#ifndef __IIC_H
#define __IIC_H
#ifdef __cplusplus
extern "C" {
#endif
/*************Pin Define***************/
#define SCL_HIGHGPIO_SetBits(GPIOB, GPIO_Pin_8) //LED1点亮//P1OUT|=BIT0 //SCL P1.0
#define SCL_LOW GPIO_ResetBits(GPIOB, GPIO_Pin_8) //P1OUT&=~BIT0
#define SDA_HIGHGPIO_SetBits(GPIOB, GPIO_Pin_9) //P1OUT|=BIT1 //SDA P1.1
#define SDA_LOW GPIO_ResetBits(GPIOB, GPIO_Pin_9) //P1OUT&=~BIT1
/****************************************************/
void OLED_Initial(void);
void IIC_Start(void);
void IIC_Stop(void);
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
void LCD_Set_Pos(unsigned char x, unsigned char y);
void LCD_CLS(void);
void LCD_P8x16Str(unsigned char x,unsigned chary,unsigned char ch[]);
void LCD_P16x16Ch(unsigned char x,unsigned chary,unsigned charN);
void LCD_P6x8Str(unsigned char x,unsigned chary,unsigned char ch[]);
void Draw_BMP(unsigned char x0,unsigned chary0,unsigned char x1,unsigned chary1,unsigned char BMP[]);
#ifdef __cplusplus
}
#endif
#endif
IIC.C
#include
#include "stm32f10x_i2c.h"
#include "IIC.h"
#include "delay.h"
#include "sys.h"
#define XLevelL 0x00
#define XLevelH 0x10
#define XLevel ((XLevelH&0x0F)*16+XLevelL)
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xCF
#define X_WIDTH 128
#define Y_WIDTH 64
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OLED_Initial()
{
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* PB8,9 SCL and SDA */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(100); //延时
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x20); //Set Memory Addressing Mode
Write_IIC_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
Write_IIC_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8); //Set COM Output Scan Direction
Write_IIC_Command(0x00);//---set low column address
Write_IIC_Command(0x10);//---set high column address
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0x7f);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x3F);//
Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock pide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set pide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22); //
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x20);//0x20,0.77xVcc
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);//
Write_IIC_Command(0xaf);//--turn on oled panel
LCD_CLS();
LCD_Set_Pos(0,0);
}
/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
SCL_HIGH;
SDA_HIGH;
SDA_LOW;
SCL_LOW;
}
/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
SCL_LOW;
SDA_LOW;
SCL_HIGH;
SDA_HIGH;
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(IIC_Byte&0x80)//1?0?
SDA_HIGH;
else
SDA_LOW;
SCL_HIGH;
SCL_LOW;
IIC_Byte<<=1; //loop
}
SDA_HIGH;
SCL_HIGH;
SCL_LOW;
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
Write_IIC_Byte(0x00); //write command
Write_IIC_Byte(IIC_Command);
IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x40); //write data
Write_IIC_Byte(IIC_Data);
IIC_Stop();
}
/*********************LCD 设置坐标************************************/
void LCD_Set_Pos(unsigned char x, unsigned char y)
{
Write_IIC_Command(0xb0+y);
Write_IIC_Command(((x&0xf0)>>4)|0x10);
Write_IIC_Command((x&0x0f)|0x01);
}
/*********************LCD复位************************************/
void LCD_CLS(void)
{
unsigned char y,x;
for(y=0;y<8;y++)
{
Write_IIC_Command(0xb0+y);
Write_IIC_Command(0x01);
Write_IIC_Command(0x10);
for(x=0;x<X_WIDTH;x++)
Write_IIC_Data(0);
}
}
/***************功能描述:显示6*8一组标准ASCII字符串 显示的坐标(x,y),y为页范围0~7****************/
void LCD_P6x8Str(unsigned char x,unsigned chary,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!=' ')
{
c =ch[j]-32;
if(x>126){x=0;y++;}
LCD_Set_Pos(x,y);
for(i=0;i<6;i++)
Write_IIC_Data(F6x8[c][i]);
x+=6;
j++;
}
}
/*******************功能描述:显示8*16一组标准ASCII字符串 显示的坐标(x,y),y为页范围0~7****************/
void LCD_P8x16Str(unsigned char x,unsigned chary,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!=' ')
{
c =ch[j]-32;
if(x>120){x=0;y++;}
LCD_Set_Pos(x,y);
for(i=0;i<8;i++)
Write_IIC_Data(F8X16[c*16+i]);
LCD_Set_Pos(x,y+1);
for(i=0;i<8;i++)
Write_IIC_Data(F8X16[c*16+i+8]);
x+=8;
j++;
}
}
/*****************功能描述:显示16*16点阵显示的坐标(x,y),y为页范围0~7****************************/
void LCD_P16x16Ch(unsigned char x,unsigned chary,unsigned charN)
{
unsigned char wm=0;
unsigned int adder=32*N;//
LCD_Set_Pos(x , y);
for(wm = 0;wm < 16;wm++)//
{
Write_IIC_Data(F16x16[adder]);
adder += 1;
}
LCD_Set_Pos(x,y + 1);
for(wm = 0;wm < 16;wm++) //
{
Write_IIC_Data(F16x16[adder]);
adder += 1;
}
}
/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
void Draw_BMP(unsigned char x0,unsigned chary0,unsigned char x1,unsigned chary1,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++)
{
LCD_Set_Pos(x0,y);
for(x=x0;x<x1;x++)
{
Write_IIC_Data(BMP[j++]);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
/* Add your application code here*/
char i;
LED_GPIO_Config();
delay_init(72); //延时初始化
OLED_Initial();
LCD_P8x16Str(28,0,"OLED TEST");
LCD_P8x16Str(20,2,"OLEDDISPLAY");
LCD_P6x8Str(16,5,"315322339@qq.com");
LCD_P6x8Str(34,6,"2014-11-22");
delay_ms(1500); //延时1s
Draw_BMP(0,0,128,8,BMP);//图片显示
delay_ms(1000);
LCD_CLS();
/* Infinite loop */
while(1)
{
LED4_OFF;
delay_ms(500); //延时1s
LED4_ON;
delay_ms(500); //延时1s
for(i=0; i<8; i++)
{
LCD_P16x16Ch(i*16,0,i);//点阵显示
LCD_P16x16Ch(i*16,2,i+8);
LCD_P16x16Ch(i*16,4,i+16);
LCD_P16x16Ch(i*16,6,i+24);
}
}
}