Está en la página 1de 3

UFUProjetoInterdisciplinar201521/3

Trabalho1Prof.ErnaneCoelho
_______________________________________________________________________________________

MduloPWM(PulseWidthModulation)
Naatualidadeexisteumaenormegamademicrocontroladoresnomercadoosquaiscontemplam
umperifricointernodestinadogeraodesinaisPWM.
ComograndepartedaspropostasdeprojetosobservadosnadisciplinadeProjetoInterdisciplinar
utilizaamodulaoporlarguradepulso,opresentetrabalhovisacompreensodaoperaode
ummduloPWM,maisespecificamente,aquelevinculadoaoTimer1domicrocontroladorAtmega
328p,presentenokitArduinoUno.
Otrabalhopodeserrealizadoindividualmenteoupelogrupojformadoparaaexecuodo
projetoprincipaldadisciplina.
Otrabalhovalerat20ptsedeveserentreguenohorriodeauladodia28/10quarta(29/10
quinta).
Otrabalhocorrespondeaduasatividades:

Atividade1ExecutenoArduinoUNOocdigoemlinguagemCapresentadoabaixo.Observeque
apesardenoserutilizadaalinguagemdoArduino,talcdigopodesercompiladoecarregadoviaArduino
IDE.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

int i,j,compare;
int const vetor[] PROGMEM= {800, 842, 884, 925, 964, 1000, 1034, 1064, 1091, 1113, 1132, 1147,
1157, 1164, 1167, 1166, 1163, 1157, 1149, 1139, 1128, 1117, 1107, 1097, 1088, 1082, 1078, 1077,
1078, 1084, 1092, 1104, 1120, 1139, 1160, 1185, 1212, 1240, 1270, 1301, 1331, 1362, 1391, 1418,
1443, 1465, 1484, 1499, 1510, 1516, 1519, 1516, 1510, 1499, 1484, 1465, 1443, 1418, 1391, 1362,
1331, 1301, 1270, 1240, 1212, 1185, 1160, 1139, 1120, 1104, 1092, 1084, 1078, 1077, 1078, 1082,
1088, 1097, 1107, 1117, 1128, 1139, 1149, 1157, 1163, 1166, 1167, 1164, 1157, 1147, 1132, 1113,
1091, 1064, 1034, 1000, 964, 925, 884, 842, 800, 758, 716, 675, 636, 600, 566, 536,
509, 487, 468, 453, 443, 436, 433, 434, 437, 443, 451, 461, 472, 483, 493, 503,
512, 518, 522, 523, 522, 516, 508, 496, 480, 461, 440, 415, 388, 360, 330, 299,
269, 238, 209, 182, 157, 135, 116, 101, 90, 84, 81, 84, 90, 101, 116, 135,
157, 182, 209, 238, 269, 299, 330, 360, 388, 415, 440, 461, 480, 496, 508, 516,
522, 523, 522, 518, 512, 503, 493, 483, 472, 461, 451, 443, 437, 434, 433, 436,
443, 453, 468, 487, 509, 536, 566, 600, 636, 675, 716, 758};

main(void)
{
cli(); // disable global interrupts
DDRB = 0b00100010; // PB5 is output (28PDIP pin 19/digital arduino UNO socket pin 13))
// PB1 (used as OC1A in this app) is output (28PDIP pin 15/digital arduino
UNO socket pin 9))
PORTB = 0b00000000; // Reset Port B, eventhough only PB5 is an output
config_timer1();

i=0;
j=0;
sei(); // enable global interrupts:
while(1); //infinite loop
}

void config_timer1(void) //config fast PWM 5333.33Hz, channel A - OC1A pin=pwm non-iverting output
{ // OC1A -> 28 PDIP -> pin 15 / digital Arduino UNO socket - pin 9)
/*Timer/Counter1 Control Reg A
7 6 5 4 3 2 1 0
UFUProjetoInterdisciplinar201522/3
Trabalho1Prof.ErnaneCoelho
_______________________________________________________________________________________
|COM1A1|COM1A0|COM1B1|COM1B0| | | WGM11|WGM10|
1 0 0 0 X X 1 0
OC1Apin=normal port; OC1Bpin=pwm non-iverting output; fast pwm WGM13-10=14 */
TCCR1A = 0x82;

/*Timer/Counter1 Control Reg B


7 6 5 4 3 2 1 0
|ICNC1|ICES1| - | WGM13| WGM12| CS12 | CS11 |CS10|
0 0 X 1 1 0 0 1
noise/edge of capture not applied; WGM13-10=14; clk prescale /1 */
TCCR1B = 0x19;

ICR1 = 1599; // period=1600 // cpu_clk/fs=16MHz/10kHz=1600


OCR1A = 800; // comparison starting value 50% of the period
TCNT1 = 0x0000; // counter starts from zero

/*Timer/Counter1 interrupt mask


7 6 5 4 3 2 1 0
| - | - |ICIE1 | - | - |OCIE1B | OCIE1A|TOIE1|
x x 0 x x 0 0 1
interrupt enable (capture,compare match A and B, timer overflow) */
TIMSK1=0x01; //enable timer overflow interrupt
}

ISR(TIMER1_OVF_vect)
{
compare = pgm_read_word(&(vetor[i]));
i++;
if(i>=200) i=0;
OCR1A = compare;
j++;
if(j>=5000)
{
j=0;
PORTB ^= 0b00100000; //Toggle LED
}
}

Projeteumfiltrodesegundaordemcorrespondecascatadedoisfiltrospassabaixapassivosdeprimeira
ordemcomfreqnciadecorteem1kHz,conformefiguraabaixo.Conecteaentradadofiltronopino9do
soquetedoArduinoeasadadofiltroemumdoscanaisdeumosciloscpio.

a) Qualaformadeondaobservadanoosciloscpio?
b) Qualafreqnciafundamentaldosinalobservado?
c) Existeapresenadealgumharmnico?Qualaordem?
d) EmqualfreqnciaoLEDconectadoportaPB5pisca?
e) Refaaocdigoparaproduzirumanovaformadeondaprojetadaporvc.

UFUProjetoInterdisciplinar201523/3
Trabalho1Prof.ErnaneCoelho
_______________________________________________________________________________________

Atividade 2 Em anexo temse o arquivo de som digital digsound_3200Hz.txt. Altere o cdigo da


atividade 1 de modo a produzir a modulao PWM correspondente as amostras do arquivo de som.
Programeafreqnciadeamostragempara3200Hz.Observequeasamostrastemadimensode1bytee
correspondemanmerossemsinalcomoffsetem128,assim,aoprogramaroregistrodecomparaodo
mduloPWM,faaocondicionamentodosinaldemodoaproduzirumavarreduramximade10a90%do
perodo correspondente. Recalcule o filtro passabaixa da atividade 1 para uma freqncia de corte de
500Hz.Conecteasadadofiltronumacaixadesomcomamplificador,destasutilizadasemcomputadores
PC(utilizeapenasumcanalsinalmono).Executeonovocdigo.

a) Asadadeudioapresentaalgumainformaointeligvel?
b) Comovcdescreveriatalinformao?

También podría gustarte