初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-12-22
|
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART1_configuration(void);
void TIM5_Configuration(void);
void Delay(__IO uint32_t nCount);
int fputc(int ch, FILE *f);
int fgetc(FILE *f);
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
u32 oldcount = 0;
RCC_Configuration();
GPIO_Configuration();
USART1_configuration();
printf("\r\n This is a TIM external tigger counting expriment !\r\n");
/* Add your application code here */
TIM5_Configuration();
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE );
while(1)
{
Delay(0xfff);
if(oldcount != TIM3->CNT)
{
oldcount = TIM3->CNT;
printf("\r\n Now Number = %d\r\n",oldcount);
}
}
}
/**
* @brief Configure the Clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
/*Enable the TIM3 clock*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE );
/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/*Enable the GPIOA and GPIOC clocks*/
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE );
}
/**
* @brief Configure the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* USART1 GPIO configration ------------------------------------------------------------*/
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*TIM3_CH2 GPIO configuration-------------------------------------------------------------*/
/*Configure TIM5_CH1(PA.0) as floating input*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**
* @brief Configure the USART1.
* @param None
* @retval None
*/
void USART1_configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
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);
}
/**
* @brief Configures the TIM3 .
* @param None
* @retval None
* @attention
* TIM_Period / Auto Reload Register(ARR) = 1000 TIM_Prescaler--71
* ÖD¶ÏÖüÆúÎa = 1/(72MHZ /72) * 1000 = 1ms
*
* TIMxCLK/CK_PSC --> TIMxCNT --> TIM_Period(ARR) --> ÖD¶Ï ÇòTIMxCNTÖØÖÃÎa0ÖØD¼Æêy
*/
void TIM5_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_ICInitTypeDef TIM3_ICInitStructure;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, & TIM_TimeBaseStructure);
/*Configures the External clock Mode2. */
TIM_ETRClockMode2Config (TIM5,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0x00);
/*Sets the TIM3 Counter Register value as 0 */
TIM_SetCounter( TIM5,0);
TIM_Cmd(TIM5, ENABLE);
}
/**
* @brief redirect Function Print to USART1 .
* @param None
* @retval None
*/
int fputc(int ch, FILE *f)
{
/* ·¢Ëíò»¸ö×Ö½úêy¾Yμ½USART1 */
USART_SendData(USART1, (uint8_t) ch);
/* μè′y·¢Ëííê±Ï */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
/**
* @brief redirect Function Scanf to USART1 .
* @param None
* @retval None
*/
int fgetc(FILE *f)
{
/* μè′y′®¿ú1êäèëêy¾Y */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
/**
* @brief Inserts a delay time.
* @param nCount: specifies the delay time length.
* @retval None
*/
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
我是照着网上别人的改的。。。但是不行啊...
请问定时器对外部脉冲计数 如何配置...?
|
|