本帖最后由 芯圣电子官方QQ 于 2023-7-19 16:43 编辑
今天搞了一下另一种OLED。
SCL接P0.1,SDA接P0.0
主程序代码:
#define ALLOCATE_EXTERN
#include "HC89S105AC8.h"
#include "oled.h"
#define DEVICEADD 0x78 //24C02器件地址
#define SCL P0_1 //I2C时钟控制位
#define SDA P0_0 //I2C数据传送位
unsigned char temp1,temp2,temp3; //用于存放读写数据
void Delay_ms(unsigned int fui_i); //延时函数
void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat); //IIC发送一个字节的数据
unsigned char IIC_Read_Byte(unsigned char fuc_Add); //IIC读取一个字节的数据
char CharData[] = { "bbs.21ic.com" };
char CharData1[] = { "HC89S105A8" };
/***************************************************************************************
* @实现效果 对24C02进行读写操作
***************************************************************************************/
void main()
{
/************************************系统初始化****************************************/
CLKCON = 0x02; //选择内部高频RC为系统时钟,内部高频RC Fosc=32MHz
CLKDIV = 0x02; //Fosc 2分频得到Fcpu,Fcpu=16MHz
/**********************************IIC配置初始化***************************************/
P0M0 = P0M0 & 0xF0 | 0x0A; //P00带上拉开漏输出
P0M0 = P0M0 & 0x0F | 0x80; //P01推挽输出
SCL_MAP = 0x01; //SCL映射P01口
SDA_MAP = 0x00; //SDA映射P00口
IICCON = 0x40; //启动IIC模块
OLED_Init();
OLED_Clear();
OLED_ShowString(10,0,(u8 *)CharData,16);
OLED_ShowCHinese(10,2,0);
OLED_ShowCHinese(26,2,1);
OLED_ShowCHinese(42,2,2);
OLED_ShowString(10,4,(u8 *)CharData1,16);
OLED_ShowCHinese(10,6,8);
OLED_ShowCHinese(26,6,9);
OLED_ShowCHinese(42,6,5);
OLED_ShowCHinese(58,6,6);
OLED_ShowCHinese(74,6,7);
while (1)
{
}
}
/***************************************************************************************
* @说明 IIC发送一个字节的数据
* @参数 fuc_Add:存储的地址
* fuc_Dat:存储的数据
* @返回值 无
* @注 无
***************************************************************************************/
void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat)
{
IICCON &= ~0x08; //清除中断标志位
IICCON |= 0x20; //起始位发送起始信号
while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
IICCON &= ~0x20; //起始位停止起始信号
IICCON &= ~0x08; //清除中断标志位
IICDAT = DEVICEADD; //写入器件地址
while (IICSTA != 0x18);
IICCON &= ~0x08; //清除中断标志位
IICDAT = fuc_Add; //写入地址
while (IICSTA != 0x28);
IICCON &= ~0x08; //清除中断标志位
IICDAT = fuc_Dat; //写入数据
while (IICSTA != 0x28);
IICCON &= ~0x08; //清除中断标志位
IICCON |= 0x10; //停止位发送停止信号
}
/***************************************************************************************
* @说明 IIC读取一个字节的数据
* @参数 fuc_Add:存储的地址
* @返回值 存储地址里面的数据
* @注 无
***************************************************************************************/
unsigned char IIC_Read_Byte(unsigned char fuc_Add)
{
unsigned char fuc_Dat;
IICCON &= ~0x08; //清除中断标志位
IICCON |= 0x20; //起始位发送起始信号
while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
IICCON &= ~0x20; //起始位停止起始信号
IICCON &= ~0x08; //清除中断标志位
IICDAT = DEVICEADD; //写入器件地址
while (IICSTA != 0x18);
IICCON &= ~0x08; //清除中断标志位
IICDAT = fuc_Add; //写入读地址
while (IICSTA != 0x28);
IICCON &= ~0x08; //清除中断标志位
IICCON |= 0x20; //起始位发送起始信号
while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
IICCON &= ~0x20; //起始位停止起始信号
IICCON &= ~0x08; //清除中断标志位
IICDAT = DEVICEADD | 0x01; //写入读指令
while (IICSTA != 0x40);
IICCON &= ~0x08; //清除中断标志位
while (IICSTA != 0x58);
fuc_Dat = IICDAT; //读入数据
IICCON &= ~0x08; //清除中断标志位
IICCON |= 0x10; //停止位发送停止信号
return fuc_Dat;
}
/***************************************************************************************
* @说明 延时函数
* @参数 fui_i : 延时时间
* @返回值 无
* @注 Fcpu = 16MHz,fui_i = 1时,延时时间约为1ms
***************************************************************************************/
void Delay_ms(unsigned int fui_i)
{
unsigned int fui_j;
for (; fui_i > 0; fui_i--)
for (fui_j = 1596; fui_j > 0; fui_j--);
}
OLED.C
#include "oled.h"
#define u8 unsigned char
#define Max_Column 128
//OLED鐨勬樉瀛?
//瀛樻斁鏍煎紡濡備笅.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
extern void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat);
void Delay1(unsigned long time)
{
/*<UserCodeStart>*//*<SinOne-Tag><64>*/
for(;time>0;time--);
/*<UserCodeEnd>*//*<SinOne-Tag><64>*/
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Write_I2C_Data 鍚慜LED鍐欏叆鏁版嵁
* @param I2C_Data锛氭暟鎹?
* @retval 鏃?
*/
u8 TWI_WriteOneByte(u8 DeviceAddr,u8 DataAddr,u8 u8Data)
{
IIC_Send_Byte(DataAddr, u8Data);
return 0;
}
////OLED娓呭睆
void OLED_Clear()
{
/*<UserCodeStart>*//*<SinOne-Tag><65>*/
unsigned int i,n;
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x00,0xb0+i);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0x10);
for(n=0;n<128;n++)
{
TWI_WriteOneByte(0x78,0x40,0);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><65>*/
}
////OLED鍒濆鍖?
void OLED_Init()
{
/*<UserCodeStart>*//*<SinOne-Tag><66>*/
Delay1(100);
TWI_WriteOneByte(0x78,0x00,0xAE);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0x10);
TWI_WriteOneByte(0x78,0x00,0x40);
TWI_WriteOneByte(0x78,0x00,0xB0);
TWI_WriteOneByte(0x78,0x00,0x81);
TWI_WriteOneByte(0x78,0x00,0xFF);
TWI_WriteOneByte(0x78,0x00,0xA1);
TWI_WriteOneByte(0x78,0x00,0xA6);
TWI_WriteOneByte(0x78,0x00,0xA8);
TWI_WriteOneByte(0x78,0x00,0x3F);
TWI_WriteOneByte(0x78,0x00,0xC8);
TWI_WriteOneByte(0x78,0x00,0xD3);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0xD5);
TWI_WriteOneByte(0x78,0x00,0x80);
TWI_WriteOneByte(0x78,0x00,0xD8);
TWI_WriteOneByte(0x78,0x00,0x05);
TWI_WriteOneByte(0x78,0x00,0xD9);
TWI_WriteOneByte(0x78,0x00,0xF1);
TWI_WriteOneByte(0x78,0x00,0xDA);
TWI_WriteOneByte(0x78,0x00,0x12);
TWI_WriteOneByte(0x78,0x00,0xDB);
TWI_WriteOneByte(0x78,0x00,0x30);
TWI_WriteOneByte(0x78,0x00,0x8D);
TWI_WriteOneByte(0x78,0x00,0x14);
TWI_WriteOneByte(0x78,0x00,0xAF);
Delay1(100);
/*<UserCodeEnd>*//*<SinOne-Tag><66>*/
}
////娓呴櫎鎸囧畾鍖哄煙瀛楃x0,y0,宸︿笂瑙掑潗鏍噚1,y1,鍙充笅瑙掑潗鏍?
void OLED_PartClear(u8 x0,u8 y0,u8 x1,u8 y1)
{
/*<UserCodeStart>*//*<SinOne-Tag><67>*/
u8 i,n;
for( i=x0;i<x1;i++)
{
for( n=y0;n<y1;n++)
{
TWI_WriteOneByte(0x78,0x00,0xb0+i);
TWI_WriteOneByte(0x78,0x00,((n&0xf0)>>4)|0x10);
TWI_WriteOneByte(0x78,0x00,(n&0x0f));
TWI_WriteOneByte(0x78,0x40,0);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><67>*/
}
////m^n鍑芥暟
unsigned long oled_pow(u8 m,u8 n)
{
/*<UserCodeStart>*//*<SinOne-Tag><68>*/
u32 result=1;
while(n--)
{
result*=m;
}
return result;
/*<UserCodeEnd>*//*<SinOne-Tag><68>*/
}
////鍧愭爣璁剧疆
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
/*<UserCodeStart>*//*<SinOne-Tag><69>*/
TWI_WriteOneByte(0x78,0x00,0xb0+y);
TWI_WriteOneByte(0x78,0x00,((x&0xf0)>>4)|0x10);
TWI_WriteOneByte(0x78,0x00,(x&0x0f));
/*<UserCodeEnd>*//*<SinOne-Tag><69>*/
}
////鍦ㄦ寚瀹氫綅缃樉绀轰竴涓瓧绗?鍖呮嫭閮ㄥ垎瀛楃x:0~127y:0~8chr:瀛楃鍦板潃size:閫夋嫨瀛椾綋16/12
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{
/*<UserCodeStart>*//*<SinOne-Tag><70>*/
unsigned char c=0,i=0;
c=chr-' ';//寰楀埌鍋忕Щ鍚庣殑鍊?
if(x>Max_Column-1)
{
x=0;
y=y+2;
}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x40,F8X16[c*16+i]);
}
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x40,F8X16[c*16+i+8]);
}
}
else
{
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
{
TWI_WriteOneByte(0x78,0x40,F6x8[c][i]);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><70>*/
}
////鏄剧ず姹夊瓧
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
/*<UserCodeStart>*//*<SinOne-Tag><71>*/
u8 t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
TWI_WriteOneByte(0x78,0x40,Hzk[2*no][t]);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
TWI_WriteOneByte(0x78,0x40,Hzk[2*no+1][t]);
adder+=1;
}
/*<UserCodeEnd>*//*<SinOne-Tag><71>*/
}
/******************************************************************************************************
*鏄剧ず2涓暟瀛梮,y :璧风偣鍧愭爣len :鏁板瓧鐨勪綅鏁皊ize:瀛椾綋澶у皬num:鏁板€?0~429496729
*5)
******************************************************************************************************/
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{
/*<UserCodeStart>*//*<SinOne-Tag><72>*/
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}
else
enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
/*<UserCodeEnd>*//*<SinOne-Tag><72>*/
}
////鏄剧ず涓€涓瓧绗︿覆
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
{
/*<UserCodeStart>*//*<SinOne-Tag><73>*/
unsigned char j=0;
while(chr[j]!='\0')
{
OLED_ShowChar(x,y,chr[j],Char_Size);
x+=8;
if(x>120)
{
x=0;
y+=2;
}
j++;
}
/*<UserCodeEnd>*//*<SinOne-Tag><73>*/
}
效果图:
|
|