標(biāo)題:
stm32定時(shí)器捕捉 測(cè)脈沖 測(cè)轉(zhuǎn)速12864顯示源程序
[打印本頁]
作者:
wuguiyuan
時(shí)間:
2017-12-14 20:54
標(biāo)題:
stm32定時(shí)器捕捉 測(cè)脈沖 測(cè)轉(zhuǎn)速12864顯示源程序
stm32
使用定時(shí)器捕獲功能 測(cè)脈沖并在12864上顯示
文件內(nèi)包含所有源碼
0error 0warning
開發(fā)平臺(tái)為stm32f107 根據(jù)自己需要更改端口
1、該例程為GPIO例程。
2、使用說明
(1)工程文件路徑:例程目錄\GPIO\MDK-ARM\Project.uvproj。
(2)請(qǐng)使用MDK 4.0以上版本打開,MDK版本過低會(huì)導(dǎo)致無法識(shí)別工程。
(3)下載調(diào)試工具為ULINK。
(4)HEX文件下載到板子后,LED燈閃爍,表明例程運(yùn)行正確。
3、注意事項(xiàng)
請(qǐng)務(wù)必在下載、調(diào)試、運(yùn)行過程中,保持板子上電、ULINK連接并插在電腦上。
單片機(jī)源程序如下:
/****************************************Copyright (c)****************************************************
**--------------File Info---------------------------------------------------------------------------------
** Descriptions: The GPIO LED application function
**
**--------------------------------------------------------------------------------------------------------
** Created by: AVRman
** Version: v1.0
** Descriptions: The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "LCD_12864.H"
#include "stm32f10x_it.h"
#include <stdio.h>
uint8_t i = 0;
/* Private functions ---------------------------------------------------------*/
void GPIO_Configuration(void);
void TIM_Configuration(void);
void RCC_Configuration(void);
void NVIC_Configuration(void);
/*******************************************************************************
* Function Name : Delay
* Description : Delay Time
* Input : - nCount: Delay Time
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void SysClock_Init(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS){
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
;
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08){
;
}
}
}
/*******************************************************************************
* Function Name :
* Description : Programme
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
int main(void)
{
char str[20];
SysClock_Init(); // 初始化系統(tǒng)時(shí)鐘 72MHZ
GPIO_Configuration();
NVIC_Configuration();
RCC_Configuration();
TIM_Configuration();
/* Infinite loop */
lcd_init(); //初始化LCD12864
while (1){
if(CH2_finish)
{ //多放幾個(gè)空格,不留痕跡
sprintf( str, "%d ", (tmp16_CH2) ); //計(jì)數(shù)值
lcd_char(0x90,str);
sprintf( str, "%.1f Hz. ", (8000000.0/tmp16_CH2) ); //保留一位小數(shù)
lcd_char(0x88,str);
CH2_finish = 0;
i++;
Delay(0X01FFFF);
}
lcd_char(0x80, "脈沖波捕捉測(cè)試");
i++;
Delay(0X01FFFF);
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configure GPIO Pin
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/**
* LED1 -> PC6 , LED2 -> PC7,LED1 -> PC8 , LED2 -> PC9
*/ //管腳配置
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE , ENABLE); //要有這個(gè)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//no 12864 pin
//TIM4對(duì)應(yīng) 4路捕捉腳PB6~PB9
//信號(hào)發(fā)生器用有夾子的一組才有效
//GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //管腳重映射
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // TIM4 時(shí)基
///TIM4
TIM_DeInit(TIM4);
/*TIM_Prescaler = (720-1);大于2hz小于1khz比較準(zhǔn),計(jì)數(shù)值<=85 :10us記一次數(shù)
TIM_Prescaler = (18-1): 大于900hz 小于40Khz : 1/4us記一次數(shù)
TIM_Prescaler = (9-1): 大于900hz 小于50Khz :1/8us記一次數(shù)
計(jì)數(shù)值小于100都不怎么準(zhǔn)了
*/
//為自動(dòng)重裝值,與普通單片機(jī)一樣,滿值基本不溢出65535
TIM_TimeBaseStructure.TIM_Period = 0xffff;
//預(yù)分頻值, 使TIMx_CLK=100KHz ,系統(tǒng)時(shí)鐘運(yùn)行于72M時(shí)720分頻,定時(shí)器運(yùn)行于100KHZ,即10us每分度
TIM_TimeBaseStructure.TIM_Prescaler = (9-1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //輸入時(shí)鐘不分頻
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//ch2
// TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP; //輸入捕捉方式
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;// //輸入通道
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 = 0x0; //捕捉輸入不濾波
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //選擇IC2為始終觸發(fā)源
//TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM從模式:觸發(fā)信號(hào)的上升沿重新初始化計(jì)數(shù)器和觸發(fā)寄存器的更新事件
//TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //啟動(dòng)定時(shí)器的被動(dòng)觸發(fā)
//ch2
//TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;//| TIM_Channel_2; //輸入通道
// 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 = 0x0; //捕捉輸入不濾波
// TIM_ICInit(TIM4, &TIM_ICInitStructure);
//
// TIM_ICInitStructure.TIM_Channel = TIM_Channel_3 ;//TIM_Channel_2; //輸入通道
// //ch3
// 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 = 0x0; //捕捉輸入不濾波
// TIM_ICInit(TIM4, &TIM_ICInitStructure);
//
// TIM_ICInitStructure.TIM_Channel = TIM_Channel_4 ;//| TIM_Channel_2; //輸入通道
// //ch4
// TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //捕捉上升沿
//
// TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //捕捉中斷
//
// TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //捕捉不分頻
//
// TIM_ICInitStructure.TIM_ICFilter = 0xf; //捕捉輸入不濾波
// TIM_ICInit(TIM4, &TIM_ICInitStructure);
// /* TIM enable counter */
TIM_Cmd(TIM4, ENABLE);
/* Enable the CC2 Interrupt Request */
// TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
// TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
// TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE); //第四路有問題
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD |RCC_APB2Periph_GPIOE
| RCC_APB2Periph_AFIO , ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE );
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設(shè)置優(yōu)先級(jí)分組:先占優(yōu)先級(jí)0位,從優(yōu)先級(jí)4位
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優(yōu)先級(jí)0級(jí)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //從優(yōu)先級(jí)3級(jí)
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
復(fù)制代碼
所有資料51hei提供下載:
定時(shí)器捕捉 測(cè)脈沖 測(cè)轉(zhuǎn)速12864顯示.rar
(585.26 KB, 下載次數(shù): 102)
2017-12-14 23:46 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1