新手入门
- 积分
- 16
- 金钱
- 16
- 注册时间
- 2016-5-22
- 在线时间
- 2 小时
|
2金钱
#include "led.h"
#include "stm32f10x.h"
#include "delay.h"
#include "timer.h"
#include "chaoshengbo.h"
extern unsigned int length;
int main(void)
{
LED_Init();
delay_init();
TIM3_Int_Init(4999,7199);
while(1)
{
chaoshengbo_init();
}
}
#include "timer.h"
#include "led.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Äü
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔØ¼Ä′æÆ÷ÖüÆúμÄÖμ ¼Æêyμ½5000Îa500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ 10KhzμļÆêyÆμÂê
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIMxíaéè
}
#include "chaoshengbo.h"
#include "delay.h"
#include "timer.h"
#include "led.h"
unsigned int count=0,length=0;
void chaoshengbo_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // Echo PCout(0) // PC0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÏÂà-êäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOc.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Trig PCout(13) // PC13
GPIO_Init(GPIOC, &GPIO_InitStructure); //íÆíìêä3ö £¬IO¿úËù¶èÎa50MHz
TIM_Cmd(TIM3, DISABLE);
Trig=1;
delay_us(20);
Trig=0;
TIM_SetCounter(TIM3,0);
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)==0);
TIM_Cmd(TIM3, ENABLE);
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)==1);
count=TIM_GetCounter(TIM3);
length=count/0.17;//length=countx0.00001x340/2x100;
delay_ms(500);
if(length<40&&length!=0)
{
LED0=!LED0;
}
}
|
|