以下方法可以让输出端较为精确地输出与输入端波形完全相同,只是延时一段时间的波形。
struct S_Heap{
char Data;
unsigned int Time;
}
struct S_Heap heap[N]; //N要足够大
unsigned int heap_Depth;
假设你想要的延时时间对应的定时器中上限寄存器的值为UPTIME(该值最好远小于该寄存器的最大值)
在端口变化中断中加入以下代码:
if(heap_Depth==0) {heap[0].Data=端口电平; heap[0].Time=0;定时器中上限寄存器=UPTIME;启动并复位寄存器}
else {heap[heap_Depth].Data=端口电平; heap[heap_Depth].Time=定时器中计时器的值;}
heap_Depth++;
在定时器中断中加入以下代码:
heap_Depth--;
if(heap_Depth==0) {输出电平=heap[0].Data; 关闭定时器}
else {输出电平=heap[0].Data; 定时器中上限寄存器=heap[1].Time+UPTIME; 将heap[0]到heap[heap_Depth]中的内容左移}
|