Uživatel:Neshcmar

Z MAM wiki

Přejít na: navigace, hledání

Semestr project: Scheme of the running lights for microcontroller ATtiny2313

The principle scheme of the running lights is very simple, it will be containing, for example, 15 LEDs, that light alternatly up. These 15 LEDs have only one general anod and the cathodes are connected to ports of input-output of the microcontroller. The principle shceme contains 15 current-limiting resistors R3 to R17witch are equal to 360Ω.

Soubor:scheme_1_neshcmar.jpg

LEDs are operated by the low level of voltage on output of microcontroller . Now lets consider input-output ports. ATtiny2313 have 15 lines of input-output that are grouped in 2 ports: PortB has 8 outputs and PortD has 7 outputs. For input-output ports it is necessary to configure the registers PORTX and DDRX (variable X is a port's name) and what are they responsible for. Port statuses are shown in the table below(n = 0 to 7 is the number of port's output):


Soubor:table_neshcmar.jpg

As we can see from the table, for the DDRXn =1, port functions as an output, and logic level on its output is equal to bit PORTXn. When the value of DDRXn =0 the output of PXn will be operating as an input, and the bit PORTXn will define, whether the internal resistor is connected to an input or not.

The logic level of the output PXn is possible to consider with the help of PINXn bit. Let's pass to practice, I suggest to write the program which will light each of 15 LEDs. I tried to write the program on C. The program on C will look like that:


  1. include <avr/io.h>
  1. define SPEED 3000 //speed const

void pause (unsigned int a) //pause function { unsigned int i;

 for (i=a;i>0;i--)
  ;

}

int main(void) { unsigned char i;

 DDRB=0xff;					//Init I/O ports
 DDRD=0x7f;
 PORTB=0xff;
 PORTD=0x7f;
 while (1)						//main cycle
  {
    for (i=0;i<8;i++)			//Flashes PORTB LEDs

{ PORTB=~_BV(i); pause(SPEED); }

 	 PORTB=0xff;
    for (i=0;i<7;i++)			//Flashes PORTD LEDs

{ PORTD=~_BV(i); pause(SPEED); }

    PORTD=0x7f;
  }

}


Now I will try to explan something. The line #include connects the external avr/io.h module which is required for operating with input-output ports. The line #define SPEED 3000 is responsible for speed of a LED-switching, that means that in the program text all meanings of word SPEED will be replaced with number 3000 (like constant). The pause function (unsigned int a) determines delay, approximately it will be (8*a)/f, where value a is an argument of the function and f is frequency of clocking of the microcontroller.

In the main cycle while there is an alternate switching of LEDs for each port (as management is carried out by low level of voltage, there is a logical symbol NO that is represent by «~»). The unsigned char _BV function (b) returns byte in which the bit at number b is set on 1, and all the others are equal to 0. For example, _BV(2)==0x04.

Osobní nástroje