关于*((volatile int*)0x0000xxxx)

本文介绍如何使用C语言向指定地址的寄存器写入数据,并解释了使用volatile关键字的原因,确保编译器不会优化掉关键指令。

向某个寄存器(地址0x56000000)写入数据0x010,用c怎么写呢?

首先看如下例子:

int a;
int *p;
p=&a;

如果让a=0x010,只需要a=0x010或*p=0x010

这里p是a的地址,如果a的地址为0x56000000,那么p=0x56000000,用c怎么表示呢?

答案为:p=(int *)0x56000000

那么如果给这个地址赋值0x010,就可以写为:*((int *)0x56000000)=0x010

因为这是给寄存器赋值,为了防止编译器优化掉,需要加volatile,于是就写为:

*((volatile int *)0x56000000)=0x010

那么标准的写法如下:

#define REG  *((volatile int *)0x56000000)

REG=0x010;

/**********************************************头文件区**********************************************/ #include <REGX51.H> /**********************************************变量声明区**********************************************/ sbit input = P3^2; //方波输入引脚 unsigned char code Seg_Dula[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,//0~9 0x71,//F 0x73//P }; //段码存放数组 unsigned char code Seg_Wela[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位码存放数组 unsigned char Seg_Buf[] = {10,10,10,10,10,10,10,10};//数码管显示数据存放数组 unsigned char Seg_Pos; //数码管扫描变量 unsigned char Seg_Slow_Down; //数码管减速变量 unsigned int time_1,time_2; //占空比测量时间计数变量 volatile unsigned int freq = 0; //频率计数变量 unsigned char pwm=0; //占空比显示变量 unsigned long k; //占空比辅助计算变量 /**********************************************函数处理区**********************************************/ /*******数码管显示函数*******/ void Seg_Disp(unsigned char wela,dula) { P0 = 0x00; //段选消影 P2_6 = 1; P2_6 = 0; P0 = Seg_Wela[wela]; //读取位选 P2_7 = 1; P2_7 = 0; P0 = Seg_Dula[dula]; //读取段选 P2_6 = 1; P2_6 = 0; } /*******显示处理函数*******/ void Seg_Proc() { if(Seg_Slow_Down < 100) return; //数码管减速刷新 Seg_Slow_Down = 0; // 频率显示格式: F XXXX (高位熄灭) // 占空比显示格式: P XX Seg_Buf[5] = 12; Seg_Buf[6] = pwm / 10; Seg_Buf[7] = pwm % 10; } /*******定时器0初始化(1ms中断)*******/ void Timer0_Init(void) //1毫秒@12.000MHz { TMOD &= 0xF0; //设置定时器模式 TMOD |= 0x01; //设置定时器模式 TL0 = 0x18; //设置定时初始值 TH0 = 0xFC; //设置定时初始值 TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 ET0 = 1; EA = 1; //启用定时器 } /*******定时器1初始化(用于占空比测量)*******/ void Timer1_Init() { TMOD |= 0x10; // Timer1为16位定时器 } /*******Timer0中断服务函数*******/ void Timer0_ISR() interrupt 1 { TL0 = 0x18; //设置定时初始值 TH0 = 0xFC; //设置定时初始值 Seg_Slow_Down++; Seg_Pos = (++Seg_Pos)%8; Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos]); // 数码管动态扫描 while(input);//等待高电平时间过去 while(!input);//等待低电平时间过去 TR1=1;//启动定时器1 while(input);//等待计时完成 time_1=TH1; //记录高电平时间 time_1=time_1*256+TL1; while(!input);//等待周期结束 TR1=0;//停止定时器1 time_2=TH1; //记录周期时间 time_2=time_2*256+TL1; k=time_1;//计算占空比 k=k*100; k=k/time_2; pwm=k; freq = 1000000 / time_2; //频率转换 TH1=0;TL1=0; } /**********************************************主函数**********************************************/ void main() { Timer1_Init(); // 初始化定时器1 Timer0_Init(); // 初始化定时器0 while (1) { Seg_Proc(); } }生成以上代码流程图
06-21
/* 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" #include "stdio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* 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 ---------------------------------------------------------*/ UART_HandleTypeDef huart1; TIM_HandleTypeDef htim6; /* USER CODE BEGIN PV */ volatile uint8_t received_data = 0; volatile uint8_t data_ready = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_TIM6_Init(void); /* USER CODE BEGIN PFP */ void process_received_data(uint8_t data) { // ����LED��ʾ���� HAL_GPIO_WritePin(GPIOF, GPIO_PIN_0, (data & 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, (data & 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, (data & 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_3, (data & 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET); printf("[Receiver] �յ�����: %d (0x%X)\r\n", data, data); } /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /*to redirect printf to uart with semihosting.*/ int _write(int fd, char* ptr, int len) { HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY); return len; } /* 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_USART1_UART_Init(); MX_TIM6_Init(); /* USER CODE BEGIN 2 */ printf("ReceiveReady\r\n"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if(data_ready) { process_received_data(received_data); data_ready = 0; } HAL_Delay(10); // ����CPUռ���� } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } /** * @brief TIM6 Initialization Function * @param None * @retval None */ static void MX_TIM6_Init(void) { /* USER CODE BEGIN TIM6_Init 0 */ /* USER CODE END TIM6_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; /* USER CODE BEGIN TIM6_Init 1 */ /* USER CODE END TIM6_Init 1 */ htim6.Instance = TIM6; htim6.Init.Prescaler = 16 * 168 * 2 - 1; htim6.Init.CounterMode = TIM_COUNTERMODE_UP; htim6.Init.Period = 65535; htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim6) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM6_Init 2 */ /* USER CODE END TIM6_Init 2 */ } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_Initure; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); /*Configure GPIO pins output : PF0123 8 12(�ж�) 11(������)*/ GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_Initure.Pull = GPIO_NOPULL; GPIO_Initure.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOF, &GPIO_Initure); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_8, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_RESET); /*Configure GPIO pins : PC01234567 8 all sw */ GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; /* all sw */ GPIO_Initure.Mode = GPIO_MODE_INPUT; GPIO_Initure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_Initure); /*Configure GPIO pin : PC11����ͨ������ */ GPIO_Initure.Pin = GPIO_PIN_8 | GPIO_PIN_11; /* key1_n && key2_n*/ GPIO_Initure.Mode = GPIO_MODE_IT_RISING; GPIO_Initure.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_Initure); HAL_NVIC_SetPriority(EXTI9_5_IRQn, 4, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); HAL_NVIC_SetPriority(EXTI15_10_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); } /* USER CODE BEGIN 4 */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == GPIO_PIN_12) { // �ж����� // ��PC0-PC3��ȡ���� received_data = 0; received_data |= HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_0) ? 0x01 : 0x00; received_data |= HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1) ? 0x02 : 0x00; received_data |= HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_2) ? 0x04 : 0x00; received_data |= HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_3) ? 0x08 : 0x00; data_ready = 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 */ 编译失败
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值