[mw_shl_code=c,true]/*main.c*/
#include"sys.h"
#include"delay.h"
#include"usart.h"
#include"gpio.h"
#include"timer.h"
#include"count.h"
unsigned long int num=0,count=0,i=1;
int main()
{
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,9600);
TIM2_Init(0xffff);
TIM3_Init(71,71);
// TIM2->  SC=0;
while(1)
{
if(num==5000)
{
count=count+TIM2->CNT;
printf("%ld\t",count);
count=0;
num=0;
TIM2->CNT=0;
}
}
}
void TIM2_IRQHandler() //定时器
{
if(TIM3->SR&0x01)
{
count=count+65536;
}
TIM2->SR&=~(1<<0);
}
void TIM3_IRQHandler() //定时器
{
if(TIM3->SR&0x01)
{
num++;
}
TIM3->SR&=~(1<<0);
}
/*count.c*/
#include"sys.h"
#include"delay.h"
#include"usart.h"
#include"count.h"
void TIM2_Init(u16 arr)
{
RCC->APB1ENR|=1<<0;
RCC->APB2ENR|=1<<2;
TIM2->ARR=arr;
GPIOA->CRL&=0xfffffff0;
GPIOA->CRL|=0x00000008;
GPIOA->ODR|=1<<0; //上拉输入
TIM2->CCMR1|=1<<0; //CC1S 输入脚的选择
TIM2->CCMR1&=0xffffff0f; //IC1F清零
TIM2->CCER&=~(1<<1); //选择上升沿极性
TIM2->SMCR|=0x00000007;
TIM2->SMCR|=0x00000050;
TIM2->CR1|=1<<0;
MY_NVIC_Init(1,3,TIM2_IRQn,2);
}
void TIM3_Init(u16 arr,u16 psc) //作为计时器
{
RCC->APB1ENR|=1<<1;
TIM3->ARR=arr; //装初值
TIM3->  SC=psc;
TIM3->DIER|=1<<0;
TIM3->CR1|=0x01;
MY_NVIC_Init(1,3,TIM3_IRQn,2);
}
/*count.h*/
#ifndef COUNT_H
#define COUNT_H
#include"sys.h"
void TIM3_Init(u16 arr,u16 psc);
void TIM2_Init(u16 arr);
#endif
这个是我经过多次实验通过的计算NE555产生的脉冲的程序,菜鸟和大家分享!
希望和我一样的初学者,自己多看看ST公司给的手册,很有用![/mw_shl_code]
|