diff --git a/Inc/ssd1306.h b/Inc/ssd1306.h index 3e28ca1..53a33a5 100644 --- a/Inc/ssd1306.h +++ b/Inc/ssd1306.h @@ -2,23 +2,11 @@ #include "stm32f4xx_hal.h" #include "fonts.h" -// Deze library is door Olivier Van den Eede 2016 geschreven en aangepast voor gebruik met -// Stm32 microcontrollers en maakt gebruik van de HAL-i2c library's. -// -// Deze library is gemaakt om gebruik te kunnen maken van een ssd1306 gestuurd oled display. -// Voor het gebruik moeten zeker de onderstaande defines juist ingesteld worden. -// Zoals de gebruikte i2c poort en de groote van het scherm. -// -// De library maakt gebruik van 2 files (fonts.c/h) Hierin staan 3 fonts beschreven. -// Deze fonts kunnen zo gebruikt worden: - Font_7x10 -// - Font_11x18 -// - Font_16x26 - #ifndef ssd1306 #define ssd1306 // i2c port naam in main programma gegenereerd door cube -#define SSD1306_I2C_PORT hi2c1 +#define SSD1306_I2C_PORT hi2c1 // I2C address #define SSD1306_I2C_ADDR 0x78 // SSD1306 width in pixels @@ -26,30 +14,64 @@ // SSD1306 LCD height in pixels #define SSD1306_HEIGHT 64 +#define SSD1306_EXTERNALVCC 0x1 +#define SSD1306_SWITCHCAPVCC 0x2 +#define SSD1306_SETCONTRAST 0x81 +#define SSD1306_DISPLAYALLON_RESUME 0xA4 +#define SSD1306_DISPLAYALLON 0xA5 +#define SSD1306_NORMALDISPLAY 0xA6 +#define SSD1306_INVERTDISPLAY 0xA7 +#define SSD1306_DISPLAYOFF 0xAE +#define SSD1306_DISPLAYON 0xAF +#define SSD1306_SETDISPLAYOFFSET 0xD3 +#define SSD1306_SETCOMPINS 0xDA +#define SSD1306_SETVCOMDETECT 0xDB +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 +#define SSD1306_SETPRECHARGE 0xD9 +#define SSD1306_SETMULTIPLEX 0xA8 +#define SSD1306_SETLOWCOLUMN 0x00 +#define SSD1306_SETHIGHCOLUMN 0x10 +#define SSD1306_SETSTARTLINE 0x40 +#define SSD1306_MEMORYMODE 0x20 +#define SSD1306_COLUMNADDR 0x21 +#define SSD1306_PAGEADDR 0x22 +#define SSD1306_COMSCANINC 0xC0 +#define SSD1306_COMSCANDEC 0xC8 +#define SSD1306_SEGREMAP 0xA0 +#define SSD1306_CHARGEPUMP 0x8D + +// Scrolling #defines +#define SSD1306_ACTIVATE_SCROLL 0x2F +#define SSD1306_DEACTIVATE_SCROLL 0x2E +#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 +#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 +#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 +#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 +#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A // -// Enum voor de kleuren van het scherm Black en White +// Enum voor de kleuren van het scherm Black en White // typedef enum { - Black = 0x00, /*!< Black color, no pixel */ - White = 0x01 /*!< Pixel is set. Color depends on LCD */ + Black = 0x00, /*!< Black color, no pixel */ + White = 0x01 /*!< Pixel is set. Color depends on LCD */ } SSD1306_COLOR; // -// Struct om wijzigingen bij te houden +// Struct om wijzigingen bij te houden // typedef struct { - uint16_t CurrentX; - uint16_t CurrentY; - uint8_t Inverted; - uint8_t Initialized; + uint16_t CurrentX; + uint16_t CurrentY; + uint8_t Inverted; + uint8_t Initialized; } SSD1306_t; -// De i2c poort staat in de main +// De i2c poort staat in de main extern I2C_HandleTypeDef SSD1306_I2C_PORT; // -// De functies definities van de functies die gebruikt kunnen worden +// De functies definities van de functies die gebruikt kunnen worden // uint8_t ssd1306_Init(void); void ssd1306_Fill(SSD1306_COLOR color); @@ -60,5 +82,7 @@ char ssd1306_WriteString(char* str, FontDef Font, SSD1306_COLOR color); void ssd1306_SetCursor(uint8_t x, uint8_t y); void ssd1306_WriteCommand(uint8_t command); +void ssd1306_Scroll(void); +void startscrollright(uint8_t start, uint8_t stop); #endif diff --git a/Src/main.c b/Src/main.c index cdeb90c..ab0d3d7 100644 --- a/Src/main.c +++ b/Src/main.c @@ -1,319 +1,331 @@ -/** - ****************************************************************************** - * File Name : main.c - * Description : Main program body - ****************************************************************************** - * - * COPYRIGHT(c) 2017 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. - * - ****************************************************************************** - */ -/* Includes ------------------------------------------------------------------*/ -#include -#include "main.h" -#include "stm32f4xx_hal.h" - -/* USER CODE BEGIN Includes */ -#include "ssd1306.h" -#include "fonts.h" - -//#include "dht22.h" - -/* USER CODE END Includes */ - -/* Private variables ---------------------------------------------------------*/ -I2C_HandleTypeDef hi2c1; - -UART_HandleTypeDef huart1; - -/* USER CODE BEGIN PV */ -/* Private variables ---------------------------------------------------------*/ -char UART1_Data; -char UART_Buffer[32]; -char text[32]; -unsigned char i = 0; - -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); -void Error_Handler(void); -static void MX_GPIO_Init(void); -static void MX_I2C1_Init(void); -static void MX_USART1_UART_Init(void); - -/* USER CODE BEGIN PFP */ -/* Private function prototypes -----------------------------------------------*/ - -/* 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(); - - /* Configure the system clock */ - SystemClock_Config(); - - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_I2C1_Init(); - MX_USART1_UART_Init(); - - /* USER CODE BEGIN 2 */ - ssd1306_Init(); - //DHT22_Init(); - HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1); - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6); //Toggle the state of pin PC9 - //HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_7); //Toggle the state of pin PC9 - //DHT22_Read(htim6); - - ssd1306_SetCursor(0, 53); - ssd1306_WriteString(text, Font_7x10, White); - ssd1306_UpdateScreen(); - - for(int i = 0; i < 64; i++) { - ssd1306_WriteCommand(0x40 | i); - ssd1306_UpdateScreen(); - HAL_Delay(30); - } - - //HAL_Delay(250); - - ssd1306_Fill(Black); - ssd1306_UpdateScreen(); - } - /* USER CODE END 3 */ - -} - -/** System Clock Configuration -*/ -void SystemClock_Config(void) -{ - - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_ClkInitTypeDef RCC_ClkInitStruct; - - /**Configure the main internal regulator output voltage - */ - __HAL_RCC_PWR_CLK_ENABLE(); - - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); - - /**Initializes the CPU, AHB and APB busses clocks - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = 16; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - 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_HSI; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { - Error_Handler(); - } - - /**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.ClockSpeed = 400000; - hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_16_9; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) - { - Error_Handler(); - } - -} - -/* USART1 init function */ -static void MX_USART1_UART_Init(void) -{ - - huart1.Instance = USART1; - huart1.Init.BaudRate = 9600; - huart1.Init.WordLength = UART_WORDLENGTH_8B; - huart1.Init.StopBits = UART_STOPBITS_1; - huart1.Init.Parity = UART_PARITY_NONE; - huart1.Init.Mode = UART_MODE_TX_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart1.Init.OverSampling = UART_OVERSAMPLING_16; - if (HAL_UART_Init(&huart1) != HAL_OK) - { - Error_Handler(); - } - -} - -/** 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_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET); - - /*Configure GPIO pins : PA6 PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - -} - -/* USER CODE BEGIN 4 */ -void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) -{ - char n = 0; - char UART_Aux[32]; - - if (huart->Instance == USART1) { - if (UART1_Data != '\n') { - UART_Buffer[i] = UART1_Data; - i++; - } else { - - n = sprintf(UART_Aux, "%s\r\n", UART_Buffer); - - HAL_UART_Transmit(&huart1, (uint8_t*) &UART_Aux, n, 1000); - - - // limpa o '\r' - UART_Buffer[i-1] = 0; - snprintf(text, 32, "%s", UART_Buffer); - - i = 0; - } - HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1); - } -} -/* USER CODE END 4 */ - -/** - * @brief This function is executed in case of error occurrence. - * @param None - * @retval None - */ -void Error_Handler(void) -{ - /* USER CODE BEGIN Error_Handler */ - /* User can add his own implementation to report the HAL error return state */ - while(1) - { - } - /* USER CODE END Error_Handler */ -} - -#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****/ +/** + ****************************************************************************** + * File Name : main.c + * Description : Main program body + ****************************************************************************** + * + * COPYRIGHT(c) 2017 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. + * + ****************************************************************************** + */ +/* Includes ------------------------------------------------------------------*/ +#include +#include + +#include "main.h" +#include "stm32f4xx_hal.h" + +/* USER CODE BEGIN Includes */ +#include "ssd1306.h" +#include "fonts.h" +//#include "dht22.h" +/* USER CODE END Includes */ + +/* Private variables ---------------------------------------------------------*/ +I2C_HandleTypeDef hi2c1; +UART_HandleTypeDef huart1; + +/* USER CODE BEGIN PV */ +/* Private variables ---------------------------------------------------------*/ +char UART1_Data; +char UART_Buffer[32]; +char text[32]; +int i; + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +void Error_Handler(void); +static void MX_GPIO_Init(void); +static void MX_I2C1_Init(void); +static void MX_USART1_UART_Init(void); + +/* USER CODE BEGIN PFP */ +/* Private function prototypes -----------------------------------------------*/ + +/* 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(); + + /* Configure the system clock */ + SystemClock_Config(); + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_I2C1_Init(); + MX_USART1_UART_Init(); + + /* USER CODE BEGIN 2 */ + ssd1306_Init(); + //DHT22_Init(); + HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1); + +/* + for(int y = 0; y < 129; y++) { + ssd1306_DrawPixel(y, 20, White); + } +*/ + //ssd1306_UpdateScreen(); + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + int x, msg; + //HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6); //Toggle the state of pin PC9 + //DHT22_Read(htim6); + + + //startscrollright(0x00, 0x0F); + + for(int times = 0; times < 12; times++) { + ssd1306_Scroll(); + ssd1306_UpdateScreen(); + HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_7); //Toggle the state of pin PC9 + } + + x = rand() % 60 + 1; + msg = rand() % 2 + 1; + ssd1306_SetCursor(x, 53); + ssd1306_WriteString((msg == 1) ? "GORDOS" : "LOL", Font_7x10, White); + +/* + for(int y = 0; y < 129; y++) { + ssd1306_DrawPixel(y, 63, White); + } +*/ + ssd1306_UpdateScreen(); + } + /* USER CODE END 3 */ + +} + +/** System Clock Configuration +*/ +void SystemClock_Config(void) +{ + + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_ClkInitTypeDef RCC_ClkInitStruct; + + /**Configure the main internal regulator output voltage + */ + __HAL_RCC_PWR_CLK_ENABLE(); + + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + + /**Initializes the CPU, AHB and APB busses clocks + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = 16; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + 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_HSI; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) + { + Error_Handler(); + } + + /**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.ClockSpeed = 400000; + hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_16_9; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) + { + Error_Handler(); + } + +} + +/* USART1 init function */ +static void MX_USART1_UART_Init(void) +{ + + huart1.Instance = USART1; + huart1.Init.BaudRate = 115200; + huart1.Init.WordLength = UART_WORDLENGTH_8B; + huart1.Init.StopBits = UART_STOPBITS_1; + huart1.Init.Parity = UART_PARITY_NONE; + huart1.Init.Mode = UART_MODE_TX_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart1.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart1) != HAL_OK) + { + Error_Handler(); + } + +} + +/** 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_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET); + + /*Configure GPIO pins : PA6 PA7 */ + GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + +} + +/* USER CODE BEGIN 4 */ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +{ + char n = 0; + char UART_Aux[32]; + + if (huart->Instance == USART1) { + if (UART1_Data != '\n') { + UART_Buffer[i] = UART1_Data; + i++; + } else { + + n = sprintf(UART_Aux, "%s\r\n", UART_Buffer); + + HAL_UART_Transmit(&huart1, (uint8_t*) &UART_Aux, n, 1000); + + + // limpa o '\r' + UART_Buffer[i-1] = 0; + snprintf(text, 32, "%s", UART_Buffer); + + i = 0; + } + HAL_UART_Receive_IT(&huart1, (uint8_t*) &UART1_Data, 1); + } +} +/* USER CODE END 4 */ + +/** + * @brief This function is executed in case of error occurrence. + * @param None + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler */ + /* User can add his own implementation to report the HAL error return state */ + while(1) + { + } + /* USER CODE END Error_Handler */ +} + +#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****/ diff --git a/Src/ssd1306.c b/Src/ssd1306.c index d78ce71..74ad1c6 100644 --- a/Src/ssd1306.c +++ b/Src/ssd1306.c @@ -1,213 +1,246 @@ #include"ssd1306.h" // Databuffer voor het scherm -static uint8_t SSD1306_Buffer[SSD1306_WIDTH * SSD1306_HEIGHT / 8]; +//static uint8_t SSD1306_Buffer[SSD1306_WIDTH * SSD1306_HEIGHT / 8]; +static uint8_t SSD1306_Buffer[SSD1306_HEIGHT * SSD1306_WIDTH / 8]; // Een scherm-object om lokaal in te werken static SSD1306_t SSD1306; +void startscrollright(uint8_t start, uint8_t stop){ + ssd1306_WriteCommand(SSD1306_RIGHT_HORIZONTAL_SCROLL); + ssd1306_WriteCommand(0X00); + ssd1306_WriteCommand(start); + ssd1306_WriteCommand(0X00); + ssd1306_WriteCommand(stop); + ssd1306_WriteCommand(0X00); + ssd1306_WriteCommand(0XFF); + ssd1306_WriteCommand(SSD1306_ACTIVATE_SCROLL); +} + +void ssd1306_Scroll() +{ + static uint32_t end = SSD1306_WIDTH * 8; + uint8_t bit_antigo = 0; + + for(uint32_t addr = 0; addr < end; addr++) { + if (addr > 0x79) { + bit_antigo = SSD1306_Buffer[addr] & 0x01; + } + + SSD1306_Buffer[addr] >>= 1; + bit_antigo <<= 7; + SSD1306_Buffer[addr - SSD1306_WIDTH] |= bit_antigo; + } +} // -// Een byte sturen naar het commando register -// Kan niet gebruikt worden buiten deze file +// Een byte sturen naar het commando register +// Kan niet gebruikt worden buiten deze file // void ssd1306_WriteCommand(uint8_t command) { - HAL_I2C_Mem_Write(&hi2c1, SSD1306_I2C_ADDR, 0x00,1, &command, 1, 10); + HAL_I2C_Mem_Write(&hi2c1, SSD1306_I2C_ADDR, 0x00,1, &command, 1, 10); } // -// Het scherm initialiseren voor gebruik +// Het scherm initialiseren voor gebruik // uint8_t ssd1306_Init(void) -{ - // Even wachten zodat het scherm zeker opgestart is - HAL_Delay(100); - - /* Init LCD */ - ssd1306_WriteCommand(0xAE); //display off - ssd1306_WriteCommand(0x20); //Set Memory Addressing Mode - ssd1306_WriteCommand(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid - ssd1306_WriteCommand(0xB0); //Set Page Start Address for Page Addressing Mode,0-7 - ssd1306_WriteCommand(0xC8); //Set COM Output Scan Direction - ssd1306_WriteCommand(0x00); //---set low column address - ssd1306_WriteCommand(0x10); //---set high column address - ssd1306_WriteCommand(0x40); //--set start line address - ssd1306_WriteCommand(0x81); //--set contrast control register - ssd1306_WriteCommand(0xFF); - ssd1306_WriteCommand(0xA1); //--set segment re-map 0 to 127 - ssd1306_WriteCommand(0xA6); //--set normal display - ssd1306_WriteCommand(0xA8); //--set multiplex ratio(1 to 64) - ssd1306_WriteCommand(0x3F); // - ssd1306_WriteCommand(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content - ssd1306_WriteCommand(0xD3); //-set display offset - ssd1306_WriteCommand(0x00); //-not offset - ssd1306_WriteCommand(0xD5); //--set display clock divide ratio/oscillator frequency - ssd1306_WriteCommand(0xF0); //--set divide ratio - ssd1306_WriteCommand(0xD9); //--set pre-charge period - ssd1306_WriteCommand(0x22); // - ssd1306_WriteCommand(0xDA); //--set com pins hardware configuration - ssd1306_WriteCommand(0x12); - ssd1306_WriteCommand(0xDB); //--set vcomh - ssd1306_WriteCommand(0x20); //0x20,0.77xVcc - ssd1306_WriteCommand(0x8D); //--set DC-DC enable - ssd1306_WriteCommand(0x14); // - ssd1306_WriteCommand(0xAF); //--turn on SSD1306 panel - - /* Clearen scherm */ - ssd1306_Fill(Black); - - /* Update screen */ - ssd1306_UpdateScreen(); - - /* Set default values */ - SSD1306.CurrentX = 0; - SSD1306.CurrentY = 0; - - /* Initialized OK */ - SSD1306.Initialized = 1; - - /* Return OK */ - return 1; +{ + // Even wachten zodat het scherm zeker opgestart is + HAL_Delay(100); + + ssd1306_WriteCommand(SSD1306_DISPLAYOFF); // 0xAE + ssd1306_WriteCommand(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 + ssd1306_WriteCommand(0x80); // the suggested ratio 0x80 + + ssd1306_WriteCommand(SSD1306_SETMULTIPLEX); // 0xA8 + ssd1306_WriteCommand(SSD1306_HEIGHT - 1); + + ssd1306_WriteCommand(SSD1306_SETDISPLAYOFFSET); // 0xD3 + ssd1306_WriteCommand(0x0); // no offset + ssd1306_WriteCommand(SSD1306_SETSTARTLINE | 0x0); // line #0 + ssd1306_WriteCommand(SSD1306_CHARGEPUMP); // 0x8D + + ssd1306_WriteCommand(0x14); + + ssd1306_WriteCommand(SSD1306_MEMORYMODE); // 0x20 + ssd1306_WriteCommand(0x00); // 0x0 act like ks0108 + ssd1306_WriteCommand(SSD1306_SEGREMAP | 0x1); + ssd1306_WriteCommand(SSD1306_COMSCANDEC); + + ssd1306_WriteCommand(SSD1306_SETCOMPINS); // 0xDA + ssd1306_WriteCommand(0x12); + ssd1306_WriteCommand(SSD1306_SETCONTRAST); // 0x81 + ssd1306_WriteCommand(0xCF); + + ssd1306_WriteCommand(SSD1306_SETPRECHARGE); + ssd1306_WriteCommand(0xF1); + + ssd1306_WriteCommand(SSD1306_SETVCOMDETECT); // 0xDB + ssd1306_WriteCommand(0x40); + ssd1306_WriteCommand(SSD1306_DISPLAYALLON_RESUME); // 0xA4 + ssd1306_WriteCommand(SSD1306_NORMALDISPLAY); // 0xA6 + + ssd1306_WriteCommand(SSD1306_DEACTIVATE_SCROLL); + + ssd1306_WriteCommand(SSD1306_DISPLAYON);//--turn on oled panel + + /* Clearen scherm */ + ssd1306_Fill(Black); + + /* Update screen */ + ssd1306_UpdateScreen(); + + /* Set default values */ + SSD1306.CurrentX = 0; + SSD1306.CurrentY = 0; + + /* Initialized OK */ + SSD1306.Initialized = 1; + + /* Return OK */ + return 1; } // -// We zetten de hele buffer op een bepaalde kleur -// color => de kleur waarin alles moet +// We zetten de hele buffer op een bepaalde kleur +// color => de kleur waarin alles moet // void ssd1306_Fill(SSD1306_COLOR color) { - /* Set memory */ - uint32_t i; + /* Set memory */ + uint32_t i; - for(i = 0; i < sizeof(SSD1306_Buffer); i++) - { - SSD1306_Buffer[i] = (color == Black) ? 0x00 : 0xFF; - } + for(i = 0; i < sizeof(SSD1306_Buffer); i++) + { + SSD1306_Buffer[i] = (color == Black) ? 0x00 : 0xFF; + } } // -// Alle weizigingen in de buffer naar het scherm sturen +// Alle weizigingen in de buffer naar het scherm sturen // void ssd1306_UpdateScreen(void) { - uint8_t i; - - for (i = 0; i < 8; i++) { - ssd1306_WriteCommand(0xB0 + i); - ssd1306_WriteCommand(0x00); - ssd1306_WriteCommand(0x10); + uint8_t i; + + for (i = 0; i < 8; i++) { + ssd1306_WriteCommand(0xB0 + i); + ssd1306_WriteCommand(0x00); + ssd1306_WriteCommand(0x10); - // We schrijven alles map per map weg - HAL_I2C_Mem_Write(&hi2c1,SSD1306_I2C_ADDR,0x40,1,&SSD1306_Buffer[SSD1306_WIDTH * i],SSD1306_WIDTH,100); - } + // We schrijven alles map per map weg + HAL_I2C_Mem_Write(&hi2c1,SSD1306_I2C_ADDR,0x40,1,&SSD1306_Buffer[SSD1306_WIDTH * i],SSD1306_WIDTH,100); + } } // -// 1 pixel op het scherm tekenen -// X => X coordinaat -// Y => Y coordinaat -// color => kleur die pixel moet krijgen +// 1 pixel op het scherm tekenen +// X => X coordinaat +// Y => Y coordinaat +// color => kleur die pixel moet krijgen // void ssd1306_DrawPixel(uint8_t x, uint8_t y, SSD1306_COLOR color) { - if (x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT) - { - // We gaan niet buiten het scherm schrijven - return; - } - - // Kijken of de pixel geinverteerd moet worden - if (SSD1306.Inverted) - { - color = (SSD1306_COLOR)!color; - } - - // We zetten de juiste kleur voor de pixel - if (color == White) - { - SSD1306_Buffer[x + (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8); - } - else - { - SSD1306_Buffer[x + (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8)); - } + if (x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT) + { + // We gaan niet buiten het scherm schrijven + return; + } + + // Kijken of de pixel geinverteerd moet worden + if (SSD1306.Inverted) + { + color = (SSD1306_COLOR)!color; + } + + // We zetten de juiste kleur voor de pixel + if (color == White) + { + SSD1306_Buffer[x + (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8); + } + else + { + SSD1306_Buffer[x + (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8)); + } } // -// We willen 1 char naar het scherm sturen -// ch => char om weg te schrijven -// Font => Font waarmee we gaan schrijven -// color => Black or White +// We willen 1 char naar het scherm sturen +// ch => char om weg te schrijven +// Font => Font waarmee we gaan schrijven +// color => Black or White // char ssd1306_WriteChar(char ch, FontDef Font, SSD1306_COLOR color) { - uint32_t i, b, j; - - // Kijken of er nog plaats is op deze lijn - if (SSD1306_WIDTH <= (SSD1306.CurrentX + Font.FontWidth) || - SSD1306_HEIGHT <= (SSD1306.CurrentY + Font.FontHeight)) - { - // Er is geen plaats meer - return 0; - } - - // We gaan door het font - for (i = 0; i < Font.FontHeight; i++) - { - b = Font.data[(ch - 32) * Font.FontHeight + i]; - for (j = 0; j < Font.FontWidth; j++) - { - if ((b << j) & 0x8000) - { - ssd1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR) color); - } - else - { - ssd1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR)!color); - } - } - } - - // De huidige positie is nu verplaatst - SSD1306.CurrentX += Font.FontWidth; - - // We geven het geschreven char terug voor validatie - return ch; + uint32_t i, b, j; + + // Kijken of er nog plaats is op deze lijn + if (SSD1306_WIDTH <= (SSD1306.CurrentX + Font.FontWidth) || + SSD1306_HEIGHT <= (SSD1306.CurrentY + Font.FontHeight)) + { + // Er is geen plaats meer + return 0; + } + + // We gaan door het font + for (i = 0; i < Font.FontHeight; i++) + { + b = Font.data[(ch - 32) * Font.FontHeight + i]; + for (j = 0; j < Font.FontWidth; j++) + { + if ((b << j) & 0x8000) + { + ssd1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR) color); + } + else + { + ssd1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR)!color); + } + } + } + + // De huidige positie is nu verplaatst + SSD1306.CurrentX += Font.FontWidth; + + // We geven het geschreven char terug voor validatie + return ch; } // -// Functie voor het wegschrijven van een hele string -// str => string om op het scherm te schrijven -// Font => Het font dat gebruikt moet worden -// color => Black or White +// Functie voor het wegschrijven van een hele string +// str => string om op het scherm te schrijven +// Font => Het font dat gebruikt moet worden +// color => Black or White // char ssd1306_WriteString(char* str, FontDef Font, SSD1306_COLOR color) { - // We schrijven alle char tot een nulbyte - while (*str) - { - if (ssd1306_WriteChar(*str, Font, color) != *str) - { - // Het karakter is niet juist weggeschreven - return *str; - } - - // Volgende char - str++; - } - - // Alles gelukt, we sturen dus 0 terug - return *str; + // We schrijven alle char tot een nulbyte + while (*str) + { + if (ssd1306_WriteChar(*str, Font, color) != *str) + { + // Het karakter is niet juist weggeschreven + return *str; + } + + // Volgende char + str++; + } + + // Alles gelukt, we sturen dus 0 terug + return *str; } // -// Zet de cursor op een coordinaat +// Zet de cursor op een coordinaat // void ssd1306_SetCursor(uint8_t x, uint8_t y) { - /* Set write pointers */ - SSD1306.CurrentX = x; - SSD1306.CurrentY = y; + /* Set write pointers */ + SSD1306.CurrentX = x; + SSD1306.CurrentY = y; }