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;
}
}
2 Answers 2
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.
-
\$\begingroup\$ May you are right, but still not working. \$\endgroup\$khoder akel– khoder akel2020年02月19日 12:36:53 +00:00Commented Feb 19, 2020 at 12:36
-
\$\begingroup\$ I edited it and editet the maping declaration to: int button_state_J6=PORTB2; but still give me random rading \$\endgroup\$khoder akel– khoder akel2020年02月19日 12:39:39 +00:00Commented Feb 19, 2020 at 12:39
-
\$\begingroup\$ @khoderakel Looks like mapping is correct. Could you share a schematic for your project? Have you exercised "Light_CMD_J6" to verify you can have control over GPIOs? \$\endgroup\$eeintech– eeintech2020年02月19日 13:52:40 +00:00Commented Feb 19, 2020 at 13:52
-
\$\begingroup\$ Yes you are right,No control over GPIO, only serial working fine. \$\endgroup\$khoder akel– khoder akel2020年02月19日 14:30:46 +00:00Commented Feb 19, 2020 at 14:30
-
\$\begingroup\$ Ha it's a good lead, did you have any other GPIOs you could try to control? \$\endgroup\$eeintech– eeintech2020年02月19日 22:00:22 +00:00Commented Feb 19, 2020 at 22:00
Thank you @Cisco25 for your time and help.. The problem in GPIO control was, when I flash hex file using atmel studio I selected invalid clock, the right setting is "Ext Crystal/Resonator Medium Frequency with start-up time ("16 CK + 64ms").
Explore related questions
See similar questions with these tags.