ATMEGA32保险丝位设置,低保险丝和高保险丝,16MHz,外部晶振?

时间:2017-07-19 10:10:59

标签: c embedded avr atmega

我正在使用ATMEGA32与水晶16MHz 我编写了以下代码来生成1kHz wave。

我尝试写Low- 0xFF High-0x99
但我无法得到1kHz wave。我得到了250Hz wave。而不是1kHz

请帮忙。

#define F_CPU 16000000UL 
#include <avr/io.h> //io related 
#include <util/delay.h> //delay
#include "lcd.h" //custom lib for lcd
#include <stdlib.h> //for string handling
#ifndef sbit_h
#define sbit_h
int main(void)
{

    //All Declarations Go here.

   OCR0=p*127/100; //set pwm1 duty cycle
   OCR2=OCR0;      //set pwm2 duty cycle (Output compare register)

   while(1)
   { 
      if ((bit_is_set(PIND, 3 )))
       {
           pinChange(LED8,1); //Turn on solenoid valve
           pinChange(LED1,1);
           //PORTC|=(1<<PC3);
           _delay_ms(sq); //wait for squeeze time
           TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS01)|(1<<CS00); 
           //initialise timer in PWM mode 1kHz//cs00 and cs02
           _delay_us(500);//90 degree phase shift
           TCCR2 |= (1<<WGM20)|(1<<COM21)|(1<<WGM21)|(1<<CS22); //initialise timer2 in PWM mode 1kHz//CS22 for 1kHz
           _delay_ms(w1); //wait until weld time over
           TCCR0=0x00;  //stop PWM1
           TCCR2=0x00;  //Stop PWM2
           _delay_ms(h); //wait for hold time
           pinChange(LED8,0); //turn off solenoid valve
           pinChange(LED1,0);
           TCNT0=0x00; //Reset timer counter
           TCNT2=0x00; //reset timer counter
     }
}

1 个答案:

答案 0 :(得分:0)

您是否以正确的方式设置了时钟选择fuse bits(以及可能的时钟分频器)?我开始检查CKSEL0...3CKDIV8

根据我的经验,获得所需time/frequency的精确倍数几乎总是基于错误的时钟设置。