|
100家元
买的lcd 12864 网上下载的驱动,显示是这样的,不知道哪里错了,哪位大佬能指点下
- /******************************************************************************
- **
- * \file st7567.h
- * \author IOsetting | iosetting@outlook.com
- * \date
- * \brief Library of ST7567 LCD on W806
- * \note
- * \version v0.1
- * \ingroup demo
- * \remarks test-board: HLK-W806-KIT-V1.0
- *
- ******************************************************************************/
- #ifndef __ST7567_H_
- #define __ST7567_H_
- #include "stm32f4xx_hal.h"
- #include "ascii_fonts.h"
- #include "main.h"
- #define ST7567_BUF_SIZE 1024
- #define ST7567_HARDWARE_SPI 1
- // CS: B4, B14
- #define ST7567_CS_PORT GPIOB
- #define ST7567_CS_PIN ST7567_CS_Pin
- // SCK: B1, B2, B15, B24
- #define ST7567_SCK_PORT GPIOB
- #define ST7567_SCK_PIN GPIO_PIN_10
- // MOSI: B5, B17, B26, PA7
- #define ST7567_MOSI_PORT GPIOB
- #define ST7567_MOSI_PIN GPIO_PIN_15
- // MISO: B0, B3, B16, B25
- #define ST7567_RES_PORT GPIOB
- #define ST7567_RES_PIN ST7567_RST_Pin
- #define ST7567_DC_PORT GPIOB
- #define ST7567_DC_PIN ST7567_DC_Pin
- #define ST7567_BL_PORT GPIOC
- #define ST7567_BL_PIN LED_Pin
- #if ST7567_HARDWARE_SPI
- extern SPI_HandleTypeDef hspi;
- #define ST7567_CS_LOW HAL_GPIO_WritePin(GPIOB, ST7567_CS_Pin, GPIO_PIN_RESET)
- #define ST7567_CS_HIGH HAL_GPIO_WritePin(GPIOB, ST7567_CS_Pin, GPIO_PIN_SET)
- #else
- #define ST7567_SCK_LOW HAL_GPIO_WritePin(ST7567_SCK_PORT, ST7567_SCK_PIN, GPIO_PIN_RESET)
- #define ST7567_SCK_HIGH HAL_GPIO_WritePin(ST7567_SCK_PORT, ST7567_SCK_PIN, GPIO_PIN_SET)
- #define ST7567_CS_LOW HAL_GPIO_WritePin(ST7567_CS_PORT, ST7567_CS_PIN, GPIO_PIN_RESET)
- #define ST7567_CS_HIGH HAL_GPIO_WritePin(ST7567_CS_PORT, ST7567_CS_PIN, GPIO_PIN_SET)
- #define ST7567_MOSI_LOW HAL_GPIO_WritePin(ST7567_MOSI_PORT, ST7567_MOSI_PIN, GPIO_PIN_RESET)
- #define ST7567_MOSI_HIGH HAL_GPIO_WritePin(ST7567_MOSI_PORT, ST7567_MOSI_PIN, GPIO_PIN_SET)
- #endif
- #define ST7567_BL_LOW HAL_GPIO_WritePin(ST7567_BL_PORT, ST7567_BL_PIN, GPIO_PIN_RESET)
- #define ST7567_BL_HIGH HAL_GPIO_WritePin(ST7567_BL_PORT, ST7567_BL_PIN, GPIO_PIN_SET)
- #define ST7567_DC_LOW HAL_GPIO_WritePin(ST7567_DC_PORT, ST7567_DC_PIN, GPIO_PIN_RESET)
- #define ST7567_DC_HIGH HAL_GPIO_WritePin(ST7567_DC_PORT, ST7567_DC_PIN, GPIO_PIN_SET)
- #define ST7567_RESET_LOW HAL_GPIO_WritePin(ST7567_RES_PORT, ST7567_RES_PIN, GPIO_PIN_RESET)
- #define ST7567_RESET_HIGH HAL_GPIO_WritePin(ST7567_RES_PORT, ST7567_RES_PIN, GPIO_PIN_SET)
- // X width
- #define ST7567_WIDTH 128
- // Y height
- #define ST7567_HEIGHT 64
- // Additional bytes in each row
- #define ST7567_SEG_EXPAND 4
- // X orientation
- #define ST7567_X_ORIENT ST7567_SEG_DIRECTION_REVERSE
- // Y orientation
- #define ST7567_Y_ORIENT ST7567_COM_DIRECTION_NORMAL
- /* ST7567 commands definitions */
- #define ST7567_DISPLAY_OFF 0xAE /* 0xae: Display OFF (sleep mode) */
- #define ST7567_DISPLAY_ON 0xAF /* 0xaf: Display ON in normal mode */
- #define ST7567_SET_START_LINE 0x40 /* 0x40-7f: Set display start line */
- #define ST7567_SET_START_LINE_MASK 0x3f
- #define ST7567_SET_PAGE_ADDRESS 0xB0 /* 0xb0-b7: Set page start address */
- #define ST7567_SET_PAGE_ADDRESS_MASK 0x07
- #define ST7567_SET_COLUMN_ADDRESS_MSB 0x10 /* 0x10-0x1f: Set higher column address */
- #define ST7567_SET_COLUMN_ADDRESS_MSB_MASK 0x0f
- #define ST7567_SET_COLUMN_ADDRESS_LSB 0x00 /* 0x00-0x0f: Set lower column address */
- #define ST7567_SET_COLUMN_ADDRESS_LSB_MASK 0x0F
- /**
- * SEG: 0 - 131
- */
- #define ST7567_SEG_DIRECTION_NORMAL 0xA0 /* 0xa0: Column address 0 is mapped to SEG0 */
- #define ST7567_SEG_DIRECTION_REVERSE 0xA1 /* 0xa1: Column address 128 is mapped to SEG0 */
- /**
- * COM: 0 - 63
- */
- #define ST7567_COM_DIRECTION_NORMAL 0xC0 /* 0xc0: Set COM output direction, normal mode */
- #define ST7567_COM_DIRECTION_REVERSE 0xC8 /* 0xc8: Set COM output direction, reverse mode */
- #define ST7567_INVERSE_DISPLAY_OFF 0xA6 /* 0xa6: Normal display */
- #define ST7567_INVERSE_DISPLAY_ON 0xA7 /* 0xa7: Inverse display */
- #define ST7567_ALL_PIXEL_ON 0xA5 /* 0xa5: Entire display ON */
- #define ST7567_ALL_PIXEL_NORMAL 0xA4 /* 0xa4: Resume to RAM content display */
- #define ST7567_BIAS_1_9 0xA2 /* 0xa2: Select BIAS setting 1/9 */
- #define ST7567_BIAS_1_7 0xA3 /* 0xa3: Select BIAS setting 1/7 */
- #define ST7567_READ_MODIFY_WRITE_START 0xE0 /* 0xe0: Enter the Read Modify Write mode */
- #define ST7567_READ_MODIFY_WRITE_END 0xEE /* 0xee: Leave the Read Modify Write mode */
- #define ST7567_RESET 0xE2 /* 0xe2: Software RESET */
- /**
- * This instruction controls the built-in power circuits.
- * Typically, these 3 flags are turned ON at the same time.
- */
- #define ST7567_POWER_CONTROL 0x28
- #define ST7567_POWER_CONTROL_VF 0x01
- #define ST7567_POWER_CONTROL_VR 0x02
- #define ST7567_POWER_CONTROL_VB 0x04
- /**
- * The operation voltage (V0) calculation formula is shown below:
- * (RR comes from Regulation Ratio, EV comes from EV[5:0])
- * V0 = RR X [ 1 – (63 – EV) / 162 ] X 2.1, or
- * V0 = RR X [ ( 99 + EV ) / 162 ] X 2.1
- */
- #define ST7567_REGULATION_RATIO 0x20
- #define ST7567_REGULATION_RATIO_3_0 0x00
- #define ST7567_REGULATION_RATIO_3_5 0x01
- #define ST7567_REGULATION_RATIO_4_0 0x02
- #define ST7567_REGULATION_RATIO_4_5 0x03
- #define ST7567_REGULATION_RATIO_5_0 0x04 /* Default */
- #define ST7567_REGULATION_RATIO_5_5 0x05
- #define ST7567_REGULATION_RATIO_6_0 0x06
- #define ST7567_REGULATION_RATIO_6_5 0x07
- /**
- * This is double byte instruction. The first byte set ST7567 into EV
- * adjust mode and the following instruction will change the EV setting.
- * That means these 2 bytes must be used together. They control the electronic
- * volume to adjust a suitable V0 voltage for the LCD.
- */
- #define ST7567_SET_EV 0x81
- #define ST7567_SET_EV_MASK 0x3F
- #define ST7567_SET_BOOSTER 0xF8 /* Set booster level */
- #define ST7567_SET_BOOSTER_4X 0x00
- #define ST7567_SET_BOOSTER_5X 0x01
- #define ST7567_NOP 0xE3
- #define ST7567_TEST 0xFE
- #ifndef ST7567_TIMEOUT
- #define ST7567_TIMEOUT 20000
- #endif
- /** Background color */
- #define ST7567_COLOR_BACK 0x00
- /** Front color */
- #define ST7567_COLOR_FRONT 0x01
- /**
- * [url=home.php?mod=space&uid=650075]@brief[/url] Initializes ST7567 LCD
- * @param None
- * @retval None
- */
- void ST7567_Init(void);
- /**
- * @brief Hardware reset ST7567 LCD
- * @param None
- * @retval None
- */
- void ST7567_Reset(void);
- /**
- * @brief Turn ST7567 LCD backlight on
- * @param None
- * @retval None
- */
- void ST7567_BackLight_On(void);
- /**
- * @brief Turn ST7567 LCD backlight off
- * @param None
- * @retval None
- */
- void ST7567_BackLight_Off(void);
- /**
- * @brief Turn ST7567 LCD backlight off
- * @param val: value between 0x00 ~ 0x3F
- * @retval None
- */
- void ST7567_SetContrast(uint8_t val);
- /**
- * @brief Updates buffer from internal RAM to LCD
- * @note This function must be called each time you do some changes to LCD, to update buffer from RAM to LCD
- * @param None
- * @retval None
- */
- void ST7567_UpdateScreen(void);
- /**
- * @brief Toggles pixels invertion inside internal RAM
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param None
- * @retval None
- */
- void ST7567_ToggleInvert(void);
- /**
- * @brief Fills entire LCD with desired color
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param Color: Color to be used for screen fill. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_Fill(uint8_t Color);
- /**
- * @brief Draws pixel at desired location
- * @note @ref ST7567_UpdateScreen() must called after that in order to see updated LCD screen
- * @param x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
- * @param y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
- * @param color: Color to be used for screen fill. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color);
- /**
- * @brief Sets cursor pointer to desired location for strings
- * @param x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
- * @param y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
- * @retval None
- */
- void ST7567_GotoXY(uint16_t x, uint16_t y);
- /**
- * @brief Puts character to internal RAM
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param ch: Character to be written
- * @param *Font: Pointer to @ref FontDef_t structure with used font
- * @param color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval Character written
- */
- char ST7567_Putc(char ch, FontDef_t* Font, uint8_t color);
- /**
- * @brief Puts string to internal RAM
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param *str: String to be written
- * @param *Font: Pointer to @ref FontDef_t structure with used font
- * @param color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval Zero on success or character value when function failed
- */
- char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color);
- /**
- * @brief Draws line on LCD
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x0: Line X start point. Valid input is 0 to ST7567_WIDTH - 1
- * @param y0: Line Y start point. Valid input is 0 to ST7567_HEIGHT - 1
- * @param x1: Line X end point. Valid input is 0 to ST7567_WIDTH - 1
- * @param y1: Line Y end point. Valid input is 0 to ST7567_HEIGHT - 1
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c);
- /**
- * @brief Draws rectangle on LCD
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x: Top left X start point. Valid input is 0 to ST7567_WIDTH - 1
- * @param y: Top left Y start point. Valid input is 0 to ST7567_HEIGHT - 1
- * @param w: Rectangle width in units of pixels
- * @param h: Rectangle height in units of pixels
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c);
- /**
- * @brief Draws filled rectangle on LCD
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x: Top left X start point. Valid input is 0 to ST7567_WIDTH - 1
- * @param y: Top left Y start point. Valid input is 0 to ST7567_HEIGHT - 1
- * @param w: Rectangle width in units of pixels
- * @param h: Rectangle height in units of pixels
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c);
- /**
- * @brief Draws triangle on LCD
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x1: First coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
- * @param y1: First coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
- * @param x2: Second coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
- * @param y2: Second coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
- * @param x3: Third coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
- * @param y3: Third coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color);
- /**
- * @brief Draws circle to STM buffer
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x: X location for center of circle. Valid input is 0 to ST7567_WIDTH - 1
- * @param y: Y location for center of circle. Valid input is 0 to ST7567_HEIGHT - 1
- * @param r: Circle radius in units of pixels
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c);
- /**
- * @brief Draws filled circle to STM buffer
- * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
- * @param x: X location for center of circle. Valid input is 0 to ST7567_WIDTH - 1
- * @param y: Y location for center of circle. Valid input is 0 to ST7567_HEIGHT - 1
- * @param r: Circle radius in units of pixels
- * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
- * @retval None
- */
- void ST7567_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c);
- /**
- * @brief Writes multi bytes to slave
- * @param *I2Cx: I2C used
- * @param address: 7 bit slave address, left aligned, bits 7:1 are used, LSB bit is not used
- * @param reg: register to write to
- * @param *data: pointer to data array to write it to slave
- * @param count: how many bytes will be written
- * @retval None
- */
- void ST7567_Image(uint8_t *img, uint8_t frame, uint8_t x, uint8_t y);
- /**
- * @brief Writes single byte command to slave
- * @param command: command to be written
- * @retval None
- */
- void ST7567_WriteCommand(uint8_t command);
- /**
- * @brief Writes single byte data to slave
- * @param data: data to be written
- * @retval None
- */<div class="blockcode"><blockquote>/******************************************************************************
- **
- * \file st7567.c
- * \author IOsetting | iosetting@outlook.com
- * \date
- * \brief Library of ST7567 LCD on W806
- * \note
- * \version v0.1
- * \ingroup demo
- * \remarks test-board: HLK-W806-KIT-V1.0
- *
- * B10 -> RES, RESET
- * B11 -> DC, A0
- * B14 -> CSB, Chip Select
- * B15 -> SCK, SCL, CLK, Clock
- * B16 -> LED-A, Backlight
- * B17 -> MOSI, SDA
- * GND -> GND
- * 3.3V -> VCC
- *
- * ST7567_HARDWARE_SPI - 0: Software SPI, 1: Hardware SPI
- *
- ******************************************************************************/
- #include "st7567.h"
- /* Absolute value */
- #define ABS(x) ((x) > 0 ? (x) : -(x))
- #if ST7567_X_ORIENT == ST7567_SEG_DIRECTION_REVERSE
- #define ST7567_X_OFFSET ST7567_SEG_EXPAND
- #else
- #define ST7567_X_OFFSET 0
- #endif
- /**
- * In datasheet, it says "the column address is increased (+1) after each display
- * data access (read/write). This allows MPU accessing DDRAM content continuously.
- * This feature stops at the end of each page (Column Address “83h”) because the
- * Column Address and Page Address circuits are independent. For example, both Page
- * Address and Column Address should be assigned for changing the DDRAM pointer
- * from (Page-0, Column-83h) to (Page-1, Column-0)."
- * In actual test the Page Address will grow automatically.
- */
- /* ST7567 data buffer */
- static uint8_t ST7567_Buffer_all[(ST7567_WIDTH + ST7567_SEG_EXPAND) * ST7567_HEIGHT / 8];
- /* Private ST7567 structure */
- typedef struct {
- uint16_t CurrentX;
- uint16_t CurrentY;
- uint8_t Inverted;
- uint8_t Initialized;
- } ST7567_t;
- /* Private variable */
- static ST7567_t ST7567;
- extern SPI_HandleTypeDef hspi2;
- static void ST7567_TransmitByte(uint8_t dat)
- {
- #if ST7567_HARDWARE_SPI
- ST7567_CS_LOW;
- HAL_SPI_Transmit(&hspi2, &dat, 1, ST7567_TIMEOUT);
- ST7567_CS_HIGH;
- #else
- uint8_t i;
- ST7567_CS_LOW;
- for (i = 0; i < 8; i++)
- {
- ST7567_SCK_LOW;
- if (dat & 0x80)
- {
- ST7567_MOSI_HIGH;
- }
- else
- {
- ST7567_MOSI_LOW;
- }
- ST7567_SCK_HIGH;
- dat <<= 1;
- }
- ST7567_CS_HIGH;
- #endif
- }
- static void ST7567_Transmit(uint8_t *pData, uint32_t Size, uint32_t Timeout)
- {
- #if ST7567_HARDWARE_SPI
- ST7567_CS_LOW;
- HAL_SPI_Transmit(&hspi2, pData, Size, Timeout);
- ST7567_CS_HIGH;
- #else
- while (Size-- > 0)
- {
- ST7567_TransmitByte(*(pData++));
- }
- #endif
- }
- void ST7567_WriteCommand(uint8_t command)
- {
- ST7567_DC_LOW;
- ST7567_TransmitByte(command);
- ST7567_DC_HIGH;
- }
- void ST7567_WriteData(uint8_t data)
- {
- ST7567_TransmitByte(data);
- }
- void ST7567_Init(void)
- {
- ST7567_Reset();
- ST7567_BackLight_On();
- ST7567_WriteCommand(ST7567_RESET);
- ST7567_WriteCommand(ST7567_POWER_CONTROL
- |ST7567_POWER_CONTROL_VB
- |ST7567_POWER_CONTROL_VR
- |ST7567_POWER_CONTROL_VF);
- ST7567_WriteCommand(ST7567_SET_EV);
- ST7567_WriteCommand(ST7567_SET_EV_MASK & 0x20);
- ST7567_WriteCommand(ST7567_BIAS_1_9);
- ST7567_WriteCommand(ST7567_X_ORIENT);
- ST7567_WriteCommand(ST7567_Y_ORIENT);
- ST7567_WriteCommand(ST7567_REGULATION_RATIO | ST7567_REGULATION_RATIO_5_0);
- ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_OFF);
- ST7567_WriteCommand(ST7567_DISPLAY_ON);
- ST7567_WriteCommand(ST7567_ALL_PIXEL_NORMAL);
- ST7567_WriteCommand(ST7567_SET_START_LINE | (0x00 & ST7567_SET_START_LINE_MASK));
- ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS | (0x00 & ST7567_SET_PAGE_ADDRESS_MASK));
- ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB);
- ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB);
- }
- void ST7567_Reset(void)
- {
- ST7567_RESET_LOW;
- HAL_Delay(5);
- ST7567_RESET_HIGH;
- }
- void ST7567_BackLight_On(void)
- {
- ST7567_BL_HIGH;
- }
- void ST7567_BackLight_Off(void)
- {
- ST7567_BL_LOW;
- }
- void ST7567_SetContrast(uint8_t val)
- {
- ST7567_WriteCommand(ST7567_SET_EV);
- ST7567_WriteCommand(ST7567_SET_EV_MASK & val);
- }
- void ST7567_UpdateScreen(void)
- {
- ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS | (0x00 & ST7567_SET_PAGE_ADDRESS_MASK));
- ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB);
- ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB);
- ST7567_Transmit(ST7567_Buffer_all, sizeof(ST7567_Buffer_all), ST7567_TIMEOUT);
- }
- void ST7567_ToggleInvert(void)
- {
- /* Toggle invert */
- ST7567.Inverted = !ST7567.Inverted;
- if (ST7567.Inverted)
- {
- ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_ON);
- }
- else
- {
- ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_OFF);
- }
- }
- void ST7567_Fill(uint8_t color)
- {
- /* Set memory */
- memset((void*)ST7567_Buffer_all, (color == ST7567_COLOR_BACK) ? 0x00 : 0xFF, sizeof(ST7567_Buffer_all));
- }
- void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
- {
- if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
- {
- /* Error */
- return;
- }
- if (color == ST7567_COLOR_FRONT)
- {
- ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] |= 1 << (y % 8);
- }
- else
- {
- ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] &= ~(1 << (y % 8));
- }
- }
- void ST7567_GotoXY(uint16_t x, uint16_t y)
- {
- /* Set write pointers */
- ST7567.CurrentX = x;
- ST7567.CurrentY = y;
- }
- char ST7567_Putc(char ch, FontDef_t* font, uint8_t color)
- {
- uint32_t i, b, j, k;
- for (i = 0; i < font->height; i++)
- {
- for (j = 0; j < font->bytes; j++)
- {
- b = font->data[((ch - 32) * font->height + i) * font->bytes + j];
- if (font->order == 0)
- {
- for (k = 0; k < 8 && k < font->width - j * 8; k++)
- {
- if ((b << k) & 0x80)
- {
- ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color);
- }
- else
- {
- ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) !color);
- }
- }
- }
- else
- {
- for (k = 0; k < 8 && k < font->width - j * 8; k++)
- {
- if (b & (0x0001 << k))
- {
- ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color);
- }
- else
- {
- ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) !color);
- }
- }
- }
- }
- }
- /* Increase pointer */
- ST7567.CurrentX += font->width;
- /* Return character written */
- return ch;
- }
- char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color)
- {
- /* Write characters */
- while (*str)
- {
- /* Write character by character */
- if (ST7567_Putc(*str, Font, color) != *str)
- {
- /* Return error */
- return *str;
- }
-
- /* Increase string pointer */
- str++;
- }
-
- /* Everything OK, zero should be returned */
- return *str;
- }
-
- void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c)
- {
- int16_t dx, dy, sx, sy, err, e2, i, tmp;
-
- /* Check for overflow */
- if (x0 >= ST7567_WIDTH)
- {
- x0 = ST7567_WIDTH - 1;
- }
- if (x1 >= ST7567_WIDTH)
- {
- x1 = ST7567_WIDTH - 1;
- }
- if (y0 >= ST7567_HEIGHT)
- {
- y0 = ST7567_HEIGHT - 1;
- }
- if (y1 >= ST7567_HEIGHT)
- {
- y1 = ST7567_HEIGHT - 1;
- }
-
- dx = (x0 < x1) ? (x1 - x0) : (x0 - x1);
- dy = (y0 < y1) ? (y1 - y0) : (y0 - y1);
- sx = (x0 < x1) ? 1 : -1;
- sy = (y0 < y1) ? 1 : -1;
- err = ((dx > dy) ? dx : -dy) / 2;
- if (dx == 0)
- {
- if (y1 < y0)
- {
- tmp = y1;
- y1 = y0;
- y0 = tmp;
- }
-
- if (x1 < x0)
- {
- tmp = x1;
- x1 = x0;
- x0 = tmp;
- }
-
- /* Vertical line */
- for (i = y0; i <= y1; i++)
- {
- ST7567_DrawPixel(x0, i, c);
- }
-
- /* Return from function */
- return;
- }
-
- if (dy == 0)
- {
- if (y1 < y0)
- {
- tmp = y1;
- y1 = y0;
- y0 = tmp;
- }
-
- if (x1 < x0)
- {
- tmp = x1;
- x1 = x0;
- x0 = tmp;
- }
-
- /* Horizontal line */
- for (i = x0; i <= x1; i++)
- {
- ST7567_DrawPixel(i, y0, c);
- }
-
- /* Return from function */
- return;
- }
- while (1)
- {
- ST7567_DrawPixel(x0, y0, c);
- if (x0 == x1 && y0 == y1)
- {
- break;
- }
- e2 = err;
- if (e2 > -dx)
- {
- err -= dy;
- x0 += sx;
- }
- if (e2 < dy)
- {
- err += dx;
- y0 += sy;
- }
- }
- }
- void ST7567_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c)
- {
- /* Check input parameters */
- if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
- {
- /* Return error */
- return;
- }
- /* Check width and height */
- if ((x + w) >= ST7567_WIDTH)
- {
- w = ST7567_WIDTH - x;
- }
- if ((y + h) >= ST7567_HEIGHT)
- {
- h = ST7567_HEIGHT - y;
- }
- /* Draw 4 lines */
- ST7567_DrawLine(x, y, x + w, y, c); /* Top line */
- ST7567_DrawLine(x, y + h, x + w, y + h, c); /* Bottom line */
- ST7567_DrawLine(x, y, x, y + h, c); /* Left line */
- ST7567_DrawLine(x + w, y, x + w, y + h, c); /* Right line */
- }
- void ST7567_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c)
- {
- uint8_t i;
-
- /* Check input parameters */
- if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
- {
- /* Return error */
- return;
- }
- /* Check width and height */
- if ((x + w) >= ST7567_WIDTH)
- {
- w = ST7567_WIDTH - x;
- }
- if ((y + h) >= ST7567_HEIGHT)
- {
- h = ST7567_HEIGHT - y;
- }
- /* Draw lines */
- for (i = 0; i <= h; i++)
- {
- /* Draw lines */
- ST7567_DrawLine(x, y + i, x + w, y + i, c);
- }
- }
- void ST7567_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color)
- {
- /* Draw lines */
- ST7567_DrawLine(x1, y1, x2, y2, color);
- ST7567_DrawLine(x2, y2, x3, y3, color);
- ST7567_DrawLine(x3, y3, x1, y1, color);
- }
- void ST7567_DrawFilledTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color)
- {
- int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
- yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
- curpixel = 0;
-
- deltax = ABS(x2 - x1);
- deltay = ABS(y2 - y1);
- x = x1;
- y = y1;
- if (x2 >= x1)
- {
- xinc1 = 1;
- xinc2 = 1;
- }
- else
- {
- xinc1 = -1;
- xinc2 = -1;
- }
- if (y2 >= y1)
- {
- yinc1 = 1;
- yinc2 = 1;
- }
- else
- {
- yinc1 = -1;
- yinc2 = -1;
- }
- if (deltax >= deltay)
- {
- xinc1 = 0;
- yinc2 = 0;
- den = deltax;
- num = deltax / 2;
- numadd = deltay;
- numpixels = deltax;
- }
- else
- {
- xinc2 = 0;
- yinc1 = 0;
- den = deltay;
- num = deltay / 2;
- numadd = deltax;
- numpixels = deltay;
- }
- for (curpixel = 0; curpixel <= numpixels; curpixel++)
- {
- ST7567_DrawLine(x, y, x3, y3, color);
- num += numadd;
- if (num >= den)
- {
- num -= den;
- x += xinc1;
- y += yinc1;
- }
- x += xinc2;
- y += yinc2;
- }
- }
- void ST7567_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c)
- {
- int16_t f = 1 - r;
- int16_t ddF_x = 1;
- int16_t ddF_y = -2 * r;
- int16_t x = 0;
- int16_t y = r;
- ST7567_DrawPixel(x0, y0 + r, c);
- ST7567_DrawPixel(x0, y0 - r, c);
- ST7567_DrawPixel(x0 + r, y0, c);
- ST7567_DrawPixel(x0 - r, y0, c);
- while (x < y)
- {
- if (f >= 0)
- {
- y--;
- ddF_y += 2;
- f += ddF_y;
- }
- x++;
- ddF_x += 2;
- f += ddF_x;
- ST7567_DrawPixel(x0 + x, y0 + y, c);
- ST7567_DrawPixel(x0 - x, y0 + y, c);
- ST7567_DrawPixel(x0 + x, y0 - y, c);
- ST7567_DrawPixel(x0 - x, y0 - y, c);
- ST7567_DrawPixel(x0 + y, y0 + x, c);
- ST7567_DrawPixel(x0 - y, y0 + x, c);
- ST7567_DrawPixel(x0 + y, y0 - x, c);
- ST7567_DrawPixel(x0 - y, y0 - x, c);
- }
- }
- void ST7567_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c)
- {
- int16_t f = 1 - r;
- int16_t ddF_x = 1;
- int16_t ddF_y = -2 * r;
- int16_t x = 0;
- int16_t y = r;
- ST7567_DrawPixel(x0, y0 + r, c);
- ST7567_DrawPixel(x0, y0 - r, c);
- ST7567_DrawPixel(x0 + r, y0, c);
- ST7567_DrawPixel(x0 - r, y0, c);
- ST7567_DrawLine(x0 - r, y0, x0 + r, y0, c);
- while (x < y)
- {
- if (f >= 0)
- {
- y--;
- ddF_y += 2;
- f += ddF_y;
- }
- x++;
- ddF_x += 2;
- f += ddF_x;
- ST7567_DrawLine(x0 - x, y0 + y, x0 + x, y0 + y, c);
- ST7567_DrawLine(x0 + x, y0 - y, x0 - x, y0 - y, c);
- ST7567_DrawLine(x0 + y, y0 + x, x0 - y, y0 + x, c);
- ST7567_DrawLine(x0 + y, y0 - x, x0 - y, y0 - x, c);
- }
- }
- void ST7567_Image(uint8_t *img, uint8_t frame, uint8_t x, uint8_t y)
- {
- uint32_t i, b, j;
- b = 0;
- if(frame >= img[2])
- return;
- uint32_t start = (frame * (img[3] + (img[4] << 8)));
-
- /* Go through font */
- for (i = 0; i < img[1]; i++) {
- for (j = 0; j < img[0]; j++) {
- ST7567_DrawPixel(x + j, (y + i), (uint8_t) (img[b/8 + 5 + start] >> (b%8)) & 1);
- b++;
- }
- }
- }
- void ST7567_TestDisplayRAM(void)
- {
- uint16_t x, y, pos = 0;
- for (y = 0; y < 2; y++)
- {
- for (x = 0; x < ST7567_WIDTH; x++)
- {
- ST7567_Buffer_all[pos++] = x;
- }
- ST7567_Buffer_all[pos++] = 0x11;
- ST7567_Buffer_all[pos++] = 0x11;
- ST7567_Buffer_all[pos++] = 0x11;
- ST7567_Buffer_all[pos++] = 0x11;
-
- }
- ST7567_Transmit(ST7567_Buffer_all, sizeof(ST7567_Buffer_all), ST7567_TIMEOUT);
-
- }
复制代码
void ST7567_WriteData(uint8_t data);
/**
* @brief Test ST7567 LCD Display RAM
* @param None
* @retval None
*/
void ST7567_TestDisplayRAM(void);
#endif // __ST7567_H_
- ST7567_DrawLine(0, 0, 127, 127, ST7567_COLOR_FRONT);
- //ST7567_Fill(ENABLE?ST7567_COLOR_BACK:ST7567_COLOR_FRONT);
- ST7567_UpdateScreen();
复制代码
|
|