Electronic – arduino – Programming the ATmega8 board using Arduino IDE

arduinoatmegaavrdudemicrocontroller

I made a board with an ATmega8A (SMD-TQFP package). I installed the minicore library in the Arduino IDE. Now I'm able to flash the hex file using Atmel Studio 7 and able to read serial well.

But my problem is reading the digital pin status. It doesn't work well. I searched the internet and in the minicore library, I found that in the ATmega8, pin 14 (PORT B.2) is digital pin 10. So I wrote my code, however the pin does not change its status when toggling it.

Is it a mapping problem? If so, how can I map it?

#include <TimerOne.h>

int setbit = 0;//timer 2 isr
int setbit1 = 0;
int flag=0;
int flag1=0,k=0;
int flag2=0; int Light_CMD_J6=9;
int Button_Status_J6=10;

void setup() { 
    Timer1.initialize(100000); // set a timer of length 
    Timer1.attachInterrupt( timerIsr ); //  
    Serial.begin(19200);
    pinMode(Light_CMD_J6,OUTPUT);
    pinMode(Button_Status_J6,INPUT);

}

void timerIsr()
{  
    setbit1=setbit1+1;
    if(setbit1==10)//2 seconds
    { 
        setbit1=0;
        flag2=1;
    }
}

void loop() {
    if(flag2==1)
    {
        Button_Status_J6=digitalRead(Button_Status_J6);
        Serial.print("BUTTON 6 STATE IS   =  ");
        Serial.println(Button_Status_J6);
        flag2=0;
    }
}

Best Answer

I haven't verified the mapping but I know that this line will cause you an issue:

Button_Status_J6=digitalRead(Button_Status_J6);

Here you are basically overwriting your mapping to digital pin 10, returning the result of the digitalRead into the variable Button_Status_J6, which will become either 0 or 1 on the first read.

At the top of your code, you would need to set-up another variable to read the result of the digitalRead:

int Button_Status_Read=0;

And then use it to read digital pin 10 (Button_Status_J6):

Button_Status_Read=digitalRead(Button_Status_J6);
Serial.print("BUTTON 6 STATE IS   =  ");
Serial.println(Button_Status_Read);

EDIT

The other issue was that the wrong clock source was selected for the fuse setting "SUT_CKSEL". When using an 8MHz external Crystal the following clock source needs to be selected:

Ext Crystal/Resonator Medium Frequency

To start with, it is recommended to choose the option with the maximum start-up time ("16 CK + 64ms") to ensure the crystal oscillation has enough time to stabilize. After validation, faster start-up time can be selected.