新闻中心

EEPW首页>嵌入式系统>设计应用> AVR C语句运行时间测试程序源代码

AVR C语句运行时间测试程序源代码

作者: 时间:2016-12-02 来源:网络 收藏
整个工程下载:
http://www.rayfile.com/zh-cn/files/b8f67659-d773-11df-b350-0015c55db73d/f0ec1fdf/
核心代码:
/********** AVR 运行时间测试程序 **********
* 版本.........: 1.0
* 作者.........: 陈利栋
* 目标.........: ATmega128
* 文件名.......: main.c
* 编译器.......: IAR for AVR V5.5
* 创建时间.....: 2010.10.14
* 最后修改.....: 2010.10.14
******************************************/
#include "main.h"

intputchar(intc)
{
return uart_putchar(c);
}

volatileunsignedintTimerOverflowCount=0;

intmain(void)
{
unsignedlongTimerCount=0;

UART_Init();
printf("*********** 运行时间测试 ***********rn");
printf("Build: %s %srn",__DATE__,__TIME__);
printf("时钟频率: %fMHzrn",(float)F_CPU/1000000);

TCCR1B_CS10=1;// 定时器1使能计数,无分频
TIMSK_TOIE1=1;// 定时器1使能溢出中断
__enable_ interrupt();// 总中断使能
TimerOverflowCount=0;// 溢出计数清零
TCNT1=0;// 计数器清零


/* 此处插入要运行的代码 */
/* 代码开始 */
_delay_us(123456);
/* 代码结束 */

TCCR1B_CS10=0;// 定时器1停止计数 花费3个时钟周期
TimerCount=TCNT1;
TimerCount|=((unsignedlong)TimerOverflowCount<<16);
TimerCount-=3;// 关闭定时器的时间
TimerCount-=(50*TimerOverflowCount);// 溢出中断花费的时间

printf("消耗时钟周期数...: %ld 0x%lxrn",TimerCount,TimerCount);
printf("消耗时间(单位: s): %lfrn",(double)1*TimerCount/F_CPU);
printf("消耗时间(单位:ms): %lfrn",(double)1000*TimerCount/F_CPU);
printf("消耗时间(单位:us): %lfrn",(double)1000000*TimerCount/F_CPU);
printf("消耗时间(单位:ns): %lfrn",(double)1000000000*TimerCount/F_CPU);

while(1);
}

#pragma vector = TIMER1_OVF_vect
__interruptvoidTimer1_Overflow(void)
{
TimerOverflowCount++;
}
测试效果:


关键词:AVRC语句运行时

评论


技术专区

关闭