Skip to main content

Fast PWM on ATTiny85 | timer0 / counter arduino uno

ATtiny PWM (updated)

This is a re-post of my original ATtiny85 entry. My original was sarcastic, irritated, and (although it worked) 
lackedhelpful comments. So now I've spent some time reading over the datasheet until I understood
 every single bit function. This program cycles red, green and blue on an RGB LED using the three 
PWM pins available to an ATtiny85, which are driven by both of the ATtiny85's Timer/Counters 
Image result for attiny85

example 
/*
all my ATtiny85 chips have their 8MHz fuse set
by default they run at 1MHz, so adjust accordingly
this constant is used by delay.h, so make sure it stays above the include
*/
#define F_CPU 8000000

/*
io.h provides lots of handy constants
delay.h provides _delay_ms and _delay_us functions
*/
#include
#include


/*
program entry-point
*/
void main()
{
    /*
    Starting values for red, green and blue
    */
    uint8_t r=0, g=85, b=170;
    
    /*
    Port B Data Direction Register (controls the mode of all pins within port B)
    DDRB is 8 bits: [unused:unused:DDB5:DDB4:DDB3:DDB2:DDB1:DDB0]
    1<
    1<
    1<
    */
    DDRB = 1<

    /*
    Control Register A for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
    TCCR0A is 8 bits: [COM0A1:COM0A0:COM0B1:COM0B0:unused:unused:WGM01:WGM00]
    2<
    2<
    3<
    */
    TCCR0A = 2<
    
    /*
    Control Register B for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
    TCCR0B is 8 bits: [FOC0A:FOC0B:unused:unused:WGM02:CS02:CS01:CS00]
    0<
    1<
    */
    TCCR0B = 0<
    
    /*
    Control Register for Timer/Counter-1 (Timer/Counter-1 is configured with just one register: this one)
    TCCR1 is 8 bits: [CTC1:PWM1A:COM1A1:COM1A0:CS13:CS12:CS11:CS10]
    0<
    0<
    1<
    */
    TCCR1 = 0<
    
    /*
    General Control Register for Timer/Counter-1 (this is for Timer/Counter-1 and is a poorly named register)
    GTCCR is 8 bits: [TSM:PWM1B:COM1B1:COM1B0:FOC1B:FOC1A:PSR1:PSR0]
    1<
    2<
    */
    GTCCR = 1<
    
    /*
    loop forever
    */
    for (;;)
    {
        /*
        increment and boundary-check each color
        */
        if (++r>255) r=0;
        if (++g>255) g=0;
        if (++b>255) b=0;
        
        /*
        update compare registers with red, green and blue values
        */
        OCR0A = r;
        OCR0B = g;
        OCR1B = b;
        
        /*
        brief pause so we can perceive what is happening
        */
        _delay_ms(10);
    }
}

Popular posts from this blog

Automobile Engineering:-----What does LXI, VXI, ZXI mean in cars?

Almost all car manufacturers have different variants available for the same car.

The car looks the same but will have different features inside and outside.

Maruthi uses L, V, Z alphabets. and their petrol and diesel variants are identified by XI and DI respectively.

Maruthi: LXI, VXI, ZXI or LDI, VDI, ZDI




Honda: EMT, SMT, SVMT, VMT, VXMT, SAT, VAT, SCVT, VCVT 
In Honda, E, S, SV, V, VX are the variants and AT means the automatic transmission and CVT means continuous variable transmission. so if a model says VCVT that means that the variant is and it has a CVT transmission in it. In Honda cars, petrol and diesel is identified by the separate markings iVTECand iDTECrespectively
These are the different trim levels which are found in Maruti Suzuki vehicles.
Trim refers to the items that can be added to the interior and exterior of an automobile to increase its appeal.



XI denotes petrol engine
LXI is for lower variant cars
VXI includes basic features like ac, power steering  etc
ZXI includes all th…

WATER LEVEL INDICATOR

Prepared by… Name: -                  Kumawat ajay kumar sureshbhai College: -               S. & SS Gandhi Polytechnic college University: -           Gujarat Technical University Enrollenment: -     1360120324017 
INDEX Sr. no. Name 1 Introduction 2 Use of water level indicator 3 Different type of water level indicator 4

HOW TO MAKE LDR SENSOR AT HOME

HOW TO MAKE LDR SENSOR AT HOME Prepared by Name: - Kumawat ajay kumar
College: - S&S.S. Gandhi Polytechnic college, Surat
IntroductionA photo resistor or light-dependent resistor (LDR) or photocell is a light-controlled variable resistor. The resistance of a photo resistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity.
A photo resistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits.
A photo resistor is made of a high resistance semiconductor. In the dark, a photo resistor can have a resistance as high as a few mega ohms (MΩ), while in the light, a photo resistor can have a resistance as low as a few hundred ohms. If incident light on a photo resistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons and hole conduct electricity, thereby lowering resistance.
The resista…

PIEZO ELECTRICITY GENERATOR

PIEZO ELECTRICITY GENERATOR 

INDEX
Sr. no. Name 1 introduction 2 What is piezoelectricity generation 3 How it works 4 Application of piezoelectricity 5

Astable Multivibrator using 555