diff --git a/demo/spi/st7567/st7567.c b/demo/spi/st7567/st7567.c index 657efaa..79cde62 100644 --- a/demo/spi/st7567/st7567.c +++ b/demo/spi/st7567/st7567.c @@ -19,53 +19,56 @@ #define ABS(x) ((x) > 0 ? (x) : -(x)) /* ST7567 data buffer */ -static __XDATA uint8_t ST7567_Buffer_all[(ST7567_WIDTH + ST7567_SEG_EXPAND) * ST7567_HEIGHT / 8]; +__BIT ST7567_colorInverted = RESET; +uint8_t ST7567_currentX = 0; +uint8_t ST7567_currentY = 0; +static __XDATA uint8_t ST7567_Buffer_all[ST7567_WIDTH * ST7567_PAGES]; -/* Private ST7567 structure */ -typedef struct { - uint16_t CurrentX; - uint16_t CurrentY; - uint8_t Inverted; - uint8_t Initialized; -} ST7567_t; - -/* Private variable */ -static __XDATA ST7567_t ST7567; - - -static void ST7567_TransmitByte(uint8_t dat) +void ST7567_WriteData(uint8_t dat) { ST7567_CS = 0; SPI_TxRx(dat); ST7567_CS = 1; } -static void ST7567_Transmit(const uint8_t *pData, uint32_t Size) +void ST7567_WriteSameData(uint8_t dat, uint32_t size) { ST7567_CS = 0; - while (Size--) + do { - SPI_TxRx(*pData++); - } + SPI_TxRx(dat); + } while (--size); ST7567_CS = 1; } void ST7567_WriteCommand(uint8_t command) { ST7567_DC = 0; - ST7567_TransmitByte(command); + ST7567_WriteData(command); ST7567_DC = 1; } -void ST7567_WriteData(uint8_t dat) +static void ST7567_Transmit(const uint8_t *pDat, uint32_t size) { - ST7567_TransmitByte(dat); + ST7567_CS = 0; + do + { + SPI_TxRx(*pDat++); + } while (--size); + ST7567_CS = 1; +} + +void ST7567_Reset(void) +{ + ST7567_RES = 0; + SYS_Delay(5); + ST7567_RES = 1; } void ST7567_Init(void) { ST7567_Reset(); - ST7567_BackLight_On(); + ST7567_SetBackLightState(HAL_State_ON); ST7567_WriteCommand(ST7567_RESET); // adjust contrast @@ -94,21 +97,30 @@ void ST7567_Init(void) ST7567_WriteCommand(ST7567_ALL_PIXEL_NORMAL); } -void ST7567_Reset(void) +void ST7567_SetPowerSaveMode(HAL_State_t state) { - ST7567_RES = 0; - SYS_Delay(5); - ST7567_RES = 1; +#if (ST7567_MODEL == ST7567_MODEL_ST7565) + if (state == HAL_State_ON) + ST7567_WriteCommand(ST7567_MODE_SLEEP); + else + ST7567_WriteCommand(ST7567_MODE_NORMAL); +#else + if (state == HAL_State_ON) + { + ST7567_WriteCommand(ST7567_DISPLAY_OFF); + ST7567_WriteCommand(ST7567_ALL_PIXEL_ON); + } + else + { + ST7567_WriteCommand(ST7567_ALL_PIXEL_NORMAL); + ST7567_WriteCommand(ST7567_DISPLAY_ON); + } +#endif } -void ST7567_BackLight_On(void) +void ST7567_SetBackLightState(HAL_State_t state) { - ST7567_BL = 1; -} - -void ST7567_BackLight_Off(void) -{ - ST7567_BL = 0; + ST7567_BL = (state == HAL_State_ON)? SET : RESET; } void ST7567_SetContrast(uint8_t val) @@ -117,19 +129,23 @@ void ST7567_SetContrast(uint8_t val) ST7567_WriteCommand(ST7567_SET_EV_MASK & val); } -void ST7567_UpdateScreen(void) +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)); + uint8_t i = 0, *pt = ST7567_Buffer_all; + for (i = 0; i < ST7567_PAGES; i++) + { + ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS|(i & ST7567_SET_PAGE_ADDRESS_MASK)); + ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB|(0 >> 4)); + ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB|(0 & 0x0F)); + ST7567_Transmit(pt + (ST7567_WIDTH * i), ST7567_WIDTH); + } } void ST7567_ToggleInvert(void) { /* Toggle invert */ - ST7567.Inverted = !ST7567.Inverted; - if (ST7567.Inverted) + ST7567_colorInverted = !ST7567_colorInverted; + if (ST7567_colorInverted) { ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_ON); } @@ -142,11 +158,12 @@ void ST7567_ToggleInvert(void) void ST7567_Fill(uint8_t color) { /* Set memory */ - memset(ST7567_Buffer_all, (color == ST7567_COLOR_BACK) ? 0x00 : 0xFF, sizeof(ST7567_Buffer_all)); + memset((uint8_t *)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) +void ST7567_DrawPixel(uint8_t x, uint8_t y, uint8_t color) { + uint8_t page, column; if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT) { /* Error */ @@ -155,19 +172,19 @@ void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color) if (color == ST7567_COLOR_FRONT) { - ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] |= 1 << (y % 8); + ST7567_Buffer_all[x + (y / 8) * ST7567_WIDTH] |= 1 << (y % 8); } else { - ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] &= ~(1 << (y % 8)); + ST7567_Buffer_all[x + (y / 8) * ST7567_WIDTH] &= ~(1 << (y % 8)); } } void ST7567_GotoXY(uint16_t x, uint16_t y) { /* Set write pointers */ - ST7567.CurrentX = x; - ST7567.CurrentY = y; + ST7567_currentX = x; + ST7567_currentY = y; } char ST7567_Putc(char ch, FontDef_t* font, uint8_t color) @@ -185,11 +202,11 @@ char ST7567_Putc(char ch, FontDef_t* font, uint8_t color) { if ((b << k) & 0x80) { - ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color); + 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); + ST7567_DrawPixel(ST7567_currentX + (j * 8) + k, (ST7567_currentY + i), (uint8_t) !color); } } } @@ -199,11 +216,11 @@ char ST7567_Putc(char ch, FontDef_t* font, uint8_t color) { if (b & (0x0001 << k)) { - ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color); + 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); + ST7567_DrawPixel(ST7567_currentX + (j * 8) + k, (ST7567_currentY + i), (uint8_t) !color); } } } @@ -211,7 +228,7 @@ char ST7567_Putc(char ch, FontDef_t* font, uint8_t color) } /* Increase pointer */ - ST7567.CurrentX += font->width + 1; + ST7567_currentX += font->width + 1; /* Return character written */ return ch; diff --git a/demo/spi/st7567/st7567.h b/demo/spi/st7567/st7567.h index 1f6ae98..628c4f0 100644 --- a/demo/spi/st7567/st7567.h +++ b/demo/spi/st7567/st7567.h @@ -17,6 +17,15 @@ #include "fw_hal.h" +/** + * ST7565, ST7567 + * + * Display Data RAM has a matrix of 65 by 132 bits. The address ranges are: + * X=0~131(83H) (column address), Y=0~8 (page address) + * +*/ + +#define ST7567_MODEL ST7567_MODEL_ST7567 #define ST7567_CS P35 #define ST7567_MOSI P34 @@ -29,39 +38,37 @@ #define ST7567_WIDTH 128 // Y height #define ST7567_HEIGHT 64 -// Additional bytes in each row -#define ST7567_SEG_EXPAND 4 +// Display RAM Columns +#define ST7567_COLUMN_EXPAND 4 +// Display RAM Pages (8x8bit + 1bit) +#define ST7567_PAGES 8 // X orientation #define ST7567_X_ORIENT ST7567_SEG_DIRECTION_NORMAL // Y orientation -#define ST7567_Y_ORIENT ST7567_COM_DIRECTION_REVERSE +#define ST7567_Y_ORIENT ST7567_COM_DIRECTION_NORMAL +#define ST7567_MODEL_ST7565 0 +#define ST7567_MODEL_ST7567 1 /* ST7567 commands definitions */ -#define ST7567_DISPLAY_OFF 0xAE /* 0xae: Display OFF (sleep mode) */ +#define ST7567_DISPLAY_OFF 0xAE /* 0xae: Display OFF (sleep mode), default */ #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, 0 - 7 */ +#define ST7567_SET_PAGE_ADDRESS 0xB0 /* 0xb0-b7: Set page start address, 0 - 8 */ #define ST7567_SET_PAGE_ADDRESS_MASK 0x07 -#define ST7567_SET_COLUMN_ADDRESS_MSB 0x10 /* 0x10-0x1f: Set 8bit column address - high 4 bits */ -#define ST7567_SET_COLUMN_ADDRESS_MSB_MASK 0x0f +#define ST7567_SET_COLUMN_ADDRESS_MSB 0x10 /* 0x10-0x1f: Set 8bit column address - high 4 bits of 0 - 131 */ +#define ST7567_SET_COLUMN_ADDRESS_MSB_MASK 0x0F -#define ST7567_SET_COLUMN_ADDRESS_LSB 0x00 /* 0x00-0x0f: Set 8bit column address - low 4 bits */ +#define ST7567_SET_COLUMN_ADDRESS_LSB 0x00 /* 0x00-0x0f: Set 8bit column address - low 4 bits of 0 - 131 */ #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 */ +#define ST7567_SEG_DIRECTION_NORMAL 0xA0 /* 0xa0: Column address 0 is mapped to SEG0, default*/ +#define ST7567_SEG_DIRECTION_REVERSE 0xA1 /* 0xa1: Column address 83H(131) 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 */ @@ -71,11 +78,13 @@ #define ST7567_ALL_PIXEL_ON 0xA5 /* 0xa5: Entire display ON */ #define ST7567_ALL_PIXEL_NORMAL 0xA4 /* 0xa4: Resume to RAM content display */ + /* LCD bias set */ #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 */ /** @@ -112,28 +121,29 @@ #define ST7567_SET_EV 0x81 #define ST7567_SET_EV_MASK 0x3F +#if (ST7567_MODEL == ST7567_MODEL_ST7565) + #define ST7567_MODE_SLEEP 0xAC + #define ST7567_MODE_NORMAL 0xAD +#endif + #define ST7567_SET_BOOSTER 0xF8 /* Set booster level */ #define ST7567_SET_BOOSTER_4X 0x00 #define ST7567_SET_BOOSTER_5X 0x01 +#define ST7567_SET_BOOSTER_6X 0x11 #define ST7567_NOP 0xE3 #define ST7567_TEST 0xFE + #ifndef ST7567_TIMEOUT #define ST7567_TIMEOUT 20000 #endif -#if ST7567_X_ORIENT == ST7567_SEG_DIRECTION_REVERSE - #define ST7567_X_OFFSET ST7567_SEG_EXPAND -#else - #define ST7567_X_OFFSET 0 -#endif - - -/** Background color */ -#define ST7567_COLOR_BACK 0x00 /** Front color */ -#define ST7567_COLOR_FRONT 0x01 +#define ST7567_COLOR_FRONT 1U +/** Background color */ +#define ST7567_COLOR_BACK 0U + typedef struct { uint8_t width; @@ -146,13 +156,20 @@ typedef struct { extern __CODE FontDef_t Font_3x5; extern __CODE FontDef_t Font_5x7; - /** - * @brief Initializes ST7567 LCD - * @param None + * @brief Writes single byte data to ST7567 + * @param dat: data to be written * @retval None */ -void ST7567_Init(void); +void ST7567_WriteData(uint8_t dat); +void ST7567_WriteSameData(uint8_t dat, uint32_t size); + +/** + * @brief Writes single byte command to ST7567 + * @param command: command to be written + * @retval None + */ +void ST7567_WriteCommand(uint8_t command); /** * @brief Hardware reset ST7567 LCD @@ -162,18 +179,26 @@ void ST7567_Init(void); void ST7567_Reset(void); /** - * @brief Turn ST7567 LCD backlight on + * @brief Initializes ST7567 LCD * @param None * @retval None */ -void ST7567_BackLight_On(void); +void ST7567_Init(void); /** - * @brief Turn ST7567 LCD backlight off - * @param None + * @brief Powersave mode control + * @param state HAL_State_ON:powersave mode, HAL_State_OFF:work mode * @retval None */ -void ST7567_BackLight_Off(void); +void ST7567_SetPowerSaveMode(HAL_State_t state); + + +/** + * @brief Turn ST7567 LCD backlight on or off + * @param state HAL_State_ON:on, HAL_State_OFF:off + * @retval None + */ +void ST7567_SetBackLightState(HAL_State_t state); /** * @brief Turn ST7567 LCD backlight off @@ -201,7 +226,7 @@ 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 + * @param Color: Color to be used for screen fill, ST7567_COLOR_FRONT or ST7567_COLOR_BACK * @retval None */ void ST7567_Fill(uint8_t Color); @@ -214,7 +239,7 @@ void ST7567_Fill(uint8_t Color); * @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); +void ST7567_DrawPixel(uint8_t x, uint8_t y, uint8_t color); /** * @brief Sets cursor pointer to desired location for strings @@ -256,19 +281,4 @@ char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color); */ void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c); -/** - * @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 - */ -void ST7567_WriteData(uint8_t data); - - #endif // __ST7567_H_