Skip to main content
Became Hot Network Question
Became Hot Network Question
Code formatting.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

#include <avr/io.h> #include <avr/interrupt.h>

void setup() {

PORTB = 0;        //Reset values on port B

// After setting up the timer counter,
// set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0) | (1<<PB3); // Set the direction of PB1 to an output

// PLLCSR - PLL control and status register:
// PLL is a clock multiplier - multiplies system     8 MHz by 8 to 64 MHz
// PLL is enabled when:PLLE bit is enabled,
// CKSEL fuse is programmed to 0001.  This clock is
//   switched off in sleep modes!
PLLCSR |= (1<<PLLE);    // PLL enable

// Wait until the PLOCK bit is enabled
// before allowing the PCK to be enabled
//WaitForPLOCK();
//unsigned int i = 0;

while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
    // Do nothing until plock bit is set
}

PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source


TCCR1 =
        (1<<CTC1)    | // Enable PWM
        (1<<PWM1A)   | // Set source to pck
        (0<<(CS13))  |
        (0<<(CS12))  |
        (1<<(CS11))  |
        (1<<(CS10))  | // Clear the pin when match with ocr1x
        (1<<COM1A1);
GTCCR =   (1<<PWM1B) | (1<<COM1B1);


// Set PWM TOP value - counter will count and reset
//  after reaching this value
//            OCR1C
// 100 kHz    159
// 80 kHz    199

OCR1C = 199;


// Enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);

sei();


OCR1A = 190;  //chaning th FR

}

void loop() { PORTB |= (1 << PB3);
delay(500); PORTB &= ~(1 << PB3); delay(500); }

#include <avr/io.h>
#include <avr/interrupt.h>

void setup() {
    PORTB = 0;        //Reset values on port B

    // After setting up the timer counter,
    // set the direction of the ports to output
    DDRB |= (1<<PB1) | (1<<PB0) | (1<<PB3); // PB0, PB1 and PB3

    // PLLCSR - PLL control and status register:
    // PLL is a clock multiplier
    //      - multiplies system 8 MHz by 8 to 64 MHz
    // PLL is enabled when:PLLE bit is enabled,
    // CKSEL fuse is programmed to 0001.  This clock is
    //   switched off in sleep modes!
    PLLCSR |= (1<<PLLE);    // PLL enable

    // Wait until the PLOCK bit is enabled
    // before allowing the PCK to be enabled
    //WaitForPLOCK();
    //unsigned int i = 0;

    while ((PLLCSR & (1<<PLOCK)) == 0x00)
    {
        // Do nothing until plock bit is set
    }

    // Enable asynchronous mode, sets PWM clock source
    PLLCSR |= (1<<PCKE);

    TCCR1 = (1<<CTC1)    | // Enable PWM
            (1<<PWM1A)   | // Set source to pck
            (0<<(CS13))  |
            (0<<(CS12))  |
            (1<<(CS11))  |
            (1<<(CS10))  | // Clear the pin when match with ocr1x
            (1<<COM1A1);
    GTCCR = (1<<PWM1B) | (1<<COM1B1);

    // Set PWM TOP value - counter will count and reset
    //  after reaching this value
    //           OCR1C
    // 100 kHz   159
    // 80 kHz    199
    OCR1C = 199;

    // Enable Timer1 OVF interrupt
    TIMSK = (1<<OCIE1A) | (1<<TOIE1);

    sei();

    OCR1A = 190;  //chaning th FR
}

void loop() {
    PORTB |= (1 << PB3);
    delay(500);
    PORTB &= ~(1 << PB3);
    delay(500);
}

#include <avr/io.h> #include <avr/interrupt.h>

void setup() {

PORTB = 0;        //Reset values on port B

// After setting up the timer counter,
// set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0) | (1<<PB3); // Set the direction of PB1 to an output

// PLLCSR - PLL control and status register:
// PLL is a clock multiplier - multiplies system     8 MHz by 8 to 64 MHz
// PLL is enabled when:PLLE bit is enabled,
// CKSEL fuse is programmed to 0001.  This clock is
//   switched off in sleep modes!
PLLCSR |= (1<<PLLE);    // PLL enable

// Wait until the PLOCK bit is enabled
// before allowing the PCK to be enabled
//WaitForPLOCK();
//unsigned int i = 0;

while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
    // Do nothing until plock bit is set
}

PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source


TCCR1 =
        (1<<CTC1)    | // Enable PWM
        (1<<PWM1A)   | // Set source to pck
        (0<<(CS13))  |
        (0<<(CS12))  |
        (1<<(CS11))  |
        (1<<(CS10))  | // Clear the pin when match with ocr1x
        (1<<COM1A1);
GTCCR =   (1<<PWM1B) | (1<<COM1B1);


// Set PWM TOP value - counter will count and reset
//  after reaching this value
//            OCR1C
// 100 kHz    159
// 80 kHz    199

OCR1C = 199;


// Enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);

sei();


OCR1A = 190;  //chaning th FR

}

void loop() { PORTB |= (1 << PB3);
delay(500); PORTB &= ~(1 << PB3); delay(500); }

#include <avr/io.h>
#include <avr/interrupt.h>

void setup() {
    PORTB = 0;        //Reset values on port B

    // After setting up the timer counter,
    // set the direction of the ports to output
    DDRB |= (1<<PB1) | (1<<PB0) | (1<<PB3); // PB0, PB1 and PB3

    // PLLCSR - PLL control and status register:
    // PLL is a clock multiplier
    //      - multiplies system 8 MHz by 8 to 64 MHz
    // PLL is enabled when:PLLE bit is enabled,
    // CKSEL fuse is programmed to 0001.  This clock is
    //   switched off in sleep modes!
    PLLCSR |= (1<<PLLE);    // PLL enable

    // Wait until the PLOCK bit is enabled
    // before allowing the PCK to be enabled
    //WaitForPLOCK();
    //unsigned int i = 0;

    while ((PLLCSR & (1<<PLOCK)) == 0x00)
    {
        // Do nothing until plock bit is set
    }

    // Enable asynchronous mode, sets PWM clock source
    PLLCSR |= (1<<PCKE);

    TCCR1 = (1<<CTC1)    | // Enable PWM
            (1<<PWM1A)   | // Set source to pck
            (0<<(CS13))  |
            (0<<(CS12))  |
            (1<<(CS11))  |
            (1<<(CS10))  | // Clear the pin when match with ocr1x
            (1<<COM1A1);
    GTCCR = (1<<PWM1B) | (1<<COM1B1);

    // Set PWM TOP value - counter will count and reset
    //  after reaching this value
    //           OCR1C
    // 100 kHz   159
    // 80 kHz    199
    OCR1C = 199;

    // Enable Timer1 OVF interrupt
    TIMSK = (1<<OCIE1A) | (1<<TOIE1);

    sei();

    OCR1A = 190;  //chaning th FR
}

void loop() {
    PORTB |= (1 << PB3);
    delay(500);
    PORTB &= ~(1 << PB3);
    delay(500);
}
Source Link

Fast PWM with Arduino, the inputs outputs don't work

Hello to the community!

I am using ATTINY85 and I am programming it through the AVRISP using an Arduino UNO.

This is the code I am using :

#include <avr/io.h> #include <avr/interrupt.h>

void setup() {

PORTB = 0;        //Reset values on port B

// After setting up the timer counter,
// set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0) | (1<<PB3); // Set the direction of PB1 to an output

// PLLCSR - PLL control and status register:
// PLL is a clock multiplier - multiplies system     8 MHz by 8 to 64 MHz
// PLL is enabled when:PLLE bit is enabled,
// CKSEL fuse is programmed to 0001.  This clock is
//   switched off in sleep modes!
PLLCSR |= (1<<PLLE);    // PLL enable

// Wait until the PLOCK bit is enabled
// before allowing the PCK to be enabled
//WaitForPLOCK();
//unsigned int i = 0;

while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
    // Do nothing until plock bit is set
}

PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source


TCCR1 =
        (1<<CTC1)    | // Enable PWM
        (1<<PWM1A)   | // Set source to pck
        (0<<(CS13))  |
        (0<<(CS12))  |
        (1<<(CS11))  |
        (1<<(CS10))  | // Clear the pin when match with ocr1x
        (1<<COM1A1);
GTCCR =   (1<<PWM1B) | (1<<COM1B1);


// Set PWM TOP value - counter will count and reset
//  after reaching this value
//            OCR1C
// 100 kHz    159
// 80 kHz    199

OCR1C = 199;


// Enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);

sei();


OCR1A = 190;  //chaning th FR

}

void loop() { PORTB |= (1 << PB3);
delay(500); PORTB &= ~(1 << PB3); delay(500); }

the problem is that when I use the PWM my rest of the outputs do not work . Any idea why is this happening?

I am using the ATTINY CORE library from Spence Konde.