Go Down

Topic: Using a pin to control pwm.  (Read 1 time) previous topic - next topic


I'm not sure if this is the correct place to post, I'm pretty new to this.

I'm attempting to build a "slow dance" picture frame by working backward from the open source code. There is a section on the code I'm having trouble following.

ISR(TIMER0_COMPA_vect) { // Use pin D6 as PWM to control D4 and D5
  uint8_t sample = pgm_read_byte_near(waveform + outputIndex++);

  if (sample) {
    PORTD &= ~(1 << PIN_COIL1);
    PORTD |= (1 << PIN_COIL2);
  } else {
    PORTD |= (1 << PIN_COIL1);
    PORTD &= ~(1 << PIN_COIL2);

  if (outputIndex > WAVEFORM_SIZE) {
    outputIndex = 0;

In this code PIN_COIL1 is D4 and PIN_COIL2 is D5. I'm trying to understand how D6 is used to control D4 and D5.

I understand that without seeing the entirety of the project this may be is not enough information to get help.

Thanks for any help that can be given or direct me to where I can learn more about this :)


Do go and read the sticky so you know how to properly post code and what else we need for an informed answer.

Code: [Select]

ISR(TIMER0_COMPA_vect) { // Use pin D6 as PWM to control D4 and D5

This has nothing to do with a PWM signal as such, it's a timer interrupt, which indeed is also used by the PWM output. Pin D6 doesn't seem to have anything to do with it. Somewhere, somehow that timer interrupt will be set in your sketch.

Code: [Select]

  uint8_t sample = pgm_read_byte_near(waveform + outputIndex++);
This is reading a byte from PROGMEM, there is probably an array of sorts defined in your program, and based in it sets the coil outputs one way or another.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up