继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);   
}
欢迎测试!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
13条回答
millwood0
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.

一周热门 更多>