0

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.

Juraj
18.3k4 gold badges31 silver badges49 bronze badges
asked Mar 29, 2020 at 7:32
2
  • 2
    please 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 set Commented Mar 29, 2020 at 8:09
  • @jsotola can you please review the changed code and share your suggestion? Commented Apr 2, 2020 at 6:58

2 Answers 2

0

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 )

answered Mar 29, 2020 at 15:47
0
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
answered Apr 2, 2020 at 6:51
1
  • 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. Commented Apr 3, 2020 at 12:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.