0% found this document useful (0 votes)
49 views3 pages

Cod Lab6 Ex3

The document describes code for initializing various components like a PWM, ADC, timers and displays for a microcontroller project. It includes functions for initializing a PWM, reading ADC values, displaying values on 7-segment displays using timers and ports, and sampling the ADC to control a buzzer's frequency.

Uploaded by

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

Cod Lab6 Ex3

The document describes code for initializing various components like a PWM, ADC, timers and displays for a microcontroller project. It includes functions for initializing a PWM, reading ADC values, displaying values on 7-segment displays using timers and ports, and sampling the ADC to control a buzzer's frequency.

Uploaded by

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

int h=0, l=0, value=0, digit=0, adc=0, T=0, ms=0;

int ADCMax=1023, fMax=20000, fMin=20, freq, icr, TOP, adv=0;


void init_PWM2()
{
DDRD |= 1<<7; //PD7 – pin de ie?ire
TCCR2 = 0b01100111; //FastPWM neinversat, N=1
}

void Init_adc()
{
ADMUX = 0b01000000; //Referin?a - AVCC
ADCSRA= 0b10000111; //Activare ADC; Prescaler = 128;
}

void init_timer0()
{
SREG = 1<<7; // Global Interrupt Enable
TCCR0 = 0b00001011; //CTC-3,6; Prescaler-0,1,2
TCNT0 = 0;
OCR0 = 125;
TIMSK |= 0b00000010; //set interrupt OCM
}

void display (char p, char c)


{
PORTA &= 0b11110000;
PORTC &= 0b00000000;

switch (c)
{
case 0:
PORTC |= 0b00111111;break;
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 4:
PORTA |= 0b00001000;break;
case 3:
PORTA |= 0b00000100;break;
case 2:
PORTA |= 0b00000010;break;
case 1:
PORTA |= 0b00000001;break;
};
}

int readADC(char ch)


{
ADMUX &= 0b11100000; //Reseteaza canalul de conversie
ADMUX |= ch; //Seteaza canalul conversiei
ADCSRA |= (1<<6); //Începe conversia
while(ADCSRA & (1<<6)); //A?teapta finalizarea conversiei
l = ADCL;
h = ADCH;
return ((h << 8)| l);
}

void Timer0_OC_ISR() iv IVT_ADDR_TIMER0_COMP


{
digit++;
switch(digit)
{
case 1: display (1, value%10); break;
case 2: display (2, (value/10)%10); break;
case 3: display (3, (value/100)%10); break;
case 4: display (4, (value/1000)%10); digit=0; break;

if (ms == 100)
{
adv = (readADC(6));
freq = ((float)(fMax-fMin))/ADCmax*adv+fMin;
TOP = 1000000/freq-1;
ICR1H = TOP>>8;
ICR1L = TOP;
OCR1BH = ((TOP+1)/2)>>8;
OCR1BL = (TOP+1)/2;
value=freq/10;
ms=0;
}
else ms++;

void init_Buzzer()
{
DDRD |= 1<<4; //PD4 - output
TCCR1A = 0b00100010;
TCCR1B = 0b00011010;
}

void main()
{
DDRA = 0b00001111; //seteaza pinii de ie?ire
DDRC = 0b11111111;
init_Buzzer();
init_PWM2();
init_timer0();
Init_adc();
}

You might also like