打印
[通用8051核FLASH系列]

【芯圣电子HC89S105A测评报告】+OLED

[复制链接]
416|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 芯圣电子官方QQ 于 2023-7-19 16:44 编辑

今天搞了一下OLED,I2C接口。
SCL接P01,SDA接P00。
主程序代码:
#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读取一个字节的数据

/***************************************************************************************
  * @实现效果        对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_ShowString(10,0,"21ic",16);
    OLED_ShowCHinese(10,2,5);
        OLED_ShowCHinese(26,2,6);
        OLED_ShowCHinese(42,2,7);
        OLED_ShowCHinese(58,2,8);
        OLED_ShowCHinese(74,2,9);
        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"

extern void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat);
uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode)
{
   
   
    return 0;
}
//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                           
void I2C_Write1(uint8_t addr,uint8_t pBuffer)
{
       IIC_Send_Byte(addr, pBuffer);
}
/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  Write_I2C_Data 鍚慜LED鍐欏叆鏁版嵁
  * @param  I2C_Data锛氭暟鎹?
  * @retval 鏃?
  */
static void Write_I2C_Data(uint8_t I2C_Data)//鍐欐暟鎹?
{
   I2C_Write1(0x40,I2C_Data);               
}

/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]  Write_I2C_Command锛屽悜OLED鍐欏叆鍛戒护
  * @param  I2C_Command
  * @retval 鏃?
  */
static void Write_I2C_Command(uint8_t I2C_Command)//鍐欏懡浠?
{
    I2C_Write1(0x00,I2C_Command);                        
}


void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
        if(cmd)
                Write_I2C_Data(dat);
        else
                Write_I2C_Command(dat);
}

/********************************************
// fill_Picture
********************************************/
void fill_picture(uint8_t fill_Data)
{
        uint8_t m,n;
        for(m=0; m<8; m++)
        {
                OLED_WR_Byte(0xb0+m,0);                //page0-page1
                OLED_WR_Byte(0x00,0);                //low column start address
                OLED_WR_Byte(0x10,0);                //high column start address
                for(n=0; n<128; n++)
                {
                        OLED_WR_Byte(fill_Data,1);
                }
        }
}


//鍧愭爣璁剧疆
void OLED_Set_Pos(uint8_t x, uint8_t y)
{         
        OLED_WR_Byte(0xb0+y, OLED_CMD);
        OLED_WR_Byte(((x&0xf0)>>4)|0x10, OLED_CMD);
        OLED_WR_Byte((x&0x0f), OLED_CMD);
}            


//寮€鍚疧LED鏄剧ず   
void OLED_Display_On(void)
{
        OLED_WR_Byte(0X8D, OLED_CMD);  //SET DCDC鍛戒护
        OLED_WR_Byte(0X14, OLED_CMD);  //DCDC ON
        OLED_WR_Byte(0XAF, OLED_CMD);  //DISPLAY ON
}


//鍏抽棴OLED鏄剧ず     
void OLED_Display_Off(void)
{
        OLED_WR_Byte(0X8D, OLED_CMD);  //SET DCDC鍛戒护
        OLED_WR_Byte(0X10, OLED_CMD);  //DCDC OFF
        OLED_WR_Byte(0XAE, OLED_CMD);  //DISPLAY OFF
}        


//娓呭睆鍑芥暟,娓呭畬灞?鏁翠釜灞忓箷鏄粦鑹茬殑!鍜屾病鐐逛寒涓€鏍?!!         
void OLED_Clear(void)  
{  
        uint8_t i,n;                    
        for(i=0; i<8; i++)  
        {  
                OLED_WR_Byte (0xb0+i, OLED_CMD);    //璁剧疆椤靛湴鍧€锛?~7锛?
                OLED_WR_Byte (0x00,   OLED_CMD);      //璁剧疆鏄剧ず浣嶇疆鈥斿垪浣庡湴鍧€
                OLED_WR_Byte (0x10,   OLED_CMD);      //璁剧疆鏄剧ず浣嶇疆鈥斿垪楂樺湴鍧€   
                for(n=0; n<128; n++)
                        OLED_WR_Byte(0, OLED_DATA);
        }
}


void OLED_ON(void)  
{  
        uint8_t i,n;                    
        for(i=0; i<8; i++)  
        {  
                OLED_WR_Byte (0xb0+i,OLED_CMD);    //璁剧疆椤靛湴鍧€锛?~7锛?
                OLED_WR_Byte (0x00,OLED_CMD);      //璁剧疆鏄剧ず浣嶇疆鈥斿垪浣庡湴鍧€
                OLED_WR_Byte (0x10,OLED_CMD);      //璁剧疆鏄剧ず浣嶇疆鈥斿垪楂樺湴鍧€   
                for(n=0; n<128; n++)
                        OLED_WR_Byte(1, OLED_DATA);
        }
}


//鍦ㄦ寚瀹氫綅缃樉绀轰竴涓瓧绗?鍖呮嫭閮ㄥ垎瀛楃
//x:0~127
//y:0~63
//mode:0,鍙嶇櫧鏄剧ず;1,姝e父鏄剧ず                                 
//size:閫夋嫨瀛椾綋 16/12
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{              
        uint8_t 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++)
                        OLED_WR_Byte(F8X16[c*16 + i], OLED_DATA);
               
                OLED_Set_Pos(x, y+1);
                for(i=0; i<8; i++)
                        OLED_WR_Byte(F8X16[c*16 + i + 8], OLED_DATA);
        }
        else
        {        
                OLED_Set_Pos(x, y);
                for(i=0; i<6; i++)
                        OLED_WR_Byte(F6x8[c][i], OLED_DATA);
        }
}


//m^n鍑芥暟
uint32_t OLED_pow(uint8_t m, uint8_t n)
{
        uint32_t result = 1;         
        while(n--)
                result *= m;   
        
        return result;
}                                 


//鏄剧ず2涓暟瀛?
//x,y :璧风偣鍧愭爣         
//len :鏁板瓧鐨勪綅鏁?
//size:瀛椾綋澶у皬
//mode:妯″紡        0,濉厖妯″紡;1,鍙犲姞妯″紡
//num:鏁板€?0~4294967295);                           
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2)
{                 
        uint8_t t,temp;
        uint8_t 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);
        }
}


//鏄剧ず涓€涓瓧绗﹀彿涓?
void OLED_ShowString(uint8_t x, uint8_t y, char *chr, uint8_t Char_Size)
{
        uint8_t j=0;
        while (chr[j] != '\0')
        {               
                OLED_ShowChar(x, y, chr[j], Char_Size);
                x += 8;
                if(x>120)
                {
                        x=0;
                        y+=2;
                }
                j++;
        }
}


//鏄剧ず姹夊瓧
void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no)
{                                 
        uint8_t t,adder=0;
        OLED_Set_Pos(x,y);        
        for(t=0;t<16;t++)
        {
                OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
                adder+=1;
        }        
        OLED_Set_Pos(x,y+1);        
        for(t=0;t<16;t++)
        {        
                OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
                adder+=1;
        }                                       
}
/***********鍔熻兘鎻忚堪锛?
鏄剧ず鏄剧ずBMP鍥剧墖128脳64璧峰鐐瑰潗鏍?x,y),x鐨勮寖鍥?锝?27锛寉涓洪〉鐨勮寖鍥?锝?
*****************/
void OLED_DrawBMP(uint8_t x0, uint8_t y0,uint8_t x1, uint8_t y1, uint8_t BMP[])
{         
        unsigned int j=0;
        uint8_t x,y;

        if(y1%8 == 0)
                y = y1/8;      
        else
                y = y1/8 + 1;
        for(y=y0; y<y1; y++)
        {
                OLED_Set_Pos(x0, y);
                for(x=x0; x<x1; x++)
                {      
                        OLED_WR_Byte(BMP[j++], OLED_DATA);                    
                }
        }
}

//鍒濆鍖朣SD1306                                            
void OLED_Init(void)
{
        long int i;
        //Ddl_Delay1ms(100);
        for(i=0;i<20000;i++);
        OLED_WR_Byte(0xAE,OLED_CMD);//鍏抽棴鏄剧ず
        
        OLED_WR_Byte(0x40,OLED_CMD);//---set low column address
        OLED_WR_Byte(0xB0,OLED_CMD);//---set high column address

        OLED_WR_Byte(0xC8,OLED_CMD);//-not offset

        OLED_WR_Byte(0x81,OLED_CMD);//璁剧疆瀵规瘮搴?
        OLED_WR_Byte(0xff,OLED_CMD);

        OLED_WR_Byte(0xa1,OLED_CMD);//娈甸噸瀹氬悜璁剧疆

        OLED_WR_Byte(0xa6,OLED_CMD);//
        
        OLED_WR_Byte(0xa8,OLED_CMD);//璁剧疆椹卞姩璺暟
        OLED_WR_Byte(0x1f,OLED_CMD);
        
        OLED_WR_Byte(0xd3,OLED_CMD);
        OLED_WR_Byte(0x00,OLED_CMD);
        
        OLED_WR_Byte(0xd5,OLED_CMD);
        OLED_WR_Byte(0xf0,OLED_CMD);
        
        OLED_WR_Byte(0xd9,OLED_CMD);
        OLED_WR_Byte(0x22,OLED_CMD);
        
        OLED_WR_Byte(0xda,OLED_CMD);
        OLED_WR_Byte(0x02,OLED_CMD);
        
        OLED_WR_Byte(0xdb,OLED_CMD);
        OLED_WR_Byte(0x49,OLED_CMD);
        
        OLED_WR_Byte(0x8d,OLED_CMD);
        OLED_WR_Byte(0x14,OLED_CMD);
        
        OLED_WR_Byte(0xaf,OLED_CMD);
        OLED_Clear();
}  

效果图:

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

337

主题

3059

帖子

7

粉丝