Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2019 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"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- //#include "LCD.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- const uint16_t bb [] = {};
- #define LCD_RS_L HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET);
- #define LCD_RS_H HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET);
- #define LCD_CS_L HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
- #define LCD_CS_H HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET);
- #define LCD_RST_L HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
- #define LCD_RST_H HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
- #define LCD_BLK_L HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
- #define LCD_BLK_H HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
- void LCD_Init();
- void LCD_Fill(uint16_t color);
- void LCD_Line(uint16_t x, uint16_t y, uint16_t xy, uint16_t color, uint8_t orient);
- void LCD_Putchar_(uint16_t Xpos, uint16_t Ypos, char c, uint16_t T_color, uint16_t B_color, uint8_t *font);
- void LCD_PutString_(uint8_t x, uint8_t y, char *c, uint16_t T_color, uint16_t B_color, uint8_t space, uint8_t *font);
- void LCD_Image(uint8_t x, uint8_t y, uint16_t const *img, uint16_t w, uint16_t h);
- /* 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 ---------------------------------------------------------*/
- SPI_HandleTypeDef hspi1;
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_SPI1_Init(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_SPI1_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init();
- LCD_Fill(123);
- LCD_BLK_H;
- /* вывод изображения
- x, y - координаты
- img - массив изображения
- w - ширина
- h - высота
- */
- LCD_Image(0, 0, bb, 160, 80);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- uint16_t ret = 63488;
- LCD_Fill(ret);
- HAL_Delay(500);
- ret = 2016;
- LCD_Fill(ret);
- HAL_Delay(500);
- ret = 31;
- LCD_Fill(ret);
- 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};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 16;
- RCC_OscInitStruct.PLL.PLLN = 200;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 2;
- RCC_OscInitStruct.PLL.PLLR = 2;
- 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_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief SPI1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_SPI1_Init(void)
- {
- /* USER CODE BEGIN SPI1_Init 0 */
- /* USER CODE END SPI1_Init 0 */
- /* USER CODE BEGIN SPI1_Init 1 */
- /* USER CODE END SPI1_Init 1 */
- /* SPI1 parameter configuration*/
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_1LINE;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 10;
- if (HAL_SPI_Init(&hspi1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN SPI1_Init 2 */
- /* USER CODE END SPI1_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
- /*Configure GPIO pin : PA5 */
- GPIO_InitStruct.Pin = GPIO_PIN_5;
- 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);
- /*Configure GPIO pins : PC5 PC6 PC8 PC9 */
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- }
- /* 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****/
- uint8_t aTxBuffer[2]={0, 0};
- void Send_CMD(uint8_t dat){ //отправка команды
- LCD_RS_L;
- LCD_CS_L;
- aTxBuffer[0]=dat;
- HAL_SPI_Transmit (&hspi1, (uint8_t*)aTxBuffer, 1, 5000);
- LCD_CS_H;
- }
- void Send_DAT(uint8_t dat){ // отправка данных 8 бит
- LCD_RS_H;
- LCD_CS_L;
- aTxBuffer[0]=dat;
- HAL_SPI_Transmit (&hspi1, (uint8_t*)aTxBuffer, 1, 5000);
- LCD_CS_H;
- }
- void Send_DAT16(uint16_t dat){ // отправка данных 16 бит
- LCD_RS_H;
- LCD_CS_L;
- aTxBuffer[0] = (uint8_t)dat;
- aTxBuffer[1] = (uint8_t)(dat>>8);
- HAL_SPI_Transmit (&hspi1, (uint8_t*)aTxBuffer, 2, 5000);
- LCD_CS_H;
- }
- void LCD_Init(){ // инициализация LCD
- LCD_RST_L;
- HAL_Delay(10);
- LCD_RST_H;
- HAL_Delay(10);
- Send_CMD(0x01);
- HAL_Delay(12);
- Send_CMD(0x11);
- HAL_Delay(12);
- Send_CMD(0x3A);
- HAL_Delay(12);
- Send_DAT(5);
- Send_CMD(0x36);
- Send_DAT(0xA0);
- Send_CMD(0xB1);
- Send_DAT16(0x000F);
- Send_DAT16(0x000F);
- Send_DAT16(0x000F);
- Send_CMD(0x29);
- }
- // определение области экрана для заполнения
- void SetWindow(uint8_t startX, uint8_t startY, uint8_t stopX, uint8_t stopY) {
- Send_CMD(0x2A);
- Send_DAT(0x00);
- Send_DAT(startX);
- Send_DAT(0x00);
- Send_DAT(stopX);
- Send_CMD(0x2B);
- Send_DAT(0x00);
- Send_DAT(startY);
- Send_DAT(0x00);
- Send_DAT(stopY);
- }
- // установка адреса выводимого пикселя
- void LCD_SetCursorPosition_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
- {
- Send_CMD(0x2A);
- Send_DAT16(x1);
- Send_DAT16(x2);
- Send_CMD(0x2B);
- Send_DAT16(y1);
- Send_DAT16(y2);
- }
- // заливка дисплея цветом
- void LCD_Fill(uint16_t color){
- int i;
- SetWindow(0, 24, 160, 104);
- Send_CMD(0x2C);
- LCD_RS_H;
- LCD_CS_L;
- for(i = 0; i < 80*160; i++){
- Send_DAT16(color);
- }
- LCD_CS_H;
- }
- /* рисование линий
- x, y - координаты начала
- xy - длина
- oriеnt - ориентация: горизонтальная/вертикальная
- */
- void LCD_Line(uint16_t x, uint16_t y, uint16_t xy, uint16_t color, uint8_t orient)
- {
- uint16_t i;
- if(orient == 1){
- SetWindow(x, y+24, x, y+xy+24);
- Send_CMD(0x2C);
- for (i = 0; i <= (xy-y); i++){
- Send_DAT16(color);
- }
- }else{
- SetWindow(x, y, x+xy, y);
- Send_CMD(0x2C);
- for (i = 0; i <= (xy-x); i++){
- Send_DAT16(color);
- }
- }
- }
- /* вывод символа
- Xpos, Ypos - координаты
- с - символ
- T_color - цвет символа
- B_color - цвет фона за символом
- font - массив шрифта
- */
- void LCD_Putchar_(uint16_t Xpos, uint16_t Ypos, char c, uint16_t T_color, uint16_t B_color, uint8_t *font) {
- uint8_t j, data_w = 0, w = 0;
- uint16_t i, Yaddress = Ypos;
- uint8_t Data;
- data_w = font[0]/8;
- uint16_t offset = (c-font[2])*(font[1]*data_w) + 4;
- uint8_t width = font[0];
- SetWindow(Xpos, Yaddress+24, Xpos+width-1, Yaddress+font[1]+24);
- Send_CMD(0x2C);
- for(i = 0; i <= font[1]*data_w; i += data_w){
- for(w = 0; w < data_w; w++){
- Data = 0;
- Data = font[offset+i+w];
- for(j = 0; j < 8; j++){
- if(Data & 0x80){
- Send_DAT16(T_color);
- }else{
- Send_DAT16(B_color);
- }
- Data <<= 1;
- }
- }
- }
- }
- /* вывод сcnhjrb
- Xpos, Ypos - координаты
- с - строка
- T_color - цвет символа
- B_color - цвет фона за символом
- font - массив шрифта
- */
- void LCD_PutString_(uint8_t x, uint8_t y, char *c, uint16_t T_color, uint16_t B_color, uint8_t space, uint8_t *font){
- uint16_t X = x;
- while(*c != 0){
- LCD_Putchar_(X, y, *c++, T_color, B_color, font);
- X += font[0]+space;
- }
- }
- /* вывод изображения
- x, y - координаты
- img - массив изображения
- w - ширина
- h - высота
- */
- void LCD_Image(uint8_t x, uint8_t y, uint16_t const *img, uint16_t w, uint16_t h){
- uint16_t s = 0, dat, size;
- SetWindow(x, y+24, x+w-1, y+h+23);
- Send_CMD(0x2C);
- size = (w*h);
- while(s++ < size){
- Send_DAT16(img[s]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement