CC2530之OLED12864程序详解

暂时先贴上程序:(我再整理整理)

#include "code_tab.h"

#define  LCD_SCL   P0_4  // 串行时钟输入  
#define  LCD_SDA   P0_5  // 串行数据输入
#define  LCD_RST   P0_6  // 复位输入端 
#define  LCD_DC    P0_7  // 数据/命令控制

#define XLevelL      0x00
#define XLevelH      0x10
#define XLevel       ((XLevelH&0x0F)*16 + XLevelL)
#define Max_Column   132
#define Max_Row      32
#define Brightness   0xCF 
#define X_WIDTH      132
#define Y_WIDTH      32

#define   oledWrDat(val)    spiWriteByte(val, 1)   
#define   oledWrCmd(val)    spiWriteByte(val, 0)

void delay_ms(uint ms)
{
    unsigned int a;
    while(ms)
    {
        a = 1800;
        while(a--);
        ms--;
    }
    return;
}

/** 
 * OLED SSD1306初始化
 */
void lcdInit(void)     
{  
    P0DIR = 0xF0;      // 设置相关端口

    LCD_SCL = 1;
    LCD_RST = 0;
    delay_ms(50);
    LCD_RST = 1;        // 从上电到下面开始初始化要有足够的时间,即等待RC复位完毕   

    oledWrCmd(0xae);    // turn off oled panel
    oledWrCmd(0x00);    // set low column address
    oledWrCmd(0x10);    // set high column address
    oledWrCmd(0x40);    // set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
    oledWrCmd(0x81);    // set contrast control register
    oledWrCmd(0x8f);    // Set SEG Output Current Brightness
    oledWrCmd(0xa1);    // Set SEG/Column Mapping     0xa0左右反置 0xa1正常
    oledWrCmd(0xc8);    // Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
    oledWrCmd(0xa6);    // set normal display
    oledWrCmd(0xa8);    // set multiplex ratio(1 to 64)
    oledWrCmd(0x1f);    // 1/32 duty
    oledWrCmd(0xd3);    // set display offset   Shift Mapping RAM Counter (0x00~0x3F)
    oledWrCmd(0x00);    // not offset
    oledWrCmd(0xd5);    // set display clock divide ratio/oscillator frequency
    oledWrCmd(0x80);    // set divide ratio, Set Clock as 100 Frames/Sec
    oledWrCmd(0xd9);    // set pre-charge period
    oledWrCmd(0xf1);    // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
    oledWrCmd(0xda);    // set com pins hardware configuration
    oledWrCmd(0x02);
    oledWrCmd(0xdb);    // set vcomh
    oledWrCmd(0x40);    // Set VCOM Deselect Level
    oledWrCmd(0x20);    // Set Page Addressing Mode (0x00/0x01/0x02)
    oledWrCmd(0x02);    //
    oledWrCmd(0x8d);    // set Charge Pump enable/disable
    oledWrCmd(0x14);    // set(0x10) disable
    oledWrCmd(0xa4);    // Disable Entire Display On (0xa4/0xa5)
    oledWrCmd(0xa6);    // Disable Inverse Display On (0xa6/a7) 
    oledWrCmd(0xaf);    // --turn on oled panel
    oledFill(0x00);     //初始清屏
    oledSetPos(0,0);    
} 

/**
 * 串行数据总线传输一字节数据
 * @param[in] byte:传输的数据
 * @param[in] sel:0-写命令,1-写数据
 */
static void spiWriteByte(uchar byte, uchar sel)
{
    uchar i;

    LCD_DC = 0;  
    if(sel == 1)  LCD_DC = 1;  

    for (i=0; i<8; i++) {   // 发送一个八位数据 
        if((byte << i) & 0x80)
            LCD_SDA  = 1;
        else 
            LCD_SDA  = 0;
        LCD_SCL = 0;
        LCD_SCL = 1;
    }
}

/**
 * 设置显示坐标
 */
void oledSetPos(uchar x, uchar y) 
{ 
    x += 4;
    oledWrCmd(0xb0+y);
    oledWrCmd(((x&0xf0)>>4)|0x10);
    oledWrCmd((x&0x0f)|0x01); 
} 

/** 
 * 全屏
 */
void oledFill(uchar bmp_dat) 
{
    unsigned char y, x;

    for (y=0; y<8; y++) {
        oledWrCmd(0xb0+y);
        oledWrCmd(0x01);
        oledWrCmd(0x10);
        for(x=0; x/** 
 * 复位清屏
 */
void oledClear(void)
{
    uchar y, x; 

    for (y=0; y<8; y++) {
        oledWrCmd(0xb0+y);
        oledWrCmd(0x01);
        oledWrCmd(0x10); 
        for(x=0; x0);
    }
}

/**
 * 描述:显示6*8一组标准ASCII字符串,显示的坐标(x,y),y为页范围0~7
 */
void lcdP6x8Str(uchar x, uchar y, uchar ch[])
{
    unsigned char c=0, i=0, j=0;  

    while (ch[j] != '\0') {    
        c = ch[j]-32;
        if(x>126) {x=0; y++;}
        oledSetPos(x,y);    
        for(i=0; i<6; i++)     
            oledWrDat(F6x8[c][i]);  
        x += 6;
        j++;
    }
}

/**
 * 描述:显示8*16一组标准ASCII字符串,显示的坐标(x,y),y为页范围0~7
 */
void lcdP8x16Str(uchar x, uchar y, uchar ch[])
{
    unsigned char c=0,i=0,j=0;

    while (ch[j] != '\0') {    
        c = ch[j]-32;
        if (x>120) { 
           x = 0; 
           y++; 
        }
        oledSetPos(x,y);    
        for(i=0;i<8;i++)     
            oledWrDat(F8X16[c*16+i]);
        oledSetPos(x,y+1);    
        for(i=0; i<8; i++)     
            oledWrDat(F8X16[c*16+i+8]);  
        x+=8;
        j++;
    }
}

/**
 * 描述:显示显示BMP图片128×64起始点坐标(x0, y0),x的范围0~127,y为页的范围0~7
 */
void lcdDrawBMP(uchar x0, uchar y0, uchar x1, uchar y1, uchar BMP[])
{   
    uint j=0;
    uchar x, y;

    if(y1%8 == 0) y = y1/8;      
    else y = y1/8+1;
    for (y=y0; yfor (x=x0; x

你可能感兴趣的:(MCU,-,CC2530)