www.openedv.com
您好,欢迎您!    会员注册 | 登入 
公告:欢迎访问www.openedv.com开源电子网,开源共享共同进步,祝您新年快乐,万事如意!
设为首页 | 加入收藏
论坛首页 » AVR单片机 前往: 
关于cvavr的向导设置问题
发表人 内容
[Down] [Up]
[楼主位] feifeimao

等级:NO
注册时间:
2010/12/21 11:16
文章: 3
离线

我现在需要timer1  定时1s 中断  ;;timer2 CTC 产生方波  频率100k

晶振是11.0592   6分频


[加为好友] 回复 引用回复
[Down] [Up]
[1楼] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

回复【楼主位】 feifeimao :
-----------------------------
你的时钟6分频么?
怎么做到的?AVR只能时钟1,2,4,8...这样分频的,没看到有能6分频的。



我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
[Down] [Up]
[2楼] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

这里我用MEGA88做个生成的范例。实际代码未验证:
时钟设置如图1:


图1 时钟配置


    上图中,配置mega88的时钟为11.0592M/8.这样得到时钟频率为:1.3824Mhz
     然后,我们再来配置TIM1,如图2所示:


图2 tim1配置


     图2中,配置TIM1的时钟频率为5.4Khz,并设置没5400个时钟,就中断一次,这样得到1s的中断信号,在中断里面,使得OC1A取反。
     再来看tim2的设置,如图3所示:




图3 tim2配置


    图3中,我们配置TIM2工作在CTC模式,时钟频率为1.3824Mhz,计时器从0计数到OCR2A,然后不产生 匹配中断,但是一旦发生匹配,就取反OC2A,我们设置比较值OCR2A为6.这样,每计数6个时钟,就会在OC2A取反一次,得到100Khz左右的取反频率。

    基本达到楼主的要求,因为时钟选的很鸡肋,所以没办法做到准确的100Khz。
CVAVR 2.04.4a生成的代码如下:
#include <mega88.h>

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1H=0xEAE8 >> 8;
TCNT1L=0xEAE8 & 0xff;
// Place your code here

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 8
#pragma optsize-
CLKPR=0x80;
CLKPR=0x03;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=0 State0=T
PORTB=0x00;
DDRB=0x0A;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 5.400 kHz
// Mode: Normal top=FFFFh
// OC1A output: Toggle
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x40;
TCCR1B=0x04;
TCNT1H=0xEA;
TCNT1L=0xE8;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 1382.400 kHz
// Mode: CTC top=OCR2A
// OC2A output: Clear on compare match
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x82;
TCCR2B=0x01;
TCNT2=0x00;
OCR2A=0x06;
OCR2B=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x00;
EIMSK=0x00;
PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x01;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here

      };
}

 




我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
[Down] [Up]
[3楼] feifeimao

等级:NO
注册时间:
2010/12/21 11:16
文章: 3
离线

请问(1) 图2中clock value是随便选取的吗?
    


[加为好友] 回复 引用回复
[Down] [Up]
[4楼] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

回复【3楼】 feifeimao :
-------------------------------
clock value 不是随意设置的,这个clock其实就时钟分频后的值。AVR的外设,不是有时钟分频器么?比如1,2,4,8,16之类的分频。这个clock=主时钟/分频因子。



我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
[Down] [Up]
[5楼] feifeimao

等级:NO
注册时间:
2010/12/21 11:16
文章: 3
离线

   图2中,配置TIM1的时钟频率为5.4Khz   我是问这个


[加为好友] 回复 引用回复
[Down] [Up]
[6楼] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

回复【5楼】 feifeimao :
-------------------------------
一样的



我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
 
前往: 

Powered by ALIENTEK工作室 © 粤ICP备12000418号-1