当前位置:   article > 正文

蓝桥杯嵌入式第七届真题(完成) STM32G431

蓝桥杯嵌入式第七届真题(完成) STM32G431

蓝桥杯嵌入式第七届真题(完成) STM32G431

题目

image-20240131201129279

image-20240131201143262

image-20240131201151854

image-20240131201200654

image-20240131201208206

相关文件

main.c
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; 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 "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "myadc.h"
#include "key.h"
#include "i2c_hal.h"
#include "stdio.h"
#include "led.h"
#include "usart2.h"
#include "string.h"
#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern uint32_t adctimes;
extern float k;//K? 
extern float adcval;
extern uint32_t height;
extern uint8_t level;
extern unsigned char min,med,max;
uint8_t view = 0;
uint8_t lcdtext[30];
extern struct Key key[4];
extern uint32_t led1times,led2times,led3times;
extern uint8_t led2flag,led3flag;
extern uint8_t led1status,led2status,led3status;
extern uint8_t OneData;
extern uint8_t rxflag;
uint8_t txtext[20];
/* 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 */
void lcd_process(void);
void led_process(void);
void adc_process(void);
void key_process(void);
void rx_process(void);
/* 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_ADC2_Init();
  MX_TIM2_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
		HAL_TIM_Base_Start_IT(&htim2);
		HAL_UART_Receive_IT(&huart1,&OneData,1);
    LCD_Init();
		I2CInit(); 
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

    LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		E2PROM_Write(0x00,min);
		HAL_Delay(5);
		E2PROM_Write(0x01,med);
		HAL_Delay(5);
		E2PROM_Write(0x02,max);	
		HAL_Delay(5);
		LED_display(0x00);
    while (1)
    {
			adc_process();
			key_process();
			lcd_process();
			led_process();
			rx_process();
    /* 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};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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 = RCC_PLLM_DIV2;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks
  */
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void adc_process(void)
{
    float newadcval;
    uint8_t newlevel;
    uint32_t newheight;
    int change;
    min = E2PROM_Read(0x00);
    med = E2PROM_Read(0x01);
    max = E2PROM_Read(0x02);

    if(adctimes >= 1000)
    {
        adctimes = 0;
        newadcval = get_Adc(&hadc2);
        newheight = k * newadcval;

        if(newheight <= min)
        {
            newlevel = 0;
        }
        else if(newheight <= med)
        {
            newlevel = 1;
        }
        else if(newheight <= max)
        {
            newlevel = 2;
        }
        else
        {
            newlevel = 3;
        }


        if(level != newlevel)
        {
            led2flag = 1; // 激活液位变化指示
            change = newheight - height;
            height = newheight;
            adcval = newadcval; // 更新adcval
						level = newlevel;
            if(change > 0)
            {
                sprintf((char *)txtext, "A:H%d+L%d+U\r\n", height, level);
                HAL_UART_Transmit(&huart1, (uint8_t *)txtext, strlen((char *)txtext), 50);
            }
            else if(change < 0)
            {
                sprintf((char *)txtext, "A:H%d+L%d+D\r\n", height, level);
                HAL_UART_Transmit(&huart1, (uint8_t *)txtext, strlen((char *)txtext), 50);
            }
        }
    }
}



void lcd_process(void)
{
	switch(view)
	{
		case 0:
		{
			sprintf((char *)lcdtext,"    Liquid Level");
			LCD_DisplayStringLine(Line0,lcdtext);
			sprintf((char *)lcdtext,"   Height:%d",height);
			LCD_DisplayStringLine(Line2,lcdtext);
			sprintf((char *)lcdtext,"   ADC:%.2f",adcval);
			LCD_DisplayStringLine(Line4,lcdtext);
			sprintf((char *)lcdtext,"   Level:%d",level);
			LCD_DisplayStringLine(Line6,lcdtext);
		}
		break;
		case 1:
		{
			sprintf((char *)lcdtext,"    Parameter Setup");
			LCD_DisplayStringLine(Line0,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 1:%d",min);
			LCD_DisplayStringLine(Line2,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 2:%d",med);
			LCD_DisplayStringLine(Line4,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 3:%d",max);
			LCD_DisplayStringLine(Line6,lcdtext);
		}
		break;
		case 2:
		{
			sprintf((char *)lcdtext,"    Parameter Setup");
			LCD_DisplayStringLine(Line0,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 1:%d",min);
			LCD_SetTextColor(Green);
			LCD_DisplayStringLine(Line2,lcdtext);
			LCD_SetTextColor(White);
			sprintf((char *)lcdtext,"   Threshold 2:%d",med);
			LCD_DisplayStringLine(Line4,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 3:%d",max);
			LCD_DisplayStringLine(Line6,lcdtext);
		}
		break;
		case 3:
		{
			sprintf((char *)lcdtext,"    Parameter Setup");
			LCD_DisplayStringLine(Line0,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 1:%d",min);
			LCD_DisplayStringLine(Line2,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 2:%d",med);
			LCD_SetTextColor(Green);
			LCD_DisplayStringLine(Line4,lcdtext);
			LCD_SetTextColor(White);
			sprintf((char *)lcdtext,"   Threshold 3:%d",max);
			LCD_DisplayStringLine(Line6,lcdtext);
		}
		break;
		case 4:
		{
			sprintf((char *)lcdtext,"    Parameter Setup");
			LCD_DisplayStringLine(Line0,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 1:%d",min);
			LCD_DisplayStringLine(Line2,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 2:%d",med);
			LCD_DisplayStringLine(Line4,lcdtext);
			sprintf((char *)lcdtext,"   Threshold 3:%d",max);
			LCD_SetTextColor(Green);
			LCD_DisplayStringLine(Line6,lcdtext);
			LCD_SetTextColor(White);
		}
		break;
	
	}
}

void key_process(void)
{
	if(key[0].key_flag==1&&(view==0))
	{
		key[0].key_flag=0;
		view=1;
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
	}
	if(key[0].key_flag==1&&(view==1||view==2||view==3||view==4))
	{
		key[0].key_flag=0;
		view=0;
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
	}
	if(key[1].key_flag==1&&(view==1||view==2||view==3||view==4))
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[1].key_flag=0;
		view++;
		if(view>4)
			view=2;
	}
	
	
	if(key[2].key_flag==1&&view==2)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[2].key_flag=0;
		min+=5;
		if(min>95)
			min=5;
		E2PROM_Write(0x00,min);
	}
	else if(key[2].key_flag==1&&view==3)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[2].key_flag=0;
		med+=5;
		if(med>95)
			med=5;
		E2PROM_Write(0x01,med);
	}
	else if(key[2].key_flag==1&&view==4)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[2].key_flag=0;
		max+=5;
		if(max>95)
			max=5;
		E2PROM_Write(0x02,max);
	}
	
	
	if(key[3].key_flag==1&&view==2)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[3].key_flag=0;
		min-=5;
		if(min<5)
			min=5;
		E2PROM_Write(0x00,min);
	}
	else if(key[3].key_flag==1&&view==3)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[3].key_flag=0;
		med-=5;
		if(med<5)
			med=5;
		E2PROM_Write(0x01,med);
	}
	else if(key[3].key_flag==1&&view==4)
	{
		LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
		key[3].key_flag=0;
		max-=5;
		if(max<5)
			max=5;
		E2PROM_Write(0x01,max);
	}
}

void led_process(void)
{
    // LD1处理
    if(led1times >= 1000) // 每1秒
    {
        led1times = 0;
        led1status = !led1status;
        LED_display(led1status ? 0x01 : 0x00);
    }
    
    // LD2处理
    static int led2count = 0;
    if(led2flag && led2times >= 200) // 每0.2秒
    {
        led2times = 0;
        led2status = !led2status;
        LED_display(led2status ? 0x02 : 0x00);

        if(led2status) // 只在LED从关闭状态变为开启状态时增加计数
        {
            led2count++;
        }

        if(led2count >= 5) // 闪烁5次后停止
        {
            led2flag = 0;
            led2count = 0;
        }
    }
    
    // LD3处理
    static int led3count = 0;
    if(led3flag && led3times >= 200) // 每0.2秒
    {
        led3times = 0;
        led3status = !led3status;
        LED_display(led3status ? 0x04 : 0x00);

        if(led3status) // 只在LED从关闭状态变为开启状态时增加计数
        {
            led3count++;
        }

        if(led3count >= 5) // 闪烁5次后停止
        {
            led3flag = 0;
            led3count = 0;
        }
    }
}

void rx_process(void)
{
	if(rxflag==1)
	{
		led3flag = 1;
		rxflag = 0;
		switch(OneData)
		{
			case 'C':
			{
				sprintf((char *)txtext,"C:H%d+L%d\r\n",height,level);
				HAL_UART_Transmit(&huart1,(uint8_t *)txtext,strlen((char *)txtext),50);
			}
			break;
			case 'S':
			{
				sprintf((char *)txtext,"S:TL%d+TM%d+TH%d\r\n",min,med,max);
				HAL_UART_Transmit(&huart1,(uint8_t *)txtext,strlen((char *)txtext),50);
			}
			break;
			default:
			{
				sprintf((char *)txtext,"Error!\r\n");
				HAL_UART_Transmit(&huart1,(uint8_t *)txtext,strlen((char *)txtext),50);
			}
			break;
		}
	}
}
/* 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****/

  • 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
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551

adc_process 中1s读取一次使用1ms进入一次SysTick_Handler 中断定时

newadcval、newlevel、newheight都是最新一次数据,与上一次数据比较是否液位发生变化

led_process中题目要求三个led按要求变化,led1要一直闪烁,led2、led3满足特定条件后才变化翻转所以使用两个标志位led2flag和led3flag同时使用静态变量led2count计数闪烁五次后关闭

image-20240131201548545

i2c_hal.c
/*
  程序说明: CT117E-M4嵌入式竞赛板GPIO模拟I2C总线驱动程序
  软件环境: MDK-ARM HAL库
  硬件环境: CT117E-M4嵌入式竞赛板
  日    期: 2020-3-1
*/

#include "i2c_hal.h"

#define DELAY_TIME	20

/**
  * @brief SDA线输入模式配置
  * @param None
  * @retval None
  */
void SDA_Input_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/**
  * @brief SDA线输出模式配置
  * @param None
  * @retval None
  */
void SDA_Output_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/**
  * @brief SDA线输出一个位
  * @param val 输出的数据
  * @retval None
  */
void SDA_Output( uint16_t val )
{
    if ( val )
    {
        GPIOB->BSRR |= GPIO_PIN_7;
    }
    else
    {
        GPIOB->BRR |= GPIO_PIN_7;
    }
}

/**
  * @brief SCL线输出一个位
  * @param val 输出的数据
  * @retval None
  */
void SCL_Output( uint16_t val )
{
    if ( val )
    {
        GPIOB->BSRR |= GPIO_PIN_6;
    }
    else
    {
        GPIOB->BRR |= GPIO_PIN_6;
    }
}

/**
  * @brief SDA输入一位
  * @param None
  * @retval GPIO读入一位
  */
uint8_t SDA_Input(void)
{
	if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET){
		return 1;
	}else{
		return 0;
	}
}


/**
  * @brief I2C的短暂延时
  * @param None
  * @retval None
  */
static void delay1(unsigned int n)
{
    uint32_t i;
    for ( i = 0; i < n; ++i);
}

/**
  * @brief I2C起始信号
  * @param None
  * @retval None
  */
void I2CStart(void)
{
    SDA_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);
}

/**
  * @brief I2C结束信号
  * @param None
  * @retval None
  */
void I2CStop(void)
{
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output(0);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SDA_Output(1);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C等待确认信号
  * @param None
  * @retval None
  */
unsigned char I2CWaitAck(void)
{
    unsigned short cErrTime = 5;
    SDA_Input_Mode();
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    while(SDA_Input())
    {
        cErrTime--;
        delay1(DELAY_TIME);
        if (0 == cErrTime)
        {
            SDA_Output_Mode();
            I2CStop();
            return ERROR;
        }
    }
    SDA_Output_Mode();
    SCL_Output(0);
    delay1(DELAY_TIME);
    return SUCCESS;
}

/**
  * @brief I2C发送确认信号
  * @param None
  * @retval None
  */
void I2CSendAck(void)
{
    SDA_Output(0);
    delay1(DELAY_TIME);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C发送非确认信号
  * @param None
  * @retval None
  */
void I2CSendNotAck(void)
{
    SDA_Output(1);
    delay1(DELAY_TIME);
    delay1(DELAY_TIME);
    SCL_Output(1);
    delay1(DELAY_TIME);
    SCL_Output(0);
    delay1(DELAY_TIME);

}

/**
  * @brief I2C发送一个字节
  * @param cSendByte 需要发送的字节
  * @retval None
  */
void I2CSendByte(unsigned char cSendByte)
{
    unsigned char  i = 8;
    while (i--)
    {
        SCL_Output(0);
        delay1(DELAY_TIME);
        SDA_Output(cSendByte & 0x80);
        delay1(DELAY_TIME);
        cSendByte += cSendByte;
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
}

/**
  * @brief I2C接收一个字节
  * @param None
  * @retval 接收到的字节
  */
unsigned char I2CReceiveByte(void)
{
    unsigned char i = 8;
    unsigned char cR_Byte = 0;
    SDA_Input_Mode();
    while (i--)
    {
        cR_Byte += cR_Byte;
        SCL_Output(0);
        delay1(DELAY_TIME);
        delay1(DELAY_TIME);
        SCL_Output(1);
        delay1(DELAY_TIME);
        cR_Byte |=  SDA_Input();
    }
    SCL_Output(0);
    delay1(DELAY_TIME);
    SDA_Output_Mode();
    return cR_Byte;
}

//
void I2CInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}


uint8_t E2PROM_Read(uint8_t addr)
{
	uint8_t val;
	I2CStart();
	I2CSendByte(0xA0);
	I2CWaitAck();
	I2CSendByte(addr);
	I2CWaitAck();
	//I2CStop();
	
	I2CStart();
	I2CSendByte(0xA1);
	I2CWaitAck();
	val = I2CReceiveByte();
	I2CWaitAck();
	I2CStop();
	
	return val;
}

void E2PROM_Write(uint8_t addr,uint8_t data)
{
	I2CStart();
	I2CSendByte(0xA0);
	I2CWaitAck();
	
	I2CSendByte(addr);
	I2CWaitAck();
	
	I2CSendByte(data);
	I2CWaitAck();
	I2CStop();
}

  • 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
key.c
#include "key.h"
struct Key key[4] = {0,0,0,0};

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM2)
	{
		key[0].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
		key[1].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
		key[2].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
		key[3].key_gpio = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
		for(int i = 0;i<=3;i++)
		{
			switch(key[i].key_status)
			{
				case 0:
				{
					if(key[i].key_gpio==0)
					{
						key[i].key_status = 1;
					}
				}
				break;
				case 1:
				{
					if(key[i].key_gpio==0)
					{
						key[i].key_status = 2;
						key[i].key_flag = 1;
					}
					else
					{
						key[i].key_status = 0;
					}
				}
				break;
				case 2:
				{
					if(key[i].key_gpio==1)
					{
						key[i].key_status = 0;
					}
				
				}
			}
		}
	}

}

  • 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
led.c
#include "led.h"

uint32_t led1times,led2times,led3times;
uint8_t led1status=0,led2status=0,led3status=0;
uint8_t led2flag= 0,led3flag=0;
void LED_display(uint8_t led)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOC,led<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

注意i2c再写数据时注意要间隔5ms以上要不然会数据异常

myadc.c
#include "myadc.h"

uint32_t adctimes = 0;
float k = 100/(3.3f);//Kֵ 
uint32_t height;
float adcval;
uint8_t level;
unsigned char min=30,med=50,max=70;
float get_Adc(ADC_HandleTypeDef *hadc){
   int val = 0.0f;
   for(int i = 0; i < 5; i++)
   {
      HAL_ADC_Start(hadc);
      HAL_ADC_PollForConversion(hadc, 100); // 等待转换完成
      val += HAL_ADC_GetValue(hadc);
      HAL_ADC_Stop(hadc); // 停止ADC转换
   }
   return val * 3.3f / 4096.0f / 5.0f; // 计算平均电压值
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
usart2.c
#include "usart2.h"

uint8_t OneData;
uint8_t rxflag;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance==USART1)
	{
		rxflag = 1;
		HAL_UART_Receive_IT(huart,&OneData,1);
	}
	
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号