系统时钟中断和延时------------stm32f407zet6

参考:中文参考手册 108页 M3与M4权威指南 321页
在这里插入图片描述
Systick时钟源有两个一个是HCLK(168MHZ)和8分频的HCLK(21MHZ)。
HCLK时钟频率参考时钟树
在这里插入图片描述

**The Cortex--M processors have a small integrated timer called the SysTick (System
Tick) timer. It is integrated as a part of the NVIC and can generate the SysTick
exception (exception type #15). The SysTick timer is a simple decrement 24-bit
timer, and can run on processor clock frequency or from a reference clock frequency
(normally an on-chip clock source).**

Cortex–M处理器有一个小型的集成计时器,称为SysTick(系统)计时器。它作为NVIC的一部分进行了集成,并可以生成SysTick异常(异常类型#15)。SysTick计时器是一个简单的24位递减计时器,可以在处理器时钟频率或参考时钟频率(通常是片上时钟源)上运行。

Systick是24位向下递减的计数器,中断由NVIC控制
系统时钟中断的使用:
在这里插入图片描述

SysTick_Config()函数和中断服务函数SysTick_Handler(void)
注意 SysTick_Config()里的参数最大不超过24位(即16777215-1)
所以SYystick最大定时时间为T=1677214*1/168000000=99.86ms,不能超过它在这里插入图片描述修改为:在这里插入图片描述

在这里插入图片描述
Systick延时使用使用寄存器:
参考M3与M4权威指南316
在这里插入图片描述
在这里插入图片描述
毫秒延时和微秒延时代码:
在这里插入图片描述

另外值得注意的是:hal库调用上面的函数需要勾选下面的选项,不然会卡在启动文件那里(具体原因不太清楚,希望评论区有大佬指点)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是使用stm32zet6的hc-05红外编解码模块通过R05d协议编码格式的红外关机码控制美的空调,并同时控制温度和开关机的示例代码: ```c #include "stdio.h" #include "stm32f10x.h" #include "delay.h" // 定义红外编解码器的引脚 #define IR_PIN GPIO_Pin_0 #define IR_PORT GPIOB // 定义hc-05模块的引脚 #define BT_PIN GPIO_Pin_5 #define BT_PORT GPIOB // 定义红外关机码 uint8_t IR_OFF_CODE[] = {0xA4, 0x00, 0x00, 0x01, 0x01, 0x00, 0x51, 0x0A}; // 发送一个字节的红外编码 void send_ir_byte(uint8_t byte) { uint8_t i; // 发送8位数据 for (i = 0; i < 8; i++) { // 判断当前位是0还是1 if ((byte >> i) & 0x01) { GPIO_WriteBit(IR_PORT, IR_PIN, Bit_SET); delay_us(562); GPIO_WriteBit(IR_PORT, IR_PIN, Bit_RESET); delay_us(562); } else { GPIO_WriteBit(IR_PORT, IR_PIN, Bit_SET); delay_us(562); GPIO_WriteBit(IR_PORT, IR_PIN, Bit_RESET); delay_us(1687); } } } // 发送一个红外编码 void send_ir_code(uint8_t *code, uint8_t len) { uint8_t i; // 发送起始码 GPIO_WriteBit(IR_PORT, IR_PIN, Bit_SET); delay_us(9000); GPIO_WriteBit(IR_PORT, IR_PIN, Bit_RESET); delay_us(4500); // 发送数据码 for (i = 0; i < len; i++) { send_ir_byte(code[i]); } // 发送停止码 GPIO_WriteBit(IR_PORT, IR_PIN, Bit_SET); delay_us(562); GPIO_WriteBit(IR_PORT, IR_PIN, Bit_RESET); delay_us(562); } // 初始化hc-05模块 void init_bt_module(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 使能GPIOB和USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE); // 配置BT_PIN引脚为推挽输出 GPIO_InitStructure.GPIO_Pin = BT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BT_PORT, &GPIO_InitStructure); // 配置USART1引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // 配置USART1参数 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // 使能USART1 USART_Cmd(USART1, ENABLE); } // 发送AT指令 void send_at_command(char *command) { while (*command) { USART_SendData(USART1, *command); // 等待发送完成 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); command++; } } // 发送温度控制指令 void send_temperature_command(uint8_t temperature) { char command[10]; sprintf(command, "AT+TEMP=%d\r\n", temperature); send_at_command(command); } // 发送开关机指令 void send_power_command(uint8_t power) { char command[10]; sprintf(command, "AT+POWER=%d\r\n", power); send_at_command(command); } int main(void) { // 初始化系统时钟延时函数 SystemInit(); delay_init(); // 初始化hc-05模块 init_bt_module(); // 发送红外关机码 send_ir_code(IR_OFF_CODE, sizeof(IR_OFF_CODE)); // 发送温度控制指令 send_temperature_command(25); // 发送开关机指令 send_power_command(1); delay_ms(2000); send_power_command(0); while (1) { // 循环等待 } } ``` 在这个示例代码中,首先需要初始化hc-05模块和红外编解码器的引脚,并定义红外关机码。然后通过`send_ir_code`函数发送红外编码,通过`send_temperature_command`函数发送温度控制指令,通过`send_power_command`函数发送开关机指令。在`main`函数中,先发送红外关机码,然后发送温度控制指令和开关机指令,最后进入循环等待状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值