本帖最后由 怪伽 于 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("下降沿捕获成功....
IC1Value = %d
",&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
",&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) 测量占空比
求助,哪位大佬帮忙看看.
/* -------------------- 将 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;
*((unsigned char *) 0x4804) = ~(0x01);
// 锁定Flash
FLASH_IAPSR &= 0xF7;
/* ------------------------------ GPIO设置 --------------------------- */
CLK_PCKENR1|=1<<7; // 开启TIM1时钟
PC_DDR &= ~(1<<6); // 输入模式
PC_CR1 |= 1<<6; // 上拉输入
PC_CR2 &= ~(1<<6); // 无外部中断
/* ------------------------------ 分频设置 --------------------------- */
// 捕捉50Hz及 以上的周期和占空比
TIM1_ARRH = (arr >> 8);
TIM1_ARRL = (arr); // 50ms 最小捕获50HZ
TIM1_PSCRH = (psc >> 8);
TIM1_PSCRL = (psc); // 16分频 = 1us
TIM1_CR1 &= (~0x16); // 向上计数
TIM1_RCR = 0; // 设置重复计数器值
/* ------------------------------ 通道设置 --------------------------- */
TIM1_CCER1 &= (~0x11); // 失能通道1 2
TIM1_CCMR1 |= 0x01 ; // CC1到 TI1FP1 无滤波
TIM1_CCER1 &= (~0x02); // IC1 上升沿触发
TIM1_CCMR2 |= 0x02 ; // CC2到 TI1FP2 无滤波
TIM1_CCER1 |= 0x20; // IC2 下降沿触发
TIM1_SMCR |= 0x54; // 滤波后的定时器输入 TI1FP1 复位模式
TIM1_SR1 &= 0xF9; // 清除 CC1IF CC2IF 标志
TIM1_SR2 &= 0xFD; // 清除 CC1OF 标志
/* -------------------------------- 使能 ---------------------------- */
TIM1_IER |= 0x06; // 中断使能
TIM1_CCER1 |= 0x11; // CC1 CC2 使能
TIM1_CR1 |= 0x01; // 使能定时器1
/* ------------------------------------------------------------------- */
已解决,目前是PC6复用.
一周热门 更多>