中级会员
- 积分
- 219
- 金钱
- 219
- 注册时间
- 2016-8-5
- 在线时间
- 55 小时
|
30金钱
本帖最后由 怪伽 于 2017-11-11 10:43 编辑
-----------------------------------------------------------------------------------------------------------------------------------------
void Capture_Duty_Init(unsigned char psc,unsigned short int arr)
{
/* -------------------- 将 PC7 重新映射为TIM1_CH2 -------------------- */
do
{
FLASH_DUKR = 0xAE; // DATA EEPROM解锁密钥
asm("nop");;
FLASH_DUKR = 0x56;
asm("nop");;
}while(!(FLASH_IAPSR & 0x08));
// 启用写入闪存
FLASH_CR2 = 0x80;
FLASH_NCR2 = 0x7f;
// 操作选项字节
*((unsigned char *) 0x4803) = 0x01;
*((unsigned char *) 0x4804) = ~(0x01);
// 锁定Flash
FLASH_IAPSR &= 0xF7;
/* ------------------------------ GPIO设置 --------------------------- */
CLK_PCKENR1|=1<<7; // 开启TIM1时钟
PC_DDR &= ~(1<<7); // 输入模式
PC_CR1 |= 1<<7; // 上拉输入
PC_CR2 &= ~(1<<7); // 无外部中断
/* ------------------------------ 分频设置 --------------------------- */
TIM1_PSCRH = psc >> 8;
TIM1_PSCRL = psc & 0xFF; // 16分频 = 1us
// TIM1_CNTRH = 0x46;
// TIM1_CNTRL = 0x50;
// TIM1_ARRH =arr >> 8;
// TIM1_ARRL =arr & 0xFF; // 20us
/* ------------------------------ 通道设置 --------------------------- */
TIM1_CCER1 &= 0xEE; // 关闭IC1 IC2
TIM1_CCMR1 |= 0x02 ; // CC1到 TI2FP1
TIM1_CCMR2 |= 0x01; // CC2到 TI2FP2
TIM1_CCER1 &= 0xDD;
TIM1_CCER1 |= 0x13; // CC2上升沿触发 CC1下降沿触发 并 CC1 CC2 使能
TIM1_SMCR |= 0x64; // 滤波后的定时器输入 TI2FP2 复位模式
// 清除标志位
TIM1_SR1 &= 0xF9; // 清除 CC1IF CC2IF 标志
TIM1_SR2 &= 0xFD; // 清除 CC1OF 标志
// 使能
TIM1_IER |= 0x06; // 允许捕获中断
TIM1_CR1 |= 0x01; // 使能定时器1
/* ------------------------------------------------------------------- */
}
-----------------------------------------------------------------------------------------------------------------------------------------
#pragma vector = 14 // 设置定时器1重载的中断向量号 = 14
__interrupt void TIM1_CAP_COM_IRQHandler(void)
{
if (((TIM1_SR1 & 0x02) != RESET ) && ((TIM1_IER & 0x02) != RESET ))
{
IC1Value = TIM1_CCR1H;
IC1Value = (unsigned char)((IC1Value << 8) + TIM1_CCR1L);
TIM1_SR1 = (~0x02); // 清除 CC1 中断标志位
printf("下降沿捕获成功....\r\n IC1Value = %d \r\n",&IC1Value);
}else
if (((TIM1_SR1 & 0x04) != RESET ) && ((TIM1_IER & 0x04) != RESET ))
{
IC2Value = TIM1_CCR2H;
IC2Value = (unsigned char)((IC2Value << 8) + TIM1_CCR2L);
TIM1_SR1 = (~0x04); // 清除 CC2 中断标志位
printf("上升沿捕获成功....IC2Value = %d \r\n",&IC2Value);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
以下为串口打印的数据: 不管占空比是多少 频率是多少的输入,都是这个数值.
-----------------------------------------------------------------------------------------------------------------------------------------
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
下降沿捕获成功....
IC1Value = 411
上升沿捕获成功....IC2Value = 412
-----------------------------------------------------------------------------------------------------------------------------------------
那个上升沿捕获 是在杜邦线抬起来的一瞬间产生的.其他情况只要发生中断,就会一直打印 411.
说明: 芯片:STM8S103F3 封装SSOP-20 功能C7(TIM1_CH2) 测量占空比
求助,哪位大佬帮忙看看.
|
最佳答案
查看完整内容[请看2#楼]
/* -------------------- 将 PC6 重新映射为TIM1_CH1 -------------------- */
do
{
FLASH_DUKR = 0xAE; // DATA EEPROM解锁密钥
asm("nop");;
FLASH_DUKR = 0x56;
asm("nop");;
}while(!(FLASH_IAPSR & 0x08));
// 启用写入闪存
FLASH_CR2 = 0x80;
FLASH_NCR2 = 0x7f;
// 操作选项字节
*((unsigned char *) 0x4803) = 0x01;
...
|