簡體   English   中英

Atmega32A配16MHZ晶振運行慢

[英]Atmega32A with 16MHZ crystal oscillator runs slowly

我決定在 Arduino 中對 Atmega32A 進行編程,以享受其眾多的 I/O 引腳。 為此,我在 boards.txt 文件中指定了電路板定義,還在 pins_arduino.h 中定義了引腳映射。 但是,單片機運行緩慢,好像外部晶振沒有生效。 電路板定義和管腳映射如下所示:

boards.txt 文件中的 Atmega32A 定義:

mega32-16.name=ATmega32-External 16mhz
mega32-16.build.mcu=atmega32
mega32-16.build.f_cpu=16000000L
mega32-16.build.core=arduino:arduino
mega32-16.build.variant=mega32


mega32-16.upload.tool=avrdude
mega32-16.upload.protocol=arduino
mega32-16.upload.maximum_size=32000

mega32-16.bootloader.low_fuses=0xEF
mega32-16.bootloader.high_fuses=0x89
#mega32-16.bootloader.extended_fuses=0xFD

**pins_arduino.h **

/*
    Author:    Eric Conner
    Email:     Eric@EricConnerApps.com
    Date:      06-10-2013
    Project:   ATmega32 for Arduino IDE
    Version:   v1.2
*/

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#include <avr/pgmspace.h>

#define NOT_ON_TIMER 0
#define TIMER0A 1
#define TIMER0B 2
#define TIMER1A 3
#define TIMER1B 4
#define TIMER2  5

#undef TCCR2A

#define NOT_A_PORT 0
#define NOT_ON_TIMER 0

/*
                        ATMEL ATmega32
                       
                          +---\\---+
       (XCK/T0) D0 PB0  01|        |40  PA0 AI7 D31 (ADC0)
           (T1) D1 PB1  02|        |39  PA1 AI6 D30 (ADC1)
    (INT2/AIN0) D2 PB2  03|        |38  PA2 AI5 D29 (ADC2)
     (OC0/AIN1) D3 PB3  04|        |37  PA3 AI4 D28 (ADC3)
           (SS) D4 PB4  05|        |36  PA4 AI3 D27 (ADC4)
         (MOSI) D5 PB5  06|        |35  PA5 AI2 D26 (ADC5)
         (MISO) D6 PB6  07|        |34  PA6 AI1 D25 (ADC6)
          (SCK) D7 PB7  08|        |33  PA7 AI0 D24 (ADC7)
                 RESET  09|        |32  AREF
                   VCC  10|        |31  GND
                   GND  11|        |30  AVCC
                 XTAL2  12|        |29  PC7 D23 (TOSC2)
                 XTAL1  13|        |28  PC6 D22 (TOSC1)
          (RXD) D8 PD0  14|        |27  PC5 D21 (TDI)
          (TXD) D9 PD1  15|        |26  PC4 D20 (TDO)
        (INT0) D10 PD2  16|        |25  PC3 D19 (TMS)
        (INT1) D11 PD3  17|        |24  PC2 D18 (TCK)
        (OC1B) D12 PD4  18|        |23  PC1 D17 (SDA)
        (OC1A) D13 PD5  19|        |22  PC0 D16 (SCL)
        (ICP1) D14 PD6  20|        |21  PD7 D15 (OC2)
                          +--------+
*/



const static uint8_t SDA = 17; //correct
const static uint8_t SCL = 16; //correct

const static uint8_t SS   = 4;
const static uint8_t MOSI = 5;
const static uint8_t MISO = 6;
const static uint8_t SCK  = 7;

#define NUM_DIGITAL_PINS            32
#define NUM_ANALOG_INPUTS           8
#define analogInputToDigitalPin(p)  ((p < 8) ? (p) + 24 : -1)
#define digitalPinHasPWM(p)         ((p) == 4 || (p) == 5 || (p) == 7)

#ifdef ARDUINO_MAIN

const uint16_t PROGMEM port_to_mode_PGM[] = {
    NOT_A_PORT,
    (uint16_t) &DDRA,
    (uint16_t) &DDRB,
    (uint16_t) &DDRC,
    (uint16_t) &DDRD,
    NOT_A_PORT,
    NOT_A_PORT,
};

const uint16_t PROGMEM port_to_output_PGM[] = {
    NOT_A_PORT,
    (uint16_t) &PORTA,
    (uint16_t) &PORTB,
    (uint16_t) &PORTC,
    (uint16_t) &PORTD,
    NOT_A_PORT,
    NOT_A_PORT,
};

const uint16_t PROGMEM port_to_input_PGM[] = {
    NOT_A_PORT,
    (uint16_t) &PINA,
    (uint16_t) &PINB,
    (uint16_t) &PINC,
    (uint16_t) &PIND,
    NOT_A_PORT,
    NOT_A_PORT,
};

const uint8_t PROGMEM digital_pin_to_port_PGM[32] = {
    PB,  // PB0 ** D0
    PB,  // PB1 ** D1
    PB,  // PB2 ** D2
    PB,  // PB3 ** D3
    PB,  // PB4 ** D4
    PB,  // PB5 ** D5
    PB,  // PB6 ** D6
    PB,  // PB7 ** D7
    PD,  // PD0 ** D8
    PD,  // PD1 ** D9
    PD,  // PD2 ** D10
    PD,  // PD3 ** D11
    PD,  // PD4 ** D12
    PD,  // PD5 ** D13
    PD,  // PD6 ** D14
    PD,  // PD7 ** D15
    PC,  // PC0 ** D16
    PC,  // PC1 ** D17
    PC,  // PC2 ** D18
    PC,  // PC3 ** D19
    PC,  // PC4 ** D20
    PC,  // PC5 ** D21
    PC,  // PC6 ** D22
    PC,  // PC7 ** D23
    PA,  // PA7 ** A0 D24
    PA,  // PA6 ** A1 D25
    PA,  // PA5 ** A2 D26
    PA,  // PA4 ** A3 D27
    PA,  // PA3 ** A4 D28
    PA,  // PA2 ** A5 D29
    PA,  // PA1 ** A6 D30
    PA,  // PA0 ** A7 D31
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[32] = {   
    _BV(0),  // PB0 ** D0
    _BV(1),  // PB1 ** D1
    _BV(2),  // PB2 ** D2
    _BV(3),  // PB3 ** D3
    _BV(4),  // PB4 ** D4
    _BV(5),  // PB5 ** D5
    _BV(6),  // PB6 ** D6
    _BV(7),  // PB7 ** D7
    _BV(0),  // PD0 ** D8
    _BV(1),  // PD1 ** D9
    _BV(2),  // PD2 ** D10
    _BV(3),  // PD3 ** D11
    _BV(4),  // PD4 ** D12
    _BV(5),  // PD5 ** D13
    _BV(6),  // PD6 ** D14
    _BV(7),  // PD7 ** D15
    _BV(0),  // PC0 ** D16
    _BV(1),  // PC1 ** D17
    _BV(2),  // PC2 ** D18
    _BV(3),  // PC3 ** D19
    _BV(4),  // PC4 ** D20
    _BV(5),  // PC5 ** D21
    _BV(6),  // PC6 ** D22
    _BV(7),  // PC7 ** D23
    _BV(7),  // PA7 ** A0 D24
    _BV(6),  // PA6 ** A1 D25
    _BV(5),  // PA5 ** A2 D26
    _BV(4),  // PA4 ** A3 D27
    _BV(3),  // PA3 ** A4 D28
    _BV(2),  // PA2 ** A5 D29
    _BV(1),  // PA1 ** A6 D30
    _BV(0),  // PA0 ** A7 D31
};

const uint8_t PROGMEM digital_pin_to_timer_PGM[32] = {
    NOT_ON_TIMER,  // PB0 ** D0
    NOT_ON_TIMER,  // PB1 ** D1
    NOT_ON_TIMER,  // PB2 ** D2
    TIMER0A,       // PB3 ** D3
    NOT_ON_TIMER,  // PB4 ** D4
    NOT_ON_TIMER,  // PB5 ** D5
    NOT_ON_TIMER,  // PB6 ** D6
    NOT_ON_TIMER,  // PB7 ** D7
    NOT_ON_TIMER,  // PD0 ** D8
    NOT_ON_TIMER,  // PD1 ** D9
    NOT_ON_TIMER,  // PD2 ** D10
    NOT_ON_TIMER,  // PD3 ** D11
    TIMER1B,       // PD4 ** D12
    TIMER1A,       // PD5 ** D13
    NOT_ON_TIMER,  // PD6 ** D14
    TIMER2,        // PD7 ** D15
    NOT_ON_TIMER,  // PC0 ** D16
    NOT_ON_TIMER,  // PC1 ** D17
    NOT_ON_TIMER,  // PC2 ** D18
    NOT_ON_TIMER,  // PC3 ** D19
    NOT_ON_TIMER,  // PC4 ** D20
    NOT_ON_TIMER,  // PC5 ** D21
    NOT_ON_TIMER,  // PC6 ** D22
    NOT_ON_TIMER,  // PC7 ** D23
    NOT_ON_TIMER,  // PA7 ** A0 D24
    NOT_ON_TIMER,  // PA6 ** A1 D25
    NOT_ON_TIMER,  // PA5 ** A2 D26
    NOT_ON_TIMER,  // PA4 ** A3 D27
    NOT_ON_TIMER,  // PA3 ** A4 D28
    NOT_ON_TIMER,  // PA2 ** A5 D29
    NOT_ON_TIMER,  // PA1 ** A6 D30
    NOT_ON_TIMER,  // PA0 ** A7 D31
};

#endif
#endif

我的問題是,調用 delay(100) 函數導致延遲超過 1000 毫秒而不是 100 毫秒。 如何根據 16MHZ 振盪器獲得正確的時鍾頻率?

我試圖改變晶體振盪器但沒有任何改變。

AFAIK,只有幾件事可能會影響不正確的延遲:

  • F_CPU ,它存在於您的配置中,我相信已經傳遞給您的編譯器 - 我會檢查一下。
  • 時鍾熔絲設置 - 檢查CKDIV8等。
  • 與時鍾相關的寄存器 - 檢查 CLKPR、CLKPCE 等。注意,如果熔絲被編程為正確的速度,則可能不需要此步驟。
  • 振盪器的錯誤值。

非常感謝所有以某種方式幫助過我的人。 通過執行以下步驟,我能夠讓微控制器與 16MHZ 外部振盪器一起工作:

  1. 安裝AVRDUDE
  2. 通過 ISP 接口將 USBtinyISP 連接到微控制器。
  3. 在命令提示符下運行以下命令來設置微控制器的低保險絲和高保險絲: avrdude -c usbtiny -p m32 -U lfuse:w:0xbf:m -U hfuse:w:0xce:m

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM