本帖最后由 潘长青 于 2018-11-15 12:57 编辑
外部计数器模式,计数乱跳,怎么肥四?求帮助。这块是光电计数模块的:
[mw_shl_code=c,true]
#include "sys.h"
#include "counter.h"
#define Dir1 PCout(0)
#define Dir2 PCout(1)
void Counter_Init(uint arr, uint psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
//IO_Init_Motor_Dir
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC, GPIO_Pin_0);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
//TIMER_Init
/*****GPIO*****/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/*****TIM1*****/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_InitStructure.TIM_Prescaler=psc;
TIM_InitStructure.TIM_Period=arr;
TIM_InitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_InitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_InitStructure);
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
/*******ENABLE******/
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1,ENABLE);
}
void Stop(void)
{
Dir1=0;Dir2=0;
}
void Go_B(void)
{
Stop();
Dir1=1;Dir2=0;
}
void Go_S(void)
{
Stop();
Dir1=0;Dir2=1;
}
[/mw_shl_code]
这块是主函数的:
[mw_shl_code=c,true]
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "counter.h"
int main(void)
{
u8 lcd_id[12];
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LCD_Init();
LCD_Clear(WHITE);
sprintf((char*)lcd_id,"LCD ID:%04X",lcddev.id);
Counter_Init(200, 0);
Go_S();
while(1)
{
LCD_ShowString(30,40,210,24,24,"TEST_COUNTER");
LCD_ShowxNum(30,70,TIM1->CNT,5,24,0);
if(TIM1->CNT==200)
Go_B();
}
}
[/mw_shl_code]下面是截图:
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>