标题:
stm32单片机源码分享,0.68英寸OLED源码,模拟I2C。
[打印本页]
作者:
qzc13457
时间:
2018-9-25 15:41
标题:
stm32单片机源码分享,0.68英寸OLED源码,模拟I2C。
单片机资料分享,0.68英寸OLED源码,模拟I2C。
#include "stm32f10x.h"
#include "oled.h"
#include "delay.h"
#include "font.h"
/**
** 初始化IIC接口
**/
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructer;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; //1--SCL 2--SDA
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructer);
}
/**
** 设置SDA为输出
**/
void SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructer;
GPIO_InitStructer.GPIO_Pin= GPIO_Pin_2;
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructer);
}
/**
** 设置SDA为输入
**/
void SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructer;
GPIO_InitStructer.GPIO_Pin= GPIO_Pin_2;
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructer);
}
//开始信号
void IIC_Start(void)
{
SDA_OUT();
IIC_SDA_1;
IIC_SCL_1;
delay_us(2);
IIC_SDA_0;
delay_us(2);
IIC_SCL_0;
delay_us(2);
}
void IIC_Stop(void)
{
IIC_SCL_1;
IIC_SDA_0;
delay_us(2);
IIC_SDA_1;
delay_us(2);
}
/*
* 返回1--应答出错
* 放回0--应答正确
*/
u8 IIC_Wait_Ask(void)
{
int count=0;
SDA_IN();
IIC_SCL_1;
delay_us(2);
while( READ_SDA )
{
count++;
if(count>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_0;
delay_us(2);
return 0;
}
//写一个字节
void IIC_WriteByte(u8 data)
{
u8 i;
SDA_OUT();
for(i=0;i<8;i++)
{
IIC_SCL_0;
delay_us(2);
if(data & 0x80) //MSB,从高位开始一位一位传输
IIC_SDA_1;
else
IIC_SDA_0;
IIC_SCL_1;
delay_us(2);
IIC_SCL_0;
data<<=1;
}
}
u8 IIC_ReadByte(void)
{
u8 data,i;
IIC_SDA_1;
delay_us(2);
for(i=0;i<8;i++)
{
data<<=1;
IIC_SCL_0;
delay_us(2);
IIC_SCL_1;
delay_us(2);
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7))
data=data | 0x01;
else
data=data & 0xFE;
}
IIC_SCL_0;
delay_us(2);
return data;
}
void WriteCmd(u8 command)
{
IIC_Start();
IIC_WriteByte(0x78);//OLED地址
IIC_Wait_Ask();
IIC_WriteByte(0x00);//寄存器地址
IIC_Wait_Ask();
IIC_WriteByte(command);
IIC_Wait_Ask();
IIC_Stop();
}
void WriteDat(u8 data)
{
IIC_Start();
IIC_WriteByte(0x78);//OLED地址
IIC_Wait_Ask();
IIC_WriteByte(0x40);//寄存器地址
IIC_Wait_Ask();
IIC_WriteByte(data);
IIC_Wait_Ask();
IIC_Stop();
}
void OLED_Init(void)
{
delay_ms(100); //这里的延时很重要
WriteCmd(0xAE); //display off
WriteCmd(0x20); //Set Memory Addressing Mode
WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
WriteCmd(0xc8); //Set COM Output Scan Direction
WriteCmd(0x00); //---set low column address
WriteCmd(0x10); //---set high column address
WriteCmd(0x40); //--set start line address
WriteCmd(0x81); //--set contrast control register
WriteCmd(0xff); //亮度调节 0x00~0xff
WriteCmd(0xa1); //--set segment re-map 0 to 127
WriteCmd(0xa6); //--set normal display
WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
WriteCmd(0x3F); //
WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
WriteCmd(0xd3); //-set display offset
WriteCmd(0x00); //-not offset
WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
WriteCmd(0xf0); //--set divide ratio
WriteCmd(0xd9); //--set pre-charge period
WriteCmd(0x22); //
WriteCmd(0xda); //--set com pins hardware configuration
WriteCmd(0x12);
WriteCmd(0xdb); //--set vcomh
WriteCmd(0x20); //0x20,0.77xVcc
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
// WriteCmd(0xAE); //display off
// WriteCmd(0x00); //Set Memory Addressing Mode
// WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
// WriteCmd(0x40); //Set Page Start Address for Page Addressing Mode,0-7
// WriteCmd(0x81); //Set COM Output Scan Direction
// WriteCmd(0xff); //---set low column address
// WriteCmd(0xa1); //---set high column address
// WriteCmd(0xc8); //--set start line address
// WriteCmd(0xa6); //--set contrast control register
// WriteCmd(0xa8); //亮度调节 0x00~0xff
// WriteCmd(0x3f); //--set segment re-map 0 to 127
// WriteCmd(0xd3); //--set normal display
// WriteCmd(0x00); //--set multiplex ratio(1 to 64)
// WriteCmd(0xd5); //
// WriteCmd(0x80); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
// WriteCmd(0xd9); //-set display offset
// WriteCmd(0xf1); //-not offset
// WriteCmd(0xda); //--set display clock divide ratio/oscillator frequency
// WriteCmd(0x12); //--set divide ratio
// WriteCmd(0xdb); //--set pre-charge period
// WriteCmd(0x40); //
// WriteCmd(0x20); //--set com pins hardware configuration
// WriteCmd(0x02);
// WriteCmd(0x8d); //--set vcomh
// WriteCmd(0x14); //0x20,0.77xVcc
// WriteCmd(0xa4); //--set DC-DC enable
// WriteCmd(0xa6); //
// WriteCmd(0xaf); //--turn on oled panel
// OLED_Fill(0x00);
// OLED_SetPos(0,0);
}
/**
* @brief OLED_ON,将OLED从休眠中唤醒
* @param 无
* @retval 无
*/
void OLED_ON(void)
{
WriteCmd(0X8D); //设置电荷泵
WriteCmd(0X14); //开启电荷泵
WriteCmd(0XAF); //OLED唤醒
}
/**
* @brief OLED_SetPos,设置光标
* @param x,光标x位置
* y,光标y位置
* @retval 无
*/
void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
/**
* @brief OLED_Fill,填充整个屏幕
* @param fill_Data:要填充的数据
* @retval 无
*/
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(void)//清屏
{
OLED_Fill(0x00);
}
/**
* @brief OLED_ShowStr,显示codetab.h中的ASCII字符,有6*8和8*16可选择
* @param x,y : 起始点坐标(x:0~127, y:0~7);
* ch[] :- 要显示的字符串;
* TextSize : 字符大小(1:6*8 ; 2:8*16)
* @retval 无
*/
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(F8x16[c][i]); //6*8
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;
}
}
复制代码
oled.rar
2018-9-25 15:41 上传
点击文件名下载附件
下载积分: 黑币 -5
2.3 KB, 下载次数: 48, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1