新手上路
- 积分
- 42
- 金钱
- 42
- 注册时间
- 2013-10-29
- 在线时间
- 5 小时
|
#include "encoding_disk.h"
#include "delay.h"
#include "usart.h"
/**************************************************************************************
********ÂëÅìêäèë¼ì2a*********°′¼üêäèë
TIM3 óÃà′¼ì2aÂëÅì
PA.6-> RUN-A2 TIM3-CH1//
PA.7-> RUN-B2 TIM3-CH2//
PB.0-> RUN-A1 TIM3-CH3//
PB.1-> RUN-B1 TIM3-CH4//
Õy½»±àÂëÆ÷ò23ÆÔöá¿ê½±àÂëÆ÷
****************************************************************************************/
#define ENCODER_TIMER TIM3 // Encoder unit connected to TIM3
#define ENCODER_PPR (u16)(100) // number of pulses per revolution
#define SPEED_BUFFER_SIZE 8
#define COUNTER_RESET (u16)0
#define ICx_FILTER (u8) 6 // 6<-> 670nsec
#define TIMx_PRE_EMPTION_PRIORITY 1
#define TIMx_SUB_PRIORITY 0
#define ENCODER_TIM_PERIOD 0xffff//65536-1
#define MAX_COUNT 10000//10000 1ms 10000
static volatile u16 hEncoder_Timer_Overflow;
void EncodingDisk_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM1, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIOA
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling //
TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1; //
// TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//TIM_ICPolarity_BothEdge
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter =ICx_FILTER;//
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);//
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
TIM3->CNT = COUNTER_RESET;
TIM_Cmd(ENCODER_TIMER, ENABLE);
// System.Device.Encoder.Enc_GetCount = Enc_GetCount;
}
s16 Enc_GetCount(void)
{
static u16 lastCount = 0;
u16 curCount = ENCODER_TIMER->CNT;//?????
s32 dAngle = curCount - lastCount;
if(dAngle >= MAX_COUNT)
{
dAngle -= ENCODER_TIM_PERIOD;
}
else if(dAngle < -MAX_COUNT)
{
dAngle += ENCODER_TIM_PERIOD;
}
lastCount = curCount;
return (s16)dAngle;
}
主函数
int main(void)
{
s16 temp,NUM;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
EncodingDisk_Init();
Usart_Init(9600); //′®¿ú3õê¼»ˉÎa9600
GPIO_SetBits (GPIOA,GPIO_Pin_11);
while(1)
{
NUM = TIM_GetCounter(TIM3);
if(NUM!=temp)
{
temp=NUM;
printf("%d\r\n",NUM);
}
}
现在出不来码盘的值,找不到具体是哪里出的问题。
|
|