file-type

深入了解font_8x8.c与font_8x16.c文件

RAR文件

4星 · 超过85%的资源 | 下载需积分: 43 | 533KB | 更新于2025-03-01 | 43 浏览量 | 110 下载量 举报 收藏
download 立即下载
在讨论的文件信息中,提到的两个文件名“font_8x8.c”和“font_8x16.c”暗示了这两个文件是与字体相关的C语言源代码文件。文件名中的数字“8x8”和“8x16”表示字体的点阵尺寸,其中第一个数字代表字符的宽度,第二个数字代表字符的高度,单位是像素点。 ### 知识点一:点阵字体的概念 点阵字体是通过一系列点(像素)的排列组合形成的字符图形。它是由固定大小的点阵像素阵列构成,用于计算机显示或打印输出。点阵字体的特点是占用存储空间小,并且渲染速度快,尤其在低分辨率的显示设备上表现良好。 ### 知识点二:点阵字体的格式 在C语言源代码中实现点阵字体,通常会定义一个二维数组来表示每个字符的点阵图形。比如,对于一个8x8的字体文件,每个字符的点阵数组可能为8行8列的二维数组,而8x16字体则为8行16列。 ### 知识点三:C语言中点阵字体的实现 在C语言中,实现点阵字体通常需要定义字符的映射表,将字符映射到其对应的点阵数组。这可能涉及到字符编码与点阵数组索引之间的转换。源代码文件中可能包含多个这样的映射表,每个映射表对应一种字体大小。 ### 知识点四:使用8x16点阵字体的优势 在标题中提到“我用的是8x16的,可以用的”,这意味着相较于8x8字体,8x16字体在显示设备上会显得更加清晰,占用的垂直空间更多,适合显示较长的文本行,尤其是对于需要高可读性的应用场景。 ### 知识点五:相关技术细节 在处理点阵字体文件时,一些关键的技术细节包括: 1. 字库初始化:加载点阵字体文件到内存,并进行必要的初始化设置。 2. 字符渲染:将点阵字体数组中的数据转化为像素点输出到屏幕。 3. 字符宽度和高度的处理:确保不同字符具有相同的宽度和高度,以便文本行对齐。 4. 滚动和换行:实现文本的滚动和换行功能,特别是当文本超出屏幕或窗口大小时。 ### 知识点六:应用场景 8x8和8x16点阵字体广泛应用于嵌入式系统、单片机开发、老旧计算机系统和特定的显示设备,比如LED显示屏和LCD显示器。这些系统往往对资源有严格的限制,所以点阵字体能够提供一种资源效率高、实现简单的方法来显示文本。 ### 知识点七:文件压缩和解压缩 对于文件名列表中的“font”,可以推断在实际应用中,点阵字体的源代码或数据文件可能需要被压缩存储以节省空间,尤其是在存储容量有限的嵌入式设备中。文件压缩可以采用各种压缩工具或算法(如gzip、bzip2、zlib等)。 ### 知识点八:标签的含义 在描述中提到的“font_8x16”这个标签,很可能是用来标识所使用的字体大小,这在查找、选择和使用字体文件时是非常有用的。 综上所述,涉及的知识点包括点阵字体的概念、格式、技术实现细节、应用场景,以及文件压缩和标签的用途。这些内容为理解点阵字体文件的特性、使用和优化提供了全面的介绍。

相关推荐

filetype

基于stm32cubemx,stm32f103c8t6,四针脚的oled,帮我写雨滴传感器的驱动文件,实现测量雨量大小并在屏幕上,雨量处理数据和显示数据在main.c中写,其中oled.c:#include "oled.h" #include "oledfont.h" //几个变量声明 uint8_t **Hzk; //初始化命令 uint8_t CMD_Data[]={ 0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6, 0xA8, 0x3F, 0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05, 0xD9, 0xF1, 0xDA, 0x12, 0xD8, 0x30, 0x8D, 0x14, 0xAF}; void WriteCmd() { uint8_t i = 0; for(i=0; i<27; i++){ HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,CMD_Data+i,1,0x100); } } //向设备写控制命令 void OLED_WR_CMD(uint8_t cmd) { HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100); } //向设备写数据 void OLED_WR_DATA(uint8_t data) { HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0x100); } //初始化oled屏幕 void OLED_Init(void) { HAL_Delay(200); WriteCmd(); } //清屏size12 size16要清两行,其他函数有类似情况 void OLED_Clear() { uint8_t i,n; for(i=0;i<8;i++) { OLED_WR_CMD(0xb0+i); OLED_WR_CMD (0x00); OLED_WR_CMD (0x10); for(n=0;n<128;n++) OLED_WR_DATA(0); } } //清行 void OLED_Clearrow(uint8_t i) { uint8_t n; OLED_WR_CMD(0xb0+i); OLED_WR_CMD (0x00); OLED_WR_CMD (0x10); for(n=0;n<128;n++) OLED_WR_DATA(0); } //开启OLED显示 void OLED_Display_On(void) { OLED_WR_CMD(0X8D); //SET DCDC命令 OLED_WR_CMD(0X14); //DCDC ON OLED_WR_CMD(0XAF); //DISPLAY ON } //关闭OLED显示 void OLED_Display_Off(void) { OLED_WR_CMD(0X8D); //SET DCDC命令 OLED_WR_CMD(0X10); //DCDC OFF OLED_WR_CMD(0XAE); //DISPLAY OFF } void OLED_Set_Pos(uint8_t x, uint8_t y) { OLED_WR_CMD(0xb0+y); OLED_WR_CMD(((x&0xf0)>>4)|0x10); OLED_WR_CMD(x&0x0f); } void OLED_On(void) { uint8_t i,n; for(i=0;i<8;i++) { OLED_WR_CMD(0xb0+i); //设置页地址(0~7) OLED_WR_CMD(0x00); //设置显示位置—列低地址 OLED_WR_CMD(0x10); //设置显示位置—列高地址 for(n=0;n<128;n++) OLED_WR_DATA(1); } //更新显示 } unsigned int oled_pow(uint8_t m,uint8_t n) { unsigned int result=1; while(n--)result*=m; return result; } //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 16/12 void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size) { unsigned char c=0,i=0; c=chr-' ';//得到偏移后的值 if(x>128-1){x=0;y=y+2;} if(Char_Size ==16) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_DATA(F8x16[c*16+i]); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_DATA(F8x16[c*16+i+8]); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_DATA(F6x8[c][i]); } } //显示2个数字 //x,y :起点坐标 //len :数字的位数 //size:字体大小 //mode:模式 0,填充模式;1,叠加模式 //num:数值(0~4294967295); void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size2) { uint8_t t,temp; uint8_t enshow=0; for(t=0;t<len;t++) { temp=(num/oled_pow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { OLED_ShowChar(x+(size2/2)*t,y,' ',size2); continue; }else enshow=1; } OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); } } //显示一个字符号串 void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size) { unsigned char j=0; while (chr[j]!='\0') { OLED_ShowChar(x,y,chr[j],Char_Size); x+=8; if(x>120){x=0;y+=2;} j++; } } //显示汉字 //hzk 用取模软件得出的数组 void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no) { uint8_t t,adder=0; OLED_Set_Pos(x,y); for(t=0;t<16;t++) { OLED_WR_DATA(Hzk[2*no][t]); adder+=1; } OLED_Set_Pos(x,y+1); for(t=0;t<16;t++) { OLED_WR_DATA(Hzk[2*no+1][t]); adder+=1; } } oled.h:#ifndef __OLED_H_ #define __OLED_H_ #include "stdint.h" #include "i2c.h" void WriteCmd(void); //向设备写控制命令 void OLED_WR_CMD(uint8_t cmd); //向设备写数据 void OLED_WR_DATA(uint8_t data); //初始化oled屏幕 void OLED_Init(void); //清屏 void OLED_Clear(void); //清行 void OLED_Clearrow(uint8_t i); //开启OLED显示 void OLED_Display_On(void); //关闭OLED显示 void OLED_Display_Off(void); //设置光标 void OLED_Set_Pos(uint8_t x, uint8_t y); void OLED_On(void); //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 16/12 void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size); //显示2个数字 //x,y :起点坐标 //len :数字的位数 //size:字体大小 //mode:模式 0,填充模式;1,叠加模式 //num:数值(0~4294967295); void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size2); //显示一个字符号串 void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size); //显示汉字 //hzk 用取模软件得出的数组 void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no); #endif main.c:/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** ** This notice applies to any and all portions of this file * that are not between comment pairs USER CODE BEGIN and * USER CODE END. Other portions of this file, whether * inserted by the user or by software development tools * are owned by their respective copyright owners. * * COPYRIGHT(c) 2019 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "i2c.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "oled.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ uint8_t A[]="hdilisa !!"; //初始化oled屏幕 OLED_Init(); //开启OLED显示 OLED_Display_On(); //清屏 OLED_Clear(); // OLED_ShowNum(10,10,10,8,8); // OLED_ShowChar(0, 0,'C',16); OLED_ShowString(0,0,A,sizeof(A)); // //清行 // OLED_Clearrow(2); // OLED_Clearrow(3); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

filetype

/*********************************** 本驱动文件仅适配HAL库版本 ***********************************/ // 在 oled.c 文件顶部添加 // 声明外部定义的句柄 #include "font.h" //字库文件 #include "oled.h" //声明 extern I2C_HandleTypeDef hi2c1; /* 控制宏 */ #define LEFT 0x27 #define RIGHT 0x26 #define UP 0X29 #define DOWM 0x2A #define ON 0xA7 #define OFF 0xA6 /* IIC接口选择 */ #define IICx hi2c1 //oled显示尺寸 uint16_t const displayWidth = 128; uint16_t const displayHeight = 64; /* OLED显存 [0]0 1 2 3 ... 127 [1]0 1 2 3 ... 127 [2]0 1 2 3 ... 127 [3]0 1 2 3 ... 127 [4]0 1 2 3 ... 127 [5]0 1 2 3 ... 127 [6]0 1 2 3 ... 127 [7]0 1 2 3 ... 127 */ static uint8_t OLED_RAM[8][128];//定义GDDRAM缓存区 void HAL_I2C_WriteByte(uint8_t addr,uint8_t data) { uint8_t TxData[2] = {addr,data}; HAL_I2C_Master_Transmit(&IICx,0X78,(uint8_t*)TxData,2,10); } /************************************************************** Prototype : void WriteCmd(uint8_t IIC_Command) Parameters : IIC_Command return : none Description : 写命令 ***************************************************************/ void WriteCmd(uint8_t IIC_Command) { HAL_I2C_WriteByte(0x00, IIC_Command); } /************************************************************** Prototype : void WriteDat(uint8_t IIC_Data) Parameters : IIC_Data return : none Description : 写数据 ***************************************************************/ void WriteDat(uint8_t IIC_Data) { HAL_I2C_WriteByte(0x40, IIC_Data); } /************************************************************** Prototype : void OLED_Init(void) Parameters : none return : none Description : 初始化OLED模块 ***************************************************************/ void OLED_Init(void) { HAL_Delay(500); WriteCmd(0xAE); //开显示 WriteCmd(0x20); //设置内存寻址模式 WriteCmd(0x10); //00,水平寻址模式;01,垂直寻址模式;10,页面寻址模式(重置);11,无效 WriteCmd(0xb0); //为页面寻址模式设置页面开始地址,0-7 WriteCmd(0x00); //---设置低列地址 WriteCmd(0x10); //---设置高列地址 WriteCmd(0xc8); //设置COM输出扫描方向 WriteCmd(0x40); //--设置起始行地址 WriteCmd(0x81); //--set contrast control register WriteCmd(0xff); //亮度调节 0x00~0xff WriteCmd(0xa1); //--设置段重新映射0到127 WriteCmd(0xa6); //--设置正常显示 WriteCmd(0xa8); //--设置复用比(1 ~ 64) WriteCmd(0x3F); // WriteCmd(0xa4); //0xa4,输出遵循RAM内容;0xa5,Output忽略RAM内容 WriteCmd(0xd3); //-设置显示抵消 WriteCmd(0x00); //-not offset WriteCmd(0xd5); //--设置显示时钟分频/振荡器频率 WriteCmd(0xf0); //--设置分率 WriteCmd(0xd9); //--设置pre-charge时期 WriteCmd(0x22); // WriteCmd(0xda); //--设置com大头针硬件配置 WriteCmd(0x12); WriteCmd(0xdb); //--设置vcomh WriteCmd(0x20); //0x20,0.77xVcc WriteCmd(0x8d); //--设置DC-DC WriteCmd(0x14); // WriteCmd(0xaf); //--打开oled面板 OLED_FullyClear();//清屏 } /************************************************************** Prototype : void OLED_ON(void) Parameters : none return : none Description : 将OLED从休眠中唤醒 ***************************************************************/ void OLED_ON(void) { WriteCmd(0X8D); //设置电荷泵 WriteCmd(0X14); //开启电荷泵 WriteCmd(0XAF); //OLED唤醒 } /************************************************************** Prototype : void OLED_OFF(void) Parameters : none return : none Description : 让OLED休眠 -- 休眠模式下,OLED功耗不到10uA ***************************************************************/ void OLED_OFF(void) { WriteCmd(0X8D); //设置电荷泵 WriteCmd(0X10); //关闭电荷泵 WriteCmd(0XAE); //OLED休眠 } /************************************************************** Prototype : void OLED_RefreshRAM(void) Parameters : none return : none Description : 全屏填充 ***************************************************************/ void OLED_RefreshRAM(void) { // 页寻址模式填充 for(uint16_t m = 0; m < displayHeight/8; m++) { WriteCmd(0xb0+m); //设置页地址b0~b7 WriteCmd(0x00); //设置显示位置—列低地址00-0f WriteCmd(0x10); //设置显示位置—列高地址10-1f for(uint16_t n = 0; n < displayWidth; n++) { WriteDat(OLED_RAM[m][n]); } } } /************************************************************** Prototype : void OLED_ClearRAM(void) Parameters : none return : none Description : 清除数据缓冲区 ***************************************************************/ void OLED_ClearRAM(void) { for(uint16_t m = 0; m < displayHeight/8; m++) { for(uint16_t n = 0; n < displayWidth; n++) { OLED_RAM[m][n] = 0x00; } } } /************************************************************** Prototype : void OLED_Fill(uint8_t fill_Data) Parameters : fill_Data 填充的1字节数据 return : none Description : 全屏填充 0x00~0xff ***************************************************************/ void OLED_FullyFill(uint8_t fill_Data) { for(uint16_t m = 0; m < displayHeight/8; m++) { for(uint16_t n = 0; n < displayWidth; n++) { OLED_RAM[m][n] = fill_Data; } } OLED_RefreshRAM(); } /************************************************************** Prototype : void OLED_FullyClear(void) Parameters : none return : none Description : 全屏清除 ***************************************************************/ void OLED_FullyClear(void) { OLED_FullyFill(RESET_PIXEL); } /************************************************************** Prototype : void OLED_SetPixel(int16_t x, int16_t y, uint8_t set_pixel) Parameters : x,y -- 起始点坐标(x:0~127, y:0~63); set_pixel 该点的数据 SET_PIXEL = 1, RESET_PIXEL = 0 return : none Description : 设置坐标像素点数据 ***************************************************************/ void OLED_SetPixel(int16_t x, int16_t y, uint8_t set_pixel) { if (x >= 0 && x < displayWidth && y >= 0 && y < displayHeight) { if(set_pixel){ OLED_RAM[y/8][x] |= (0x01 << (y%8)); } else{ OLED_RAM[y/8][x] &= ~(0x01 << (y%8)); } } } /************************************************************** Prototype : void OLED_GetPixel(int16_t x, int16_t y) Parameters : x,y -- 起始点坐标(x:0~127, y:0~63); return : PixelStatus 像素点状态 SET_PIXEL = 1, RESET_PIXEL = 0 Description : 获得坐标像素点数据 ***************************************************************/ PixelStatus OLED_GetPixel(int16_t x, int16_t y) { if(OLED_RAM[y/8][x] >> (y%8) & 0x01) return SET_PIXEL; return RESET_PIXEL; } /************************************************************** Prototype : void OLED_ShowStr(int16_t x, int16_t y, uint8_t ch[], uint8_t TextSize) Parameters : x,y -- 起始点坐标(x:0~127, y:0~63); ch[] -- 要显示的字符串; TextSize -- 字符大小(1:6*8 ; 2:8*16) return : none Description : 显示codetab.h中的ASCII字符,有6*8和8*16可选择 ***************************************************************/ void OLED_ShowStr(int16_t x, int16_t y, uint8_t ch[], uint8_t TextSize) { if (x >= 0 && x < displayWidth && y >= 0 && y < displayHeight) { int32_t c = 0; uint8_t j = 0; switch(TextSize) { case 1: { while(ch[j] != '\0') { c = ch[j] - 32; if(c < 0) //无效字符 break; if(x >= 125 || (127-x < 6))//一行最大显示字符数:21字节显示,多出两列,不显示 || 剩余列小于6不能显示完整字符,换行显示 { x = 0; y += 8;//换行显示 if(63 - y < 8) // 不足以显示一行时不显示 break; } for(uint8_t m = 0; m < 6; m++) { for(uint8_t n = 0; n < 8; n++) { OLED_SetPixel(x+m, y+n, (F6x8[c][m] >> n) & 0x01); } } x += 6; j++; } }break; case 2: { while(ch[j] != '\0') { c = ch[j] - 32; if(c < 0) //无效字符 break; if(x >= 127 || (127-x < 8))//16字节显示 || 剩余列小于8不能显示完整字符,换行显示 { x = 0; y += 16;//换行显示 if(63 - y < 16) // 不足以显示一行时不显示 break; } for(uint8_t m = 0; m < 2; m++) { for(uint8_t n = 0; n < 8; n++) { for(uint8_t i = 0; i < 8; i++) { OLED_SetPixel(x+n, y+i+m*8, (F8X16[c][n+m*8] >> i) & 0x01); } } } x += 8; j++; } }break; } } OLED_RefreshRAM(); } /************************************************************** Prototype : void OLED_ShowCN(int16_t x, int16_t y, uint8_t* ch) Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); CN[]:汉字在codetab.h中的索引 return : none Description : 显示codetab.h中的汉字,16*16点阵 ***************************************************************/ void OLED_ShowCN(int16_t x, int16_t y, uint8_t* ch) { if (x >= 0 && x < displayWidth && y >= 0 && y < displayHeight) { int32_t len = 0,offset = sizeof(F16x16_CN[0].index); while(ch[len] != '\0') { if(x >= 127 || (127-x < 16))//8个汉字显示||剩余列小于16不能显示完整字符,换行显示 { x = 0; y += 16; if(63 - y < 16) // 不足以显示一行时不显示 break; } //需要处理输入数据大于显示数据的问题 for(uint8_t i = 0; i < sizeof(F16x16_CN)/sizeof(GB2312_CN); i++) { if(((F16x16_CN[i].index[0] == ch[len]) && (F16x16_CN[i].index[1] == ch[len+1]))){ for(uint8_t m = 0; m < 2; m++) //页 { for(uint8_t n = 0; n < 16; n++) // 列 { for(uint8_t j = 0; j < 8; j++) // 行 { OLED_SetPixel(x+n, y+j+m*8, (F16x16_CN[i].encoder[n+m*16] >> j) & 0x01); } } } x += 16; len += offset; break; } else if(F16x16_CN[i].index[0] == ch[len] && ch[len] == 0x20){ for(uint8_t m = 0; m < 2; m++) { for(uint8_t n = 0; n < 16; n++) { for(uint8_t j = 0; j < 8; j++) { OLED_SetPixel(x+n, y+j+m*8, (F16x16_CN[i].encoder[n+m*16] >> j) & 0x01); } } } x += 16; len++; break; } } } } OLED_RefreshRAM(); } /************************************************************** Prototype : void OLED_Show_MixedCH(int16_t x, int16_t y, uint8_t* ch) Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); CN[]:汉字在codetab.h中的索引 return : none Description : 显示codetab.h中的汉字,16*16点阵,英文,8*16点阵 ***************************************************************/ void OLED_ShowMixedCH(int16_t x, int16_t y, uint8_t* ch) { if (x >= 0 && x < displayWidth && y >= 0 && y < displayHeight) { int32_t len = 0, c,offset = sizeof(F16x16_CN[0].index); while(ch[len] != '\0') { if(ch[len] >= 0xa1)//GB2312从0xA1A0开始 { for(uint8_t i = 0; i < sizeof(F16x16_CN)/sizeof(GB2312_CN); i++) { if(((F16x16_CN[i].index[0] == ch[len]) && (F16x16_CN[i].index[1] == ch[len+1]))) { if(x >= 127|| (127-x < 16))//8个汉字显示||剩余列小于16不能显示完整字符,换行显示 { x = 0; y += 16; if(63 - y < 16) // 不足以显示一行时不显示 break; } for(uint8_t m = 0; m < 2; m++) //页 { for(uint8_t n = 0; n < 16; n++) //列 { for(uint8_t j = 0; j < 8; j++) //行 { OLED_SetPixel(x+n, y+j+m*8, (F16x16_CN[i].encoder[n+m*16] >> j) & 0x01); } } } x += 16; len += offset; break; } } } else if(ch[len] <= 127)//ASCII编码范围0-127 { c = ch[len] - 32; if(c < 0) // 无效字符 break; if(x >= 127 || (127-x < 8))//16字节显示 || 剩余列小于8不能显示完整字符,换行显示 { x = 0; y += 16; if(63 - y < 16) // 不足以显示一行时不显示 break; } for(uint8_t m = 0; m < 2; m++) { for(uint8_t n = 0; n < 8; n++) { for(uint8_t i = 0; i < 8; i++) { OLED_SetPixel(x+n, y+i+m*8, (F8X16[c][n+m*8] >> i) & 0x01); } } } x += 8; len++; } } } OLED_RefreshRAM(); } /*************************************************************** Prototype : void OLED_DrawBMP(int16_t x0,int16_t y0,int16_t L,int16_t H,const uint8_t BMP[]) Parameters : (x0,y0)坐标长L宽H区域绘制图像BMP 0<=x0<=127 0<=y0<=63 0<=L+x0<=127 0<=H+y0<= 63 图像取模 纵向取模,字节倒序 return : none Description : 区域图像绘制,显示BMP位图,格式使用二维数组存储 ***************************************************************/ void OLED_DrawBMP(int16_t x0,int16_t y0,int16_t L,int16_t H,const uint8_t BMP[]) { if (x0 >= 0 && x0 < displayWidth && x0+L <= displayWidth &&\ y0 >= 0 && y0 < displayHeight && y0+H <= displayHeight) { uint8_t *p = (uint8_t *)BMP; for(int16_t y = y0; y < y0+H; y+=8) { for(int16_t x = x0; x < x0+L; x++) { for(int16_t i = 0; i < 8; i++) { // OLED_SetPixel(x, y+i, ((*((uint8_t *)BMP+(x-x0)+L*((y-y0)/8))) >> i) & 0x01); OLED_SetPixel(x, y+i, ((*p) >> i) & 0x01); } p++; } } } OLED_RefreshRAM(); } /*************************************************************** Prototype : void OLED_AreaFill(int16_t x0,int16_t y0,int16_t L,int16_t H) Parameters : 区域内容清除,(x0,y0)坐标长L宽H区域 0<=x0<=127 0<=y0<=63 0<=L+x0<=127 0<=H+y0<= 63 图像取模 纵向取模,字节倒序 return : none Description : 规定区域内容填充 ***************************************************************/ void OLED_AreaFill(int16_t x0,int16_t y0,int16_t L,int16_t H, uint8_t fill_data) { if (x0 >= 0 && x0 < displayWidth && x0+L <= displayWidth &&\ y0 >= 0 && y0 < displayHeight && y0+H <= displayHeight) { for(int16_t y = y0; y < y0+H; y++) { for(int16_t x = x0; x < x0+L; x++) { for(int16_t i = 0; i < 8; i++) { OLED_SetPixel(x, y+i, (fill_data >> i) & SET_PIXEL); } } } OLED_RefreshRAM(); } } /*************************************************************** Prototype : void OLED_AreaCLR(int16_t x0,int16_t y0,int16_t L,int16_t H) Parameters : (x0,y0)坐标长L宽H区域 0<=x0<=127 0<=y0<=63 0<=L+x0<=127 0<=H+y0<= 63 图像取模 纵向取模,字节倒序 return : none Description : 规定区域内容清除 ***************************************************************/ void OLED_AreaClear(int16_t x0,int16_t y0,int16_t L,int16_t H) { if (x0 >= 0 && x0 < displayWidth && x0+L <= displayWidth &&\ y0 >= 0 && y0 < displayHeight && y0+H <= displayHeight) { for(int16_t y = y0; y < y0+H; y+=8) { for(int16_t x = x0; x < x0+L; x++) { for(int16_t i = 0; i < 8; i++) { OLED_SetPixel(x, y+i, RESET_PIXEL); } } } OLED_RefreshRAM(); } } /*************************************************************** Prototype : void OLED_FullyToggle(void) Parameters : none return : none Description : 缓冲区数据取反后刷新到GDDRAM ***************************************************************/ void OLED_FullyToggle(void) { for(uint16_t m = 0; m < displayHeight/8; m++) { for(uint16_t n = 0; n < displayWidth; n++) { OLED_RAM[m][n] = ~OLED_RAM[m][n]; } } OLED_RefreshRAM(); } /*************************************************************** Prototype : void OLED_AreaToggle(int16_t x0,int16_t y0,int16_t L,int16_t H) Parameters : (x0,y0)坐标长L宽H区域 0<=x0<=127 0<=y0<=63 0<=L+x0<=127 0<=H+y0<= 63 图像取模 纵向取模,字节倒序 return : none Description : 规定区域内容取反 ***************************************************************/ void OLED_AreaToggle(int16_t x0,int16_t y0,int16_t L,int16_t H) { if (x0 >= 0 && x0 < displayWidth && x0+L <= displayWidth &&\ y0 >= 0 && y0 < displayHeight && y0+H <= displayHeight) { for(int16_t y = y0; y < y0+H; y+=8) { for(int16_t x = x0; x < x0+L; x++) { for(int16_t i = 0; i < 8; i++) { OLED_SetPixel(x, y+i, !OLED_GetPixel(x, y+i)); } } } OLED_RefreshRAM(); } } /**************************************************************** 全屏垂直偏移,0->63方向 方向垂直向上,范围0-63 方向垂直向下,范围63-0 ****************************************************************/ void OLED_VerticalShift(void) { for(uint8_t i = 0; i < displayHeight; i++) { WriteCmd(0xd3);//设置显示偏移,0->63方向 WriteCmd(i);//偏移量 HAL_Delay(40);//延时时间 } } /**************************************************************** 屏幕内容水平全屏滚动播放 左 LEFT 0x27 右 RIGHT 0x26 ****************************************************************/ void OLED_HorizontalShift(uint8_t direction) { WriteCmd(direction);//设置滚动方向 WriteCmd(0x00);//虚拟字节设置,默认为0x00 WriteCmd(0x00);//设置开始页地址 WriteCmd(0x05);//设置每个滚动步骤之间的时间间隔的帧频 WriteCmd(0x07);//设置结束页地址 WriteCmd(0x00);//虚拟字节设置,默认为0x00 WriteCmd(0xff);//虚拟字节设置,默认为0xff WriteCmd(0x2f);//开启滚动-0x2f,禁用滚动-0x2e,禁用需要重写数据 } /**************************************************************** 屏幕内容垂直水平全屏滚动播放 上 UP 0x29 下 DOWN 0x2A ****************************************************************/ void OLED_VerticalAndHorizontalShift(uint8_t direction) { WriteCmd(direction);//设置滚动方向 WriteCmd(0x00);//虚拟字节设置,默认为0x00 WriteCmd(0x00);//设置开始页地址 WriteCmd(0x05);//设置每个滚动步骤之间的时间间隔的帧频 WriteCmd(0x07);//设置结束页地址 WriteCmd(0x01);//垂直滚动偏移量 WriteCmd(0x2f);//开启滚动-0x2f,禁用滚动-0x2e,禁用需要重写数据 } /**************************************************************** 屏幕内容取反显示 开 ON 0xA7 关 OFF 0xA6 默认此模式,设置像素点亮 ****************************************************************/ void OLED_DisplayMode(uint8_t mode) { WriteCmd(mode); } /**************************************************************** 屏幕亮度调节 intensity 0-255 默认为0x7f ****************************************************************/ void OLED_IntensityControl(uint8_t intensity) { WriteCmd(0x81); WriteCmd(intensity); }这些代码怎么在main.c中调用使用

filetype

用MSP430F5529 LauchPad作为是实验板,IAR Embedded Workbench IDE-MSP430为工具,根据要求及下方部分代码完成实验二,给出完整代码。 实验二: 通过对MSP430F5529开发板编程,从IO口产生一个25Hz方波信号,并用AD模块测量该方波信号的峰值幅度值显示于OLED上,要求: 1、利用MSP430定时器和通用IO产生25Hz方波。 2、利用MSP430内部AD测量出该方波峰值幅度并显示在OLED上。 详细要求如下: 1.利用定时器和IO口产生方波 初始化IO口设置,初始化定时器设置并是能中断,编写中断函数内改变IO口的高低电平 2.利用AD测量方波的峰值幅度 设置ADC对应的IO口为复用输入模式,使能参考电压、打开AD转换功能;设置采样时钟源及参数、参考电压大小,使能中断、使能转换,编写ADC12中断函数,在中断函数中读取ADC采样值并对采样值进行转换得到电压实际值。 main.c #include <msp430.h> #include "IIC.h" /********************** PWM波输出端口定义 ***********************/ void init_IO(void) { //指定输出端口选择复用模式 //指定输出端口为输出模式 } /********************* PWM波输出定时器配置 (此代码使用TA0.1输出PWM 由P1.2输出 可自行修改为其他端口) ********************/ int Vpp=0; //幅值 int ccr0= //初始化频率(根据下面的定义自行计算) int ccr1= //初始化占空比 (根据下面定义自行计算) void timer_A0_1_init() //TA0.1输出PWM 由P1.2输出 { //ACLK,增计数 (也可以选择其他时钟源,自行选择) //输出模式7 TA0CCR0 = ccr0; //时钟频率为32768HZ(ACLK) TA0CCR1 = ccr1; //占空比为CCR1/CCR0 } /******************** ADC配置模块 (以下注释配置并不唯一,仅供参考,可以自行选择需要用的寄存器来进行配置) *******************/ void ADC_init() { //AD接入端口复用 //端口输出模式选择为输入 //配置ADC12CTLx使能参考电压 //配置ADC12CTLx打开AD,设置采样时钟 //配置ADC12CTLx配置采样保持、分频系数、时钟源、转换序列 //配置ADC12MCTLx设置参考电压源以及参考电压大小 //配置ADC12IE使能AD中断 //配置ADC12CTLx使能AD转换 } void delayms(int t) //延时t ms { while(t--) { int m=200; while(m--); } } /******************* 计算采样出来的电压值 (提供一种方法,仅供参考,可以自行写) ***************/ int get_Vpp() //得到Vpp { int n; int min=0,max=4095,temp; for(n=0;n<1000;n++) { temp=ADC12MEM0; if(temp<min) min=temp; if(temp>max) max=temp; } return //返回一个计算后的电压值(自行写出计算公式) } void LCD_Show() //LCD显示函数 { LCD_ShowString(8,8,"Vpp=",16); LCD_ShowString(72,8,"mV",16); LCD_ShowNum(40,8,Vpp,4,16); } void init() //初始化 { __enable_interrupt(); init_IO(); timer_A0_1_init(); ADC_init(); LCD_Init(); init_key(); } void main( void ) { WDTCTL = WDTPW + WDTHOLD; //关闭开门狗 init(); //初始化 while(1){ Vpp=get_Vpp(); LCD_Show(); } } 这里是附加的IIC.c代码 #include "msp430.h" #include "IIC.h" #include "font.h" //IIC延时函数 void IIC_delayus(unsigned int i) { unsigned int j,k; for(k=0;k<i;k++) for(j=0;j<4;i++); } //IIC初始化函数 void IIC_ioinit() { P6DIR|=BIT1|BIT5; //配置IIC IO口为输出方向 P6REN|=BIT1|BIT5; //配置IIC IO口使能上拉电阻 } //开始一个IIC通信 void IIC_start() { SCL_HIGH; SDA_HIGH; //IIC_delayus(2); SDA_LOW; //IIC_delayus(2); SCL_LOW; } //停止IIC通信 void IIC_stop() { SCL_LOW; SDA_LOW; //IIC_delayus(2); SCL_HIGH; SDA_HIGH; //IIC_delayus(2); } //IIC发送一个byte void IIC_writebyte(unsigned char IIC_byte) { unsigned char i; for(i=0;i<8;i++) { if(IIC_byte&0x80) SDA_HIGH; else SDA_LOW; SCL_HIGH; SCL_LOW; IIC_byte<<=1; //loop } SDA_HIGH; SCL_HIGH; SCL_LOW; } void IIC_writecmd(unsigned char IIC_command) { IIC_start(); IIC_writebyte(0x78); //Slave address,SA0=0 IIC_writebyte(0x00); //write command IIC_writebyte(IIC_command); IIC_stop(); } void IIC_writedata(unsigned char IIC_data) { IIC_start(); IIC_writebyte(0x78); IIC_writebyte(0x40); //write data IIC_writebyte(IIC_data); IIC_stop(); } /**************************LCD 设置坐标*******************************/ void LCD_Set_Pos(unsigned char x, unsigned char y) { IIC_writecmd(0xb0+y); IIC_writecmd(((x&0xf0)>>4)|0x10); IIC_writecmd((x&0x0f)|0x01); } /***************************LCD复位************************************/ void LCD_CLS(void) { unsigned char y,x; for(y=0;y<8;y++) { IIC_writecmd(0xb0+y); IIC_writecmd(0x01); IIC_writecmd(0x10); for(x=0;x<X_WIDTH;x++) IIC_writedata(0); } } /***************************LCD初始化************************************/ void LCD_Init(void) { IIC_ioinit(); IIC_writecmd(0xAE); //display off IIC_writecmd(0x20); //Set Memory Addressing Mode IIC_writecmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid IIC_writecmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7 IIC_writecmd(0xc8); //Set COM Output Scan Direction IIC_writecmd(0x00);//---set low column address IIC_writecmd(0x10);//---set high column address IIC_writecmd(0x40);//--set start line address IIC_writecmd(0x81);//--set contrast control register IIC_writecmd(0x7f); IIC_writecmd(0xa1);//--set segment re-map 0 to 127 IIC_writecmd(0xa6);//--set normal display IIC_writecmd(0xa8);//--set multiplex ratio(1 to 64) IIC_writecmd(0x3F);// IIC_writecmd(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content IIC_writecmd(0xd3);//-set display offset IIC_writecmd(0x00);//-not offset IIC_writecmd(0xd5);//--set display clock divide ratio/oscillator frequency IIC_writecmd(0xf0);//--set divide ratio IIC_writecmd(0xd9);//--set pre-charge period IIC_writecmd(0x22); // IIC_writecmd(0xda);//--set com pins hardware configuration IIC_writecmd(0x12); IIC_writecmd(0xdb);//--set vcomh IIC_writecmd(0x20);//0x20,0.77xVcc IIC_writecmd(0x8d);//--set DC-DC enable IIC_writecmd(0x14);// IIC_writecmd(0xaf);//--turn on oled panel LCD_CLS(); LCD_Set_Pos(0,0); } //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 16/12 void LCD_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size) { unsigned char c=0,i=0; c=chr-' ';//得到偏移后的值 if(x>Max_Column-1) { x=0; y=y+2; } if(Char_Size ==16) { LCD_Set_Pos(x,y); for(i=0;i<8;i++) IIC_writedata(F8X16[c*16+i]); LCD_Set_Pos(x,y+1); for(i=0;i<8;i++) IIC_writedata(F8X16[c*16+i+8]); } else { LCD_Set_Pos(x,y); for(i=0;i<6;i++) IIC_writedata(F6x8[c][i]); } } //显示一个字符号串 void LCD_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char Char_Size) { unsigned char j=0; while (chr[j]!='\0') { LCD_ShowChar(x,y,chr[j],Char_Size); x+=8; if(x>128) { x=0; y+=2; } j++; } } //m^n函数 unsigned long oled_pow(unsigned char m,unsigned char n) { unsigned long result=1; while(n--)result*=m; return result; } //显示一个数字 void LCD_ShowNum(unsigned char x,unsigned char y,unsigned long num,unsigned char len,unsigned char size2) { unsigned char t,temp; //unsigned char enshow=0; for(t=0;t<len;t++) { temp=(num/oled_pow(10,len-t-1))%10; /*if(enshow==0&&t<(len-1)) { if(temp==0) { LCD_ShowChar(x+(size2/2)*t,y,' ',size2); continue; }else enshow=1; }*/ LCD_ShowChar(x+(size2/2)*t,y,temp+'0',size2); } }

baozhb
  • 粉丝: 8
上传资源 快速赚钱