28035spi驱动OLED

 

犯了几个小错误后,终于成功刷屏了。下面说下具体步骤。

首先说下OLED的类型。128*160,ST7735,采用的通信方式为4线串行,即CS,D/CX,SDA,SCL。

1、配置Gpio

    使用Gpio10-CS,Gpio11-D/CX,Gpio16-SDA,Gpio18-SCL,代码如下:

void Gpio_init()

{

EALLOW;
  // Enable SPI-A on GPIO16 、 GPIO18
   GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0;   // Enable pullup on GPIO16
      GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0;   // Enable pullup on GPIO18

   GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // asynch input
      GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // asynch input
   
   GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1;  // GPIO16 = SPISIMOA
      GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1;  // GPIO18 = SPICLKA

 

//CS

GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0;   // Enable pullup on GPIO10
   GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0;  // GPIO10 = GPIO10
   GpioCtrlRegs.GPADIR.bit.GPIO10 = 1;   // GPIO10 = output
  
//Enable GPIO11,register choose ,low command,high data
   GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;   // Enable pullup on GPIO11
   GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0;  // GPIO11 = GPIO11
   GpioCtrlRegs.GPADIR.bit.GPIO11 = 1;   // GPIO10 = output

EDIS;

}

 

2、Initialize SPIA

由于使用的是标准spi,所以SPIFFTX,SPIFFRX和SPIFFCT可以不管。对于spi来说,是要配置以下参数

a、master or slaver

b、上升沿或下降沿发送数据

c、相位和极性

d、发送使能,中断使能

e、baud rate

 

这里我选用的是master模式,Rising edge with delay,发送使能,中断禁止,baud rate=lspclk/4,代码如下:

void spi_init()
{   
 SpiaRegs.SPICCR.all =0x0007;              // Reset on, rising edge, 16-bit char bits 
 SpiaRegs.SPICTL.all =0x000e;           // Enable master mode, normal phase,
                                                 // enable talk, and SPI int disabled.
 SpiaRegs.SPIBRR =0x0001;         
    SpiaRegs.SPICCR.all =0x0087;           // Relinquish SPI from Reset  
    SpiaRegs.SPIPRI.bit.FREE = 1;                // Set so breakpoints don't disturb xmission
}

3、接下来是LedInit

这部分代码较长,主要是复位,刷新率,以及Power和Gamma的一些设置:

void Lcd_init()
{
 LcdCommandWrite(0x11);//Sleep out
 DELAY_US(100000);
 
 //ST7735R Frame Rate
 LcdCommandWrite(0xB1);
 LcdDataWrite(0x01);
 LcdDataWrite(0x2C);
 LcdDataWrite(0x2D);
 
 LcdCommandWrite(0xB2);
 LcdDataWrite(0x01);
 LcdDataWrite(0x2C);
 LcdDataWrite(0x2D);
 
 LcdCommandWrite(0xB3);
 LcdDataWrite(0x01);
 LcdDataWrite(0x2C);
 LcdDataWrite(0x2D); 
 
 LcdDataWrite(0x01);
 LcdDataWrite(0x2C);
 LcdDataWrite(0x2D);
 //------------------End ST7735R Frame Rate----------------//
 
 LcdCommandWrite(0xB4);//Column inversion
 LcdDataWrite(0x07);
 
 
 //-------------------ST7735R Power Sequence-----------------//
 LcdCommandWrite(0xC0);
 LcdDataWrite(0xA2);
 LcdDataWrite(0x02);
 LcdDataWrite(0x84);
 
 LcdCommandWrite(0xC1);
 LcdDataWrite(0xC5);
 
 LcdCommandWrite(0xC2);
 LcdDataWrite(0x0A);
 LcdDataWrite(0x00);
 
 LcdCommandWrite(0xC3);
 LcdDataWrite(0x8A);
 LcdDataWrite(0x2A);
 
 LcdCommandWrite(0xC4);
 LcdDataWrite(0x8A);
 LcdDataWrite(0xEE);
 //------------End ST7735R Power Sequence-------------------//
 
 LcdCommandWrite(0xC5);//VCOM
 LcdDataWrite(0x0E);
 LcdCommandWrite(0x36);//MX, MY, RGB Mode
 LcdDataWrite(0xC8);
 
 //-----------------ST7735R Gamma Sequence------------------//
 LcdCommandWrite(0xE0);
 LcdDataWrite(0x02);
 LcdDataWrite(0x1C);
 LcdDataWrite(0x07);
 LcdDataWrite(0x12);
 LcdDataWrite(0x37);
 LcdDataWrite(0x32);
 LcdDataWrite(0x29);
 LcdDataWrite(0x2D);
 LcdDataWrite(0x29);
 LcdDataWrite(0x25);
 LcdDataWrite(0x2B);
 LcdDataWrite(0x39);
 LcdDataWrite(0x00);
 LcdDataWrite(0x01);
 LcdDataWrite(0x03);
 LcdDataWrite(0x10);
 
 LcdCommandWrite(0xE1);
 LcdDataWrite(0x03);
 LcdDataWrite(0x1D);
 LcdDataWrite(0x07);
 LcdDataWrite(0x06);
 LcdDataWrite(0x2E);
 LcdDataWrite(0x2C);
 LcdDataWrite(0x29);
 LcdDataWrite(0x2D);
 LcdDataWrite(0x2E);
 LcdDataWrite(0x2E);
 LcdDataWrite(0x37);
 LcdDataWrite(0x3F);
 LcdDataWrite(0x00);
 LcdDataWrite(0x00);
 LcdDataWrite(0x02);
 LcdDataWrite(0x10);
 
 LcdCommandWrite(0x2A);
 LcdDataWrite(0x00);
 LcdDataWrite(0x02);
 LcdDataWrite(0x00);
 LcdDataWrite(0x81);
 
 LcdCommandWrite(0x2B);
 LcdDataWrite(0x00);
 LcdDataWrite(0x01);
 LcdDataWrite(0x00);
 LcdDataWrite(0xA0);
 //----------------------End ST7735R Gamma Sequence------------//
 
 LcdCommandWrite(0x3A);
 LcdDataWrite(0x05);
 //write_command(0x3A);//65k mode
 //write_data(0x05);
 LcdCommandWrite(0x29);
}

4、这部分主要是操作屏幕的4个函数:

void spi_xmit(uchar a);

void LcdDataWrite(uchar a);

void LcdCommandWrite(uchar a);

void dsp_single_colour(uchar a, uchar b);

 

void spi_xmit(uchar a)
{
 
    SpiaRegs.SPITXBUF=(a<<8);
    while(SpiaRegs.SPISTS.bit.INT_FLAG !=1) { }
    dummy = SpiaRegs.SPIRXBUF;
    //DELAY_US(1);
}

 

void LcdDataWrite(uchar a)
{
 GpioDataRegs.GPACLEAR.bit.GPIO10 = 1;   // Load output latch/Lcd cs enable
 GpioDataRegs.GPASET.bit.GPIO11 = 1;   // Load output latch/register choose data
 spi_xmit(a); 
 GpioDataRegs.GPASET.bit.GPIO10 = 1;   // Load output latch/Lcd cs disable 
}

 

void LcdCommandWrite(uchar a)
{
 GpioDataRegs.GPACLEAR.bit.GPIO10 = 1;   // Load output latch/Lcd cs enable
 GpioDataRegs.GPACLEAR.bit.GPIO11 = 1;   // Load output latch/register choose command
 spi_xmit(a);
 GpioDataRegs.GPASET.bit.GPIO10 = 1;   // Load output latch/Lcd cs disable
}

 

void dsp_single_colour(uchar a, uchar b)
{
 uchar i,j;
 //RamAdressSet();
 for (i=0;i<160;i++)
    for (j=0;j<128;j++)
    {
        LcdDataWrite(a);
        LcdDataWrite(b);
    }
}

5、最后就是main()了:

 

int main()

{

    ……

    void Gpio_init();

    void spi_init();

    void Lcd_init();

    LcdCommandWrite(0x2c);

    for(;;)

    {

    dsp_single_colour(0xff,0xff);//white
    DELAY_US(500000); 
     
    dsp_single_colour(0x00,0x00);//black
    DELAY_US(500000); 
     
    dsp_single_colour(0xf8,0x00);//red
    DELAY_US(500000); 
     
    dsp_single_colour(0x07,0xe0);//green
    DELAY_US(500000); 
     
    dsp_single_colour(0x00,0x1f);//blue
    DELAY_US(500000);

    }

    return 0;

}

你可能感兴趣的:(DSP)