As in the title, I am using two timers Timer1 and Timer2 pwm outputs. I setup the timers and serial comms and the main loop is just transmitting some test characters. All serial comms are established using arduino functions. On scope, I see that when I start Serial Monitor on my computer, the pwm outputs momentarily reset to zero. Is this a known bug? Is there some way to avoid this?
Code follows:
void setup(){
GTCCR = (1<<TSM)|(1<<PSRASY)|(1<<PSRSYNC); // Halt all Timers
// TIMER1 settings --------------------------------------------------
TCCR1A = 0; // Reset TIMER 1 Control Register A
TCCR1B = 0; // Reset TIMER 1 Control Register B
TCCR1A |= (1 << COM1A1) | //
(0 << COM1A0) | // Clear OC1A on Compare match up counting
(1 << COM1B1) | //
(1 << COM1B0) | // Set OC1B on Compare match up counting
(0 << WGM10) |
(0 << WGM11) ;
TCCR1B |= (0 << WGM12) |
(1 << WGM13) | // PWM Phase Freq correct, TOP=ICR1, fund freq 60Hz, switching freq 15.63kHz
(1 << CS10) ; // No Prescalar
ICR1 = 0x0209; // TOP=523, Switching Freq = 15.6kHz, sinetable256
// TIMER1 default dutycycle
OCR1A = 0x00;
OCR1B = 0x00;
// TIMER1 interrupt settings
TIMSK1 = 0; // Reset Timer1 interrupt mask register
TIMSK1 |= (1 << TOIE1) ; // Timers1 Overflow interrupt TOV1 bit set when TCN1=BOTTOM
TIFR1 = 0; // Reset Timer1 interrupt flag register
// TIMER2 settings -------------------------------------------------------
TCCR2A = 0; // Reset TIMER 2 Control Register A
TCCR2B = 0; // Reset TIMER 2 Control Register B
TCCR2A |= (1 << COM2A1) | //
(0 << COM2A0) | // Clear OC2A on Compare match up counting
(1 << COM2B1) | //
(1 << COM2B0) | // Set OC2B on Compare match up counting
(1 << WGM20) |
(1 << WGM21) ; // Fast PWM, TOP=0xFF, 62.74kHz
TCCR2B |= (0 << WGM22) |//
(1 << CS20) ; // No Prescalar
// TIMER2 default dutycycle
OCR2A = 0x00;
OCR2B = 0xFF;
// TIMER2 interrupt settings
TIMSK2 = 0; // Reset Timer2 Interrupt mask register
// TIMSK2 |= (1 << TOIE2) ; // TIMER2 Overflow interrupt TOV2 bit set when TCN2=BOTTOM
// TIMER2 asynchronus status register
ASSR = 0; // Reset Async status register, TIMER2 clk = CPU clk
// set all timers to the same value
TCNT1 = 0x0000; // set timer1 to 0
TCNT2 = 0x00; // set timer2 to 0
GTCCR = 0; // release all timers
// Serial communication settings---------------------------------------
Serial.begin(9600);
}
void loop(){
// prints value unaltered, i.e. the raw binary version of the
// byte. The serial monitor interprets all bytes as
// ASCII, so 33, the first number, will show up as '!'
Serial.write(thisByte);
Serial.print(", dec: ");
// prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for Serial.print() and Serial.println(),
// so no modifier is needed:
Serial.print(thisByte);
// But you can declare the modifier for decimal if you want to.
//this also works if you uncomment it:
// Serial.print(thisByte, DEC);
Serial.print(", hex: ");
// prints value as string in hexadecimal (base 16):
Serial.print(thisByte, HEX);
Serial.print(", oct: ");
// prints value as string in octal (base 8);
Serial.print(thisByte, OCT);
Serial.print(", bin: ");
// prints value as string in binary (base 2)
// also prints ending line break:
Serial.println(thisByte, BIN);
// if printed last visible character '~' or 126, stop:
if(thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing
while(true) {
continue;
}
}
// go on to the next character
thisByte++;
}