Home > Blockchain >  Atmega32A with 16MHZ crystal oscillator runs slowly
Atmega32A with 16MHZ crystal oscillator runs slowly

Time:12-17

I have decided to program Atmega32A in Arduino to enjoy its numerous I/O pins. To achieve that, I specified the board definition in the boards.txt file and also defined the pin mappings in the pins_arduino.h. However, the microcontroller runs slowly as if the external crystal oscillator does not take effect. The board definitions and pin mappings are shown below:

Atmega32A definition in boards.txt file:

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:     [email protected]
    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

My problem here is, calling delay(100) function causes delay of more than 1000 milli seconds instead of 100 milli seconds. What do I do to get correct clock frequency according to 16MHZ oscillator?

I tried to change the crystall oscillator but nothing changes.

CodePudding user response:

AFAIK, there's only few things that might impact incorrect delay:

  • F_CPU, which is present in your config and I believe already passed to your compiler - I would check this.
  • Clock fuse settings - check CKDIV8, etc.
  • Clock related registers - check CLKPR, CLKPCE, etc. Note, this step might be not required if fuses are programmed for a correct speed.
  • Wrong value of an oscillator.

CodePudding user response:

Thanks a lot to everyone who helped me in one way or the other. I was able to get the microcontroller working with 16MHZ external oscillator by taking the following steps:

  1. Installed AVRDUDE
  2. Connected USBtinyISP to the microcontroller through ISP interface.
  3. Run the following command in the command prompt to set low and high fuses of the microcontroller: avrdude -c usbtiny -p m32 -U lfuse:w:0xbf:m -U hfuse:w:0xce:m
  • Related