#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "Bianmaqi.h"
#include "Delay.h"
#include "timer2.h"
/***************************************************/延时函数
#include "Delay.h"
#include "stm32f10x.h"
/**
* @brief 微秒级延时
* @param xus 延时时长,范围:0~233015
* @retval 无
*/
void Delay_us(uint32_t xus)
{
SysTick->LOAD = 72 * xus; //设置定时器重装值
SysTick->VAL = 0x00; //清空当前计数值
SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器
while(!(SysTick->CTRL & 0x00010000)); //等待计数到0
SysTick->CTRL = 0x00000004; //关闭定时器
}
/**
* @brief 毫秒级延时
* @param xms 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_ms(uint32_t xms)
{
while(xms--)
{
Delay_us(1000);
}
}
/**
* @brief 秒级延时
* @param xs 延时时长,范围:0~4294967295
* @retval 无
*/
void Delay_s(uint32_t xs)
{
while(xs--)
{
Delay_ms(1000);
}
}
/***************************************************/编码器
#include "stm32f10x.h" // Device header
#include "Bianmaqi.h"
void Bianmaqi_init(void)
{
//1**使能时钟
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA,ENABLE);
//2**定义GPIO
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//3**初始化时基单元
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_Channel_1|TIM_Channel_2;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_Period=65536-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=1-1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
//4**初始化IC模块(部分)两个Pin口
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICStructInit(&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
TIM_ICInitStruct.TIM_ICFilter=0xF;
// TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
// TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
// TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3,&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_Channel=TIM_Channel_2;
TIM_ICInitStruct.TIM_ICFilter=0xF;
// TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
// TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
// TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM3,&TIM_ICInitStruct);
//5**配置编码器
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Falling,TIM_ICPolarity_Rising);//控制极性
//6.使能定时器TIM3
TIM_Cmd(TIM3,ENABLE);
}
int16_t get_cnt(void)
{
int16_t temp=0;
temp=TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3,0);
return temp;
}
/***************************************************/定时器TIM2
#include "stm32f10x.h"
#include "timer2.h"
extern int num;
void timer2_init(void)
{
//**1.打开总的RCC,要配置GPIO从而传入外部时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//因为定时器2是通用定时器,在APB1总线上
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//**2.配置定时器接入外部时钟 同时配置外部时钟输入的引脚
TIM_InternalClockConfig(TIM2);//设置定时器2使用内部时钟,可不写,上电默认
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
//TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00);//第二个设置分频,第三个设置触发方式,第四个设置采样频率(滤波器)
//**3.配置时基单元
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//设置分频
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//设置向上l计数
TIM_TimeBaseInitStruct.TIM_Period=10000-1;//设置预装载值 //外部时钟的话用不了这么高的频率
TIM_TimeBaseInitStruct.TIM_Prescaler=7200-1;//设置预分频值
// TIM_TimeBaseInitStruct.TIM_Period=10-1;//设置预装载值
// TIM_TimeBaseInitStruct.TIM_Prescaler=2-1;//设置预分频值
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;//高级计数器功能,重复计数器
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);//初始化时基单元
TIM_ClearFlag(TIM2,TIM_FLAG_Update);//初始化时会产生标志位,需要清除
//**4.使能时基单元的中断
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//**5.配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//分组
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
//**6.开始计数
TIM_Cmd(TIM2,ENABLE);
}
/*
//配置中断函数
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
num++;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
*/
/***************************************************/主函数
int16_t speed=0;
int main(void)
{
timer2_init();
OLED_Init();
Bianmaqi_init();
OLED_ShowString(1,1,"Hello,my honey");
while(1)
{
OLED_ShowSignedNum(2,1,speed,6);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
speed=get_cnt();
Delay_ms (10);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
|