這是一個(gè)簡單的方波發(fā)生器,主要使用TimerOne庫,可讓您在引腳9產(chǎn)生PWM信號(hào)。
硬件部件:
Arduino Nano R3 × 1個(gè)
Adafruit標(biāo)準(zhǔn)LCD-16x2藍(lán)色白色× 1個(gè)
按鈕開關(guān),瞬間 × 3
單圈電位器-10k歐姆× 1個(gè)
軟件應(yīng)用程序和在線服務(wù):
Arduino IDE
這是一個(gè)簡單的方波發(fā)生器,主要使用TimerOne庫,使您可以在引腳9處生成PWM信號(hào),范圍約為5Hz至1 Mhz,并且可以將占空比從0調(diào)整到100%。
原理圖:
設(shè)備非常易于構(gòu)建,僅包含幾個(gè)組件:
Arduino Nano微控制器
液晶顯示器
三個(gè)上拉電阻
三個(gè)按鈕
脈沖發(fā)生器可以使用連接到Arduino數(shù)字輸入6和7的按鈕來調(diào)整脈沖重復(fù)周期。13個(gè)輸入引腳可讓您調(diào)整占空比。持續(xù)時(shí)間和占空比讀數(shù)顯示在LCD 16×2指示器的第一行中,頻率讀數(shù)顯示在第二行中。調(diào)整脈沖重復(fù)周期的最小步長是1μs,因此頻率將離散變化,例如1μs是1 MHz,2μs是500 kHz,3μs是333.333 Hz,依此類推,并且隨著頻率的降低,其調(diào)整的平滑度增加。這在較高的頻率上是不切實(shí)際的,但這就是簡化的代價(jià)。
為了可視化輸出信號(hào),我使用了小型單通道示波器。最后,將設(shè)備安裝在合適的盒子中,這是電子實(shí)驗(yàn)室中的另一個(gè)有用工具。
源碼:
#include 《TimerOne.h》
#include 《LiquidCrystal.h》
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// RS,E,D4,D5,D6,D7
unsigned long t=1000,f,k=512;// default 1000 μs (1000 Hz), meander, pulse duration is equal to duty cycle k = 512 (50%)
byte k1,kn,kn1,kn2;
int drive,drive0;
void setup()
{
lcd.begin(16, 2);// LCD 16X2
pinMode(10, OUTPUT);
pinMode(6,INPUT);// button at input 6
pinMode(7,INPUT);// button at input 7
pinMode(13,INPUT);// button at input 13
}
void loop()
{
Timer1.initialize(t); // period
Timer1.pwm(9, k); // k - fill factor 0-1023. We remove the signal from the output 9
kn=digitalRead(6);// button input 6 (- pulse period)
kn1=digitalRead(7);// button input 7 (+ pulse period)
kn2=digitalRead(13);// button input 13 (+ circle fill factor)
if(kn==HIGH){ // decreasing the period
drive++;
if(drive《30){
t=t-1;
}
// if the button is held for a long time, the correction of the pulse period x10 x100 x1000 is accelerated
else if(drive》30 && drive《60 ){
t=t-10;
}
else if(drive》=60 && drive《100){
t=t-100;
}
else if(drive》=100){
t=t-1000;
}
}
else{
drive=0;
}
if(kn1==HIGH){// adding a period
drive0++;
if(drive0《30){
t=t+1;
// if the button is held for a long time, the correction of the period x10 x100 x1000 is accelerated
}
else if(drive0》30 && drive0《60 ){
t=t+10;
}
else if(drive0》=60 && drive0《100){
t=t+100;
}
else if(drive0》=100){
t=t+1000;
}
}
else{
drive0=0;
}
if(t==0 || t》300000){ // limiting the pulse duration to the minimum, if 0 μs or more than 300 ms (3.33 Hz), then the period is 1 μs
t=1;
}
if(t》200000 && t《300000){ // limiting the pulse duration to the maximum, if more than 200 ms, but less than 300 ms (3.33 Hz), then the period is 200 ms (5 Hz)
t=200000;
}
f=1000000/t; // calculate the frequency
k1=k*100/1024; // calculate% fill factor
if(kn2==HIGH){// button for adjusting the fill factor (in a circle from 50 to 100%, then from 0 to 100%)
k=k+16;// step 16 out of 1024 (you can do 8 for smoother adjustment)
}
if(k==1024){
k=0;
}
// displaying information on the indicator
lcd.setCursor(0,0);
lcd.print(“T=”);
lcd.print(t);
lcd.print(“ us”);
lcd.setCursor(12,0);
lcd.print(k1);
lcd.print(“ %”);
lcd.setCursor(0,1);
lcd.print(“F=”);
lcd.print(f);
lcd.print(“ Hz”);
delay(300);
lcd.setCursor(0,0);
lcd.print(“ ”);
lcd.setCursor(0,1);
lcd.print(“ ”);
}
責(zé)任編輯:pj
-
微控制器
+關(guān)注
關(guān)注
48文章
7658瀏覽量
152183 -
發(fā)生器
+關(guān)注
關(guān)注
4文章
1369瀏覽量
61878 -
PWM信號(hào)
+關(guān)注
關(guān)注
3文章
95瀏覽量
20218
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
基于PIC16F877A的方波信號(hào)發(fā)生器電路設(shè)計(jì)
頻率可調(diào)的方波信號(hào)發(fā)生器設(shè)計(jì)及電路
![頻率可調(diào)的<b class='flag-5'>方波</b><b class='flag-5'>信號(hào)</b><b class='flag-5'>發(fā)生器</b>設(shè)計(jì)及電路](https://file1.elecfans.com//web2/M00/A4/F7/wKgZomUMNmKAMzaBAADG2LC_1BQ512.jpg)
基于FPGA和PWM的多路信號(hào)發(fā)生器設(shè)計(jì)
![基于FPGA和<b class='flag-5'>PWM</b>的多路<b class='flag-5'>信號(hào)</b><b class='flag-5'>發(fā)生器</b>設(shè)計(jì)](https://file1.elecfans.com//web2/M00/A6/EB/wKgZomUMQUaAEAtVAAA3Kd8Er84669.png)
評(píng)論