继C51精确延时函数,再次写的PIC18精确延时函数

2020-02-07 09:39发布

/* PIC18 software interface header */
#define Fosc 11059200                    //XTAL = 11.0592MHz
#define Fcy    (Fosc/4)                    //2764800Hz

void
User_Delay_Ms(uchar number)    //delay=((((num1*4)+4)*num2+6)*number+13)/Fcy        
{
    static unsigned char     num1,
                         num2;
    do
    {
        num2 = 10;
        do
        {
            num1 = Fcy/40322;   
            while(--num1);   
        }while(--num2);   
    }while(--number);   
}
欢迎测试!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
wangqh1983
1楼-- · 2020-02-07 14:32
 精彩回答 2  元偷偷看……
JQ_Lin
2楼-- · 2020-02-07 16:24
即C51精确延时函数,再次写的PIC18精确延时函数

即——继
进来后才明白。
wangqh1983
3楼-- · 2020-02-07 20:42

(原文件名:2012-2-18 17-41-27.png)
wangqh1983
4楼-- · 2020-02-07 22:53
可以在头文件定义系统时钟!

(原文件名:2012-2-18 18-28-47.png)
millwood0
5楼-- · 2020-02-08 02:40
usually, you don't need that precise of a delay: something in the rough neighbor would typically work.

plus, if you have interrupt,  your delay routines will produce different actual delays.

"num1 = Fcy/40322;     "

I would redefine 40322 so that it can be easily changed when you move the routine to a different chip / architecture.

"            while(--num1);     "

this will create a problem when num1 is calculated to be 0 - from very low Fcy for example. I would use "while (num1--) continue;" instead.

I use a similar approach, except that mine was built up, from delay(cycles); to delay_us(us) which calls delay() and then delay_ms(ms) which calls delay_us(), all of which is based on pre-defined F_CPU.

this allows the code to be fairly easily ported.
chenhuwyl
6楼-- · 2020-02-08 05:19
 精彩回答 2  元偷偷看……

一周热门 更多>