新手上路
- 积分
- 43
- 金钱
- 43
- 注册时间
- 2017-7-14
- 在线时间
- 12 小时
|
5金钱
#include "pbdata.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void TIM3_Configuration(void);
void TIM4_Configuration(void);
void NVIC_Configuration(void);
extern u8 TIM4CH1_CAPTURE_STA;//2¶»ñ±ê¼Ç
extern u16 TIM4CH1_CAPTURE_VAL;//2¶»ñÖμ
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
int main(void)
{
u32 temp=0;
u32 temp1=0;
u8 i;
float distance;
RCC_Configuration(); //Ïμí3ê±Öó3õê¼»ˉ
GPIO_Configuration();//¶Ë¿ú3õê¼»ˉ
USART_Configuration();//′®¿ú3õê¼»ˉ
TIM3_Configuration();
TIM4_Configuration();
NVIC_Configuration();
while(1)
{
if(TIM4CH1_CAPTURE_STA&0X80)//2¶»ñ3é1|
{
temp=TIM4CH1_CAPTURE_STA&0X3F;//tempÎaòç3ö′Îêy
temp*=1000;//
printf("val:%d \r\n",TIM4CH1_CAPTURE_VAL);
temp+=TIM4CH1_CAPTURE_VAL;//¸ßμçÆ½×üê±¼ä
TIM4CH1_CAPTURE_STA=0;//Çå0
}
// distance=temp/116.0;
printf("TIME:%d us\r\n",temp);//¾ààë
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
}
void RCC_Configuration(void)
{
SystemInit();//72m
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//TIM32úéú·½2¨
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//TIM42¶»ñ»ØÏìDÅoÅ
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6 êäèë2¶»ñí¨μà
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB6 ??
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_6);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1,ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void TIM3_Configuration(void)//40kHz·½2¨′¥·¢
{
int CCR1_Val=25;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//2¿·Öó3éäμ½B5êä3ö
//¶¨ê±Æ÷3õê¼»ˉ
TIM_TimeBaseStruct.TIM_Period=49;//3õÖμ
TIM_TimeBaseStruct.TIM_Prescaler=35;//Ô¤·ÖÆμ ((1+TIM_Prescaler )/ê±Öó)*(1+TIM_Period )
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòéÏ
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
//pwm 3õê¼»ˉ
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçÆ½·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //μ±¶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçÆ½
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE);
}
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ICInitTypeDef TIM_ICInitStructure;
//¶¨ê±Æ÷3õê¼»ˉ
TIM_TimeBaseStruct.TIM_Period=999;//3õÖμ
TIM_TimeBaseStruct.TIM_Prescaler=71;//Ô¤·ÖÆμ ((1+TIM_Prescaler )/ê±Öó)*(1+TIM_Period )
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòéÏ
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStruct);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
TIM_Cmd(TIM4,ENABLE );
}
|
|