搜索
bottom↓
回复: 3

仿马老师书中的第十一章测频法测量频率测不准

[复制链接]

出0入0汤圆

发表于 2009-7-7 21:03:32 | 显示全部楼层 |阅读模式
/*****************************************************
Chip type           : ATtiny2313V
Clock frequency     : 4.000000 MHz
Memory model        : Tiny
External SRAM size  : 0
Data Stack size     : 32
*****************************************************/
#include <tiny2313.h>
#include <stdio.h>                  
                     
#define uchar unsigned char  
bit time_1ms_ok;
unsigned int  time0_old,time0_new;
unsigned int freq;      
unsigned char freq_time;

// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void) //TO为定时用
{
time0_new=TCNT1;
time_1ms_ok=1;

}

void measure_freq(void);


void main(void)
{

#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif



// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x01;
TCNT0=0x00;
OCR0A=0x80; //改变此值,可是测得的频率无变化
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: T1 pin Falling Edge
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x06;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

GIMSK=0x00;
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

USICR=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 4800
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x33;


ACSR=0x80;

#asm("sei")
time0_old=0;
while (1)
      {
   
       measure_freq();
   
      };
}     

void measure_freq(void)
{
   if(time_1ms_ok)
     {
       if(time0_new>=time0_old)
         freq=freq + (time0_new - time0_old);
       else
         freq=freq + (65536 - time0_old + time0_new);
       time0_old=time0_new;
       if(++freq_time>=50) //改变此值50,测得的频率无变化
        {
          freq_time=0;   
         printf("B%i\n\r",freq);
      
          freq=0;
        }
       time_1ms_ok=0;
   }
}

出0入0汤圆

 楼主| 发表于 2009-7-7 21:07:00 | 显示全部楼层

(原文件名:b.JPG)
上图为在串口中显示的频率值

出0入0汤圆

 楼主| 发表于 2009-7-9 22:15:51 | 显示全部楼层
等待中

出0入0汤圆

 楼主| 发表于 2009-11-3 18:54:57 | 显示全部楼层
是程序问题,经过马老师指点不应用printf,后来改用UDR来发送,已能测出频率。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-2 15:43

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

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