怎样用430C写出比较准确的延时子函数

pascallee   2006-8-8 17:41 楼主
例如要Delay200us(),怎么算啊.430IAR中有没有象51中可以计算运行时间的工具啊?

回复评论 (15)

IAR里面有 void __delay_cycles(unsigned long __cycles);精确延时几个时钟。配合时钟频率宏定义可以满足你的要求了。
点赞  2006-8-14 16:50
在软件件仿真时可以看到CPU寄存器下有周期计数器,可以看到一条指令可是一段程序执行的指令周期数,通过设置的主频就可以知道一条指令可是一段程序执行的准确时间
点赞  2006-8-14 17:05
谢谢!
点赞  2006-8-15 15:21
请问武斌,void __delay_cycles(unsigned long __cycles);在iar库里有吗?需要包含什么头文件呢?刚在IAR帮助里找了一下没找到。
点赞  2006-8-16 17:56
这是要你自已写吧,只是给你一个函数框架
点赞  2006-8-21 08:43
哦,谢谢。我还以为函数库里有这个函数呢。
点赞  2006-8-22 17:35
void __delay_cycles(unsigned long __cycles);是本征函数,跟_NOP()一样,跟编译器有关,不是自己写的。
在EW3.21意识都有。
INTRINSICS.h 文件里面,还有好多有用的本征函数可以使用。可以提高效率,不过也降低了可移植性。
点赞  2006-8-23 20:11
精确到周期的延时, 还是自己写一个比较好, 很简单. 用C写一个延时函数, 编译后根据list文件和每个指令的执行周期再修改,  必要时嵌入汇编, 或者完全用汇编写这个函数. 写完以后最好做成库, 一劳永逸. 做了这些以后你就知道了编译器如何使用寄存器, 参数列表和返回值分配在哪些寄存器里, 还有指令的执行时间.
点赞  2006-8-24 16:28
引用:

void __delay_cycles(unsigned long __cycles);是本征函数,跟_NOP()一样,跟编译器有关,不是自己写的。
在EW3.21意识都有。
INTRINSICS.h 文件里面,还有好多有用的本征函数可以使用。可以提高效率,不过也降低了可移植性。
刚看了下3.40A里没有这个本征函数。
点赞  2006-8-25 17:36
好好找找。
/**************************************************
*
* Intrinsic functions for the IAR Embedded Workbench for MSP430.
*
* Copyright 2002-2006 IAR Systems. All rights reserved.
*
* $Revision: 1.18 $
*
**************************************************/

#ifndef __INTRINSICS_H
#define __INTRINSICS_H

#ifndef _SYSTEM_BUILD
#pragma system_include
#endif

#pragma language=s××e
#pragma language=extended

/*
* The return value of "__get_interrupt_state".
*/

typedef unsigned short istate_t;


#ifdef __cplusplus
extern "C"
{
#endif

__intrinsic void __no_operation(void);
__intrinsic void __ena××e_interrupt(void);
__intrinsic void __disa××e_interrupt(void);

__intrinsic istate_t __get_interrupt_state(void);
__intrinsic void __set_interrupt_state(istate_t);

__intrinsic void __op_code(unsigned short);

__intrinsic unsigned short __swap_bytes(unsigned short);

__intrinsic void __bic_SR_register(unsigned short);
__intrinsic void __bis_SR_register(unsigned short);
__intrinsic unsigned short __get_SR_register(void);

__intrinsic void __bic_SR_register_on_exit(unsigned short);
__intrinsic void __bis_SR_register_on_exit(unsigned short);
__intrinsic unsigned short __get_SR_register_on_exit(void);

__intrinsic unsigned short __bcd_add_short(unsigned short,
unsigned short);

__intrinsic unsigned long __bcd_add_long (unsigned long,
unsigned long);

__intrinsic unsigned long long __bcd_add_long_long(unsigned long long,
unsigned long long);

/*
* Support for efficient switch:es. E.g. switch(__even_in_range(x, 10))
*
* Note that the value must be even and in the range from 0 to
* __bound, inclusive. No code will be generated that checks this.
*
* This is typically used inside interrupt dispatch functions, to
* switch on special processor registers like TAIV.
*/

__intrinsic unsigned short __even_in_range(unsigned short __value,
unsigned short __bound);

/* Insert a delay with a specific number of cycles. */
__intrinsic void __delay_cycles(unsigned long __cycles);

/*
* The following R4/R5 intrinsic functions are only ××aila××e when
* the corresponding register is locked.
*/

__intrinsic unsigned short __get_R4_register(void);
__intrinsic void __set_R4_register(unsigned short);

__intrinsic unsigned short __get_R5_register(void);
__intrinsic void __set_R5_register(unsigned short);

__intrinsic unsigned short __get_SP_register(void);
__intrinsic void __set_SP_register(unsigned short);

/*
* The following functions are ××aila××e in extended mode (--core=430X)
* to access the upper memory above 64kB as a data area.
*/
__intrinsic void __data20_write_char (unsigned long __addr,
unsigned char __value);

__intrinsic void __data20_write_short(unsigned long __addr,
unsigned short __value);

__intrinsic void __data20_write_long (unsigned long __addr,
unsigned long __value);

__intrinsic unsigned char __data20_read_char (unsigned long __addr);
__intrinsic unsigned short __data20_read_short(unsigned long __addr);
__intrinsic unsigned long __data20_read_long (unsigned long __addr);

/*
* The following two functions can be used to access 20-bit SFRs in the
* lower 64kB. They are only ××aila××e in extended mode (--core=430X).
*/
__intrinsic void __data16_write_addr (unsigned short __addr,
unsigned long __value);

__intrinsic unsigned long __data16_read_addr (unsigned short __addr);


#ifdef __cplusplus
}
#endif


/*
* Alias for locations used for global register varia××es. For example,
* "__no_init __regvar int x @ __R4;".
*/

#define __R4 4
#define __R5 5

/*
* Control bits in the processor status register, SR.
*/

#define __SR_GIE (1<<3)
#define __SR_CPU_OFF (1<<4)
#define __SR_OSC_OFF (1<<5)
#define __SR_SCG0 (1<<6)
#define __SR_SCG1 (1<<7)


/*
* Functions for controlling the processor operation modes.
*/

#define __low_power_mode_0() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF))

#define __low_power_mode_1() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0))

#define __low_power_mode_2() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG1))

#define __low_power_mode_3() \
(__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1))

#define __low_power_mode_4() \
(__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1 \
| __SR_OSC_OFF))

#define __low_power_mode_off_on_exit() \
(__bic_SR_register_on_exit( __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1 \
| __SR_OSC_OFF))


#pragma language=restore

#endif /* __INTRINSICS_H */
点赞  2006-8-26 11:10
一般的程序我想用不着这么烦麻了
点赞  2006-8-28 08:45
呵呵,可能是我这个版本比较低,我这里面没有的,奇怪了,我装的是3.40啊,看来应该去下载一下更新了,那个可能就有了。
/**************************************************
*
* Intrinsic functions for the MSP430 IAR Systems C/EC++ Compiler.
*
* Copyright 2002 IAR Systems. All rights reserved.
*
* $Revision: 1.14 $
*
**************************************************/

#ifndef __INTRINSICS_H
#define __INTRINSICS_H

#pragma language=extended


/*
* The return value of "__get_interrupt_state".
*/

typedef unsigned short istate_t;

#ifdef __cplusplus
extern "C"
{
#endif

__intrinsic void __no_operation(void);
__intrinsic void __ena××e_interrupt(void);
__intrinsic void __disa××e_interrupt(void);

__intrinsic istate_t __get_interrupt_state(void);
__intrinsic void __set_interrupt_state(istate_t);

__intrinsic void __op_code(unsigned short);

__intrinsic unsigned short __swap_bytes(unsigned short);

__intrinsic void __bic_SR_register(unsigned short);
__intrinsic void __bis_SR_register(unsigned short);
__intrinsic unsigned short __get_SR_register(void);
__intrinsic void __bic_SR_register_on_exit(unsigned short);
__intrinsic void __bis_SR_register_on_exit(unsigned short);
__intrinsic unsigned short __get_SR_register_on_exit(void);

__intrinsic unsigned short __bcd_add_short(unsigned short, unsigned short);
__intrinsic unsigned long __bcd_add_long (unsigned long , unsigned long);
__intrinsic unsigned long long __bcd_add_long_long(
unsigned long long, unsigned long long);

/* Support for efficient switch:es. E.g. switch(__even_in_range(TAIV, 10)) */
__intrinsic unsigned short __even_in_range(unsigned short, unsigned short);

/*
* The following intrinsic functions are only ××aila××e when the
* corresponding register is locked.
*/

__intrinsic unsigned short __get_R4_register(void);
__intrinsic void __set_R4_register(unsigned short);

__intrinsic unsigned short __get_R5_register(void);
__intrinsic void __set_R5_register(unsigned short);

__intrinsic unsigned short __get_SP_register(void);
__intrinsic void __set_SP_register(unsigned short);

/*
* The following functions are ××aila××e in extended mode (--core=430X)
* to access the upper memory above 64kB as a data area.
*/
__intrinsic void __data20_write_char (unsigned long addr, unsigned char data);
__intrinsic void __data20_write_short(unsigned long addr, unsigned short data);
__intrinsic void __data20_write_long (unsigned long addr, unsigned long data);
__intrinsic unsigned char __data20_read_char (unsigned long addr);
__intrinsic unsigned short __data20_read_short(unsigned long addr);
__intrinsic unsigned long __data20_read_long (unsigned long addr);

/*
* The following two functions can be used to access 20-bit SFRs in the
* lower 64kB. They are only ××aila××e in extended mode (--core=430X).
*/
__intrinsic void __data16_write_addr (unsigned short addr, unsigned long data);
__intrinsic unsigned long __data16_read_addr (unsigned short addr);


#ifdef __cplusplus
}
#endif


/*
* Alias for locations used for global register varia××es. For example,
* "__no_init __regvar int x @ __R4;".
*/

#define __R4 4
#define __R5 5

/*
* Control bits in the processor status register, SR.
*/

#define __SR_GIE (1<<3)
#define __SR_CPU_OFF (1<<4)
#define __SR_OSC_OFF (1<<5)
#define __SR_SCG0 (1<<6)
#define __SR_SCG1 (1<<7)


/*
* Functions for controlling the processor operation modes.
*/

#define __low_power_mode_0() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF))

#define __low_power_mode_1() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0))

#define __low_power_mode_2() (__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG1))

#define __low_power_mode_3() \
(__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1))

#define __low_power_mode_4() \
(__bis_SR_register( __SR_GIE \
| __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1 \
| __SR_OSC_OFF))

#define __low_power_mode_off_on_exit() \
(__bic_SR_register_on_exit( __SR_CPU_OFF \
| __SR_SCG0 \
| __SR_SCG1 \
| __SR_OSC_OFF))


#pragma language=default

#endif /* __INTRINSICS_H */
点赞  2006-8-28 10:42
好看点,如果用8m频率延时1ms时应该是__delay_cycles(8000);对吧
点赞  2006-9-4 17:21
请帮忙,我是刚开始用MSP430F149,我用了它的P5口和P4口做LED显示用的A-G段码和位选信号口,请问是否都要接上拉电阻,才能让它识别有确定的高电平呢?
点赞  2006-9-8 11:00
我自己写的延时函数不知道为什么就直接忽略了
点赞  2012-9-13 11:13
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复