关于用按下按钮来代替输入波形到ICP的捕获的试验。望马老师百忙中抽时间给学生看一
请问马老师,我正在做输入捕获的试验,请问您是否能用AVR-51板上的按钮连ICP端口,每按一下按钮,就测按钮高电平的持续时间。但是我快速地按下按钮使ICP在高低电平间变化,但输出端口C连接着的8个发光二极管
一点反映都没有?
我的主要源代码如下:用MEGA16 和 AVR-51 试验板
//以下定义为Timer1作捕捉测试用
#define pulse_out PORTC //define Port C as output for pulse width
#define ICP PIND.6 //define ICP so we can read the pin
unsigned char ov_counter; //counter for timer 1 overflow
unsigned int rising_edge_H, rising_edge_L, falling_edge_H, falling_edge_L, ; //storage for times
unsigned long pulse_clocks;
unsigned long falling_edge;
unsigned long rising_edge;
// Timer 1 overflow interrupt service routine
interrupt void timer1_ovf_isr(void)
{
ov_counter++; //increment counter when overflow occurs.
}
// Timer 1 input capture interrupt service routine
interrupt void timer1_capt_isr(void)
{
// Place your code here
//Check for rising or falling edge by checking the
//level on ICP. If high, the interrupt must have
//triggered by a rising edge and if not, the trigger
//must have been a falling edge.
if(ICP)
{
rising_edge_L = ICR1L; //save start time for pulse
rising_edge_H = ICR1H;
rising_edge = rising_edge_H<<8 + rising_edge_L;
TCCR1B = TCCR1B & 0xBF; //set to trigger on falling edge next
ov_counter = 0; //clear overflow counter for this measurement
}
else
{ falling_edge_L = ICR1L; //save falling edge time
falling_edge_H = ICR1H;
falling_edge = falling_edge_H<<8 + falling_edge_L;
TCCR1B = TCCR1B | 0x40; //set for rising edge trigger next
pulse_clocks = (unsigned long)falling_edge
- (unsigned long)rising_edge
+ (unsigned long)ov_counter * 0x10000; //calculation
pulse_out = pulse_clocks/500; //output milliseconds to Port C
}
}
void main(void)
{
PORTD=0x00;
DDRD=0x00;
DDRC = 0xFF; //set Port C for output
PORTC = 0xFF;
TCCR1B = 0b11000010; //Timer 1 input to "clock/8",
//"enable First inputcapture on rising edge" and
// "noise canceller"
//0xC2
TIMSK = 0b100100; //unmask timer 1 overflow and
//capture interrupts
// 0x24
while (1)
{
};
}
-----此内容被xingsharp于2005-10-23,17:48:27编辑过
-----此内容被xingsharp于2005-10-23,17:48:54编辑过
-----此内容被xingsharp于2005-10-23,17:51:03编辑过 现在我改用AVR-51中的方波信号源来接ICP,但在输出端(端口C8个口连AVR-51板上的8个光电二极管)还是没有任何显示?? 现在找到问题的原因是,我还把没把使能全局中断的语句加入(#asm("sei")),现在测试通过了。。 我也想学捕获脉冲宽度,
弄了好久没弄出来\
xingsharp
能公开你的代码吗?
页:
[1]