STM32F042網絡資源很少,F042 驅動I2C OLED程序.
0.png (6.76 KB, 下載次數: 25)
下載附件
2018-9-18 16:37 上傳
單片機源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f0xx_hal.h"
- /* USER CODE BEGIN Includes */
- #define address 0x78
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- I2C_HandleTypeDef hi2c1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- unsigned char buffer12864[8][128] = { 0x00,};
- const unsigned char logo[8][128] = {
- 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
- …………
- …………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
- 0x00, 0x06, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //~
- };
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_I2C1_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- void Writecom(uint8_t COM);
- void Writedata(uint8_t DATA);
- void SSD1306_Init(void);
- void clear(uint8_t data);
- void Draw_Point(uint8_t x, uint8_t y);
- void Draw_Line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2);
- void Draw_byte(uint8_t page, uint8_t lie, uint8_t data);
- void Draw_byte_clear(uint8_t page, uint8_t lie, uint8_t data);
- void show_char(uint8_t page, uint8_t lie, uint8_t data);
- void show_char_clear(uint8_t page, uint8_t lie, uint8_t data);
- void show_string(uint8_t page, uint8_t lie, uint8_t *Data, uint16_t length);
- void show_string_clear(uint8_t page, uint8_t lie, uint8_t *Data, uint16_t length);
- void display_logo(void);
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- 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 */
- SSD1306_Init(); //SSD1306_Init
- HAL_Delay(100);
-
- // display_logo();//logo
- // HAL_Delay(1000);
-
- clear(0x00);
-
- uint8_t xx[6] = "hello!";
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- display_logo();
- while(1);
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI48;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
- PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* I2C1 init function */
- static void MX_I2C1_Init(void)
- {
- hi2c1.Instance = I2C1;
- hi2c1.Init.Timing = 0x2000090E;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure Analogue filter
- */
- if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure Digital filter
- */
- if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, LED_Pin|GPIO_PIN_7, GPIO_PIN_RESET);
- /*Configure GPIO pin : PB8 */
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /*Configure GPIO pin : LED_Pin */
- GPIO_InitStruct.Pin = LED_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : PB7 */
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- void Writecom(uint8_t COM)
- {
- HAL_I2C_Mem_Write(&hi2c1,address,0X00,1,&COM,1,10);
- }
- /*=====i2c???=====*/
- void Writedata(uint8_t DATA)
- {
- HAL_I2C_Mem_Write(&hi2c1,address,0X40,1,&DATA,1,10);
- }
- /*=====OLED ???=====*/
- void SSD1306_Init()
- {
- Writecom(0XAE);//display off
- //HAL_GPIO_WritePin(GPIOA,PA9_Pin,GPIO_PIN_RESET);
- Writecom(0X00);//set lower column address
- Writecom(0X10);//set higher column address
- Writecom(0X40);//set display start line
- Writecom(0XB0);//set page address
- Writecom(0X81);//set contract control
- Writecom(0XCF);// VCC Generated by Internal DC/DC Circuit
- Writecom(0XA1);//set segment remap column address 127 is mapped to SEG0
- Writecom(0XA6);//normal / reverse normal display
- Writecom(0XA8);//multiplex ratio
- Writecom(0X3F);//1/64
- Writecom(0XC8);//Com scan direction remapped mode. Scan from COM[N-1] to COM0
- Writecom(0XD3);//set display offset
- Writecom(0X00);
- Writecom(0XD5);//set osc division
- Writecom(0X80);
- Writecom(0XD9);//set pre-charge period
- Writecom(0X11);
- Writecom(0XDa);//set COM pins
- Writecom(0X12);
- Writecom(0X8d);/*set charge pump enable*/
- Writecom(0X14);
- Writecom(0Xdb);//Set VcomH
- Writecom(0X20);
- Writecom(0XAF);//display ON
- }
- /*===========================????===========================*/
- void clear(uint8_t data)
- {
- // Writecom(0XAE);//display OFF
- static unsigned char i, j;
- Writecom(0x00);//set lower column address
- Writecom(0x10);//set higher column address
- for (j = 0; j < 8; j++)
- {
- Writecom(0xB0 + j); //set page address
- for (i = 0; i < 128; i++)
- {
- buffer12864[j][i] = 0x00;
- Writedata(data);
- }
- }
- // Writecom(0XAF);//display ON
- }
- /*===========================??===========================*/
- void Draw_Point(uint8_t x, uint8_t y)
- {
- static unsigned char page, higher, lower, cache1, cache2;
- page = y / 8;
- higher = x / 16;
- lower = x % 16;
- cache1 = 0x01 << (y % 8);
- cache2 = buffer12864[ PAGE][x] | cache1;
- buffer12864[ PAGE][x] = cache2;
- Writecom(0xB0 + page); //set page address, page<=7
- Writecom(0x10 + higher); //set higher column address, higher<=7
- Writecom(0x00 + lower);//set lower column address, lower<=15
- Writedata(cache2);
- }
- void Draw_Line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
- {
- int x , y;
- float k;
-
- if(x1 == x2)
- {
- y =y2 - y1;
- if(y<0)y=0-y;
- for(x = 0; x <y; x++)
- {
- if(y > 0)Draw_Point(x1, y1 + x);
- else if(y < 0)Draw_Point(x1, y1 - x);
- else Draw_Point(x1, y1);
- }
- }
-
- if(y1 == y2)
- {
- y =x2 - x1;
- if(y<0)y=0-y;
- for(x = 0; x < y; x++)
- {
- if(y > 0)Draw_Point(x1 + x, y1);
- else Draw_Point(x1 - x, y1);
- }
- }
- if(x1 < x2)
- {
- y = x2 - x1;
- k = (y2 - y1)/y;
- for(x = 0 ; x <= y; x++)
- {
- Draw_Point(x1 + x, y1 + (int)(x * k));
- }
- }
- else
- {
- y = x1 - x2;
- k = (y1 - y2)/y;
- for(x = 0 ; x <= y; x++)
- {
- Draw_Point(x2 + x, y2 + (int)(x * k));
- }
- }
-
-
- /*
-
-
- uint8_t t;
- uint8_t xerr=0,yerr=0,delta_x,delta_y,distance;
- uint8_t incx,incy,uRow,uCol;
- delta_x=x2-x1;
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1;
- else if(delta_x==0)incx=0;
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if(delta_y==0)incy=0;
- else{incy=-1;delta_y=-delta_y;}
- if( delta_x>delta_y)distance=delta_x;
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )
- {
- Draw_Point(uRow,uCol);
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- } */
- }
- /*===========================?????===========================*/
- void Draw_byte(uint8_t page, uint8_t lie, uint8_t data)
- {
- static unsigned char higher, lower, cache;
- higher = lie / 16;
- lower = lie % 16;
- cache = buffer12864[ PAGE][lie] | data;
- buffer12864[ PAGE][lie] = cache;
- Writecom(0xB0 + page); //set page address, page<=7
- Writecom(0x10 + higher); //set higher column address, higher<=7
- Writecom(0x00 + lower);//set lower column address, lower<=15
- Writedata(cache);
- }
- /*===========================????===========================*/
- void Draw_byte_clear(uint8_t page, uint8_t lie, uint8_t data) //half
- {
- static unsigned char higher, lower, cache;
- higher = lie / 16;
- lower = lie % 16;
- cache = buffer12864[ PAGE][lie] | data;
- Writecom(0xB0 + page); //set page address, page<=7
- Writecom(0x10 + higher); //set higher column address, higher<=7
- Writecom(0x00 + lower);//set lower column address, lower<=15
- Writedata(cache);
- }
- /*===========================?????===========================*/
- void show_char(uint8_t page, uint8_t lie, uint8_t data)
- {
- if ((data <= '~') && (data >= ' '))
- {
- for (int m = 0; m < 8; m++)
- {
- Draw_byte(page, lie + m, F8X16[data - 32][m]);
- Draw_byte(page + 1, lie + m, F8X16[data - 32][m + 8]);
- }
- }
- }
- /*===========================?????===========================*/
- void show_char_clear(uint8_t page, uint8_t lie, uint8_t data)
- {
- if ((data <= '~') && (data >= ' '))
- {
- for (int m = 0; m < 8; m++)
- {
- Draw_byte_clear(page, lie + m, F8X16[data - 32][m]);
- Draw_byte_clear(page + 1, lie + m, F8X16[data - 32][m + 8]);
- }
- }
- else
- {
- for (int m = 0; m < 8; m++)
- {
- Draw_byte_clear(page, lie + m, F8X16[0][m]);
- Draw_byte_clear(page + 1, lie + m, F8X16[0][m + 8]);
- }
- }
- }
- /*===========================??????===========================*/
- void show_string(uint8_t page, uint8_t lie, uint8_t *Data, uint16_t length)
- {
- uint8_t m=0;
- if((128-length*8-lie)>0)
- {
- for (m = 0; m < length; m++)
- {
- show_char(page, lie + m * 8, *Data++);
- }
- }
- else
- {
- for (m = 0; m < ((128-lie)/8); m++)
- {
- show_char(page, lie + m * 8, *Data++);
- }
- for (m = 0; m < length; m++)
- {
- show_char(page+2, m * 8, *Data++);
- }
- }
- }
- /*===========================??????===========================*/
- void show_string_clear(uint8_t page, uint8_t lie, uint8_t *Data, uint16_t length) //
- {
- uint8_t m=0;
- if((128-length*8-lie)>0)
- {
- for (m = 0; m < length; m++)
- {
- show_char(page, lie + m * 8, *Data++);
- }
- }
- else
- {
- for (m = 0; m < ((128-lie)/8); m++)
- {
- show_char_clear(page, lie + m * 8, *Data++);
- }
- for (m = 0; m < length; m++)
- {
- show_char_clear(page+2, m * 8, *Data++);
- }
- }
- }
- /*===========================????===========================*/
- void display_logo(void)
- {
- Writecom(0XAE);//display OFF
- static unsigned char x, y, m, n;
- for (x = 0; x < 8; x++)
- {
- for (y = 0; y < 128; y++)
- {
- Draw_byte_clear(x, y, logo[x][y]); // pgm_read_byte(&logo[x][y]) ????flash????
- }
- }
- Writecom(0XAF);//display ON
- for (m = 0; m < 8; m++) //?????
- {
- for (n = 0; n < 64; n++)
- {
- Writecom(0XD3);//set display offset
- Writecom(0X00 + n);
- HAL_Delay(1);
- }
- }
- for (m = 0; m < 5; m++)
- {
- for (n = 0; n < 64; n++)
- {
- Writecom(0XD3);//set display offset
- Writecom(0X00 + n);
- HAL_Delay(2);
- }
- }
- Writecom(0XD3);//set display offset
- Writecom(0X00);
- HAL_Delay(100);
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void _Error_Handler(char * file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- 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
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼
全部資料51hei下載地址:
STM32F042G_IIC_OLED.rar
(3.32 MB, 下載次數: 67)
2018-9-18 16:38 上傳
點擊文件名下載附件
OLED 下載積分: 黑幣 -5
|