/*硬件连接*/
// PB0<----PC4
/****************************************************************************************
*开发环境:IAR for stm8 v6.5.3
*硬件平台:STM8L-DISCOVERY
*功能说明:本例程,通过CC0输出32.768KHz的方波,提供给TIM2的捕获输入引脚PB0测量
*作 者:茗风
****************************************************************************************/
#include"iostm8l152c6.h"
#include"stdint.h"
uint16_t tim2_ccr1=0,tim2_ccr2=0;
uint8_t f_finish=0;
/******************************************************************************************************
* 名 称:void CLOCK_Init(void)
* 功 能:通过PC4输出选择的时钟
* 入口参数:无
* 出口参数:无
* 说 明:PC4输出32.768KHz的方波,提供给TIM2的捕获引脚PB0测量
* 范 例:无
******************************************************************************************************/
void CLOCK_Init(void)
{
PC_CR1_C14 =1;//推挽输出
PC_CR2_C24 =1;//高速输出
PC_DDR_DDR4 =1;//PC4输出
CLK_CCOR_CCODIV=0x00;//选择分频值
/*
*0x00 CCO divided by 1
*0x01 CCO divided by 2
*0x02 CCO divided by 4
*0x03 CCO divided by 8
*0x04 CCO divided by 16
*0x05 CCO divided by 32
*0x06 CCO divided by 64
*0x07 CCO divided by 64
*/
CLK_CCOR_CCOSEL =0x08;//选择输出时钟,这里选择外部32.768KHz的时钟做测试
/*
*0x01 HSI clock output clock source
*0x02 LSI clock output clock source
*0x04 HSE clock output clock source
*0x08 LSE clock output clock source
*/
}
/******************************************************************************************************
* 名 称:void TIMER2_Init(void)
* 功 能:STM8定时器2捕获功能初始化
* 入口参数:无
* 出口参数:无
* 说 明:PB0为捕获输入引脚,使用通道0
* 范 例:无
******************************************************************************************************/
void TIMER2_Init(void)
{
PB_DDR_DDR0 =0;//输入
PB_CR1_C10 =0;//浮空输入
// PB_CR1_C10 =1;//上拉输入
PB_CR2_C20 =0;//禁止外部中断功能
CLK_PCKENR1_PCKEN10=1;//打开定时器2时钟
//------设置TIM2时钟分频值------
TIM2_PSCR_PSC=0;//分频值 2M/2^0=2M/1=2000000Hz
//-重装值,TIM2从0计数到此值,发生溢出-
// TIM2_ARRH=0;
// TIM2_ARRL=100;
//----AUTO_RELOAD 预装载使能----
TIM2_CR1_ARPE =0;//不通过预装载寄存器
TIM2_CR1_URS=1;//仅当计数器溢出时才发生中断请求
TIM2_CR1_UDIS=1;//禁止更新事件//计数器溢出属于更新事件
TIM2_CR1_DIR=0;//向上计数
// TIM2_CCER1_CC1E=0;//清零使能位,为了配置寄存器
// TIM2_CCER1_CC2E=0;//清零使能位,为了配置寄存器
TIM2_CCMR1=0;
// TIM2_CCMR1=0x00; //IC1F=0000 输入信号不分频
TIM2_CCMR1 |=0x01;//CC1S=01 IC1 is mapped on TI1FP1
/*00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1FP1
10: CC1 channel is configured as input, IC1 is mapped on TI2FP1
11: Reserved */
TIM2_CCMR2=0;
// TIM2_CCMR2=0x00;//IC1PSC=0 输入信号不分频
TIM2_CCMR2 |=0x02;// TIM2_CCMR2_CC2S=0x2
/* 00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2FP2
10: CC2 channel is configured as input, IC2 is mapped on TI1FP2
11:CC2 channel is configured as input, IC2 is mapped on TRC */
TIM2_CCER1_CC1P=0;//上升沿时发生捕获
TIM2_CCER1_CC2P=1;//下降沿时发生捕获
TIM2_SMCR=0x54;//
// TIM2_SMCR_TS=0x05;//101 iput 1(TI1FP1) 头文件有错误
// TIM2_SMCR_SMS=0x04;//复位触发模式
TIM2_CCER1_CC1E=1;//使能捕获功能
TIM2_CCER1_CC2E=1;//使能捕获功能
TIM2_IER_CC1IE=1;//开启捕获中断
TIM2_IER_CC2IE=1;//开启捕获中断
TIM2_CR1_CEN=1;//开启计数
}
void main(void)
{
static float tmp=0;
CLOCK_Init();
TIMER2_Init();
asm("rim"); //enable interrupts
while(1)
{
if(f_finish==0x01)
{
//1000000us/2000000=0.5us
//测量到的周期为 0.5us*59=29.5us
//测量到的周期为 1000000us/29.5us=33.898KHz
//实际CCO输出的频率为32.768KHz
tmp=1000/((float)tim2_ccr1*0.5);//tmp的值为测量到的频率
f_finish=0;
asm("nop");
}
// asm("wfi");
}
}
#pragma vector=TIM2_CAPCOM_CC1IF_vector
__interrupt void TIM2_CAPCOM_CC1IF_ISR (void)
{
if(TIM2_SR1_CC1IF)
{
tim2_ccr1=TIM2_CCR1H;
tim2_ccr1<<=8;
tim2_ccr1+=TIM2_CCR1L;
TIM2_SR1_CC1IF=0;//清除中断标志位
f_finish=1;
//tim2_ccr1得到的时间为整个周期的时间
}
else if(TIM2_SR1_CC2IF)
{
tim2_ccr2=TIM2_CCR2H;
tim2_ccr2<<=8;
tim2_ccr2+=TIM2_CCR2L;
TIM2_SR1_CC2IF=0;//清除中断标志位
//tim2_ccr2得到的时间为高电平持续时间
}
}
|