Please refer my below code,
const int led_pin = PB5;
const uint16_t t1_load = 0;
const uint16_t t1_comp = 43750;
int blinkcount;
void setup() {
// put your setup code here, to run once:
DDRB |= (1<< led_pin);
TCCR1A = 0;
//TCCR1B = 0X03;
TCCR1B |= (1 << CS12);
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS10);
TCNT1 = t1_load;
OCR1A = t1_comp;
TIMSK1 = (1 << OCIE1A);
Serial.begin(9600);
sei();
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
}
ISR(TIMER1_COMPA_vect) {
TCNT1 = t1_load;
PORTB ^= (1 << led_pin);
blinkcount = blinkcount + 1;
Serial.println(blinkcount);
}
the Serial.println(blinkcount); not working or the serial window does not show up.
Apologies if it sound stupid as I'm new to programming.
-
2please format your code .... you don't print inside ISR .... increment variable and set a second flag variable instead ... check for flag in loop() and print if the flag is setjsotola– jsotola2020年03月29日 08:09:16 +00:00Commented Mar 29, 2020 at 8:09
-
@jsotola can you please review the changed code and share your suggestion?Fatpanda– Fatpanda2020年04月02日 06:58:18 +00:00Commented Apr 2, 2020 at 6:58
2 Answers 2
ISR are simply not intended for this abuse.
If you want to use Arduino level functions, you must have good reasons why you need to mix them with direct arv-gcc hardware-accessing code (or vice versa).
If you're new to programming and to Arduino, simply ignore that level (for now).
DDRB |= BV(PB5);
is easier understandable if you write
pinMode(13,OUTPUT);
( or even use LED_BUILTIN instead of 13 )
const uint16_t t1_load = 0;
const uint16_t t1_comp = 43750;
int blinkcount;
int flag, flag_1, a, i;
void setup() {
// put your setup code here, to run once:
DDRB |= (1<< led_pin);
TCCR1A = 0;
//TCCR1B = 0X03;
TCCR1B |= (1 << CS12); //for 256 prescalar
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS10);
/* TCCR1B &= ~ (1 << CS12);
TCCR1B |= (1 << CS11);
TCCR1B &= ~(1 << CS10);*/
TCNT1 = t1_load;
OCR1A = t1_comp;
TIMSK1 = (1 << OCIE1A);
Serial.begin(9600);
sei();
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
/* blinkcount = blinkcount + 1;
if(blinkcount++)
{
flag = 1;
}
if(flag == 1)
{
Serial.println("ok");
}*/
}
void loop_1()
{
i = blinkcount;
//for(i=0; i<=blinkcount; i++);
//{
i = i + 1;
//if(i++)
// {
flag = 1;
flag_1 = flag;
//}
if(flag_1 == 1)
{
Serial.println(flag_1);
}
// }
}
ISR(TIMER1_COMPA_vect) {
TCNT1 = t1_load;
PORTB ^= (1 << led_pin);
//blinkcount = blinkcount + 1;
//Serial.println(blinkcount);
loop_1();
}```
I've changed the code according to your suggestion, the tx led on arduino blinks but serial monitor does not pop up
-
loop_1 is (logically) still part of the ISR. Serial.println() not working in ISR is a fact, not a question. Don't use ISRs if you don't need OR understand it. In your case both conditions apply. Sorry.DataFiddler– DataFiddler2020年04月03日 12:38:31 +00:00Commented Apr 3, 2020 at 12:38