通过USART串口实现单片机printf()调试

日志

返回日志列表
通过USART串口实现单片机printf()调试 编辑于 2018-9-18 16:53 阅读(0)

赞
评论
转载
分享
复制地址
编辑

上一篇 | 已经是最后一篇

开通黄钻
通过USART串口实现单片机printf()调试

通过USART串口实现单片机printf()调试

图片

图片

图片

资料参照:《零死角玩转STM32-中极篇》。不知如何在没有硬件环境下仿真编码器模式接口?

/MAIN.CTIM3编码器模式 TIM2PWM模式**********/
#include<stm32f10x_lib.h>
#include<stdio.h>

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
vu16 CCR1_Val = 0x8000;
vu16 CCR2_Val = 0x4000;
//vu16 CCR3_Val = 0x2000;
//vu16 CCR4_Val = 0x1000;
ErrorStatus HSEStartUpStatus ;
void RCC_Configuration(void );
void GPIO_Configuration(void );
void NVIC_Configuration(void );
//void TIMER2_Init(void);
//void TIMER2_PWM_Init(void);
void Tim3_Init(void);
void TIMER2_PWM_Init(void);
void UART2_Init(void);
void Delay();
int fputc(int ch,FILE * f);

int main()
{
//FLASH->ACR = 0x00000012;//__EFI_ACR_Val;
RCC_Configuration( );
GPIO_Configuration( );
//NVIC_Configuration( );
Tim3_Init();
TIMER2_PWM_Init();
UART2_Init();
while(1)
{

}
}
int fputc(int ch,FILE * f) //重定向printf()函数是调用了重定向fputc函数通过串口打出来到串口调试助手
{
USART_SendData(USART2,(unsigned char)ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET)
{
}
return(ch);
}

void UART2_Init()
{
USART_InitStructure.USART_BaudRate=9600;//波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //8数据位
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1停止位
USART_InitStructure.USART_Parity=USART_Parity_No; //无奇偶校验位
USART_InitStructure.USART_Mode=USART_Mode_Tx; //发送使能
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //无流控
USART_Init(USART2,&USART_InitStructure);
USART_ClockInitStructure.USART_Clock= USART_Clock_Disable; //串口时钟禁止
USART_ClockInitStructure.USART_CPOL=USART_CPOL_High; //高边缘有效
USART_ClockInitStructure.USART_CPHA=USART_CPHA_1Edge; //数据在第一个时钟边缘被捕获
USART_ClockInitStructure.USART_LastBit=USART_LastBit_Disable;//
USART_ClockInit(USART2,&USART_ClockInitStructure); //
USART_Cmd(USART2, ENABLE);//串口2使能
while(1)
{
Delay(10000);
USART_SendData(USART2,TIM3->CNT);
//printf(“woshi”);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET)
{
}
Delay(10000);
//printf(“woshi\r\n”);
printf(“TIM3->CNT=%d”,TIM3->CNT);
}
}
void Tim3_Init(void)
{
/配置时间基******/
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //周期计数
TIM_TimeBaseStructure.TIM_Prescaler = 0xFFF;//分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x03;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising , TIM_ICPolarity_Rising); //编码模式3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 2;//ICx_FILTER;2次滤波 比较滤波器
TIM_ICInit(TIM3, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清中断标志位
//Reset counter设定初始值
TIM_SetCounter(TIM3, 0x7ff1);
//使能计数器
TIM_Cmd(TIM3, ENABLE);
//TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);

}
/*****************************************************************/
void TIMER2_PWM_Init(void)
{
/配置时间基******/
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0xFF;//分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x03;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //模式
// TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Disable);
/**********************/
TIM_Cmd(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
}

void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET )
{

}           
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource() != 0x08)
{

}
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}

void GPIO_Configuration(void )
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOB);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_7|GPIO_Pin_6
|GPIO_Pin_5|GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_7|GPIO_Pin_6
|GPIO_Pin_5|GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
// GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4);
}

/**
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
**/
void Delay(int num)
{
int i,k;
for(i=num;i>0;i–)
{
for(k=100;k>0;k–)
{
;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xx-xx-xxx-xxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值