美文网首页
(基础外围设备)软件SPI

(基础外围设备)软件SPI

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-08-31 19:58 被阅读17次

#include "lcd.h"
#include "delay.h"

#define uint8_t unsigned char
#define uint16_t unsigned short


#define   Write_Command(A) spi_r_datcmd(A,OLED_CMD);


static uint8_t OLED_GRAM[8][128];
static uint8_t led_on_flg=0;

/***********************************************************************
//SPI function
************************************************************************/
static void spi_pin_init(void)
{
  
  P0DIR |= 0x01;      //P00 设置输出  
  P0SEL &= ~0x01;

  P0DIR |= 0x02;      //P01 设置输出  
  P0SEL &= ~0x02;  

  P0DIR |= 0x04;      //P02 设置输出  
  P0SEL &= ~0x04;

  P0DIR |= 0x08;      //P03 设置输出  
  P0SEL &= ~0x08; 

  P0DIR |= 0x10;      //P04 设置输出  
  P0SEL &= ~0x10;

  OLED_CS_Set();
    

}
static void spi_r_datcmd(unsigned char dat,unsigned char cmd)     //spiдcmd:0?? 1ú??
{
    unsigned char i;             //data????
    if(cmd)
        OLED_DC_Set();    //??  dc=1
    else
        OLED_DC_Clr();        //??  dc=0

    OLED_CS_Clr();         //?? cs=0
    delay_us(1);
    for(i=0; i<8; i++)
    {
        OLED_SCLK_Clr();       //clk=0   ??
        if(dat&0x80)
            OLED_SDIN_Set();
        else
            OLED_SDIN_Clr();
        OLED_SCLK_Set();       //clk=1
        dat<<=1;
    }
    delay_us(1);
    OLED_CS_Set();          //??cs=1
    OLED_DC_Set();             //dc=1
    delay_us(1);
}


void writedata(unsigned char datatowrite)//д??
{
    spi_r_datcmd(datatowrite,1);
}
/************************************************************
OLED DRIVER
*************************************************************/
//96*32
void oled_set_pos(unsigned char x, unsigned char y)
{
    uint16_t x0,y0;
    x0=x+X_OFFSET;
    y0=y+Y_OFFSET;
    spi_r_datcmd(0xb0+y0,OLED_CMD);//b0 ae
    spi_r_datcmd(((x0&0xf0)>>4)|0x10,OLED_CMD);
    spi_r_datcmd((x0&0x0f),OLED_CMD);

}
void OLED_DrawPoint(unsigned char x,unsigned char y,unsigned char t)
{

    unsigned char pos,bx,temp=0;
    if(x>127||y>(64+7))
    {
        temp=2;
        return;
    }
    pos=y/8;
    bx=y%8;
    temp=(1<<(bx));

#ifdef  REVERSE_OLED
    if(t)OLED_GRAM[pos][x]&=~temp;
    else OLED_GRAM[pos][x]|=temp;
#else
    if(t)OLED_GRAM[pos][x]|=temp;
    else OLED_GRAM[pos][x]&=~temp;
#endif
}
void FillArea(uint16_t startx,uint16_t start_y,uint16_t xsize,uint16_t ysize)
{
    uint16_t y,x,pgs,st_page;

    st_page=start_y/8;
    pgs=ysize/8;

    if( (start_y%8)&&((st_page+pgs)<(OLED_Y_SIZE/8)))
    {
        pgs++;
    }

    for(y=0; y<pgs; y++)
    {
        oled_set_pos(startx,st_page+y);
        for(x=0; x<xsize; x++)
        {
            writedata(OLED_GRAM[st_page+y][startx+x]);
        }
    }

}

void oled_display_on_off(bool is_open)
{
    if (is_open)
    {
        spi_r_datcmd(0X8D,OLED_CMD);  //SET DCDC
//    if((time.hour>7)&&(time.hour<19))
        spi_r_datcmd(0X95,OLED_CMD);  //DCDC ON
//    else
//        spi_r_datcmd(0X14,OLED_CMD);  //DCDC ON

        spi_r_datcmd(0XAF,OLED_CMD);  //DISPLAY ON
        led_on_flg=1;
    } else
    {
        spi_r_datcmd(0X8D,OLED_CMD);  //SET DCDC
        spi_r_datcmd(0X10,OLED_CMD);  //DCDC OFF
        spi_r_datcmd(0XAE,OLED_CMD);  //DISPLAY OFF
        led_on_flg=0;
    }
}
/*********************************************************************
* @fn      ss1306SetMirror
*
* @brief   set Mirrored display
*
* @param   en --1 enable Mirror , 0 resume normal display
*
* @return  None
*/
void ss1306SetMirror(uint8_t en)
{
    // the scan direction has been remapped in the oled design, so need reverse the setting here
    if(en == 0)
        en = 1;
    else
        en = 0 ;
    spi_r_datcmd((0xC0|(en<<3)),OLED_CMD);
    spi_r_datcmd((0xA0|en),OLED_CMD);
}

void oled_init(void)
{
    led_on_flg = led_on_flg;
    spi_pin_init();

    OLED_RST_Set();
    delay_ms(100);
    OLED_RST_Clr();
    delay_ms(100);
    OLED_RST_Set();

    spi_r_datcmd(0xAE,OLED_CMD);//--turn off oled panel

    spi_r_datcmd(0x00,OLED_CMD);//---set low column address 00
    spi_r_datcmd(0x10,OLED_CMD);//---set high column address 10

    spi_r_datcmd(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
    spi_r_datcmd(0x81,OLED_CMD);//--set contrast control register 0x81
    spi_r_datcmd(0xff,OLED_CMD); // Set SEG Output Current Brightness CF

    spi_r_datcmd(0xA0,OLED_CMD);//--Set SEG/Column Mapping     0xa0???? 0xa1??
    spi_r_datcmd(0xC0,OLED_CMD);//Set COM/Row Scan Direction   0xc0???? 0xc8??




    spi_r_datcmd(0xA6,OLED_CMD);//--set normal display A6
    spi_r_datcmd(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
    spi_r_datcmd(0x3f,OLED_CMD);//--1/64 duty
    spi_r_datcmd(0xD3,OLED_CMD);//-set display offset   Shift Mapping RAM Counter (0x00~0x3F)0xD3
    spi_r_datcmd(0x00,OLED_CMD);//-not offset

    spi_r_datcmd(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
    spi_r_datcmd(0xf0,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec

    spi_r_datcmd(0xD9,OLED_CMD);//--set pre-charge period
    spi_r_datcmd(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
    spi_r_datcmd(0xDA,OLED_CMD);//--set com pins hardware configuration
    spi_r_datcmd(0x12,OLED_CMD);
    spi_r_datcmd(0xDB,OLED_CMD);//--set vcomh
    spi_r_datcmd(0x30,OLED_CMD);//Set VCOM Deselect Level  40
    spi_r_datcmd(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
    spi_r_datcmd(0x02,OLED_CMD);//
    spi_r_datcmd(0x8D,OLED_CMD);//--set Charge Pump enable/disable
//    spi_r_datcmd(OLED_LIGHT,OLED_CMD);//--set(0x10) disable  0x95  0x14
//    if((hour>7)&&(hour<19))
    spi_r_datcmd(0X95,OLED_CMD);  //DCDC ON
//    else
//        spi_r_datcmd(0X14,OLED_CMD);  //DCDC ON

    spi_r_datcmd(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
    spi_r_datcmd(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
    spi_r_datcmd(0xAF,OLED_CMD);//--turn off oled panel
    oled_clear();
    oled_display_on_off(false);//oled_display_off();
#ifdef TRUN_180_EN
    ss1306SetMirror(0);
#endif
}


void oled_clear(void)
{
    uint8_t i,j;

    Write_Command(0x2E);            // Deactivate Scrolling  Deactivate_Scroll();

    for(i=0; i<8; i++)
        for(j=0; j<128; j++)
#ifdef  REVERSE_OLED
            OLED_GRAM[i][j]=0xff;
#else
            OLED_GRAM[i][j]=0;
#endif
//    FillArea(0,0,128,64);
}

void oled_load_icon(uint16_t startx,uint16_t starty,uint16_t xszie,uint16_t ysize,const uint8_t* icon)
{
    uint16_t l_y,l_num,x0,y0,m,x,data_index=0,bit;
    l_num=ysize/8;
    if(ysize%8)
        l_num++;

    ysize=8*l_num;
    y0=starty;
    x0=startx;
    for(l_y=0; l_y<l_num; l_y++)
    {
        for(x=0; x<xszie; x++)
        {
            m=icon[data_index++];
            for(bit=0; bit<8; bit++)
            {
                OLED_DrawPoint(x0+x,y0+8*l_y+bit,(m&0x01));
                m>>=1;
            }
        }
    }
}
void oled_show_icon(void)
{
    FillArea(0,0,OLED_X_SIZE,OLED_Y_SIZE);
}
//头文件============================================
#ifndef __LCD_H__
#define __LCD_H__

#include "all.h"



#define OLED_CMD  0
#define OLED_DATA 1
#define OLED_MODE 0




#define OLED_X_SIZE                 64
#define OLED_Y_SIZE                 48
#define X_OFFSET                        32
#define Y_OFFSET                    2
#define TRUN_180_EN


#define OLED_CS_Clr()           P0_0=0
#define OLED_CS_Set()           P0_0=1

#define OLED_RST_Clr()          P0_1=0
#define OLED_RST_Set()          P0_1=1

#define OLED_DC_Clr()           P0_2=0
#define OLED_DC_Set()           P0_2=1

#define OLED_SCLK_Clr()         P0_3=0
#define OLED_SCLK_Set()         P0_3=1

#define OLED_SDIN_Clr()         P0_4=0
#define OLED_SDIN_Set()         P0_4=1




void oled_init(void);
void oled_clear(void);
void oled_display_on_off(bool is_open);
void oled_load_icon(unsigned short startx,unsigned short starty,unsigned short xszie,unsigned short ysize,const unsigned char* icon);
void oled_show_icon(void);
void Horizontal_Scroll(unsigned char left, unsigned char Start_Page, unsigned char End_Page, unsigned char Interval);


#endif

//例子======================
#include "all.h" 
#include "delay.h"
#include "lis3dh_driver.h"
#include "lcd.h"

const unsigned char num6_9[]=
{
    //0
    0x00,0xFF,0x01,0x01,0x01,0xFF,0x00,0x01,0x01,0x01,0x01,0x01,
    //1
    0x00,0x00,0x01,0xFF,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,
    //2
    0x00,0xF1,0x11,0x11,0x11,0x1F,0x00,0x01,0x01,0x01,0x01,0x01,
    //3
    0x00,0x11,0x11,0x11,0x11,0xFF,0x00,0x01,0x01,0x01,0x01,0x01,
    //4
    0x00,0x1F,0x10,0x10,0x10,0xFF,0x00,0x00,0x00,0x00,0x00,0x01,
    //5
    0x00,0x1F,0x11,0x11,0x11,0xF1,0x00,0x01,0x01,0x01,0x01,0x01,
    //6
    0x00,0xFF,0x11,0x11,0x11,0xF1,0x00,0x01,0x01,0x01,0x01,0x01,
    //7
    0x00,0x01,0x01,0x01,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x01,
    //8
    0x00,0xFF,0x11,0x11,0x11,0xFF,0x00,0x01,0x01,0x01,0x01,0x01,
    //9
    0x00,0x1F,0x11,0x11,0x11,0xFF,0x00,0x01,0x01,0x01,0x01,0x01,

};
void main(void)
{   

  unsigned char i;
  CLKCONCMD &= ~0x40;               //设置系统时钟源为32MHZ晶振
  while(CLKCONSTA & 0x40);          //等待晶振稳定
  CLKCONCMD &= ~0x47;               //设置系统主时钟频率为32MHZ   

  oled_init();
  oled_display_on_off(true);

  while(1)
  {
    for (i=0;i<10;i++)
    {
      oled_load_icon(0,0,6,9,(unsigned char*)&num6_9[i*12]);
      oled_show_icon();
      delay_ms(1000);
    }
  }

      
}



















123qqq.png

相关文章

  • (基础外围设备)软件SPI

  • (基础外围设备)SPI

    我需要硬件来验证我的SPI驱动是否写对了,我选择Li3dh模块,在转动LI3DH时,三个轴的数据不同。

  • 9.28

    SPI,串行同步全双工外围设备接口。包括MOSI,MISO,SCK,CS四线. 启动根据时钟极性CPOL,0低1高...

  • 2018.1.15

    今天是嵌入式学习第四周周一。 今天主要学习的是SPI接口技术,顾名思义就是串行外围设备接口。SPI区别与I2C的是...

  • 1.0 dubbo源码解析之@SPI

    @SPI是了解dubbo源码的基础,dubbo-spi是通过jdk-spi优化而来的,对于jdk-spi的原理这里...

  • SPI分类

    按业务边界分类 内部业务扩展点SPI 外部业务防腐层SPI 基础设施防腐层SPI 按CQS分类 写入SPI 读取S...

  • 你能谈谈Dubbo SPI扩展原理吗

    原创: 奔头哥 最好有AOP、IOC、MVC框架基础和dubbo使用基础再阅读噢。什么是SPI spi全称Serv...

  • SPI协议详解

    SPI总线系统是一种同步串行外设接口,它可以使MCU与各种外围设备以串行方式进行通信以交换信息。正是由于有了通信方...

  • Java基础系列-SPI你认识吗

    原创文章,转载请标注出处:《Java基础系列-SPI你认识吗》 一、SPI是什么 SPI是相对API而言的。 AP...

  • Android BLE开发入门到进阶(一)

    基础概念设备角色1.中心设备:中心设备相对比价强大,用来连接其他外围设备。例如:手机2.外围设备:一般是非常小的或...

网友评论

      本文标题:(基础外围设备)软件SPI

      本文链接:https://www.haomeiwen.com/subject/hmuewftx.html