Go Down

Topic: Timer2 interrupts and Serial.println interferrence (Read 158 times) previous topic - next topic

tangent2

Board:  Arduino  UNO.
Micro-controller on board:  Atmega 328P-PU.
LEDs connected to pins 3 and 11.  Shows the two PWM waveforms.
Goal:  measure current using Hall Effect sensor, ACS770ECB-200U and transfer to PC.

I found  "The Serial.prints are actually set in an ISR, so they will be affecting interrupts."
and
"- -and timer interrupts are higher priority than serial interrupts - -".

This would explain why the  "I'm here2"  is not sent to the Serial Monitor after I set the TOIE2 bit.

Would you please direct me to a method of sending data to the Serial Monitor when using/changing Timer2 interrupt(s) for PWM.

Thank you.

Code: [Select]

void setup()
{//                       Put your setup code here, to run once:
Serial.begin(9600);            // open serial port.
delay(20);                     //  Put a delay in to open port.
Serial.println (F("Hello World"));
Serial.println (F(""));        // Blank line.
delay(30);                     // Must delay 'some' to allow buffer to be xmtd.
//
unsigned long StartTime;
unsigned long time;
StartTime = micros();   //overflows (goes back to zero) after approximately 70 minutes.
//
Serial.print(F("Start Time in microsecs : "));   // Use macro F( ) to save SRAM
Serial.println(StartTime); 
delay(50);            // Must delay 'some' to allow buffer to be xmtd.
//
pinMode(3, OUTPUT);   // Connected to  Output Compare   OC2 B 
pinMode(11, OUTPUT);  // Connected to  Output Compare   OC2 A
//   Clear Timer Control Registers, just in case
TCCR2A = 0;
Serial.println(F("I'm here1")); 
delay(50);            // Must delay 'some' to allow buffer to be xmtd.
//
TIMSK2 = _BV (TOIE2);     // TC2 Interrupt Mask Register.  Enable the timer 2 interrupt
Serial.println(F("I'm here2")); 
delay(50);            // Must delay 'some' to allow buffer to be xmtd.
TIFR2 = _BV (TOV2);       //  TC2 Interrupt Flag Register.  Clear TOV2 (by writing a 1)  Overflow Flag
//    Set up the Timer/Counter Control Register 2, A & B
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
//     CS22=0  CS21=1      cs20=1   prescaler for 32
  TCCR2B = _BV(CS21) | _BV(CS20);   //  This will route Tclk to Timer 2 through scaler
//    Set up the Output Compare Register 2,   A & B
  OCR2A = 32;         //  This is for monitoring the pin 11
  OCR2B = 50;         //  This is for monitoring the pin  3
//
}                     //  end of setup
void loop() {
  // put your main code here, to run repeatedly:
}


Serial Monitor shows:

Hello World

Start Time in microsecs : 50132
I'm here1


PaulS

Before f**king around with timers, you are supposed to disable interrupts. When you are done, you re-enable them. You do NOT try to print while interrupts are disabled.
The art of getting good answers lies in asking good questions.

pylon

Code: [Select]
TIMSK2 = _BV (TOIE2);     // TC2 Interrupt Mask Register.  Enable the timer 2 interrupt


Why do you enable the timer 2 interrupt if you just want to have a PWM output?
Is this your complete code or does that include an interrupt handler for that interrupt (which even might print something to the serial interface)?

tangent2

PaulS:  Thank you for your guidance.  I will modify my code.

pylon:  Good detective work.  Yes, this is the first section of code. 
I need this code to produce a PWM with a period of 512 micro-seconds and an indication (interrupt; "stay away from delays") it has completed a period.  I want the same PWM duty cycle repeated 8789 times or approximately 4.5 seconds. 
I want an ISR to count the periods and flag when the 8789 is reached.
Then the PWM duty cycle is modified and another 8789 PWM's are generated.
I also want to measure the current (ACS770ECB-200U) for the first couple of pulses of each set of 8789 pulses and send the data to a PC.
Thank you for your time.

DrAzzy

Is the isr defined somewhere? If not, then I'd expect it to barf when the interrupt first fired...
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

pylon


Go Up