[mw_shl_code=c,true]LZ 为啥我用TIM定时不好使呢 你看我程序有问题吗 打印到串口上都是0[/mw_shl_code]
[mw_shl_code=c,true]#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
#include "stm32f10x_tim.h"
//ALIENTEK Mini STM32开发板范例代码2
//按键输入实验
//技术支持:www.openedv.com
//广州市星翼电子科技有限公司
void TIM2_IRQHandler(void)
{
if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_12)==0)
GPIO_SetBits(GPIOE,GPIO_Pin_12);
else
GPIO_ResetBits(GPIOE,GPIO_Pin_12);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
}
void Tim2_Init(u16 prr,u16 prc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);//复位TIM2定时器
TIM_TimeBaseStructure.TIM_Period = prr;
TIM_TimeBaseStructure.TIM_Prescaler = prc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
void GPIOs_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 echo
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //PA8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置为推挽输出 Trig
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);//初始设置为低电平
}
void TrigSetBit()
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
void TrigResetBit()
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}
uint8_t ReadEcho()
{
return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7);
}
int main(void)
{
u16 t;
u16 count;
float length;
SystemInit();
uart_init(9600);
Tim2_Init(49999,71);
GPIOs_Init();
KEY_Init(); //初始化与按键连接的硬件接口
delay_init();
delay_ms(5);
while(1)
{
t=KEY_Scan(0); //得到键值
if(t==KEY0_PRES||t==KEY1_PRES||t==WKUP_PRES)//如果三个按键有一个按下了 就进行一次测距
{
printf("one try\n");
TrigSetBit();
delay_us(20);
TrigResetBit();
TIM2->CNT=0;
while(ReadEcho()==0);
TIM_Cmd(TIM2, ENABLE);
while((ReadEcho()==1)&&(TIM2->CNT<TIM2->ARR-10));
TIM_Cmd(TIM2,DISABLE);
count=TIM2->CNT;
length=count/58.0;
printf("%d s",count);//向串口打印测距对应的计数值
printf("\n");
printf(" \n");
printf("one try completed\n");
delay_ms(200);
}
}
}
[/mw_shl_code]
我之前 还是过用 SysTick 也不好使。。。http://www.openedv.com/posts/list/0/37778.htm?privmsg=25314&&sysid=4#213217 |