新手入门
- 积分
- 4
- 金钱
- 4
- 注册时间
- 2018-11-25
- 在线时间
- 0 小时
|
设置的定时器1发送PWM,定时器2接收,现在程序运行时一直进定时器2,没有进定时器1,都是用寄存器编写的,这个是怎么回事?求助
u8 SendPuls(u8 Halfperiod)
{
GPIO_Config();
TIM1_DeInit();
TIM2_DeInit();
//TIM1_Config(Halfperiod); //发射设置
TIM1_ARRH = Halfperiod/256; //装载频率
TIM1_ARRL = Halfperiod%256;
float Duty = 0.5;
float a;
a = Halfperiod*Duty;
TIM1_CCR1H = ((u16)(a))/256; //装载占空比
TIM1_CCR1L = ((u16)(a))%256;
TIM1_CNTRH = 0;
TIM1_CNTRL = 0;
TIM1_CR1 &= 0xFE;
// delay_us(10);
TIM1_CCMR1 |= 0x60; //PWM1模式
TIM1_CR1 &= 0x8F; //中央对齐上下计数上下溢出置1
TIM1_CCMR1 |= 0x08;
TIM1_CCMR1 &= 0xFC;
TIM1_CCER1 &= 0xFD; //OC1高电平有效
TIM1_CCMR1 |=0x08;
TIM1_CCER1 |= 0x01; //输出使能
//TIM1_EGR |= 0x01; //事件产生
//TIM1_OISR |=0x00; //输出空闲高电平
TIM1_BKR |= 0x80; //主动输出
//TIM2_Config(Halfperiod); //接收设置
PulsHi = (BurstCount + 6) * Halfperiod / 32;
PulsLo = (BurstCount - 5) * Halfperiod / 32;
rx_stat = 0; //接收空闲
TIM2_CCMR1 &= 0x0F; //无滤波器
TIM2_CCER1_CC1P = 1; //低电平触发
//TIM1_CR1 &= 0x8FD;
TIM2_CCER1 |= 0x01; //使能捕获
TIM2_IER = 0x02; //更新中断使能
TIM2_CCMR1 |= 0x04; //通道选择
tx_stat = 1; //置位开始发送
//TIM1_SR1 = 0; //清除标志
//TIM2_SR1 = 0;
asm("rim");
TIM1_EGR = 0x01;
TIM1_IER = 0x02; //通道1比较允许
TIM1_CR1 |= 0x01; //使能计数
TIM2_CR1 |= 0x01;
while (tx_stat != 0); //等待发送空闲状态结束
//TIM1_CR1 &= 0xFE;
//InCapture = (u16)TIM2_CCR1H <<8;
//InCapture |=TIM2_CCR1L;
switch (rx_stat)
{
case 0: //接收空闲
TimeCount = -1; //无数据接收
break;
case 1: //接收进行
while(rx_stat == 1);
//break;
case 2: //接收完成
TimeCount = InCapture; //保存接收脉冲
rx_stat = 0;
if ( !((TimeCount > PulsLo) && (TimeCount < PulsHi))) //判断脉冲有效性
{
TimeCount = 2;
}
break;
}
asm("sim");
return TimeCount;
}
void GPIO_Config(void)
{
PC_DDR_DDR6 = 1;
PC_CR1_C16 = 1;
PC_CR2_C26 = 0;
PD_DDR_DDR4 = 0;
PD_CR1_C14 = 1;
PD_CR2_C24 = 0;
PD_DDR_DDR3 = 1;
PD_CR1_C13 = 1;
PD_CR2_C23 = 0;
PD_ODR_ODR3 = 0;
PD_DDR_DDR2 = 1;
PD_CR1_C12 = 1;
PD_CR2_C22 = 0;
PD_ODR_ODR2 = 1;
}
#pragma vector = 0x0C
__interrupt void TIM1_CAP_COM_IRQHandler(void)
{
static u16 CarrierCount;
CarrierCount ++;
if(CarrierCount ++ >= 25)
{
CarrierCount = 0;
tx_stat = 0; //发送结束
PC_DDR_DDR6 = 0;
TIM1_ClearITPendingBit(TIM1_IT_CC1);
}
}
#pragma vector = 0x0E
__interrupt void TIM2_CAP_COM_IRQHandler(void)
{
u8 intCount = TIM2_CCER1_CC1P;
TIM2_ClearFlag(TIM2_FLAG_CC1);
switch(intCount)
{
case 0:
TIM2_CNTRH = 0;
TIM2_CNTRL = 0;
TIM2_CCER1_CC1P = 1;
rx_stat = 1; //开始接收
break;
case 1:
InCapture = (u16)TIM2_CCR1H <<8;
InCapture |= TIM2_CCR1L;
TIM2_CCER1_CC1P = 0;
rx_stat = 0; //发送结束
//TIM1_BKR &= 0x7F; //停止输出
//PC_ODR_ODR6 = 0;
//PD_ODR_ODR3 = 0;
//TIM1_CR1 &= 0xFE;
break;
}
|
|