野火32指南者
- #include <oled.h>
- #include <oledfont.h>
- #if OLED_MODE == 2
- //2 为IIC通信
- //3 为SPI通信
- void OLED_WR_Byte(unsigned char dat, unsigned char cmd)
- {
-
- if (cmd == OLED_DATA)
- OLED_WritrDate(dat);
- else
- OLED_WriteCommand(dat);
- }
- void OLED_WritrDate(unsigned char dat)
- {
- IIC_Start();
- Write_IIC_Byte (0x78);
- IIC_Wait_Ack();
- Write_IIC_Byte(0x40);//数字寄存器
- IIC_Wait_Ack();
- Write_IIC_Byte(dat);
- IIC_Wait_Ack();
- IIC_Stop();
- }
- void OLED_WriteCommand(unsigned char dat)
- {
- IIC_Start();
- Write_IIC_Byte (0x78);
- IIC_Wait_Ack();
- Write_IIC_Byte(0x00);//命令寄存器
- IIC_Wait_Ack();
- Write_IIC_Byte(dat);
- IIC_Wait_Ack();
- IIC_Stop();
- }
- #else
- void OLED_WR_Byte(unsigned char dat, unsigned char cmd)
- {
- Write_SPI_Byte(dat, cmd);
- }
- #endif
- void Initial_OLED_ssd1306(void)
- {
- //若模拟VCC GND 最好有延时再开始写数据
- //OELD_GPIO_Init();
- OLED_WR_Byte(0xAE,OLED_CMD);//关显示
-
- OLED_WR_Byte(0x00,OLED_CMD);//列地址 低 00-0F
- OLED_WR_Byte(0x10,OLED_CMD);//列地址 高 10-1F
-
- OLED_WR_Byte(0x40,OLED_CMD);//显示起始行
- OLED_WR_Byte(0xB0,OLED_CMD);//--设置页地址 B0-B7
-
- OLED_WR_Byte(0x81,OLED_CMD); // 对比度 00~FF 越大越亮哦
- OLED_WR_Byte(0xFF,OLED_CMD);//--128
-
- OLED_WR_Byte(0xA1,OLED_CMD);//段重定义设置
- OLED_WR_Byte(0xA6,OLED_CMD);//正常A6/反向A7 显示模式
-
- OLED_WR_Byte(0xA8,OLED_CMD);//设置驱动路数 1-64
- OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty -----63
-
- OLED_WR_Byte(0xC8,OLED_CMD);//扫描方式 0xC8 0xC0
- OLED_WR_Byte(0xD3,OLED_CMD);//显示偏移offset
- OLED_WR_Byte(0x00,OLED_CMD);//
-
- OLED_WR_Byte(0xD5,OLED_CMD);//设置时钟分频、震荡频率
- OLED_WR_Byte(0x80,OLED_CMD);//[3:0]分频 [7:4]震荡频率
-
- OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
- OLED_WR_Byte(0x05,OLED_CMD);//
-
- OLED_WR_Byte(0xD9,OLED_CMD);//预充电期
- OLED_WR_Byte(0xF1,OLED_CMD);//
-
- OLED_WR_Byte(0xDA,OLED_CMD);//引脚配置
- OLED_WR_Byte(0x12,OLED_CMD);//可选的引脚、不可重映射
-
- OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh(没试过)
- OLED_WR_Byte(0x30,OLED_CMD);//0.83*Vcc 0x20: 0.77*Vcc 0x00: 0.65Vcc
-
- OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵
- OLED_WR_Byte(0x14,OLED_CMD);//开启 0x10关闭
- //delay_ms(100);
- OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
-
- OLED_Clear();
- OLED_Set_Pos(0,0);
-
-
- }
- //*****************打点函数******************
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WR_Byte(0xb0+y,OLED_CMD);
- OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);//高地址
- OLED_WR_Byte((x&0x0f),OLED_CMD); //低地址
- }
- //*****************显示一个字符******************
- void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
- {
- unsigned char gap=0,i=0;
- gap=chr-' ';//得到偏移 即差值
- if(Char_Size ==16)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[gap*16+i],OLED_DATA);
-
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[gap*16+i+8],OLED_DATA);
- }
- else if(Char_Size == 6)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<6;i++)
- OLED_WR_Byte(F6x8[gap][i],OLED_DATA);
- }
- }
- //*****************显示字符串******************
- void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char 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++;
- }
- }
- //搭配显示数字函数一起用
- //返回:m^n
- unsigned int oled_pow(unsigned char m,unsigned char n)
- {
- unsigned int result=1;
- while(n--)result*=m;
- return result;
- }
- /*
- *****************显示数字******************len:数的位数
- */
- void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int 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))//当len设置过大情况下,用空格替代
- {
- if(temp==0)//高位的0无效,空格占据
- {
- if(size2 == 16)
- OLED_ShowChar(x+(size2/2)*t,y,' ',size2);//调整间隔、紧挨显示
- else if(size2 == 6)
- OLED_ShowChar(x+size2*t,y,' ',size2);//调整间隔、紧挨显示
- continue; //取下一个位
- }
- else
- enshow=1; //可以开始显示
- }
- //enshow==1后,开始执行以下程序
- if(size2 == 16)
- OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); //调整间隔、紧挨显示
- else if(size2 == 6)
- OLED_ShowChar(x+size2*t,y,temp+'0',size2);//调整间隔、紧挨显示
- }
-
-
-
- }
- //*****************清屏函数******************
- void OLED_Clear (void)
- {
- unsigned char i, n;
- for (i = 0; i <8; i++)
- {
- OLED_Set_Pos(0, i);
- for (n =0; n<128; n++)OLED_WR_Byte(0x00, OLED_DATA);
- }
- }
- //*****************清除部分屏幕******************
- void OLED_Clear_Part(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2 )
- {
- unsigned char i, n;
-
- for (i = x1; i < x2; i++)
- {
- OLED_Set_Pos(x1, i);
- for (n = y1; n < y2; n++)
- {
- OLED_WR_Byte(0x00, OLED_DATA);
- }
- }
- }
- //*****************屏幕全满******************
- void OLED_Full (void)
- {
- unsigned char i, n;
- for (i = 0; i <8; i++)
- {
- OLED_Set_Pos(0, i);
- for (n =0; n<128; n++)OLED_WR_Byte(0xff, OLED_DATA);
-
- }
- }
- //*****************显示图片******************
- void ShowBmp (unsigned char *bmp)
- {
- int i, n;
- for (i = 0; i <8; i++)
- {
- OLED_Set_Pos(0, i);
- for (n =0; n<128; n++)OLED_WR_Byte(bmp[i*128+n], OLED_DATA);
- }
- }
- // //*****************显示一个汉字******************
- //void OLED_Show_A_CHinese(unsigned char x,unsigned char y,unsigned char* ch,unsigned char Char_Size)
- //{
- // unsigned char t,i;
- // OLED_Set_Pos(x,y);
- // for(i=0;i<10;i++)
- // {
- // if(Char_Size == 16 && Chinese_16[i].name[0] == ch[0] && Chinese_16[i].name[1] == ch[1])
- // {
- // for(t=0;t<16;t++)
- // {
- // OLED_WR_Byte(Chinese_16[i].DaTa[t],OLED_DATA);
- // }
- // OLED_Set_Pos(x,y+1);
- // for(t=16;t<32;t++)
- // {
- // OLED_WR_Byte(Chinese_16[i].DaTa[t],OLED_DATA);
- // }
- // }
- // }
- //}
- //*****************显示许多汉字******************
- //void OLED_Show_Many_Chinese(unsigned char x, unsigned char y, unsigned char *chr, unsigned char Char_Size)
- //{
- // unsigned char j = 0, ch[2];
- // while (chr[j]!='\0')
- // {
- // ch[0] = chr[j];
- // ch[1] = chr[j + 1];
- // OLED_Show_A_CHinese(x,y,ch,Char_Size);
- // x+=16;
- // if(x>120){x=0;y+=2;}
- // j+=2;
- // }
- //}
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "gpio.h"
- #include "oled.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #define uchar unsigned char
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- uchar ch;
- /* 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();
- /* USER CODE BEGIN 2 */
- Initial_OLED_ssd1306();
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- OLED_ShowNum(0,0,202,3,16);
- HAL_Delay(500);
- /* 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 RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- 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 buses 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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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,
- ex: 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****/
复制代码 |