123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- #include "lcd/lcd.h"
- #include "lcd/oledfont.h"
- #include "lcd/bmp.h"
- u16 BACK_COLOR; //Background color
- /******************************************************************************
- Function description: LCD serial data write function
- Entry data: serial data to be written to dat
- Return value: None
- ******************************************************************************/
- void LCD_Writ_Bus(u8 dat)
- {
- #if SPI0_CFG == 1
- OLED_CS_Clr();
- while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE));
- spi_i2s_data_transmit(SPI0, dat);
- while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE));
- spi_i2s_data_receive(SPI0);
- OLED_CS_Set();
- #elif SPI0_CFG == 2
- spi_dma_enable(SPI0, SPI_DMA_TRANSMIT);
- #else
- u8 i;
- OLED_CS_Clr();
- for(i=0;i<8;i++)
- {
- OLED_SCLK_Clr();
- if(dat&0x80)
- OLED_SDIN_Set();
- else
- OLED_SDIN_Clr();
- OLED_SCLK_Set();
- dat<<=1;
- }
- OLED_CS_Set();
- #endif
- }
- /******************************************************************************
- Function description: LCD write data
- Entry data: data written by dat
- Return value: None
- ******************************************************************************/
- void LCD_WR_DATA8(u8 dat)
- {
- OLED_DC_Set();//Write data
- LCD_Writ_Bus(dat);
- }
- /******************************************************************************
- Function description: LCD write data
- Entry data: data written by dat
- Return value: None
- ******************************************************************************/
- void LCD_WR_DATA(u16 dat)
- {
- OLED_DC_Set();//Write data
- LCD_Writ_Bus(dat>>8);
- LCD_Writ_Bus(dat);
- }
- /******************************************************************************
- Function description: LCD write command
- Entry data: command written by dat
- Return value: None
- ******************************************************************************/
- void LCD_WR_REG(u8 dat)
- {
- OLED_DC_Clr();//Write command
- LCD_Writ_Bus(dat);
- }
- /******************************************************************************
- Function description: Set start and end addresses
- Entry data: x1, x2 set the start and end addresses of the column
- y1, y2 set the start and end addresses of the line
- Return value: None
- ******************************************************************************/
- void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2)
- {
- if(USE_HORIZONTAL==0)
- {
- LCD_WR_REG(0x2a);//Column address settings
- LCD_WR_DATA(x1+26);
- LCD_WR_DATA(x2+26);
- LCD_WR_REG(0x2b);//Row address setting
- LCD_WR_DATA(y1+1);
- LCD_WR_DATA(y2+1);
- LCD_WR_REG(0x2c);//Memory write
- }
- else if(USE_HORIZONTAL==1)
- {
- LCD_WR_REG(0x2a);//Column address settings
- LCD_WR_DATA(x1+26);
- LCD_WR_DATA(x2+26);
- LCD_WR_REG(0x2b);//Row address setting
- LCD_WR_DATA(y1+1);
- LCD_WR_DATA(y2+1);
- LCD_WR_REG(0x2c);//Memory write
- }
- else if(USE_HORIZONTAL==2)
- {
- LCD_WR_REG(0x2a);//Column address settings
- LCD_WR_DATA(x1+1);
- LCD_WR_DATA(x2+1);
- LCD_WR_REG(0x2b);//Row address setting
- LCD_WR_DATA(y1+26);
- LCD_WR_DATA(y2+26);
- LCD_WR_REG(0x2c);//Memory write
- }
- else
- {
- LCD_WR_REG(0x2a);//Column address settings
- LCD_WR_DATA(x1+1);
- LCD_WR_DATA(x2+1);
- LCD_WR_REG(0x2b);//Row address setting
- LCD_WR_DATA(y1+26);
- LCD_WR_DATA(y2+26);
- LCD_WR_REG(0x2c);//Memory write
- }
- }
- #if SPI0_CFG == 2
- /*!
- \brief configure the DMA peripheral
- \param[in] none
- \param[out] none
- \retval none
- */
- void dma_config(void)
- {
- dma_parameter_struct dma_init_struct;
- /* SPI0 transmit dma config:DMA0,DMA_CH2 */
- dma_deinit(DMA0, DMA_CH2);
- dma_struct_para_init(&dma_init_struct);
- dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI0);
- dma_init_struct.memory_addr = (uint32_t)image;
- dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
- dma_init_struct.priority = DMA_PRIORITY_LOW;
- dma_init_struct.number = FRAME_SIZE;
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init(DMA0, DMA_CH2, &dma_init_struct);
- /* configure DMA mode */
- dma_circulation_disable(DMA0, DMA_CH2);
- dma_memory_to_memory_disable(DMA0, DMA_CH2);
- }
- #endif
- #if SPI0_CFG == 1
- /*!
- \brief configure the SPI peripheral
- \param[in] none
- \param[out] none
- \retval none
- */
- void spi_config(void)
- {
- spi_parameter_struct spi_init_struct;
- /* deinitilize SPI and the parameters */
- OLED_CS_Set();
- spi_struct_para_init(&spi_init_struct);
- /* SPI0 parameter config */
- spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
- spi_init_struct.device_mode = SPI_MASTER;
- spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
- spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
- spi_init_struct.nss = SPI_NSS_SOFT;
- spi_init_struct.prescale = SPI_PSC_8;
- spi_init_struct.endian = SPI_ENDIAN_MSB;
- spi_init(SPI0, &spi_init_struct);
- spi_crc_polynomial_set(SPI0,7);
- spi_enable(SPI0);
- }
- #endif
- /******************************************************************************
- Function description: LCD initialization function
- Entry data: None
- Return value: None
- ******************************************************************************/
- void Lcd_Init(void)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- #if SPI0_CFG == 1
- rcu_periph_clock_enable(RCU_AF);
- rcu_periph_clock_enable(RCU_SPI0);
- /* SPI0 GPIO config: NSS/PA4, SCK/PA5, MOSI/PA7 */
- gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 |GPIO_PIN_6| GPIO_PIN_7);
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
- spi_config();
- #elif SPI0_CFG == 2
- rcu_periph_clock_enable(RCU_DMA0);
- rcu_periph_clock_enable(RCU_SPI0);
- /* SPI0 GPIO config: NSS/PA4, SCK/PA5, MOSI/PA7 */
- gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7);
- /* SPI0 GPIO config: MISO/PA6 */
- gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
- //dma_config();
- dma_channel_enable(DMA0,DMA_CH2);
- #elif SPI0_CFG == 3
- gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
- gpio_bit_reset(GPIOA, GPIO_PIN_5 | GPIO_PIN_7);
- gpio_bit_reset(GPIOB, GPIO_PIN_2);
- #endif
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1);
- gpio_bit_reset(GPIOB, GPIO_PIN_0 | GPIO_PIN_1);
- OLED_RST_Clr();
- delay_1ms(200);
- OLED_RST_Set();
- delay_1ms(20);
- OLED_BLK_Set();
- LCD_WR_REG(0x11); // turn off sleep mode
- delay_1ms(100);
- LCD_WR_REG(0x21); // display inversion mode
- LCD_WR_REG(0xB1); // Set the frame frequency of the full colors normal mode
- // Frame rate=fosc/((RTNA x 2 + 40) x (LINE + FPA + BPA +2))
- // fosc = 850kHz
- LCD_WR_DATA8(0x05); // RTNA
- LCD_WR_DATA8(0x3A); // FPA
- LCD_WR_DATA8(0x3A); // BPA
- LCD_WR_REG(0xB2); // Set the frame frequency of the Idle mode
- // Frame rate=fosc/((RTNB x 2 + 40) x (LINE + FPB + BPB +2))
- // fosc = 850kHz
- LCD_WR_DATA8(0x05); // RTNB
- LCD_WR_DATA8(0x3A); // FPB
- LCD_WR_DATA8(0x3A); // BPB
- LCD_WR_REG(0xB3); // Set the frame frequency of the Partial mode/ full colors
- LCD_WR_DATA8(0x05);
- LCD_WR_DATA8(0x3A);
- LCD_WR_DATA8(0x3A);
- LCD_WR_DATA8(0x05);
- LCD_WR_DATA8(0x3A);
- LCD_WR_DATA8(0x3A);
- LCD_WR_REG(0xB4);
- LCD_WR_DATA8(0x03);
- LCD_WR_REG(0xC0);
- LCD_WR_DATA8(0x62);
- LCD_WR_DATA8(0x02);
- LCD_WR_DATA8(0x04);
- LCD_WR_REG(0xC1);
- LCD_WR_DATA8(0xC0);
- LCD_WR_REG(0xC2);
- LCD_WR_DATA8(0x0D);
- LCD_WR_DATA8(0x00);
- LCD_WR_REG(0xC3);
- LCD_WR_DATA8(0x8D);
- LCD_WR_DATA8(0x6A);
- LCD_WR_REG(0xC4);
- LCD_WR_DATA8(0x8D);
- LCD_WR_DATA8(0xEE);
- LCD_WR_REG(0xC5); /*VCOM*/
- LCD_WR_DATA8(0x0E);
- LCD_WR_REG(0xE0);
- LCD_WR_DATA8(0x10);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x02);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x07);
- LCD_WR_DATA8(0x02);
- LCD_WR_DATA8(0x07);
- LCD_WR_DATA8(0x0A);
- LCD_WR_DATA8(0x12);
- LCD_WR_DATA8(0x27);
- LCD_WR_DATA8(0x37);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x0D);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x10);
- LCD_WR_REG(0xE1);
- LCD_WR_DATA8(0x10);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0x03);
- LCD_WR_DATA8(0x0F);
- LCD_WR_DATA8(0x06);
- LCD_WR_DATA8(0x02);
- LCD_WR_DATA8(0x08);
- LCD_WR_DATA8(0x0A);
- LCD_WR_DATA8(0x13);
- LCD_WR_DATA8(0x26);
- LCD_WR_DATA8(0x36);
- LCD_WR_DATA8(0x00);
- LCD_WR_DATA8(0x0D);
- LCD_WR_DATA8(0x0E);
- LCD_WR_DATA8(0x10);
- /* DLX powersave settings */
- LCD_WR_REG(0XFC); // Enable Gate power save mode
- LCD_WR_DATA8(0XC0); // 0XFC[7:6]=GCV_Enable[1:0]=10'b→ Gate Pump Clock Frequency disable
- // 0XFC[3:2]=CLK_Variable[1:0]=11'b→ Save Power Ability is Large
- LCD_WR_REG(0x3A); // define the format of RGB picture data
- LCD_WR_DATA8(0x05); // 16-bit/pixel
- //
- LCD_WR_REG(0x36);
- if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x08);
- else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC8);
- else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x78);
- else LCD_WR_DATA8(0xA8);
- LCD_WR_REG(0x29); // Display On
- }
- /******************************************************************************
- Function description: LCD clear screen function
- Entry data: None
- Return value: None
- ******************************************************************************/
- void LCD_Clear(u16 Color)
- {
- u16 i,j;
- LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
- for(i=0;i<LCD_W;i++)
- {
- for (j=0;j<LCD_H;j++)
- {
- LCD_WR_DATA(Color);
- }
- }
- }
- #if 0
- /******************************************************************************
- Function description: LCD display Chinese characters
- Entry data: x, y starting coordinates
- index Chinese character number
- size font size
- Return value: None
- ******************************************************************************/
- void LCD_ShowChinese(u16 x,u16 y,u8 index,u8 size,u16 color)
- {
- u8 i,j;
- u8 *temp,size1;
- if(size==16){temp=Hzk16;}//选择字号
- if(size==32){temp=Hzk32;}
- LCD_Address_Set(x,y,x+size-1,y+size-1); //设置一个汉字的区域
- size1=size*size/8;//一个汉字所占的字节
- temp+=index*size1;//写入的起始位置
- for(j=0;j<size1;j++)
- {
- for(i=0;i<8;i++)
- {
- if((*temp&(1<<i))!=0)//从数据的低位开始读
- {
- LCD_WR_DATA(color);//点亮
- }
- else
- {
- LCD_WR_DATA(BACK_COLOR);//不点亮
- }
- }
- temp++;
- }
- }
- #endif
- /******************************************************************************
- Function description: LCD draws point
- Entry data: x, y starting coordinates
- Return value: None
- ******************************************************************************/
- void LCD_DrawPoint(u16 x,u16 y,u16 color)
- {
- LCD_Address_Set(x,y,x,y);//设置光标位置
- LCD_WR_DATA(color);
- }
- /******************************************************************************
- Function description: LCD draws a large dot
- Entry data: x, y starting coordinates
- Return value: None
- ******************************************************************************/
- void LCD_DrawPoint_big(u16 x,u16 y,u16 color)
- {
- LCD_Fill(x-1,y-1,x+1,y+1,color);
- }
- /******************************************************************************
- Function description: fill color in the specified area
- Entry data: xsta, ysta starting coordinates
- xend, yend termination coordinates
- Return value: None
- ******************************************************************************/
- void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
- {
- u16 i,j;
- LCD_Address_Set(xsta,ysta,xend,yend); //设置光标位置
- for(i=ysta;i<=yend;i++)
- {
- for(j=xsta;j<=xend;j++)LCD_WR_DATA(color);//设置光标位置
- }
- }
- /******************************************************************************
- Function description: draw a line
- Entry data: x1, y1 starting coordinates
- x2, y2 terminating coordinates
- Return value: None
- ******************************************************************************/
- void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color)
- {
- u16 t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy,uRow,uCol;
- delta_x=x2-x1; //计算坐标增量
- delta_y=y2-y1;
- uRow=x1;//画线起点坐标
- uCol=y1;
- if(delta_x>0)incx=1; //设置单步方向
- else if (delta_x==0)incx=0;//垂直线
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if (delta_y==0)incy=0;//水平线
- else {incy=-1;delta_y=-delta_x;}
- if(delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴
- else distance=delta_y;
- for(t=0;t<distance+1;t++)
- {
- LCD_DrawPoint(uRow,uCol,color);//画点
- xerr+=delta_x;
- yerr+=delta_y;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- /******************************************************************************
- Function description: draw a rectangle
- Entry data: x1, y1 starting coordinates
- x2, y2 terminating coordinates
- Return value: None
- ******************************************************************************/
- void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color)
- {
- LCD_DrawLine(x1,y1,x2,y1,color);
- LCD_DrawLine(x1,y1,x1,y2,color);
- LCD_DrawLine(x1,y2,x2,y2,color);
- LCD_DrawLine(x2,y1,x2,y2,color);
- }
- /******************************************************************************
- Function description: draw circle
- Entry data: x0, y0 center coordinates
- r radius
- Return value: None
- ******************************************************************************/
- void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color)
- {
- int a,b;
- // int di;
- a=0;b=r;
- while(a<=b)
- {
- LCD_DrawPoint(x0-b,y0-a,color); //3
- LCD_DrawPoint(x0+b,y0-a,color); //0
- LCD_DrawPoint(x0-a,y0+b,color); //1
- LCD_DrawPoint(x0-a,y0-b,color); //2
- LCD_DrawPoint(x0+b,y0+a,color); //4
- LCD_DrawPoint(x0+a,y0-b,color); //5
- LCD_DrawPoint(x0+a,y0+b,color); //6
- LCD_DrawPoint(x0-b,y0+a,color); //7
- a++;
- if((a*a+b*b)>(r*r))//Determine whether the points to be drawn are too far away
- {
- b--;
- }
- }
- }
- /******************************************************************************
- Function description: display characters
- Entry data: x, y starting point coordinates
- num characters to display
- mode 1 superimposed mode 0 non-superimposed mode
- Return value: None
- ******************************************************************************/
- void LCD_ShowChar(u16 x,u16 y,u8 num,u8 mode,u16 color)
- {
- u8 temp;
- u8 pos,t;
- u16 x0=x;
- if(x>LCD_W-16||y>LCD_H-16)return; //Settings window
- num=num-' ';//Get offset value
- LCD_Address_Set(x,y,x+8-1,y+16-1); //Set cursor position
- if(!mode) //Non-overlapping
- {
- for(pos=0;pos<16;pos++)
- {
- temp=asc2_1608[(u16)num*16+pos]; //Call 1608 font
- for(t=0;t<8;t++)
- {
- if(temp&0x01)LCD_WR_DATA(color);
- else LCD_WR_DATA(BACK_COLOR);
- temp>>=1;
- x++;
- }
- x=x0;
- y++;
- }
- }else//overlapping mode
- {
- for(pos=0;pos<16;pos++)
- {
- temp=asc2_1608[(u16)num*16+pos]; //Call 1608 font
- for(t=0;t<8;t++)
- {
- if(temp&0x01)LCD_DrawPoint(x+t,y+pos,color);//Draw a dot
- temp>>=1;
- }
- }
- }
- }
- /******************************************************************************
- Function description: display string
- Entry data: x, y starting point coordinates
- *p string start address
- Return value: None
- ******************************************************************************/
- void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 color)
- {
- while(*p!='\0')
- {
- if(x>LCD_W-16){x=0;y+=16;}
- if(y>LCD_H-16){y=x=0;LCD_Clear(RED);}
- LCD_ShowChar(x,y,*p,0,color);
- x+=8;
- p++;
- }
- }
- /******************************************************************************
- Function description: display numbers
- Entry data: base m, n exponent
- Return value: None
- ******************************************************************************/
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- /******************************************************************************
- Function description: display numbers
- Entry data: x, y starting point coordinates
- num number to display
- len number of digits to display
- Return value: None
- ******************************************************************************/
- void LCD_ShowNum(u16 x,u16 y,u16 num,u8 len,u16 color)
- {
- u8 t,temp;
- u8 enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/mypow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- LCD_ShowChar(x+8*t,y,' ',0,color);
- continue;
- }else enshow=1;
-
- }
- LCD_ShowChar(x+8*t,y,temp+48,0,color);
- }
- }
- /******************************************************************************
- Function description: display decimal
- Entry data: x, y starting point coordinates
- num decimal to display
- len number of digits to display
- Return value: None
- ******************************************************************************/
- void LCD_ShowNum1(u16 x,u16 y,float num,u8 len,u16 color)
- {
- u8 t,temp;
- // u8 enshow=0;
- u16 num1;
- num1=num*100;
- for(t=0;t<len;t++)
- {
- temp=(num1/mypow(10,len-t-1))%10;
- if(t==(len-2))
- {
- LCD_ShowChar(x+8*(len-2),y,'.',0,color);
- t++;
- len+=1;
- }
- LCD_ShowChar(x+8*t,y,temp+48,0,color);
- }
- }
- /******************************************************************************
- Function description: display 160x40 16bit (RGB565) picture
- Entry data: x, y starting point coordinates
- Return value: None
- ******************************************************************************/
- void LCD_ShowPicture(const char *img, u16 x1,u16 y1,u16 x2,u16 y2)
- {
- int i;
- u32 size = 2 * (1 + x2 - x1) * (1 + y2 - y1);
- LCD_Address_Set(x1,y1,x2,y2);
- for(i=0;i<size;i++)
- {
- // LCD_WR_DATA8(image[i*2+1]);
- LCD_WR_DATA8(img[i]);
- }
- }
|