stm32移植millis()记录系统开机运行时间

本文介绍如何在STM32微控制器上移植millis()函数,利用RTC模块记录系统开机后的运行时间,实现精确的时间计数功能。
摘要由CSDN通过智能技术生成

首先初始化RTC和millis

	MILLIS_Init();									//初始化RTC为1Hz,同时初始化millis

MILLIS_Init() 函数的实现

void MILLIS_Init(void)
{
#ifdef USE_LSE
	NVIC_InitTypeDef NVIC_InitStructure;
	 /* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR| RCC_APB1Periph_BKP, ENABLE);  //使能电源时钟和备份区域时钟
	
	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(ENABLE);     //取消备份区写保护
	
	/* Reset Backup Domain */
	
	if(PWR_GetFlagStatus(PWR_FLAG_WU) == SET)   //判断是否被唤醒
	{
		PWR_ClearFlag(PWR_FLAG_WU);	// 清除唤醒标志
	}else
	{
		BKP_DeInit();    //复位备份区域
	}
	
	/* Enable LSE */
	RCC_LSEConfig(RCC_LSE_ON);
	/* Wait till LSE is ready */
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
	{}
	
	/* Select LSE as RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	
	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);
	
	/* Wait for RTC registers synchronization */
	RTC_WaitFo
) + RECTANGLE_WIDTH / 2, 2 * (RECTANGLE_HEIGHT + 2)); line.setStroke(Color.GREEN); lines.add温度控制系统STM32程序主要分为以下几个部分: 1. 传感器驱动程序:该部分代码用于驱动温度传感器,读取环境温度并将温度信号传输到(line); pane.getChildren().add(line); } // Generate result text resultText = new Text(""); resultText.setFont控制器。 2. 控制器程序:该部分代码负责处理传感器信号并根据预设的(Font.font("Arial", FontWeight.BOLD, 16)); resultText.setFill(Color.BLACK); resultText.setX(20); resultText.setY温度范围发出指令。可以使用PID(比例、积分、微分)控制算法来调节(3 * (RECTANGLE_HEIGHT + 2) + 20); pane.getChildren().add(resultText); // Generate buttons 环境温度。 3. 执行器驱动程序:该部分代码用于驱动加热器或冷却器,根据控制器发出的指令来调节环境温度。 以下是一个简单的温度控制系统 HBox buttonsBox = new HBox(10); buttonsBox.setAlignment(Pos.CENTER); Button fcfsButton = new Button("FCFS的STM32程序示例: ```c #include "stm32f10x.h" #include "stm32f10x_gpio.h" "); fcfsButton.setOnAction(event -> { fcfsScheduler(); }); Button sstfButton = new Button("SSTF#include "stm32f10x_adc.h" #include "stm32f10x_tim.h" #define ADC1_DR_Address ((u32)0x4001244C) void RCC_Configuration(void); void GPIO_Configuration(void); void ADC_Configuration(void); void TIM_Config"); sstfButton.setOnAction(event -> { sstfScheduler(); }); Button scanButton = new Button("SCAN"); uration(void); void Delay(__IO uint32_t nCount); uint16_t ADC_ConvertedValue; int main(void) { RCC_Configuration(); GPIO_Configuration(); ADC_Configuration(); TIM_Configuration(); while (1) { ADC_SoftwareStart scanButton.setOnAction(event -> { scanScheduler(); }); Button cscanButton = new Button("C-SCAN"); cscanButtonConvCmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADC_ConvertedValue = ADC_GetConversionValue(ADC1); TIM_SetCompare1(TIM3, ADC_ConvertedValue/4); Delay.setOnAction(event -> { cscanScheduler(); }); Button resetButton = new Button("Reset"); resetButton.setOnAction(event -> { (1000); } } void RCC_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | reset(); }); buttonsBox.getChildren().addAll(fcfsButton, sstfButton, scanButton, cscanButton, reset RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode =Button); pane.getChildren().add(buttonsBox); // Generate scene Scene scene = new Scene(new Group(pane)); GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure primaryStage.setScene(scene); primaryStage.show(); } private void fcfsScheduler() { timeline = new Timeline(); for (int.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_DeInit(ADC i = 0; i < REQUESTS_COUNT; i++) { int request = requests[i]; Line line = lines.get(i); 1); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; timeline.getKeyFrames().add(new KeyFrame(Duration.millis(i * 100), event -> { int diff = Math.abs(request - ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init( headPosition); headPosition = request; headRectangle.setX(headPosition * (RECTANGLE_WIDTH + 2)); line.setStroke(ColorADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_239Cycles5); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_Get.RED); resultText.setText("FCFS: " + (i + 1) + " requests, total head movements: " + diffResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); ); })); } timeline.play(); } private void sstfScheduler() { timeline = new Timeline(); List ADC_TempSensorVrefintCmd(ENABLE); } void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure<Integer> remainingRequests = new ArrayList<>(); for (int i = 0; i < REQUESTS_COUNT; i++) { remaining.TIM_Period = 4095; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockRequests.add(requests[i]); } for (int i = 0; i < REQUESTS_COUNT; i++) { int minDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode = Integer.MAX_VALUE; int minIndex = 0; for (int j = 0; j < remainingRequests.size(); j_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInit++) { int request = remainingRequests.get(j); int diff = Math.abs(request - headPosition); if (diff < minStructure); TIM_Cmd(TIM3, ENABLE); } void Delay(__IO uint32_t nCount) { while(nCount--) { ) { min = diff; minIndex = j; } } int request = remainingRequests.remove(minIndex); } } ``` 上面的代码用于控制PWM信号,PWM信号的占空比由读取到的温度值决定,从而控制加热器或冷却器的输出功率,以达到调节环境 Line line = lines.get(i); timeline.getKeyFrames().add(new KeyFrame(Duration.millis(i * 100), event -> { 温度的目的。当然,这只是一个简单的示例,实际的温度控制系统可能需要 headPosition = request; headRectangle.setX(headPosition * (RECTANGLE_WIDTH + 2)); line.setStroke(Color.RED); result更加复杂的控制算法和更多的传感器、执行器等组件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值