#include "main.h" #include "lcd.h" #include "io.h" #include "pict24x20.h" #include "font16x16r.h" #include "bat.h" #include "bmp.h" void SendDAT(uint8_t byteToSend) { GPIO_SetBits(GPIOB, LCD_RS); // data while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) {} SPI_I2S_SendData(SPI1, byteToSend); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) {} } void SendCMD(uint8_t byteToSend) { GPIO_ResetBits(GPIOB, LCD_RS); // comand while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) {} SPI_I2S_SendData(SPI1, byteToSend); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) {} } void LCDclr(uint16_t X, uint8_t Y, uint16_t L, uint8_t H, uint16_t color) { SendCMD(ILI9341_MAC); SendDAT(Orient_Book); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+L-1)>>8); SendDAT(X+L-1); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(Y); SendDAT(0); SendDAT(Y+H-1); SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); // data for (uint32_t i = 0; i < L*H; i++) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) {} SPI_I2S_SendData(SPI1, color>>8); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) {} SPI_I2S_SendData(SPI1, color); // while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) {} } } void DrawPicture16x1(uint16_t X, uint16_t Y, uint8_t Pic) { SendCMD(ILI9341_MAC); SendDAT(Orient_Book); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+15)>>8); SendDAT(X+15); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(Y); SendDAT(0); SendDAT(Y); // 240 SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); // data for (uint16_t i = 0; i < 16*2; i++) { SPI_I2S_SendData(SPI1, bat[i+32*Pic]); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); } } void DrawBat(uint8_t bat) { uint8_t cb = 5; if (bat > 24) {bat=24;} if (bat < 9) { cb=6; } if (bat < 5) { cb=7; } for (uint16_t m = 2; m < 23; m++) { if (bat > (22-m)) { DrawPicture16x1(0, m, cb); } else { DrawPicture16x1(0, m, 4); } } } void Frame(unsigned short X, unsigned short Y, unsigned short L, unsigned short H, unsigned short color, unsigned short background) { SendCMD(ILI9341_MAC); SendDAT(Orient_Book); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+L-1)>>8); SendDAT(X+L-1); SendCMD(ILI9341_PAGE_ADDR); SendDAT(Y>>8); SendDAT(Y); SendDAT((Y+H-1)>>8); SendDAT(Y+H-1); SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); // data for (uint16_t i = Y; i < Y+H; i++) { for (uint16_t j = X; j < X+L; j++) { unsigned short send_color=background; if (background == RED) send_color = (((12-((i)/2)))<<11); if (background == GREEN) send_color = (((12-((i)/2)))<<6); if (background == BLUE) send_color = (((12-((i)/2)))); if (H>200) { if (j==40||j==80||j==120||j==160||j==200||j==240||j==280) {send_color = GRID;} if (i==Y+36||i==Y+72||i==Y+144||i==Y+180) {send_color = GRID;} if (i==Y+108) {send_color = LGRID;} } if ((i==Y)|(j==X+L-1)|(j==X)|(i==Y+H-1)) { send_color=color; } if (((i==Y+1)&(j==X+1))|((i==Y+1)&(j==X+L-2))|((i==Y+H-2)&(j==X+1))|((i==Y+H-2)&(j==X+L-2))) { send_color=color; } if (((i==Y)&(j==X))|((i==Y)&(j==X+L-1))|((i==Y+H-1)&(j==X))|((i==Y+H-1)&(j==X+L-1))) { send_color=0; } // while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); SPI_I2S_SendData(SPI1, send_color>>8); // while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); SPI_I2S_SendData(SPI1, send_color); } } } void DrawSim16(uint8_t digit, uint16_t X, uint16_t Y, uint16_t color, uint16_t background) { SendCMD(ILI9341_MAC); SendDAT(Orient_Book); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+15)>>8); SendDAT(X+15); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(Y); SendDAT(0); SendDAT(Y+15); SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); // data for (uint16_t j = 0; j < 2*16; j++) { for (uint16_t i = 0; i < 8; i++) { unsigned short send_color=background; if (background == RED) send_color = (((10-((j/2)/2)))<<11); if (background == GREEN) send_color = (((10-((j/2)/2)))<<6); if (background == BLUE) send_color = (((10-((j/2)/2)))); if (((font16x16[digit*2*16+j]<<(i))&0x80)!=0) { send_color = color; } while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); SPI_I2S_SendData(SPI1, send_color>>8); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); SPI_I2S_SendData(SPI1, send_color); } } } void DrawString(const char * s, unsigned short int x, unsigned short int y, unsigned short int color, unsigned short int background) { unsigned short int h=0; for (unsigned short int i = 0; s[i]>0; i++) { if (s[i]==46) { i++; DrawSim16(s[i]-32, x+h, y, color, background); Point(x+h-1, y+13, color); h=h+14; } else { DrawSim16(s[i]-32, x+h, y, color, background); h=h+14; } } } void DrawSim24x20(uint16_t X, uint16_t Y, uint16_t color, uint8_t digit) { SendCMD(ILI9341_MAC); SendDAT(0xE8); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+23)>>8); SendDAT(X+23); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(Y); SendDAT(0); SendDAT(Y+19); SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); // data for (uint16_t j = 0; j < 3*20; j++) { for (uint16_t i = 0; i < 8; i++) { if (((pict24x20[digit*3*20+j]<<(i))&0x80)==0) { SendDAT(0); SendDAT(0); // SendDAT(((12-((j/3)+Y)/3))); } else { SendDAT(color>>8); SendDAT(color); } } } } void Point(uint16_t X, uint16_t Y, uint16_t color) // 3x3 { SendCMD(ILI9341_MAC); SendDAT(Orient_Book); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(X>>8); SendDAT(X); SendDAT((X+2)>>8); SendDAT(X+2); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(Y); SendDAT(0); SendDAT(Y+2); SendCMD(ILI9341_GRAM); GPIO_SetBits(GPIOB, LCD_RS); for (uint16_t i = 0; i < 3*3; i++) { SendDAT(color>>8); SendDAT(color); } } void LCDinit(void) { GPIO_ResetBits(GPIOB, LCD_RESET); Delay(10000); GPIO_SetBits(GPIOB, LCD_RESET); SendCMD(ILI9341_RESET); Delay(10000); SendCMD(ILI9341_POWERA); SendDAT(0x39); SendDAT(0x2C); SendDAT(0x00); SendDAT(0x34); SendDAT(0x02); SendCMD(ILI9341_POWERB); SendDAT(0x00); SendDAT(0xC1); SendDAT(0x30); SendCMD(ILI9341_DTCA); SendDAT(0x85); SendDAT(0x00); SendDAT(0x78); SendCMD(ILI9341_DTCB); SendDAT(0x00); SendDAT(0x00); SendCMD(ILI9341_POWER_SEQ); SendDAT(0x64); SendDAT(0x03); SendDAT(0x12); SendDAT(0x81); SendCMD(ILI9341_PRC); SendDAT(0x20); SendCMD(ILI9341_POWER1); SendDAT(0x23); SendCMD(ILI9341_POWER2); SendDAT(0x10); SendCMD(ILI9341_VCOM1); SendDAT(0x3E); SendDAT(0x28); SendCMD(ILI9341_VCOM2); SendDAT(0x86); SendCMD(ILI9341_MAC); SendDAT(Orient_Book); // SendDAT(0x48); SendCMD(ILI9341_PIXEL_FORMAT); SendDAT(0x55); SendCMD(ILI9341_FRC); SendDAT(0x00); SendDAT(0x18); SendCMD(ILI9341_DFC); SendDAT(0x08); SendDAT(0x82); SendDAT(0x27); SendCMD(ILI9341_3GAMMA_EN); SendDAT(0x00); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(0x00); SendDAT(0x00); SendDAT(0x01); // SendDAT(0x00); SendDAT(0x3F); // SendDAT(0xEF); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0x00); SendDAT(0x00); SendDAT(0x00); // SendDAT(0x01); SendDAT(0xEF); // SendDAT(0x3F); SendCMD(ILI9341_GAMMA); SendDAT(0x01); SendCMD(ILI9341_PGAMMA); SendDAT(0x0F); SendDAT(0x31); SendDAT(0x2B); SendDAT(0x0C); SendDAT(0x0E); SendDAT(0x08); SendDAT(0x4E); SendDAT(0xF1); SendDAT(0x37); SendDAT(0x07); SendDAT(0x10); SendDAT(0x03); SendDAT(0x0E); SendDAT(0x09); SendDAT(0x00); SendCMD(ILI9341_NGAMMA); SendDAT(0x00); SendDAT(0x0E); SendDAT(0x14); SendDAT(0x03); SendDAT(0x11); SendDAT(0x07); SendDAT(0x31); SendDAT(0xC1); SendDAT(0x48); SendDAT(0x08); SendDAT(0x0F); SendDAT(0x0C); SendDAT(0x31); SendDAT(0x36); SendDAT(0x0F); SendCMD(ILI9341_SLEEP_OUT); Delay(100000); SendCMD(ILI9341_DISPLAY_ON); } uint8_t ReadSPI(void) { while (!(SPI1->SR & SPI_SR_TXE)) {} *((__IO uint8_t *)&SPI1->DR) = 0x00; while (!(SPI1->SR & SPI_SR_RXNE)) {} return (uint8_t)SPI1->DR; } uint8_t ReadSPI2(void) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, 0x00); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(SPI1); } void SendBMP(void) { SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); for (uint16_t i = 0; i < 54; i++) { while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} USART_SendData(USART2, bmp[i]); } SendCMD(ILI9341_MAC); SendDAT(0xA8); SendCMD(ILI9341_COLUMN_ADDR); SendDAT(0); SendDAT(0); SendDAT(1); SendDAT(0x3F); SendCMD(ILI9341_PAGE_ADDR); SendDAT(0); SendDAT(0); SendDAT(0); SendDAT(0xEF); SendCMD(0x2E); GPIO_SetBits(GPIOB, LCD_RS); // data uint8_t spi_res = ReadSPI(); for (uint32_t i = 0; i < 240*320; i++) { uint8_t spi_res0 = ReadSPI(); uint8_t spi_res1 = ReadSPI(); uint8_t spi_res2 = ReadSPI(); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} USART_SendData(USART2, spi_res2); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} USART_SendData(USART2, spi_res1); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} USART_SendData(USART2, spi_res0); } SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); }