搜索
bottom↓
回复: 4

用M48内部时钟做程序时间为1秒但实际为1.6秒,请各位大哥指教(有具体代码)

[复制链接]

出0入0汤圆

发表于 2009-4-16 15:24:59 | 显示全部楼层 |阅读模式
//各位大哥,小弟有一问题向大家指教,用定时器做灯闪的程序,原本意图是
//1秒钟闪一下,但最终结果是1.6秒闪一下(用秒表计时的,大概测一下)
//ICC-AVR application builder : 2009-4-16 14:56:33
// Target : M48
// Crystal: 1.0000Mhz
// 实验目的使PB6驱动的LED灯1秒钟闪一下//
#include <iom48v.h>
#include <macros.h>
unsigned char j;
unsigned char i;
void port_init(void)
{
PORTB = 0x40;
DDRB  = 0x40;
PORTC = 0x00; //m103 output only
DDRC  = 0x00;
PORTD = 0x00;
DDRD  = 0x00;
}

//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 10mSec
// actual value:  9.984mSec (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x64; //set count
TCCR0A = 0x00;
TCCR0B = 0x03; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
j=1;
//TIMER0 has overflowed
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();

MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;

TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources

PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

//
void main(void)
{
init_devices();
while(1)
    {
        if(j==1)
           {
            j=0;
                i++;
                if(i>=100)//计数1秒
                  {
                  i=0;
                  PORTB^=0x40;//端口反转一次
          }
            }
     }
//insert your functional code here...
}
//实际做出来的动作大约是1.6秒闪一下,请各位大哥指教,熔丝里设置的是8M,8分频

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

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2009-4-16 15:45:02 | 显示全部楼层
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
       if(++i>=100)//计数1秒
                  {
                  i=0;
                  PORTB^=0x40;//端口反转一次
          }
TCNT0 = 0x64;//TIMER0 has overflowed
}

void main(void)
{
init_devices();
while(1)  ;    //insert your functional code here...
}

试试这样会不会好一点。

出0入0汤圆

 楼主| 发表于 2009-4-16 15:54:26 | 显示全部楼层
结果还是一样的

出0入0汤圆

 楼主| 发表于 2009-4-16 16:00:27 | 显示全部楼层
对了,忘了写“TCNT0 = 0x64;”了,请问大虾这个是怎么算出来的

出0入0汤圆

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

本版积分规则

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

GMT+8, 2024-7-23 07:15

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

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