0% found this document useful (0 votes)
12 views12 pages

ED - Ex Laburi

The document contains code examples for using microcontrollers to control LEDs, read button inputs, and display values on 7-segment displays. It includes 3 code examples for each of 4 labs: Lab 1 examples control an LED based on a button input and increment a variable on each button press. Lab 2 examples show how to display digits on a 7-segment display by selecting digits and segments. Lab 3 examples implement a timer to count seconds and minutes for a stopwatch displayed on a 7-segment display. Lab 4 example increments a 5-digit counter displayed on a 7-segment display when a button is pressed.

Uploaded by

Brigitta Erdei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

ED - Ex Laburi

The document contains code examples for using microcontrollers to control LEDs, read button inputs, and display values on 7-segment displays. It includes 3 code examples for each of 4 labs: Lab 1 examples control an LED based on a button input and increment a variable on each button press. Lab 2 examples show how to display digits on a 7-segment display by selecting digits and segments. Lab 3 examples implement a timer to count seconds and minutes for a stopwatch displayed on a 7-segment display. Lab 4 example increments a 5-digit counter displayed on a 7-segment display when a button is pressed.

Uploaded by

Brigitta Erdei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Electronica digitala

LABORATOR 1:

1.
void main()
{ DDRB |=(1<<7);
DDRB &=~(1<<3);
for(;;)
{
if(PINB & (1<<3))
PORTB |=(1<<7);
else
PORTB&=~(1<<7) ;
}
}

2.

Void main()
{ DDRD |= 0b11111111; //Pini de ieșire
DDRB &= ~(1<<2); //PB2 – Intrare

for(;;)
{ if(PINB & (1<<2)) //testare pin PB2
{
if(starePB2 == 0) //variabila va ține minte starea
{
starePB2 = 1; //anterioară a pin-ului PB2
PORTB ^= (1<<6);//se schimbă starea LED-ului
}
}else
starePB2 = 0;
}
}

3.
int starePB2=0, n=0;
void main()
{
DDRD |= 0b11111111
; //Pini de ie?ire
DDRB &= ~(1<<2); //PB2 - Intrare
for(;;)
{
if(PINB & (1<<2)) //testare pin PB2
{ if(starePB2 == 0) //variabila va ?ine
minte starea
{ starePB2 = 1; //anterioarã a pin-ului PB2
n++; //se incrementeazã variabila n
}
}else
starePB2 = 0;
PORTD = n;
}
}

LABORATOR 2:
1.
PORTA -selecteaza digitul de afisaj
PORTC- selecteaza ce sa se afiseze(abcdefg)

void main()
{
DDRA |= 0b00001111; //PA0-PA3 – Ie.iri
DDRC |= 0b11111111; //PC0-PC7 – Ie.iri
for(;;)
{
PORTA = 0b00001000; //PA3 – HIGH (activare DIGIT 1)
PORTC = 0b00110000;
Delay_ms(4);
PORTA=0b00000100;
PORTC=0b01110110;
Delay_ms(4);
PORTA=0b00000010;
PORTC=0b01111001;
Delay_ms(4);
PORTA=0b00000001;
PORTC = 0b00111111; //PC0-PC7 cod pentru cifra 1

Delay_ms(4);

}
}

2.
int value=4521;
void display(char p, char c)
{
PORTA &= 0b11110000; //liniile de sel. digit - oprite
PORTC &= 0b00000000; //liniile de sel. Seg. - oprite
switch(c){
case 0:PORTC |= 0b00111111;break;//codificare cifra 0
case 1:PORTC |= 0b00000110;break;
case 2:PORTC |= 0b01011011;break;
case 3:PORTC |= 0b01001111;break;
case 4:PORTC |= 0b01100110;break;
case 5:PORTC |= 0b01101101;break;
case 6:PORTC |= 0b01111101;break;
case 7:PORTC |= 0b00000111;break;
case 8:PORTC |= 0b01111111;break;
case 9:PORTC |= 0b01100111;break;
}
switch(p){
case 1:PORTA |= 0b00000001;break;
case 2:PORTA |= 0b00000010;break;
case 3:PORTA |= 0b00000100;break;
case 4:PORTA |= 0b00001000;break;
}
}

void main()
{
while(1)
{
display(4, value%10);
display(3, (value/10)%10);
display(2, (value/100)%10);
display(1, (value/1000)%10);
}
}

3.

LABORATOR 3
1.

void init_timer()
{
TCCR0 = 0b00000011; //prescaler 64, mod normal de operare
TCNT0 = 0;
}
void display(char p, char c)
{
PORTA &= 0b11110000; //liniile de sel. digit - oprite
PORTC &= 0b00000000; //liniile de sel. Seg. - oprite
switch(c)
{
case 0:PORTC |= 0b00111111;break;//codificare cifra 0
case 1:PORTC |= 0b00000110;break;
case 2:PORTC |= 0b01011011;break;
case 3:PORTC |= 0b01001111;break;
case 4:PORTC |= 0b01100110;break;
case 5:PORTC |= 0b01101101;break;
case 6:PORTC |= 0b01111101;break;
case 7:PORTC |= 0b00000111;break;
case 8:PORTC |= 0b01111111;break;
case 9:PORTC |= 0b01100111;break;
}
switch(p)
{
case 1:PORTA |= 0b00000001;break;
case 2:PORTA |= 0b00000010;break;
case 3:PORTA |= 0b00000100;break;
case 4:PORTA |= 0b00001000;break;
}
}
int ms=0, s=0, m=0;

void main()
{ DDRA=0b00001111; //iesiri
DDRC=0b11111111; //iesiri
init_timer();

for(;;)
{
if(TCNT0>=125)
{
TCNT0 = 0; //resetare software
ms++;
if(ms>=1000)
{ //împlinirea unei secunde
ms=0;
s++;
}
if(s==60)
{
s=0;
m++;
}
}
display(4,s%10);
display(3,(s/10)%10);
display(2,(m%10));
PORTC |= 0b10000000; //pune punct
display(1,(m/10)%10);
}
}

2.

void init_timer()
{
TCCR2 = 0b00000100; //prescaler 64, mod normal de operare
TCNT2 = 0;
}
void display(char p, char c)
{
PORTA &= 0b11110000; //liniile de sel. digit - oprite
PORTC &= 0b00000000; //liniile de sel. Seg. - oprite
switch(c)
{
case 0:PORTC |= 0b00111111;break;//codificare cifra 0
case 1:PORTC |= 0b00000110;break;
case 2:PORTC |= 0b01011011;break;
case 3:PORTC |= 0b01001111;break;
case 4:PORTC |= 0b01100110;break;
case 5:PORTC |= 0b01101101;break;
case 6:PORTC |= 0b01111101;break;
case 7:PORTC |= 0b00000111;break;
case 8:PORTC |= 0b01111111;break;
case 9:PORTC |= 0b01100111;break;
}
switch(p)
{
case 1:PORTA |= 0b00000001;break;
case 2:PORTA |= 0b00000010;break;
case 3:PORTA |= 0b00000100;break;
case 4:PORTA |= 0b00001000;break;
}
}
int ms=0, s=0, m=0;

void main()
{ DDRA=0b00001111; //iesiri
DDRC=0b11111111; //iesiri
init_timer();

for(;;)
{
if(TCNT2>=125)
{
TCNT2 = 0; //resetare software
ms++;
if(ms>=1000)
{ //împlinirea unei secunde
ms=0;
s++;
}
if(s==60)
{
s=0;
m++;
}
}
display(4,s%10);
display(3,(s/10)%10);
display(2,(m%10));
PORTC |= 0b10000000; //pune punct
display(1,(m/10)%10);
}
}

3.
int ms=0, s=0;
int starePin=0;
bit k;
void display(char p, char c)
{
PORTA &= 0b11110000; //liniile de sel. digit - oprite
PORTC &= 0b00000000; //liniile de sel. Seg. - oprite
switch(c)
{
case 0:PORTC |= 0b00111111;break;//codificare cifra 0
case 1:PORTC |= 0b00000110;break;
case 2:PORTC |= 0b01011011;break;
case 3:PORTC |= 0b01001111;break;
case 4:PORTC |= 0b01100110;break;
case 5:PORTC |= 0b01101101;break;
case 6:PORTC |= 0b01111101;break;
case 7:PORTC |= 0b00000111;break;
case 8:PORTC |= 0b01111111;break;
case 9:PORTC |= 0b01100111;break;
}
switch(p)
{
case 1:PORTA |= 0b00000001;break;
case 2:PORTA |= 0b00000010;break;
case 3:PORTA |= 0b00000100;break;
case 4:PORTA |= 0b00001000;break;
}
}
void init_timer()
{
TCCR2 = 0b00000100; //prescaler 64, mod normal de operare
TCNT2 = 0; //se initializeaza numaratorul cu 0
}
void main()
{
DDRB &=~(1<<0);//intrare buton reset
DDRB &=~(1<<1); //intrare buton start stop

DDRA=0b00001111; //iesiri
DDRC=0b11111111; //iesiri

init_timer();
while(1)
{ if(PINB &(1<<0)) //VERIFICARE STARE PB0 DACA E APASAT
{ ms=s=0;
}
if(PINB&(1<<1))
{ if(starePin==0)
{starePin=1;
k^=1;//se schimba starea
}
}
else
starePin=0;
if(k==0)
{ if(TCNT2>=125)
{TCNT2=0;
ms++;
if(ms>=1000)
{ms=0;
s++;
}
}
}
display(1,(s/10)%10);
display(2,s%10);
PORTC |= 0b10000000; //pune punct
display(3,(ms/10)%10);
display(4,ms%10);
}
}

LAB 4
1.
int value=0000;
void display(char p, char c)
{
PORTA &= 0b11110000; //liniile de sel. digit - oprite
PORTC &= 0b00000000; //liniile de sel. Seg. - oprite
switch(c){
case 0:PORTC |= 0b00111111;break;//codificare cifra 0
case 1:PORTC |= 0b00000110;break;
case 2:PORTC |= 0b01011011;break;
case 3:PORTC |= 0b01001111;break;
case 4:PORTC |= 0b01100110;break;
case 5:PORTC |= 0b01101101;break;
case 6:PORTC |= 0b01111101;break;
case 7:PORTC |= 0b00000111;break;
case 8:PORTC |= 0b01111111;break;
case 9:PORTC |= 0b01100111;break;
}
switch(p){
case 1:PORTA |= 0b00000001;break;
case 2:PORTA |= 0b00000010;break;
case 3:PORTA |= 0b00000100;break;
case 4:PORTA |= 0b00001000;break;
}
}

void main()
{
DDRA =0b00001111;
DDRB &=~(1<<3);
while(1)
{
if( PINB&(1<<3))
value++;
if(value>99999)
value=0;
display(4, value%10);
display(3, (value/10)%10);
display(2, (value/100)%10);
display(1, (value/1000)%10);
}
}

You might also like