以下是c文件
#include "stm32f2xx.h"
#include "measure.h"
#include "tax_usart.h"
#include
#include
#define FILE_AMOUNT 5 //
/*
* bit0=s1,bit1=s2
*/
static uint8_t volatile pre_status;
static uint8_t volatile tmp_status;
static uint8_t volatile file_count=0;
static enum MEASURE_STATUS measure_current_status;
static uint32_t volatile pulse_count=0;
static uint16_t volatile stop_count=0;
static uint32_t volatile pulse_L=0; //
static uint8_t volatile s1_s2=0; //
//
/*******************************************************************************
* Function Name : measure_init
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void measure_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(MEASURE_PORT_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = MEASURE_S1_PIN | MEASURE_S2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MEASURE_PORT, &GPIO_InitStructure);
pulse_count=0;
stop_count=0;
measure_current_status=MEASURE_NULL;
tmp_status=GPIO_ReadInputDataBit(MEASURE_PORT,MEASURE_S1_PIN);
tmp_status |= (GPIO_ReadInputDataBit(MEASURE_PORT,MEASURE_S2_PIN)<<1);
tmp_status &= 0x03;
pre_status = tmp_status;
}
void SetOilPulse(uint32_t pulses)
{
pulse_L=pulses;
}
/*******************************************************************************
* Function Name : measure_cook
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void measure_hook(void)
{
tmp_status=GPIO_ReadInputDataBit(MEASURE_PORT,MEASURE_S1_PIN);
tmp_status |= (GPIO_ReadInputDataBit(MEASURE_PORT,MEASURE_S2_PIN)<<1);
tmp_status &= 0x03;
if(measure_current_status == MEASURE_NULL){
if(tmp_status == pre_status){
return;
}
}
if(measure_current_status == MEASURE_ING){
if(tmp_status == pre_status){
stop_count++;
if(stop_count > TIME_OUT){
measure_current_status=MEASURE_END;
stop_count=0;
}
return;
}
}
if((measure_current_status == MEASURE_END) || (tmp_status == pre_status)){
return;
}
if(file_count++ < FILE_AMOUNT){ //
return ;
}
file_count=0;
if(measure_current_status != MEASURE_ING){ // 区分第一路,第二路脉冲
if((pre_status == 0x00) || (pre_status == 0x03)){
//
if((pre_status & 0x01) == (tmp_status & 0x01)){
//
s1_s2=0;
}else {
//
s1_s2=1;
}
}else {
//
if((pre_status & 0x02) == (tmp_status & 0x02)){
//
s1_s2=0;
}else {
//
s1_s2=1;
}
}
}
if(s1_s2 == 0){
switch(pre_status)
{
case 0x00:
{
if(tmp_status == 0x01){
pulse_count++;
}else if(tmp_status == 0x02){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x01:
{
if(tmp_status == 0x03){
pulse_count++;
}else if(tmp_status == 0x00){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x03:
{
if(tmp_status == 0x02){
pulse_count++;
}else if(tmp_status == 0x01){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x02:
{
if(tmp_status == 0x00){
pulse_count++;
}else if(tmp_status == 0x03){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
default:
break;
}
}else if(s1_s2 == 1){
switch(pre_status)
{
case 0x00:
{
if(tmp_status == 0x02){
pulse_count++;
}else if(tmp_status == 0x01){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x02:
{
if(tmp_status == 0x03){
pulse_count++;
}else if(tmp_status == 0x00){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x03:
{
if(tmp_status == 0x01){
pulse_count++;
}else if(tmp_status == 0x02){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
case 0x01:
{
if(tmp_status == 0x00){
pulse_count++;
}else if(tmp_status == 0x03){
if(pulse_count != 0){
#if EN_NEG_PULSE_DEC
if(pulse_count > 0){
pulse_count--;
}
#endif
}
}
break;
}
default:
break;
}
}
measure_current_status = MEASURE_ING;
stop_count=0;
pre_status=tmp_status;
}
/*
* ¶áè¡óí῱»·Å′óáË100±¶
data_ptr óíá¿£¬tmp_pulse Âö3åêy
*/
enum MEASURE_STATUS read_oil(uint32_t * data_ptr,uint32_t * tmp_pulse)
{
if(measure_current_status == MEASURE_END){
*data_ptr=((pulse_count*10000)/pulse_L);
*tmp_pulse=pulse_count;
pulse_count=0;
measure_current_status=MEASURE_NULL;
return MEASURE_END;
}else if (measure_current_status == MEASURE_ING) {
*data_ptr=((pulse_count*10000)/pulse_L);
*tmp_pulse=pulse_count;
}else{
*data_ptr=0;
*tmp_pulse=0;
}
return measure_current_status;
}
以下是h文件
#ifndef _MEASURE_H
#define _MEASURE_H
#define EN_NEG_PULSE_DEC 0 // 0 反向脉冲不减,1 反向脉冲减
#define TIME_OUT (10000) //
#define MEASURE_PORT GPIOE
#define MEASURE_PORT_CLK RCC_AHB1Periph_GPIOE
#define MEASURE_S1_PIN GPIO_Pin_14
#define MEASURE_S1_SOURCE GPIO_PinSource14
#define MEASURE_S2_PIN GPIO_Pin_15
#define MEASURE_S2_SOURCE GPIO_PinSource15
enum MEASURE_STATUS{
MEASURE_NULL,
MEASURE_ING,
MEASURE_END,
};
void measure_init(void);
void measure_hook( void);
enum MEASURE_STATUS read_oil(uint32_t * data_ptr,uint32_t *);
void SetOilPulse(uint32_t pulses);
#endif
以上是测量正交编码的程序,求指点
回复 楼主 lnhjsdf 的帖子
不用这么麻烦,定时器有编码器模式
我也想试试那个定时器接收正交编码,可是没用的起来。只能那一天自己试试了
回复 板凳 huo_hu 的帖子
我也想试试那个定时器接收正交编码,可是没用的起来。只能那一天自己试试了
void TIM4_Init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM4 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = T4_OVERFLOW; //溢出设置
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
//TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 15;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(TIM4, TIM_FLAG_Update|TIM_IT_CC1|TIM_IT_CC2);
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
//TIM_ITConfig(TIM4, TIM_FLAG_Update, ENABLE);
//Reset counter
TIM4->CNT=0x8000;
// ENC_Clear_Speed_Buffer();
TIM_Cmd(TIM4, ENABLE);
}
TIM4编码器模式