搜索
bottom↓
回复: 12

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

[复制链接]

出0入0汤圆

发表于 2012-2-18 12:48:34 | 显示全部楼层 |阅读模式
/* 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);   
}
欢迎测试!

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 2012-2-18 12:49:36 | 显示全部楼层
C51精确延时函数地址:
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=4925141&bbs_page_no=1&search_mode=3&search_text=wangqh1983&bbs_id=9999

出0入0汤圆

发表于 2012-2-18 13:38:25 | 显示全部楼层
即C51精确延时函数,再次写的PIC18精确延时函数

即——继
进来后才明白。

出0入0汤圆

 楼主| 发表于 2012-2-18 17:41:36 | 显示全部楼层

(原文件名:2012-2-18 17-41-27.png)

出0入0汤圆

 楼主| 发表于 2012-2-18 18:29:27 | 显示全部楼层
可以在头文件定义系统时钟!

(原文件名:2012-2-18 18-28-47.png)

出0入0汤圆

发表于 2012-2-18 19:49:06 | 显示全部楼层
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.

出0入0汤圆

发表于 2012-2-21 12:49:39 | 显示全部楼层
感谢!!

出0入0汤圆

 楼主| 发表于 2012-2-25 11:53:59 | 显示全部楼层
感谢你的评说,不过也确实有道理。
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.
但是这个形参如果输入0,将是错误的;不过也让人很难理解。不会有人需要0ms的延时吧。有些时候要求时序比较严格的情况下,我们可以将总中断关闭。

出0入0汤圆

发表于 2012-2-25 20:59:19 | 显示全部楼层
"不会有人需要0ms的延时吧。"

it is about the (predictable) behavior of the code: delay_ms(2) is 1 ms, delay_ms(1) is 1ms, delay_ms(0) is 256ms (or32kms, depending on the data type).

that kind of behavior is unexpected for most people.

出0入0汤圆

 楼主| 发表于 2012-2-26 07:25:27 | 显示全部楼层
it is about the (predictable) behavior of the code: delay_ms(2) is 1 ms

(原文件名:2012-2-26 7-23-49.png)
这个不会啊,但是输入0是错误的。

出0入0汤圆

 楼主| 发表于 2012-2-26 07:38:14 | 显示全部楼层
I would use "while (num1--) continue;" instead.

如果将代码改成while(number--);汇编代码将会增加,须从新计算Tcy。

出0入0汤圆

发表于 2014-3-27 13:38:48 | 显示全部楼层
感谢分享!

出0入0汤圆

发表于 2014-3-27 15:58:10 | 显示全部楼层
考虑到中断了吗?呵呵
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-22 08:38

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表