# 介面 實驗九 ## 工作日誌 機械3A 孫颯馬 ### 遭遇問題 ### 流程 1. 線性功率放大驅動電路實驗 2. H全橋驅動電路實驗 ### 實驗數據 ## 1. 線性功率放大驅動電路實驗 :::info ![](https://i.imgur.com/TvKsDfX.png) ::: 喇叭 :::spoiler 1k 1v ![](https://i.imgur.com/7Digvql.png) 2v ![](https://i.imgur.com/fNOT2ur.png) ::: :::spoiler 2k 1v ![](https://i.imgur.com/A84wscl.png) 2v ![](https://i.imgur.com/0ypS6aE.png) 5v ![](https://i.imgur.com/C7ThYqZ.png) ::: :::spoiler 5k 1v ![](https://i.imgur.com/AzI85GB.png) 2v ![](https://i.imgur.com/Mk04Ozx.png) 5v ![](https://i.imgur.com/VfhROtp.png) ::: :::spoiler 10k 1v ![](https://i.imgur.com/whExAWl.png) 2v ![](https://i.imgur.com/Xyl1Elb.png) 5v ![](https://i.imgur.com/GbiaplY.png) ::: 馬達 :::spoiler 5k ![](https://i.imgur.com/S7VLwNP.png) ::: LED :::spoiler led ![](https://i.imgur.com/K64Urvg.png) ![](https://i.imgur.com/2wjUUKt.png) ::: 音響 :::spoiler led ![](https://i.imgur.com/8HSNDyT.png) ![](https://i.imgur.com/gsLZxNI.png) ::: ## 2. H全橋驅動電路實驗 :::info 電路圖 ![](https://i.imgur.com/O72dXpr.png) ::: 喇叭 :::spoiler Click Me ![](https://i.imgur.com/7nLWhgT.png) ![](https://i.imgur.com/MeykIFo.png) ![](https://i.imgur.com/tanv3uK.png) ![](https://i.imgur.com/asDOMe0.png) ![](https://i.imgur.com/DYOLvRe.png) ::: 馬達 :::spoiler Click Me ![](https://i.imgur.com/IBb7rTT.png) ![](https://i.imgur.com/nK8NwuD.png) ::: LED :::spoiler Click Me ![](https://i.imgur.com/pPtJ4hc.png) ![](https://i.imgur.com/P0oPaK0.png) ::: ## 驗收 :::info 延續H全橋驅動電路的喇吧實驗,換成音響,利用改變PWM之波的最大寬度(改變頻率),分別使用兩個按鈕使音響可以做出兩首不同曲子。 ::: :::spoiler 程式 #include "c4mlib.h" #include "math.h" #include "stdlib.h" #include "stdio.h" #include <avr/io.h> void F_set(); void PWM1_set(); void doremi(); void doremi2(); uint16_t Width_x = 0, Width_y = 0, Width_z = 0,i=0; uint16_t N = 2,comnc0_1 = 2,wgmn0_1 = 2,wgmn2_3 = 2,DATA_1 = 1,xx,yy; // 261 293 329 349 392 440 493 522 uint16_t DATA=1590; uint16_t freq_D[8]={11059200/(2*8*261),11059200/(2*8*293) ,11059200/(2*8*329) ,11059200/(2*8*349) ,11059200/(2*8*392), 11059200/(2*8*440), 11059200/(2*8*493) ,11059200/(2*8*522)}; int outdata=0,frequence; int fre=0,doremi_flag=0,melody=0; int dodososo[14]={0,0,4,4,5,5,4,3,3,2,2,1,1,0}; int main(void) { C4M_DEVICE_set(); DATA=freq_D[0]; PWM1_set(); REGFPT( &DDRD , 0x0C , 0 , 0 );//D01腳輸入 while(1) { F_set(); _delay_ms(500); if(doremi_flag==1){ doremi(); _delay_ms(300); } if(doremi_flag==2){ doremi2(); _delay_ms(300); } } return 0; } void F_set() { REGFGT(&PIND,0x0C,0,&outdata);//讀取值 if (outdata == 4 && doremi_flag==0) { doremi_flag=1; printf("D2\n"); } else if (outdata == 8 && doremi_flag==0) { doremi_flag=2; printf("D3\n"); } DATA=freq_D[fre]; frequence = 11059200/(2*8*DATA); printf("frequency = %d\n",frequence); } void PWM1_set() { REGFPT( &TCCR1A , 0x03 , 0 , wgmn0_1 );//波行模式選擇-ICRA提供最高值 REGFPT( &TCCR1B , 0x18 , 3 , wgmn2_3 );//波行模式選擇-中間對齊 REGFPT( &TCCR1B , 0x07 , 0 , N);//除頻值選擇 REGFPT( &TCCR1A , 0xC0 , 6 , 3);//PWMA_波行輸出選擇_正負波選擇 REGFPT( &TCCR1A , 0x30 , 4 , 2);//PWMB_波行輸出選擇_正負波選擇 REGFPT( &DDRB , 0x20 , 5 , 1 );//B5接腳輸出 REGFPT( &DDRB , 0x40 , 6 , 1 );//B6接腳輸出 REGPUT( &ICR1L , 2 , &DATA );//設定PWM1最大波寬 REGPUT( &TCNT1L , 2 , &DATA_1 );//調整PWM1時脈計數-設1是為了讓TCNT1從1開始計數到TOP然後來回到bottom REGPUT( &OCR1AL , 2 , &Width_x );//調整PWM1a輸出波寬,設定週期_波寬設定 REGPUT( &OCR1BL , 2 , &Width_y );//調整PWM1b輸出波寬,設定週期_波寬設定 REGFPT( &TIMSK , 0X04 , 2 , 1 );//設定中斷禁制能 sei();//至能 xx = DATA/2; yy = DATA/2; REGPUT( &OCR1AL , 2 , &xx );//調整PWM1a輸出波寬,設定週期_波寬設定 REGPUT( &OCR1BL , 2 , &yy );//調整PWM1b輸出波寬,設定週期_波寬設定 } void doremi() { melody=melody+1; fre=dodososo[melody]; if(melody==-1){ melody=7; } if(melody==14){ melody=0; doremi_flag=0; fre=0; } } void doremi2() { melody=melody+1; if(melody<=7){ fre=fre+1; } if(melody>7){ fre=fre-1; } if(melody==14){ fre=0; melody=0; doremi_flag=0; } } ISR(TIMER1_OVF_vect) { PWM1_set(); } ::: 驗收影片 https://youtube.com/shorts/UlwAmXe8OHk