【旧文更新】基于STM32L496的低功耗ADXL345加速度读取、TMP75温度检测
文章目录
- 关于旧文新发
- 加速度和温度检测
- 附录:Cortex-M架构的SysTick系统定时器精准延时和MCU位带操作
- SysTick系统定时器精准延时
- 延时函数
- 阻塞延时
- 非阻塞延时
- 位带操作
- 位带代码
- 位带宏定义
- 总线函数
- 一、位带操作理论及实践
- 二、如何判断MCU的外设是否支持位带
- 附录:关于旧文新发
关于旧文新发
为何要进行旧文新发?
因为我在2023年博客之星评选中发现 有的人转载、抄袭他人文章 稍微改动几下也能作为高质量文章入选
所以我将把我的旧文重新发一次 然后也这样做
2023年博客之星规则:
加速度和温度检测
工程:
download.csdn.net/download/weixin_53403301/87435438
这两个芯片都用一个I2C口来进行通信 地址不同
分别是:
#define TMP75_Slave_Add 0x4F
#define ADXL345_Slave_Add 0x53
I2C采用模拟I2C
#include "stm32l496xx.h"
#include "main.h"
#include <stdint.h>
#include "DELAY.h"
#include "I2C.h"
void I2C_AN_SDA_OUT(void)
{
HAL_GPIO_DeInit(GPIOC,GPIO_PIN_0);
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void I2C_AN_SDA_IN(void)
{
HAL_GPIO_DeInit(GPIOC,GPIO_PIN_0);
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void I2C_AN_Start(void)
{
I2C_AN_SDA_OUT();
Set_I2C_SDA;
Set_I2C_SCL;
delay_us(4);
Reset_I2C_SDA;
delay_us(4);
Reset_I2C_SCL;
}
void I2C_AN_Stop(void)
{
I2C_AN_SDA_OUT();
Reset_I2C_SCL;
Reset_I2C_SDA;
delay_us(4);
Set_I2C_SCL;
Set_I2C_SDA;
delay_us(4);
}
uint8_t I2C_AN_Wait_Ack(void)
{
uint8_t ucErrTime=0;
Set_I2C_SDA;
I2C_AN_SDA_IN();
delay_us(1);
Set_I2C_SCL;
delay_us(1);
while(I2C_READ_SDA)
{
ucErrTime++;
delay_us(1);
if(ucErrTime>250)
{
I2C_AN_Stop();
return 1;
}
}
Reset_I2C_SCL;
return 0;
}
void I2C_AN_Ack(void)
{
Reset_I2C_SCL;
I2C_AN_SDA_OUT();
Reset_I2C_SDA;
delay_us(2);
Set_I2C_SCL;
delay_us(2);
Reset_I2C_SCL;
}
void I2C_AN_No_Ack(void)
{
Reset_I2C_SCL;
I2C_AN_SDA_OUT();
Set_I2C_SDA;
delay_us(2);
Set_I2C_SCL;
delay_us(2);
Reset_I2C_SCL;
}
void I2C_AN_Send_Byte(uint8_t txd)
{
uint8_t t;
I2C_AN_SDA_OUT();
Reset_I2C_SCL;//数据开始传输的时候需要拉低时钟线
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
{
Set_I2C_SDA;
}
else
{
Reset_I2C_SDA;
}
txd=txd<<1;
delay_us(2); // 不同的器件有不同的要求,根据要求进行配置
Set_I2C_SCL;
delay_us(2);
Reset_I2C_SCL;
delay_us(2);
}
}
uint8_t I2C_AN_Read_Byte(void)
{
unsigned char i,receive=0;
I2C_AN_SDA_IN();
for(i=0;i<8;i++ )
{
Reset_I2C_SCL;
delay_us(2);
Set_I2C_SCL;
receive<<=1;
if(I2C_READ_SDA) receive|=0x01;
delay_us(1);
}
return receive;
}
uint8_t I2C_AN_Slave_Ack(uint8_t slave_add)
{
uint8_t dat=0;
I2C_AN_Start();
I2C_AN_Send_Byte(slave_add);
dat=I2C_AN_Wait_Ack();
I2C_AN_Stop();
return dat;
}
void WriteOneByte(uint8_t DeviceAdd,uint8_t PointAddr,uint8_t DataToWrite)
{
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd);
I2C_AN_Wait_Ack();
I2C_AN_Send_Byte(PointAddr);
I2C_AN_Wait_Ack();
Set_I2C_SDA;
I2C_AN_Send_Byte(DataToWrite);
I2C_AN_Wait_Ack();
I2C_AN_Stop();
delay_ms(30);
}
void WriteOneWord(uint8_t DeviceAdd,uint8_t PointAddr,uint16_t DataToWrite)
{
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd);
I2C_AN_Wait_Ack();
I2C_AN_Send_Byte(PointAddr);
I2C_AN_Wait_Ack();
Set_I2C_SDA;
I2C_AN_Send_Byte((DataToWrite>>8)&0xff);
I2C_AN_Wait_Ack();
I2C_AN_Send_Byte(DataToWrite&0xff);
I2C_AN_Wait_Ack();
I2C_AN_Stop();
delay_ms(30);
}
uint8_t ReadOneByte(uint8_t DeviceAdd, uint8_t PointAddr)
{
uint8_t temp=0;
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd);
I2C_AN_Wait_Ack();
I2C_AN_Send_Byte(PointAddr);
I2C_AN_Wait_Ack();
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd|0x01);
I2C_AN_Wait_Ack();
temp=I2C_AN_Read_Byte();
I2C_AN_Stop();
return temp;
}
uint16_t ReadOneWord(uint8_t DeviceAdd, uint8_t PointAddr)
{
uint8_t tempH=0;
uint8_t tempL=0;
uint16_t temp=0;
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd);
I2C_AN_Wait_Ack();
I2C_AN_Send_Byte(PointAddr);
I2C_AN_Wait_Ack();
I2C_AN_Start();
I2C_AN_Send_Byte(DeviceAdd|0x01);
I2C_AN_Wait_Ack();
tempH=I2C_AN_Read_Byte();
I2C_AN_Ack();
Set_I2C_SDA;
tempL=I2C_AN_Read_Byte();
I2C_AN_No_Ack();
I2C_AN_Stop();
temp = (tempH << 8) | tempL ;
return temp;
}
对于ADXL345:
要将ADXL345_POWER_CTL寄存器写入0x08打开测量功能
ADXL345_DATA_FORMAT寄存器采用默认的十位数据 ±2g
十位精度就是1024
±2g也就是4g
4/1024=0.00390625g
其他寄存器可以不配置
读取时 读取X Y Z值
转成有符号整型再*单位即可
void Init_ADXL345(void)
{
uint8_t dat=0;
// dat = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DEVID);
// printf("[INFO] ADXL345_DEVID: %x\n",dat);
WriteOneByte((ADXL345_Slave_Add<<1),ADXL345_POWER_CTL,0x08);
WriteOneByte((ADXL345_Slave_Add<<1),ADXL345_DATA_FORMAT,0x00);
}
void Count_ADXL345(void)
{
uint8_t dat_H=0;
uint8_t dat_L=0;
uint16_t dat=0;
float x=0.0f;
float y=0.0f;
float z=0.0f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAX0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAX1);
dat = (dat_H<<8)|dat_L;
x=((int16_t)dat)*0.00390625f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAY0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAY1);
dat = (dat_H<<8)|dat_L;
y=((int16_t)dat)*0.00390625f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAZ0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAZ1);
dat = (dat_H<<8)|dat_L;
z=((int16_t)dat)*0.00390625f;
printf("[INFO] x: %0.4f y: %0.4f z: %0.4f\n",x,y,z);
}
对于TMP75
直接读温度寄存器
0x00
读出来的数据右移4位 后计算
uint16_t Read_TMP75(void)
{
uint16_t tmp=0;
// tmp=I2C0_Send_add_Read_2(TMP75_Slave_Add,0x00);
// tmp=tmp>>4;
tmp = ReadOneWord(((TMP75_Slave_Add<<1)|0x01),0x00);
tmp = tmp>>4;
return tmp;
}
float Count_TMP75(void)
{
float real_tmp =0;
uint16_t tmp = Read_TMP75();
if (tmp<0x800)
{
real_tmp = tmp*0.0625;
}
else if (tmp>=0x800 && tmp <0x1000)
{
tmp = 0x1000 - tmp;
real_tmp = -(tmp*0.0625);
}
else
{
real_tmp = -273.15;
}
printf("[INFO] TMP75_Temp: %0.4f\n",real_tmp);
return real_tmp;
}
main.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "DELAY.h"
#include "GPIO.h"
#include "I2C.h"
#include "ADXL345_CMD.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TMP75_Slave_Add 0x4F
#define ADXL345_Slave_Add 0x53
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#pragma import(__use_no_semihosting_swi)
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;
UART_HandleTypeDef huart4;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
uint8_t RxBuffer=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_UART4_Init(void);
/* USER CODE BEGIN PFP */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void _sys_exit(int x)
{
x = x;
}
void _ttywrch(int ch)
{
ch = ch;
}
//串口打印配置函数 printf
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);
HAL_UART_Transmit(&huart4,(uint8_t *)&ch,1,0xFFFF);
return ch;
}
//串口接收函数 scanf
int fgetc(FILE *f)
{
int ch = 0;
HAL_UART_AbortReceive_IT(&huart2);
HAL_UART_Receive(&huart2,(uint8_t *)&ch,1,0xFFFF);
HAL_UART_Receive_IT(&huart2,&RxBuffer,1);
return ch;
}
/*!
* @brief 进入低功耗模式
*
* @param [in] mode_flag: 模式标志
* 0/大于4 不进入任何模式,1 进入睡眠,2 进入停止,3 进入待机,4 关机
* [in] WakeUpPinPolarity: 待机模式下WKUP唤醒引脚极性配置,其他模式无用
*
* @return None
*/
void Enter_Low_PWR(uint8_t mode_flag,uint32_t WakeUpPinPolarity)
{
switch(mode_flag)
{
case 0:
{
printf("[INFO] 不进入低功耗模式\n");
break;
}
case 1:
{
printf("[INFO] 进入睡眠模式\n");
delay_ms(10); //消抖
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI);
break;
}
case 2:
{
printf("[INFO] 进入停止模式\n");
delay_ms(10); //消抖
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON,PWR_SLEEPENTRY_WFI);
break;
}
case 3:
{
printf("[INFO] 三秒后进入待机模式\n");
delay_ms(3000);
printf("[INFO] 进入待机模式\n");
HAL_PWR_EnableWakeUpPin(WakeUpPinPolarity);
delay_ms(10); //消抖
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTANDBYMode();
break;
}
case 4:
{
printf("[INFO] 三秒后进入关机模式\n");
delay_ms(3000);
printf("[INFO] 进入关机模式\n");
HAL_PWR_EnableWakeUpPin(WakeUpPinPolarity);
delay_ms(10); //消抖
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWREx_EnterSHUTDOWNMode();
break;
}
default:
{
printf("[INFO] 不进入低功耗模式\n");
break;
}
}
}
uint16_t Read_TMP75(void)
{
uint16_t tmp=0;
// tmp=I2C0_Send_add_Read_2(TMP75_Slave_Add,0x00);
// tmp=tmp>>4;
tmp = ReadOneWord(((TMP75_Slave_Add<<1)|0x01),0x00);
tmp = tmp>>4;
return tmp;
}
float Count_TMP75(void)
{
float real_tmp =0;
uint16_t tmp = Read_TMP75();
if (tmp<0x800)
{
real_tmp = tmp*0.0625;
}
else if (tmp>=0x800 && tmp <0x1000)
{
tmp = 0x1000 - tmp;
real_tmp = -(tmp*0.0625);
}
else
{
real_tmp = -273.15;
}
printf("[INFO] TMP75_Temp: %0.4f\n",real_tmp);
return real_tmp;
}
void Init_ADXL345(void)
{
uint8_t dat=0;
// dat = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DEVID);
// printf("[INFO] ADXL345_DEVID: %x\n",dat);
WriteOneByte((ADXL345_Slave_Add<<1),ADXL345_POWER_CTL,0x08);
WriteOneByte((ADXL345_Slave_Add<<1),ADXL345_DATA_FORMAT,0x00);
}
void Count_ADXL345(void)
{
uint8_t dat_H=0;
uint8_t dat_L=0;
uint16_t dat=0;
float x=0.0f;
float y=0.0f;
float z=0.0f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAX0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAX1);
dat = (dat_H<<8)|dat_L;
x=((int16_t)dat)*0.00390625f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAY0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAY1);
dat = (dat_H<<8)|dat_L;
y=((int16_t)dat)*0.00390625f;
dat_L = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAZ0);
dat_H = ReadOneByte((ADXL345_Slave_Add<<1),ADXL345_DATAZ1);
dat = (dat_H<<8)|dat_L;
z=((int16_t)dat)*0.00390625f;
printf("[INFO] x: %0.4f y: %0.4f z: %0.4f\n",x,y,z);
}
/* 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_RTC_Init();
MX_USART2_UART_Init();
MX_UART4_Init();
/* USER CODE BEGIN 2 */
delay_ms(10);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_SET);
printf("123\n");
Init_ADXL345();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
Count_ADXL345();
Count_TMP75();
delay_ms(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
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 2;
RCC_OscInitStruct.PLL.PLLN = 8;
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_3) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief RTC Initialization Function
* @param None
* @retval None
*/
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
/**
* @brief UART4 Initialization Function
* @param None
* @retval None
*/
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
HAL_UART_Receive_IT(&huart2,&RxBuffer,1);
/* USER CODE END USART2_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_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8, GPIO_PIN_SET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
/*Configure GPIO pins : PC13 PC2 PC3 PC4
PC5 PC6 PC7 PC9
PC12 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_9
|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PC0 PC1 PC8 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PA0 PA1 PA5 PA6
PA7 PA8 PA9 PA10
PA11 PA12 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_5|GPIO_PIN_6
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA4 */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 PB10
PB11 PB12 PB13 PB14
PB15 PB3 PB4 PB5
PB6 PB7 PB8 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : PD2 */
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pin : PH3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart==&huart2)
{
HAL_UART_Transmit(&huart2,&RxBuffer,1,0xFFFF);
HAL_UART_Receive_IT(&huart2,&RxBuffer,1);
}
if(huart==&huart4)
{
HAL_UART_Transmit(&huart4,&RxBuffer,1,0xFFFF);
HAL_UART_Receive_IT(&huart4,&RxBuffer,1);
}
}
/* 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 */
__disable_irq();
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 /* USE_FULL_ASSERT */
I2C采用的是模拟I2C 所以要对时序 如果工作频率不一样 则需要自行更改延时
附录:Cortex-M架构的SysTick系统定时器精准延时和MCU位带操作
SysTick系统定时器精准延时
延时函数
SysTick->LOAD中的值为计数值
计算方法为工作频率值/分频值
比如工作频率/1000 则周期为1ms
以ADuCM4050为例:
#include "ADuCM4050.h"
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 26000000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能52MHz的系统定时器
while(ms--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 26000000/1000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能52MHz的系统定时器
while(us--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
其中的52000000表示芯片的系统定时器频率 32系列一般为外部定时器频率的两倍
Cortex-M架构SysTick系统定时器阻塞和非阻塞延时
阻塞延时
首先是最常用的阻塞延时
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while(ms--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while(us--)
{
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
}
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
50000000表示工作频率
分频后即可得到不同的延时时间
以此类推
那么 不用两个嵌套while循环 也可以写成:
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
但是这种写法有个弊端
那就是输入ms后,最大定时不得超过计数值,也就是不能超过LOAD的最大值,否则溢出以后,则无法正常工作
而LOAD如果最大是32位 也就是4294967295
晶振为50M的话 50M的计数值为1s 4294967295计数值约为85s
固最大定时时间为85s
但用嵌套while的话 最大可以支持定时4294967295*85s
非阻塞延时
如果采用非阻塞的话 直接改写第二种方法就好了:
void delay_ms(unsigned int ms)
{
SysTick->LOAD = 50000000/1000*ms-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
void delay_us(unsigned int us)
{
SysTick->LOAD = 50000000/1000/1000*us-1; // Count from 255 to 0 (256 cycles) 载入计数值 定时器从这个值开始计数
SysTick->VAL = 0; // Clear current value as well as count flag 清空计数值到达0后的标记
SysTick->CTRL = 5; // Enable SysTick timer with processor clock 使能26MHz的系统定时器
//while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set 等待
//SysTick->CTRL = 0; // Disable SysTick 关闭系统定时器
}
将等待和关闭定时器语句去掉
在使用时加上判断即可变为阻塞:
delay_ms(500);
while ((SysTick->CTRL & 0x00010000)==0);
SysTick->CTRL = 0;
在非阻塞状态下 可以提交定时器后 去做别的事情 然后再来等待
不过这样又有一个弊端 那就是定时器会自动重载 可能做别的事情以后 定时器跑过了 然后就要等85s才能停下
故可以通过内部定时器来进行非阻塞延时函数的编写
基本上每个mcu的内部定时器都可以配置自动重载等功能 网上资料很多 这里就不再阐述了
位带操作
位带代码
M3、M4架构的单片机 其输出口地址为端口地址+20 输入为+16
M0架构的单片机 其输出口地址为端口地址+12 输入为+8
以ADuCM4050为列:
位带宏定义
#ifndef __GPIO_H__
#define __GPIO_H__
#include "ADuCM4050.h"
#include "adi_gpio.h"
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIO0_ODR_Addr (ADI_GPIO0_BASE+20) //0x40020014
#define GPIO0_IDR_Addr (ADI_GPIO0_BASE+16) //0x40020010
#define GPIO1_ODR_Addr (ADI_GPIO1_BASE+20) //0x40020054
#define GPIO1_IDR_Addr (ADI_GPIO1_BASE+16) //0x40020050
#define GPIO2_ODR_Addr (ADI_GPIO2_BASE+20) //0x40020094
#define GPIO2_IDR_Addr (ADI_GPIO2_BASE+16) //0x40020090
#define GPIO3_ODR_Addr (ADI_GPIO3_BASE+20) //0x400200D4
#define GPIO3_IDR_Addr (ADI_GPIO3_BASE+16) //0x400200D0
#define P0_O(n) BIT_ADDR(GPIO0_ODR_Addr,n) //输出
#define P0_I(n) BIT_ADDR(GPIO0_IDR_Addr,n) //输入
#define P1_O(n) BIT_ADDR(GPIO1_ODR_Addr,n) //输出
#define P1_I(n) BIT_ADDR(GPIO1_IDR_Addr,n) //输入
#define P2_O(n) BIT_ADDR(GPIO2_ODR_Addr,n) //输出
#define P2_I(n) BIT_ADDR(GPIO2_IDR_Addr,n) //输入
#define P3_O(n) BIT_ADDR(GPIO3_ODR_Addr,n) //输出
#define P3_I(n) BIT_ADDR(GPIO3_IDR_Addr,n) //输入
#define Port0 (ADI_GPIO_PORT0)
#define Port1 (ADI_GPIO_PORT1)
#define Port2 (ADI_GPIO_PORT2)
#define Port3 (ADI_GPIO_PORT3)
#define Pin0 (ADI_GPIO_PIN_0)
#define Pin1 (ADI_GPIO_PIN_1)
#define Pin2 (ADI_GPIO_PIN_2)
#define Pin3 (ADI_GPIO_PIN_3)
#define Pin4 (ADI_GPIO_PIN_4)
#define Pin5 (ADI_GPIO_PIN_5)
#define Pin6 (ADI_GPIO_PIN_6)
#define Pin7 (ADI_GPIO_PIN_7)
#define Pin8 (ADI_GPIO_PIN_8)
#define Pin9 (ADI_GPIO_PIN_9)
#define Pin10 (ADI_GPIO_PIN_10)
#define Pin11 (ADI_GPIO_PIN_11)
#define Pin12 (ADI_GPIO_PIN_12)
#define Pin13 (ADI_GPIO_PIN_13)
#define Pin14 (ADI_GPIO_PIN_14)
#define Pin15 (ADI_GPIO_PIN_15)
void GPIO_OUT(unsigned int port,unsigned int pin,unsigned int flag);
void GPIO_BUS_OUT(unsigned int port,unsigned int num);
void P0_BUS_O(unsigned int num);
unsigned int P0_BUS_I(void);
void P1_BUS_O(unsigned int num);
unsigned int P1_BUS_I(void);
void P2_BUS_O(unsigned int num);
unsigned int P2_BUS_I(void);
void P3_BUS_O(unsigned int num);
unsigned int P3_BUS_I(void);
#endif
总线函数
#include "ADuCM4050.h"
#include "adi_gpio.h"
#include "GPIO.h"
void GPIO_OUT(unsigned int port,unsigned int pin,unsigned int flag)
{
switch(port)
{
case 0:{
switch(pin)
{
case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_0));};break;
case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_1));};break;
case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_2));};break;
case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_3));};break;
case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_4));};break;
case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_5));};break;
case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_6));};break;
case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_7));};break;
case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_8));};break;
case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_9));};break;
case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_10));};break;
case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_11));};break;
case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_12));};break;
case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_13));};break;
case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_14));};break;
case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT0),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT0),(ADI_GPIO_PIN_15));};break;
default:pin=0;break;
}
}break;
case 1:{
switch(pin)
{
case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_0));};break;
case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_1));};break;
case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_2));};break;
case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_3));};break;
case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_4));};break;
case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_5));};break;
case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_6));};break;
case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_7));};break;
case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_8));};break;
case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_9));};break;
case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_10));};break;
case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_11));};break;
case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_12));};break;
case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_13));};break;
case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_14));};break;
case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT1),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT1),(ADI_GPIO_PIN_15));};break;
default:pin=0;break;
}
}break;
case 2:{
switch(pin)
{
case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_0));};break;
case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_1));};break;
case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_2));};break;
case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_3));};break;
case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_4));};break;
case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_5));};break;
case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_6));};break;
case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_7));};break;
case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_8));};break;
case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_9));};break;
case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_10));};break;
case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_11));};break;
case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_12));};break;
case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_13));};break;
case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_14));};break;
case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT2),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT2),(ADI_GPIO_PIN_15));};break;
default:pin=0;break;
}
}break;
case 3:{
switch(pin)
{
case 0:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_0));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_0));};break;
case 1:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_1));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_1));};break;
case 2:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_2));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_2));};break;
case 3:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_3));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_3));};break;
case 4:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_4));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_4));};break;
case 5:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_5));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_5));};break;
case 6:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_6));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_6));};break;
case 7:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_7));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_7));};break;
case 8:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_8));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_8));};break;
case 9:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_9));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_9));};break;
case 10:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_10));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_10));};break;
case 11:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_11));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_11));};break;
case 12:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_12));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_12));};break;
case 13:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_13));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_13));};break;
case 14:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_14));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_14));};break;
case 15:if(flag==1){adi_gpio_SetHigh((ADI_GPIO_PORT3),(ADI_GPIO_PIN_15));}else{adi_gpio_SetLow((ADI_GPIO_PORT3),(ADI_GPIO_PIN_15));};break;
default:pin=0;break;
}
}break;
default:port=0;break;
}
}
void GPIO_BUS_OUT(unsigned int port,unsigned int num) //num最大为0xffff
{
int i;
for(i=0;i<16;i++)
{
GPIO_OUT(port,i,(num>>i)&0x0001);
}
}
void P0_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
P0_O(i)=(num>>i)&0x0001;
}
}
unsigned int P0_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(P0_I(i)<<i)&0xFFFF;
}
return num;
}
void P1_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
P1_O(i)=(num>>i)&0x0001;
}
}
unsigned int P1_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(P1_I(i)<<i)&0xFFFF;
}
return num;
}
void P2_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
P2_O(i)=(num>>i)&0x0001;
}
}
unsigned int P2_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(P2_I(i)<<i)&0xFFFF;
}
return num;
}
void P3_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
P3_O(i)=(num>>i)&0x0001;
}
}
unsigned int P3_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(P3_I(i)<<i)&0xFFFF;
}
return num;
}
一、位带操作理论及实践
位带操作的概念其实30年前就有了,那还是 CM3 将此能力进化,这里的位带操作是 8051 位寻址区的威力大幅加强版
位带区: 支持位带操作的地址区
位带别名: 对别名地址的访问最终作 用到位带区的访问上(注意:这中途有一个 地址映射过程)
位带操作对于硬件 I/O 密集型的底层程序最有用处
支持了位带操作后,可以使用普通的加载/存储指令来对单一的比特进行读写。在CM4中,有两个区中实现了位带。其中一个是SRAM区的最低1MB范围,第二个则是片内外设区的最低1MB范围。这两个区中的地址除了可以像普通的RAM一样使用外,它们还都有自己的“位带别名区”,位带别名区把每个比特膨胀成一个32位的字。当你通过位带别名区访问这些字时,就可以达到访问原始比特的目的。
位操作就是可以单独的对一个比特位读和写,类似与51中sbit定义的变量,stm32中通过访问位带别名区来实现位操作的功能
STM32中有两个地方实现了位带,一个是SRAM,一个是片上外设。
(1)位带本质上是一块地址区(例如每一位地址位对应一个寄存器)映射到另一片地址区(实现每一位地址位对应一个寄存器中的一位),该区域就叫做位带别名区,将每一位膨胀成一个32位的字。
(2)位带区的4个字节对应实际寄存器或内存区的一个位,虽然变大到4个字节,但实际上只有最低位有效(代表0或1)
只有位带可以直接用=赋值的方式来操作寄存器 位带是把寄存器上的每一位 膨胀到32位 映射到位带区 比如0x4002 0000地址的第0个bit 映射到位带区的0地址 那么其对应的位带映射地址为0x00 - 0x04 一共32位 但只有LSB有效 采用位带的方式用=赋值时 就是把位带区对应的LSB赋值 然后MCU再转到寄存器对应的位里面 寄存器操作时 如果不改变其他位上面的值 那就只能通过&=或者|=的方式进行
要设置0x2000 0000这个字节的第二个位bit2为1,使用位带操作的步骤有:
1、将1写入位 带别名区对应的映射地址(即0x22000008,因为1bit对应4个byte);
2、将0x2000 0000的值 读取到内部的缓冲区(这一步骤是内核完成的,属于原子操作,不需要用户操作);
3、将bit2置1,再把值写 回到0x2000 0000(属于原子操作,不需要用户操作)。
关于GPIO引脚对应的访问地址,可以参考以下公式
寄存器位带别名 = 0x42000000 + (寄存器的地址-0x40000000)32 + 引脚编号4
如:端口F访问的起始地址GPIOF_BASE
#define GPIOF ((GPIO_TypeDef *)GPIOF_BASE)
但好在官方库里面都帮我们定义好了 只需要在BASE地址加上便宜即可
例如:
GPIOF的ODR寄存器的地址 = GPIOF_BASE + 0x14
寄存器位带别名 = 0x42000000 + (寄存器的地址-0x40000000)32 + 引脚编号4
设置PF9引脚的话:
uint32_t *PF9_BitBand =
*(uint32_t *)(0x42000000 + ((uint32_t )&GPIOF->ODR– 0x40000000) *32 + 9*4)
封装一下:
#define PFout(x) *(volatile uint32_t *)(0x42000000 + ((uint32_t )&GPIOF->ODR – 0x40000000) *32 + x*4)
现在 可以把通用部分封装成一个小定义:
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
那么 设置PF引脚的函数可以定义:
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define PF_O(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PF_I(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
若使PF9输入输出则:
PF_O(9)=1; //输出高电平
uint8_t dat = PF_I(9); //获取PF9引脚的值
总线输入输出:
void PF_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PF_O(i)=(num>>i)&0x0001;
}
}
unsigned int PF_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PF_I(i)<<i)&0xFFFF;
}
return num;
}
STM32的可用下面的函数:
#ifndef __GPIO_H__
#define __GPIO_H__
#include "stm32l496xx.h"
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
#define PA_O(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PA_I(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PB_O(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PB_I(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PC_O(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PC_I(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PD_O(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PD_I(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PE_O(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PE_I(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PF_O(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PF_I(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PG_O(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PG_I(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
#define PH_O(n) BIT_ADDR(GPIOH_ODR_Addr,n) //输出
#define PH_I(n) BIT_ADDR(GPIOH_IDR_Addr,n) //输入
#define PI_O(n) BIT_ADDR(GPIOI_ODR_Addr,n) //输出
#define PI_I(n) BIT_ADDR(GPIOI_IDR_Addr,n) //输入
void PA_BUS_O(unsigned int num);
unsigned int PA_BUS_I(void);
void PB_BUS_O(unsigned int num);
unsigned int PB_BUS_I(void);
void PC_BUS_O(unsigned int num);
unsigned int PC_BUS_I(void);
void PD_BUS_O(unsigned int num);
unsigned int PD_BUS_I(void);
void PE_BUS_O(unsigned int num);
unsigned int PE_BUS_I(void);
void PF_BUS_O(unsigned int num);
unsigned int PF_BUS_I(void);
void PG_BUS_O(unsigned int num);
unsigned int PG_BUS_I(void);
void PH_BUS_O(unsigned int num);
unsigned int PH_BUS_I(void);
void PI_BUS_O(unsigned int num);
unsigned int PI_BUS_I(void);
#endif
#include "GPIO.h"
void PA_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PA_O(i)=(num>>i)&0x0001;
}
}
unsigned int PA_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PA_I(i)<<i)&0xFFFF;
}
return num;
}
void PB_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PB_O(i)=(num>>i)&0x0001;
}
}
unsigned int PB_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PB_I(i)<<i)&0xFFFF;
}
return num;
}
void PC_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PC_O(i)=(num>>i)&0x0001;
}
}
unsigned int PC_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PC_I(i)<<i)&0xFFFF;
}
return num;
}
void PD_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PD_O(i)=(num>>i)&0x0001;
}
}
unsigned int PD_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PD_I(i)<<i)&0xFFFF;
}
return num;
}
void PE_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PE_O(i)=(num>>i)&0x0001;
}
}
unsigned int PE_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PE_I(i)<<i)&0xFFFF;
}
return num;
}
void PF_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PF_O(i)=(num>>i)&0x0001;
}
}
unsigned int PF_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PF_I(i)<<i)&0xFFFF;
}
return num;
}
void PG_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PG_O(i)=(num>>i)&0x0001;
}
}
unsigned int PG_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PG_I(i)<<i)&0xFFFF;
}
return num;
}
void PH_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PH_O(i)=(num>>i)&0x0001;
}
}
unsigned int PH_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PH_I(i)<<i)&0xFFFF;
}
return num;
}
void PI_BUS_O(unsigned int num) //输入值num最大为0xFFFF
{
int i;
for(i=0;i<16;i++)
{
PI_O(i)=(num>>i)&0x0001;
}
}
unsigned int PI_BUS_I(void) //输出值num最大为0xFFFF
{
unsigned int num;
int i;
for(i=0;i<16;i++)
{
num=num+(PI_I(i)<<i)&0xFFFF;
}
return num;
}
二、如何判断MCU的外设是否支持位带
根据《ARM Cortex-M3与Cortex-M4权威指南(第3版)》中第6章第7节描述
也就是说 要实现对GPIO的位带操作 必须保证GPIO位于外设区域的第一个1MB中
第一个1MB应该是0x4010 0000之前 位带不是直接操作地址 而是操作地址映射 地址映射被操作以后 MCU自动会修改对应寄存器的值
位带区只有1MB 所以只能改0x4000 0000 - 0x400F FFFF的寄存器
像F4系列 GPIO的首地址为0x4002 0000 就可以用位带来更改
STM32L476的GPIO就不行:
AHB2的都不能用位带
ABP 还有AHB1都可以用
但是L476的寄存器里面 GPIO和ADC都是AHB2
附录:关于旧文新发
为何要进行旧文新发?
因为我在2023年博客之星评选中发现 有的人转载、抄袭他人文章 稍微改动几下也能作为高质量文章入选
所以我将把我的旧文重新发一次 然后也这样做
2023年博客之星规则:
- 自2023年1月1日起算起,平均每周创作过至少一篇高质量且非付费专栏的原创文章即可入围。由于博客之星是年度评选,所以统计时间一直截止到2023年12月17日。
- 高质量博文为80分以上原创博文,质量分查询地址:
https://www.csdn.net/qc
- 入围条件补充说明:当前的入围状态为动态,一旦未达到每周平均创作过至少一篇高质量且非付费专栏的原创文章入围资格将会跳出入围资格,若当前还未入围者通过后期创作也可入围,当下并非最终结果。