-1

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.

  • 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 – jsotola Mar 29 at 8:09
  • thank you for support – Hrishikesh Dixit Apr 2 at 5:11
  • @jsotola can you please review the changed code and share your suggestion? – Hrishikesh Dixit Apr 2 at 6:58
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 )

| improve this answer | |
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

| improve this answer | |
  • 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 Apr 3 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.