显示英文以及字符
(仅仅作为参考)
简述:
本代码处理的是ascii码的字符显示
利用软件进行取模后,对字库数据(16个点为一行,0x00形式的数据)进行检索
void display_bitmap(void)
{
uint8_t bytes_per_row = 2; // 每行占用2个字节
for (uint16_t row = 0; row < 32; row++) {
// 计算当前行数据的起始地址
const uint8_t *row_data = model + row * bytes_per_row;
// 处理当前行的每个像素
for (uint16_t col = 0; col < 16; col++) {
// 计算当前像素所在的字节和位位置
uint8_t byte_index = col / 8; // 确定在哪个字节中 (0或1)
uint8_t bit_position = 7 - (col % 8); // 计算位位置 (7到0)
// 提取像素值:检查特定位是否被设置
uint8_t pixel = row_data[byte_index] & (1 << bit_position);
// 显示像素:如果位被设置则显示'*',否则显示空格
printf("%c", pixel ? '*' : ' ');
}
// 换行到下一行
printf("\n");
}
}
一下代码从如上代码改编而来
来自 <https://chat.deepseek.com/a/chat/s/c2db2752-f68f-4806-a6b5-7a330aca20c1>
void st7789_ascii(uint16_t x1, uint16_t y1, char ch, uint16_t ascii_color, uint16_t bg_color, font_t *font) {// 字体尺寸计算uint16_t fheight = font->height;uint16_t fwidth = font->height/2;// 屏幕边界检查if (in_screen_check(x1,y1,x1 + fwidth -1,y1 + fheight -1) == false) {return;}// 颜色数据转换uint8_t color_data[2] = {(ascii_color >> 8) & 0xff, ascii_color & 0xff};uint8_t bg_color_data[2] = {(bg_color >> 8) & 0xff, bg_color & 0xff};// 设置显示区域st7789_set_range_and_prepare_mode(x1,y1,x1 + fwidth -1,y1 + fheight -1);// SPI通信准备GPIO_ResetBits(CS_PORT, CS_PIN);GPIO_SetBits(DC_PORT, DC_PIN);// 字体数据定位const uint8_t *model = font->model + (ch - ' ') * fheight * 2;// 逐像素渲染for (uint16_t row = 0; row < fheight; row++) {const uint8_t *row_data = model + row * 2;for (uint16_t col = 0; col < fwidth; col++) {uint8_t byte_index = col / 8;uint8_t bit_position = 7 - (col % 8);uint8_t pixel = row_data[byte_index] & (1 << bit_position);// 根据像素值选择颜色if (pixel) {SPI_SendData(SPI1, color_data[0]);while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);SPI_SendData(SPI1, color_data[1]);while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);} else {SPI_SendData(SPI1, bg_color_data[0]);while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);SPI_SendData(SPI1, bg_color_data[1]);while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);}}}// 通信结束处理while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) != RESET);GPIO_SetBits(CS_PORT, CS_PIN);
}
void st7789_write_ascii(uint16_t x, uint16_t y, char *str, uint16_t color, uint16_t bg_color, const font_t *font) {while (*str) {st7789_write_single_ascii(x, y, *str, color, bg_color, font);x = x + font->height / 2; // 计算下一个字符位置str++;}
}