#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
//ALIENTEK战舰STM32开发板实验4
//串口实验
//技术支持:www.openedv.com
//广州市星翼电子科技有限公司
int main(void)
{
u8 t;
u8 len;
u16 times=0;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为9600
LED_Init(); //LED端口初始化
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
printf("
您发送的消息为:
");
for(t=0;t<len;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
printf("
");//插入换行
USART_RX_STA=0;
}else
{
times++;
if(times%5000==0)
{
printf("
战舰STM32开发板 串口实验
");
printf("正点原子@ALIENTEK
");
}
if(times%200==0)printf("请输入数据,以回车键结束
");
if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
delay_ms(10);
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#ifndef __TIME3_H
#define __TIME3_H
#include "stm32f4xx_hal.h"
struct x_entry
{
unsigned int Count :8; //设置定时器溢出次数
unsigned int Sta:1; //设置捕获下降沿开始状态
unsigned int Final :1; //标定捕获完整低电平状态
};
void Time3_Init(uint32_t arr, uint16_t prd);
#endif
#include "time3.h"
#include "delay.h"
#include "stm32f4xx_hal.h"
TIM_HandleTypeDef TIME_htim;
TIM_IC_InitTypeDef Tim3_Config;
uint32_t TIM3_Val;
struct x_entry Lpwt;
void Time3_Init(uint32_t arr, uint16_t prd)
{
__HAL_RCC_TIM3_CLK_ENABLE();
TIME_htim.Instance = TIM3;
TIME_htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIME_htim.Init.CounterMode = TIM_COUNTERMODE_UP;
TIME_htim.Init.Period = arr;
TIME_htim.Init.Prescaler = prd;
HAL_TIM_IC_Init(&TIME_htim);
Tim3_Config.ICFilter = 0x06;
Tim3_Config.ICPolarity = TIM_ICPOLARITY_FALLING; //下降沿触发
Tim3_Config.ICPrescaler = TIM_ICPSC_DIV1;
Tim3_Config.ICSelection = TIM_ICSELECTION_DIRECTTI;
HAL_TIM_IC_ConfigChannel(&TIME_htim, &Tim3_Config, TIM_CHANNEL_2);
HAL_TIM_IC_Start(&TIME_htim, TIM_CHANNEL_2);
__HAL_TIM_ENABLE_IT(&TIME_htim, TIM_IT_CC2);
}
void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef Time3_GPIO_Set;
Time3_GPIO_Set.Alternate = GPIO_AF2_TIM3;
Time3_GPIO_Set.Mode = GPIO_MODE_AF_PP;
Time3_GPIO_Set.Pin = GPIO_PIN_5;
Time3_GPIO_Set.Pull = GPIO_PULLUP;
Time3_GPIO_Set.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOB, &Time3_GPIO_Set);
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);
HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
}
void TIM3_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIME_htim);
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
if(Lpwt.Final == 0)
{
if(Lpwt.Sta != 1)
{
__HAL_TIM_SET_COUNTER(&TIME_htim, 0); //重新设置Time3 计数器为0
TIM_RESET_CAPTUREPOLARITY(&TIME_htim,TIM_CHANNEL_2); //重新清空定时器触发状态,一定要有
TIM_SET_CAPTUREPOLARITY(&TIME_htim, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING); //重新设置定时器触发状态,为上边沿触发
__HAL_TIM_ENABLE_IT(&TIME_htim, TIM_IT_UPDATE); //打开定时器更新(溢出)中断
Lpwt.Sta = 1; //标记捕获到一个低电平
}
else
{
TIM3_Val = __HAL_TIM_GET_COUNTER(&TIME_htim); //读取此时Time3 计数器的值
TIM_RESET_CAPTUREPOLARITY(&TIME_htim,TIM_CHANNEL_2); //重新清空定时器触发状态,一定要有
TIM_SET_CAPTUREPOLARITY(&TIME_htim, TIM_CHANNEL_2, TIM_ICPOLARITY_FALLING); //重新设置定时器触发状态,为下边沿触发
__HAL_TIM_DISABLE_IT(&TIME_htim, TIM_IT_UPDATE); //关闭定时器更新(溢出)中断
Lpwt.Final = 1; //标记捕获到一个完整的低电平
}
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
if(!Lpwt.Final)
// if(Lpwt.Sta)
Lpwt.Count++;
}
}
一周热门 更多>