基于STM32单片机,利用光电编码器测速,光电编码器转动但是串口上显示脉冲数一直是0.
代码如下:
#include "stm32f10x.h"
#include <stdio.h>
void delay(uint32_t);
void COM_Config(void);
void GPIO_Configuration(void);
void TIM2_Configuration(void);
int fputc(int ch, FILE *f);
int fgetc( FILE *f);
int ActualSpeed=0;
int Times=1;
int main(void)
{
COM_Config();
GPIO_Configuration();
TIM2_Configuration();
while (1)
{
delay(10000000);
ActualSpeed=TIM_GetCounter(TIM2);
printf(" 实际转速=%d %d
",ActualSpeed,Times++);
}
}
void COM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/*使能GPIOD端口时钟、复用功能时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
/*使能USART2时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*串口2重映射*/
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
/*外部脉冲输入管脚 PC6 */
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*TIM2对脉冲计数*/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //?自动重装载值,值越小频率越大,值越大频率越小
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0); //外部时钟模式2,计数器在选定输入端的每个上升沿计数
TIM_SetCounter(TIM2, 0); //设置寄存器寄存值
TIM_Cmd(TIM2, ENABLE);
}
void delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
#ifndef MicroLIB
//#pragma import(__use_no_semihosting) //没有实现fgetc时需要声明该参数
/* 标准库需要的支持函数 使用printf()调试打印不需要实现该函数 */
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
/* 重定义fputc函数 如果使用MicroLIB只需要重定义fputc函数即可 */
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{}
return ch;
}
/*
可以直接使用putchar
不需要再定义 int putchar(int ch),因为stdio.h中有如下定义
#define putchar(c) putc(c, stdout)
*/
int ferror(FILE *f) {
/* Your implementation of ferror */
return EOF;
}
#endif
FILE __stdin;
int fgetc(FILE *fp)
{
int ch = 0;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{
}
ch = (int)USART2->DR & 0xFF;
putchar(ch); //回显
return ch;
}
不知道哪里出错了,请各位大神指教。
此帖出自
小平头技术问答
一周热门 更多>