《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

谈精确定时
程序匠人 发表于 2005-10-22 19:49:00  阅读全文 | 回复(0) | 引用通告 | 编辑

谈精确定时!--转载国外网站


虽然是英文,但读起来还不是很难,我这板水平都应付了,相信您更没问题!
还是那句话写给菜鸟,大虾别见笑!
How can I use my crystal for high precision timebase ?
Introduction:
O.K. its easy to divide exact 12.000000MHz. But how using other crystals, or
correct the deviation ?

The way is always the same:
First on 8051 processors, the crystal was divided to give the timer clock. On
T0, T1 of 80C51 devices by 12, on the 80C52 for timer T2 by 2, also in the
Dallas DS80C320 at T0, T1 by 4.
Then you can use the timers to count 8 or 16 bit. The remaining divider stages
can be realized in software. On using only 8 of the 128 bytes of the 8051 you
can count up to 2 Giga years (the 8051 was dust, before this time was gone)
with a 40MHz crystal !
In most cases you get no exact divider values. But this is not important, you
must then calculate the error and can correct it, if needed. E.g. if the second
differ, nobody can detect it. But if the Error was accumulated over 1 month or
year, it can be seen. So correction can be done every second, minute, hour or
day. So the error was not accumulated and also not detected.


Lets Practice:
Following an example with assembler code:

Random choice of a crystal: 12345701 Hz (use every other crystal for your
calculation)

Crystal / 12 = 1028808.417
Using Timer T0: Crystal / 12 / 65536 = 15.69837062
So you need an additional Byte in RAM to divide down to 1 second. If this byte
divide by 256 you must not reload it.
Then the reload value of the T0: Crystal / 12 / 256 = 4018.782878 rounded to
4019.

The resulting error is: 1 - 4018.782878 / 4019 = 0.000054 = 5 sec / day = 140
sec / month.
This is to high for using as RTC. So every second we use a different T0 reload
value:
Crystal / 12 - 255 * 4019 = 3963.41667 rounded to 3963.

Then the resulting error is: 1 - 1028808.417 / (255 * 4019 + 3963) = 0.0000004
= 1 sec / month = 12 sec / year.
This is enough for most RTC applications. But additional correction every
minute can reduce it further.
For extreme high accuracy also deviation over temperature was important and
must be corrected.

Programming example:
The C example is easiest to use, since it can make all calculations for you.
You must only tell the real crystal frequency, thats all.


t0_int2.a51
t0_int2.c51
Correct deviation:
To correct the deviation you need a very high accurate and also very expensive
frequency meter to count the real crystal frequency with precision over more
than 10 digits.
The cheaper way was, let the RTC running over several months. Then the
deviation can be measured in seconds to calculate the true frequency.
Repeat the previous calculation with this real frequency and program the new
values into the micro.
E.g. you found an error of +12 seconds during 30 days. Then the true frequency
was:
12.345701 Hz * (1 + 12 / (30 * 24 * 60 * 60)) = 12345758Hz. Use this to repeat
the above described calculation and further the accuracy was very good.

 

 

;************************************************************************/
;*
*/
;* Software reload Example using Timer 0
*/
;* */
;* Author: Peter Dannegger */
;* danni@specs.de */
;* */
;************************************************************************/

org 0
jmp init
org $+5*8+3 ;reserved for interrupt vectors

init: mov tmod, #1 ;set Timer 0 mode 1
mov tl0, #0FFh
mov th0, #0FFh ;overflow on next cycle
setb tr0
clr f1sec
mov Divider_1s, #0
mov ie, #10000010b

main: jbc f1sec main1 ;test and clear
jmp main

main1: cpl p1.0 ;change every 1 second
jmp main


;Timebase 1 second at 12.345701 MHz:
; 12.3456701 MHz / 12 / (255 * 4019 + 3963) = 1 second
;Deviation: 12 seconds / year


T0_reload equ 4019
T0_sec_reload equ 3963

bseg at 0
F1sec: dbit 1

dseg at 30h
Divider_1s: ds 1
cseg

curr_adr equ $

org 000Bh

jmp Timer0_interrupt

org curr_adr

Timer0_interrupt:

push psw
djnz divider_1s, ?TI1
;done 1 time every second:

clr ea ;no additional delay by other
interrupts
clr tr0 ;no overflow during addition
xch a, tl0
add a, #low(8-T0_sec_reload) ;stop for 8 cycle
xch a, tl0
xch a, th0
addc a, #high(8-T0_sec_reload)
xch a, th0
setb ea ;other interrupts enabled after next
instr.
setb tr0
pop psw
setb F1sec ;say to main: 1 second was finished
;must be cleared my main
reti

;done 255 times every second:

?TI1: clr ea ;no additional delay by other
interrupts
clr tr0 ;no overflow during addition
xch a, tl0
add a, #low(8-T0_reload) ;stop for 8 cycle
xch a, tl0
xch a, th0
addc a, #high(8-T0_reload)
xch a, th0
setb ea ;other interrupts enabled after next
instr.
setb tr0
pop psw
reti

end

 


/************************************************************************/
/* */
/* High Precise Time Base
*/
/* */
/* Author: Peter Dannegger */
/* danni@specs.de */
/* */
/************************************************************************/
#pragma cd pl(30000)


#define uchar unsigned char
#define uint unsigned int

#i nclude

/************************************************************************/
/*
*/
/* Insert your crystal frequency and you get exact 1 second */
/* without need understanding why :
*/
/*
*/
/**/ #define XTAL 12345701L
/**/
/*
*/
/************************************************************************/

#define T0_RELOAD ((uint) (XTAL / 12.0 / 256.0 + 0.5))

#define T0_RELOAD_1S ((uint) (XTAL / 12.0 - 255.0 * T0_RELOAD + 0.5))

#define T0_STOP_CYCLES 16 // calculated
with list file

bit F_1second = 0;

union bw{
int w;
struct{
char h;
char l;
}b;
};

void t0_int( void ) interrupt 1
{
static uchar prescaler = 0;
union bw i;
if( --prescaler == 0 ){
EA = 0;
TR0 = 0;
i.b.l = TL0;
i.b.h = TH0;
i.w += -T0_RELOAD_1S + T0_STOP_CYCLES; // 1 * every second
TL0 = i.b.l;
TH0 = i.b.h;
EA = 1;
TR0 = 1;
F_1second = 1;
}else{
EA = 0;
TR0 = 0;
i.b.l = TL0;
i.b.h = TH0;
i.w += -T0_RELOAD + T0_STOP_CYCLES; // 255 * every second
TL0 = i.b.l;
TH0 = i.b.h;
EA = 1;
TR0 = 1;
}
}
 
 
 

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!