Accelerometer is a very usefull sensor for detecting movement. In this project I use simply settings LSM303D sensor and LCD PCD8544 display. In this settings I choose BluePill board – STM32F103C8T6 microprocessor
If you use this project in professional device, you must now, about gravity earth and acceleration of gravity, and must implement Kalman filter in software.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
/* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f1xx_hal.h" /* USER CODE BEGIN Includes */ #include "pcd8544.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; SPI_HandleTypeDef hspi2; TIM_HandleTypeDef htim2; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ uint8_t TimerFlag=0; #define LSM303_ADRESS 0x3a #define LSM303D_TEMP_OUT 0x05 #define LSM303D_STATUS_M 0x07 #define LSM303D_WHO_AM_I 0x0f #define LSM303D_CTRL0 0x1f #define LSM303D_CTRL1 0x20 #define LSM303D_CTRL2 0x21 #define LSM303D_CTRL3 0x22 #define LSM303D_CTRL4 0x23 #define LSM303D_CTRL5 0x24 #define LSM303D_CTRL6 0x25 #define LSM303D_CTRL7 0x26 #define LSM303D_STATUS 0x27 //adresy z magnetometru - X-oś x, H-starszy bajt, L-młodszy bajt M-magnetometr #define LSM303D_OUT_X_H_M 0x09 //X-axis magnetic data. The value is expressed in 16-bit as 2’s complement this is older value #define LSM303D_OUT_X_L_M 0x08 #define LSM303D_OUT_Y_H_M 0x0b #define LSM303D_OUT_Y_L_M 0x0a #define LSM303D_OUT_Z_H_M 0x0d #define LSM303D_OUT_Z_L_M 0x0c //termometr #define LSM303D_TEMP_OUT_H 0x06 #define LSM303D_TEMP_OUT_L 0x05 //adresy z akcelerometru - X-oś x, H-starszy bajt, L-młodszy bajt A-akcelerometr #define LSM303D_OUT_X_H_A 0x29 #define LSM303D_OUT_X_L_A 0x28 #define LSM303D_OUT_Y_H_A 0x2b #define LSM303D_OUT_Y_L_A 0x2a #define LSM303D_OUT_Z_H_A 0x2d #define LSM303D_OUT_Z_L_A 0x2c /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_SPI2_Init(void); static void MX_TIM2_Init(void); static void MX_I2C1_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ //obsługa przerwania void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ if(htim->Instance == TIM2){ // Jeżeliprzerwaniepochodziodtimera 10 //TimerFlag=1; //ustaw flage HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin); //zmień stan portu LED } } // wyslij na spi wartosc zmiennej d void spiWrite(uint8_t d){ HAL_SPI_Transmit(&hspi2, &d, 1, 1000); } // odczytaj wartosc rejestru z danego adresu uint8_t readRegister(uint16_t adress){ uint8_t out[1]; uint16_t output; HAL_I2C_Mem_Read(&hi2c1, LSM303_ADRESS, adress, 1, &out, 1, 100); return out[0]; } //zapisz do rejestru wartośc pod dany adres, dane pdata uint8_t writeRegister(uint16_t adress, uint8_t *pData){ HAL_I2C_Mem_Write(&hi2c1, LSM303_ADRESS, adress, 1, &pData, 1, 100); return 1; } /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ uint8_t xha=0,yha=0,zha=0; uint8_t xla=0,yla=0,zla=0; uint8_t xhm=0,yhm=0,zhm=0; uint8_t xlm=0,ylm=0,zlm=0; uint8_t th=0,tl=0; int16_t bufferxa=0, bufferya=0,bufferza=0; uint8_t dataxa[10]; uint8_t dataya[10]; uint8_t dataza[10]; int16_t bufferxm=0, bufferym=0,bufferzm=0; uint8_t dataxm[10]; uint8_t dataym[10]; uint8_t datazm[10]; int16_t buffert=0; uint8_t datat[15]; uint8_t who_am_i=0; /* 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_SPI2_Init(); MX_TIM2_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ PCD8544_init(); // zainicjuj LCD - konfiguracja PCD8544_backlight(1); // wlacz podswietlenie LCD PCD8544_clear(); // wyczysc wyswietlacz HAL_TIM_Base_Start_IT(&htim2); // wlacz przerwania timera2 who_am_i = readRegister(LSM303D_WHO_AM_I); // sprawdź ID czujnika - jak nie typowe, powie że brak czujnika if (who_am_i == 0x49){ // Jeśli typowa wartośc powie że czujnik OK PCD8544_displaytxt("Czujnik OK", 0, 6); // wyświetl na LCD ciag kolumna zero wiersz 0 } else { PCD8544_displaytxt("Brak czujnika", 0, 6); // wyświetl na LCD ciag kolumna zero wiersz 0 } //Konfiguracja czujnika writeRegister(LSM303D_CTRL5, 0xe0|0x0c); // włącz pomiar temperatury, 50Hz writeRegister(LSM303D_CTRL1, 0x10|0x07); // włącz osie xyz i odświeżaj co 3.12Hz writeRegister(LSM303D_CTRL7, 0x00|0x00); // włącz magnetometr HAL_Delay(1000); // czekaj sekunde HAL_Delay(1000); // czekaj sekunde PCD8544_clear(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { //AKCELEROMETR xha = readRegister(LSM303D_OUT_X_H_A); //pobierz z rejestru wartosc akcelerometru dla osi x bit starszy xla = readRegister(LSM303D_OUT_X_L_A); //pobierz z rejestru wartosc akcelerometru dla osi x bit młodszy yha = readRegister(LSM303D_OUT_Y_H_A); yla = readRegister(LSM303D_OUT_Y_L_A); zha = readRegister(LSM303D_OUT_Z_H_A); zla = readRegister(LSM303D_OUT_Z_L_A); bufferxa = (((uint16_t) xha)<<4) | (xla); //połącz bit starszy i mlodszy, rzutuj na 16 bit bufferya = (((uint16_t) yha)<<4) | (yla); //przez to że bufferxa to int a nie uint odzyskujemy bufferza = (((uint16_t) zha)<<4) | (zla); //znak + lub - wartości zapisanej w rejestrze snprintf(dataxa ,10,"X: %d ",bufferxa ); //zamien na ciag ascii i dodaj opisy snprintf(dataya ,10,"Y: %d ",bufferya ); snprintf(dataza ,10,"Z: %d ",bufferza ); PCD8544_displaytxt("Akcel.", 0, 0); PCD8544_displaytxt(dataxa, 0, 1); //wyświetl na LCD ciag kolumna zero wiersz 1 PCD8544_displaytxt(dataya, 0, 2); //wyświetl na LCD ciag kolumna zero wiersz 1 PCD8544_displaytxt(dataza, 0, 3); //wyświetl na LCD ciag kolumna zero wiersz 1 //MAGNETOMETR xhm = readRegister(LSM303D_OUT_X_H_M); //pobierz z rejestru wartosc magnetometru dla osi x bit starszy xlm = readRegister(LSM303D_OUT_X_L_M); //pobierz z rejestru wartosc magnetometru dla osi x bit młodszy yhm = readRegister(LSM303D_OUT_Y_H_M); ylm = readRegister(LSM303D_OUT_Y_L_M); zhm = readRegister(LSM303D_OUT_Z_H_M); zlm = readRegister(LSM303D_OUT_Z_L_M); bufferxm = (((uint16_t) xhm)<<4) | (xlm); bufferym = (((uint16_t) yhm)<<4) | (ylm); bufferzm = (((uint16_t) zhm)<<4) | (zlm); snprintf(dataxm ,10,"|%d ",bufferxm ); snprintf(dataym ,10,"|%d ",bufferym ); snprintf(datazm ,10,"|%d ",bufferzm ); PCD8544_displaytxt("Magnet.", 42, 0); PCD8544_displaytxt(dataxm, 42, 1); //wyświetl na LCD ciag kolumna zero wiersz 1 PCD8544_displaytxt(dataym, 42, 2); //wyświetl na LCD ciag kolumna zero wiersz 1 PCD8544_displaytxt(datazm, 42, 3); //wyświetl na LCD ciag kolumna zero wiersz 1 //TERMOMETR th = readRegister(LSM303D_TEMP_OUT_H); //pobierz temperature bit starszy tl = readRegister(LSM303D_TEMP_OUT_L); //pobierz temperature bit młodszy buffert = (((uint16_t) th)<<4) | (tl); snprintf(datat ,15,"Temp.: %d C ",buffert ); PCD8544_displaytxt(datat, 0, 5); //wyświetl na LCD ciag kolumna zero wiersz 1 HAL_Delay(400); //poczekaj 400ms - zeby sie łatwo odczytywało /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; /**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(__FILE__, __LINE__); } /**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(__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.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; 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(__FILE__, __LINE__); } } /* SPI2 init function */ static void MX_SPI2_Init(void) { /* SPI2 parameter configuration*/ hspi2.Instance = SPI2; hspi2.Init.Mode = SPI_MODE_MASTER; hspi2.Init.Direction = SPI_DIRECTION_2LINES; hspi2.Init.DataSize = SPI_DATASIZE_8BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLE; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi2.Init.CRCPolynomial = 10; if (HAL_SPI_Init(&hspi2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } /* TIM2 init function */ static void MX_TIM2_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_SlaveConfigTypeDef sSlaveConfig; TIM_MasterConfigTypeDef sMasterConfig; htim2.Instance = TIM2; htim2.Init.Prescaler = 500; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 16000; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER; sSlaveConfig.InputTrigger = TIM_TS_ITR2; if (HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != 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_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(PCD_LED_GPIO_Port, PCD_LED_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, PCD_SCE_Pin|PCD_DC_Pin|PCD_RST_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : LED_Pin */ GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : PCD_LED_Pin */ GPIO_InitStruct.Pin = PCD_LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(PCD_LED_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PCD_SCE_Pin PCD_DC_Pin PCD_RST_Pin */ GPIO_InitStruct.Pin = PCD_SCE_Pin|PCD_DC_Pin|PCD_RST_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __MAIN_H #define __MAIN_H /* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private define ------------------------------------------------------------*/ #define LED_Pin GPIO_PIN_13 #define LED_GPIO_Port GPIOC #define PCD_LED_Pin GPIO_PIN_12 #define PCD_LED_GPIO_Port GPIOB #define PCD_SCE_Pin GPIO_PIN_8 #define PCD_SCE_GPIO_Port GPIOA #define PCD_DC_Pin GPIO_PIN_9 #define PCD_DC_GPIO_Port GPIOA #define PCD_RST_Pin GPIO_PIN_10 #define PCD_RST_GPIO_Port GPIOA #define SCL_Pin GPIO_PIN_6 #define SCL_GPIO_Port GPIOB #define SDA_Pin GPIO_PIN_7 #define SDA_GPIO_Port GPIOB /* ########################## Assert Selection ############################## */ /** * @brief Uncomment the line below to expanse the "assert_param" macro in the * HAL drivers code */ /* #define USE_FULL_ASSERT 1U */ /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ #ifdef __cplusplus extern "C" { #endif void _Error_Handler(char *, int); #define Error_Handler() _Error_Handler(__FILE__, __LINE__) #ifdef __cplusplus } #endif /** * @} */ /** * @} */ #endif /* __MAIN_H */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
/* * PCD8544.c * * Created on: 13.10.2017 * Author: Tomasz Pachołek */ #include "pcd8544.h" #include "main.h" #include <stdint.h> //biblioteka zdefiniowanych typów np. uint8_t #include "stm32f4xx_hal.h" #include "stm32f4xx.h" #include "stm32f4xx_it.h" void PCD8544_init(){ uint8_t contrast=0x30; //MUST CHANGE- 0x30-0x50 maybe 0x40 uint8_t bias=0x04; HAL_GPIO_WritePin(RST_GPIO_Port,RST_Pin,GPIO_PIN_RESET); HAL_Delay(100); HAL_GPIO_WritePin(RST_GPIO_Port,RST_Pin,GPIO_PIN_SET); PCD8544_writecommand(PCD8544_FUNCTIONSET | PCD8544_EXTENDEDINSTRUCTION); PCD8544_writecommand(PCD8544_SETBIAS | bias); PCD8544_writecommand(PCD8544_SETVOP | contrast); PCD8544_writecommand(PCD8544_FUNCTIONSET); PCD8544_writecommand(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL); } void PCD8544_writecommand(uint8_t command){ HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_RESET); //komenda DC-RESET =0 HAL_GPIO_WritePin(SCE_GPIO_Port,SCE_Pin,GPIO_PIN_RESET); spiWrite(command); HAL_GPIO_WritePin(SCE_GPIO_Port,SCE_Pin,GPIO_PIN_SET); //HAL_Delay(10); } void PCD8544_writedata(uint8_t data){ HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_SET); //komenda DC-RESET =0 HAL_GPIO_WritePin(SCE_GPIO_Port,SCE_Pin,GPIO_PIN_RESET); spiWrite(data); HAL_GPIO_WritePin(SCE_GPIO_Port,SCE_Pin,GPIO_PIN_SET); //HAL_Delay(10); } void PCD8544_backlight(uint8_t value){ if (value) HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET); else HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET); } void PCD8544_reset(){ HAL_GPIO_WritePin(RST_GPIO_Port,RST_Pin,GPIO_PIN_RESET); HAL_Delay(500); HAL_GPIO_WritePin(RST_GPIO_Port,RST_Pin,GPIO_PIN_SET); } /* Wyświetla tekst - znaki 6x8 s - ciąg znaków zakończony zerem x - pierwsza kolumna 0..84(96) y - wiersz 0..5(7) */ void PCD8544_displaytxt(char s[], unsigned char x, unsigned char y) { unsigned int c,j; unsigned char i,k; unsigned char font[]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5f,0x00,0x00,0x00,0x00,0x07,0x00,0x07, 0x00,0x00,0x14,0x7f,0x14,0x7f,0x14,0x00,0x24,0x2a,0x7f,0x2a,0x12,0x00,0x23,0x13, 0x08,0x64,0x62,0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00,0x00,0x03,0x00,0x00,0x00, 0x00,0x1c,0x22,0x41,0x00,0x00,0x00,0x41,0x22,0x1c,0x00,0x00,0x14,0x08,0x3e,0x08, 0x14,0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00,0x50,0x30,0x00,0x00,0x00,0x08,0x08, 0x08,0x08,0x08,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x00, 0x3e,0x51,0x49,0x45,0x3e,0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x42,0x61,0x51,0x49, 0x46,0x00,0x21,0x41,0x45,0x4b,0x31,0x00,0x18,0x14,0x12,0x7f,0x10,0x00,0x27,0x45, 0x45,0x45,0x39,0x00,0x3c,0x4a,0x49,0x49,0x30,0x00,0x03,0x71,0x09,0x05,0x03,0x00, 0x36,0x49,0x49,0x49,0x36,0x00,0x06,0x49,0x49,0x29,0x1e,0x00,0x00,0x36,0x36,0x00, 0x00,0x00,0x00,0x56,0x36,0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x14,0x14, 0x14,0x14,0x14,0x00,0x00,0x41,0x22,0x14,0x08,0x00,0x02,0x01,0x51,0x09,0x06,0x00, 0x32,0x49,0x79,0x41,0x3e,0x00,0x7e,0x11,0x11,0x11,0x7e,0x00,0x7f,0x49,0x49,0x49, 0x36,0x00,0x3e,0x41,0x41,0x41,0x22,0x00,0x7f,0x41,0x41,0x41,0x3e,0x00,0x7f,0x49, 0x49,0x49,0x41,0x00,0x7f,0x09,0x09,0x09,0x01,0x00,0x3e,0x41,0x41,0x49,0x7a,0x00, 0x7f,0x08,0x08,0x08,0x7f,0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x20,0x40,0x41,0x3f, 0x01,0x00,0x7f,0x08,0x14,0x22,0x41,0x00,0x7f,0x40,0x40,0x40,0x40,0x00,0x7f,0x02, 0x0c,0x02,0x7f,0x00,0x7f,0x04,0x08,0x10,0x7f,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00, 0x7f,0x09,0x09,0x09,0x06,0x00,0x3e,0x41,0x51,0x21,0x5e,0x00,0x7f,0x09,0x19,0x29, 0x46,0x00,0x46,0x49,0x49,0x49,0x31,0x00,0x01,0x01,0x7f,0x01,0x01,0x00,0x3f,0x40, 0x40,0x40,0x3f,0x00,0x1f,0x20,0x40,0x20,0x1f,0x00,0x3f,0x40,0x38,0x40,0x3f,0x00, 0x63,0x14,0x08,0x14,0x63,0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x61,0x51,0x49,0x45, 0x43,0x00,0x00,0x7f,0x41,0x41,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00,0x41, 0x41,0x7f,0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x40,0x40,0x40,0x40,0x40,0x00, 0x00,0x01,0x02,0x04,0x00,0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x7f,0x48,0x44,0x44, 0x38,0x00,0x38,0x44,0x44,0x44,0x20,0x00,0x38,0x44,0x44,0x48,0x7f,0x00,0x38,0x54, 0x54,0x54,0x18,0x00,0x08,0x7e,0x09,0x01,0x02,0x00,0x18,0xa4,0xa4,0xa4,0x7c,0x00, 0x7f,0x08,0x04,0x04,0x78,0x00,0x00,0x48,0x7d,0x40,0x00,0x00,0x00,0x00,0x84,0x7d, 0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00,0x00,0x41,0x7f,0x40,0x00,0x00,0x7c,0x04, 0x78,0x04,0x78,0x00,0x7c,0x08,0x04,0x04,0x78,0x00,0x38,0x44,0x44,0x44,0x38,0x00, 0xfc,0x24,0x24,0x24,0x18,0x00,0x18,0x24,0x24,0x28,0xfc,0x00,0x7c,0x08,0x04,0x04, 0x08,0x00,0x48,0x54,0x54,0x54,0x20,0x00,0x04,0x3f,0x44,0x40,0x20,0x00,0x3c,0x40, 0x40,0x20,0x7c,0x00,0x1c,0x20,0x40,0x20,0x1c,0x00,0x3c,0x40,0x30,0x40,0x3c,0x00, 0x44,0x28,0x10,0x28,0x44,0x00,0x1c,0xa0,0xa0,0xa0,0x7c,0x00,0x44,0x64,0x54,0x4c, 0x44,0x00,0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x00,0x41, 0x36,0x08,0x00,0x00,0x10,0x08,0x08,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x20,0x54,0x54,0xd4,0x78,0x00,0x38,0x44,0x46,0x45,0x20,0x00,0x38,0x54,0x54,0xd4, 0x18,0x00,0x00,0x49,0x7f,0x44,0x00,0x00,0x7c,0x08,0x06,0x05,0x78,0x00,0x38,0x44, 0x46,0x45,0x38,0x00,0x48,0x54,0x56,0x55,0x20,0x00,0x44,0x64,0x56,0x4d,0x44,0x00, 0x44,0x64,0x55,0x4c,0x44,0x00,0x7e,0x11,0x11,0x91,0x7e,0x00,0x3c,0x42,0x46,0x43, 0x24,0x00,0x7f,0x49,0x49,0xc9,0x41,0x00,0x7f,0x50,0x48,0x44,0x40,0x00,0x7e,0x04, 0x0b,0x10,0x7e,0x00,0x3c,0x42,0x46,0x43,0x3c,0x00,0x4c,0x52,0x56,0x53,0x22,0x00, 0x42,0x66,0x53,0x4a,0x46,0x00,0x42,0x62,0x53,0x4a,0x46,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; // Kody polskich liter char pl[] = {'ą','ć','ę','ł','ń','ó','ś','ź','ż','Ą','Ć','Ę','Ł','Ń','Ó','Ś','Ź','Ż'}; // Ustawia położenia pierwszej litery tekstu na ekranie LCD PCD8544_writecommand(PCD8544_SETYADDR|(y)); PCD8544_writecommand(PCD8544_SETXADDR|(x)); // Rysuje znak po znaku for(k=0; (c = s[k]); k++) { // Dopasowuje kody znaków z ogonkami for(i=0; (i<18) && (pl[i]!=c); i++) ; if(i<18) c= 0x80+i; // Wyświetla jeden znak(6x8) for(i=0, j=(c-32)*6; i<6; i++,j++) PCD8544_writedata(font[j]); } } void PCD8544_clear(){ uint16_t i; PCD8544_writecommand(PCD8544_SETYADDR); PCD8544_writecommand(PCD8544_SETXADDR); for(i=0; i<LCDWIDTH*6; i++){ PCD8544_writedata(0x00); } PCD8544_writecommand(PCD8544_SETYADDR); PCD8544_writecommand(PCD8544_SETXADDR); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
/* * PCD8544.h * * Created on: 13.10.2017 * Author: Tomasz Pachołek */ #ifndef APPLICATION_USER_PCD8544_H_ #define APPLICATION_USER_PCD8544_H_ #include "stdint.h" //biblioteka zdefiniowanych typów np. uint8_t #include "main.h" #include "stm32f1xx_hal.h" //----------------------------DEFINITIONS------------------------------// #define BLACK 1 #define WHITE 0 #define LCDWIDTH 84 #define LCDHEIGHT 48 #define PCD8544_POWERDOWN 0x04 #define PCD8544_ENTRYMODE 0x02 #define PCD8544_EXTENDEDINSTRUCTION 0x01 #define PCD8544_DISPLAYBLANK 0x0 #define PCD8544_DISPLAYNORMAL 0x4 #define PCD8544_DISPLAYALLON 0x1 #define PCD8544_DISPLAYINVERTED 0x5 // H = 0 #define PCD8544_FUNCTIONSET 0x20 #define PCD8544_DISPLAYCONTROL 0x08 #define PCD8544_SETYADDR 0x40 #define PCD8544_SETXADDR 0x80 // H = 1 #define PCD8544_SETTEMP 0x04 #define PCD8544_SETBIAS 0x10 #define PCD8544_SETVOP 0x80 //----------------------------FUNCTIONS------------------------------// extern void PCD8544_init(); extern void PCD8544_writecommand(uint8_t command); extern void PCD8544_writedata(uint8_t data); extern void PCD8544_backlight(uint8_t value); extern void PCD8544_reset(); /* Wyświetla tekst - znaki 6x8 s - ciąg znaków zakończony zerem x - pierwsza kolumna 0..84(96) y - wiersz 0..5(7) */ extern void PCD8544_displaytxt(char s[], unsigned char x, unsigned char y); extern void PCD8544_clear(); extern void spiWrite(uint8_t d); extern void PCD8544_bar(char v, unsigned char x); #endif /* APPLICATION_USER_PCD8544_H_ */ |
Hello! Nice project!
Your website has exceptional content. I bookmarked the website