0% found this document useful (0 votes)
16 views1 page

PWM

Uploaded by

Nguyen Van Cong
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)
16 views1 page

PWM

Uploaded by

Nguyen Van Cong
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/ 1

// 1.

Su dung thu vien LCD cua Arduino


#include <LiquidCrystal.h>

// 2. Cai dat chan ket noi giua LCD voi arduino


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;

// 3. Khai bao cac bien su dung trong chuong trinh


unsigned int PWM_T_us = 0; // PWM Cycle Time: Bien dung de luu chu ky xung T
unsigned int PWM_f_Hz = 0; // PWM Freq: Bien dung de luu tan so xung f
unsigned int TimerValue_us = 0; // Bien luu gia tri dem cua Timer (us)

// 4. Cai dat ket noi chan cua LCD voi Arduino


LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// 12. Chuong trinh ngat ISR_2 dung de do chu ky xung T
void ISR_2() {
unsigned long now = micros();
PWM_T_us = now - TimerValue_us; // Chu ky xung T
TimerValue_us = now;
// 10. Tinh toan tan so xung cua IC555
PWM_f_Hz = 1000000.0/(double)(4*PWM_T_us);
}

// 5. Ham khoi tao ban dau cho mot chuong trinh Arduino
void setup() {
// 6. Cai dat loai LCD su dung ( So cot va so hang)
lcd.begin(16, 2);

// 7. Luu gia tri ban dau cua Timer


TimerValue_us = micros();

// 8. Day la doan code ket noi chan xuat xung cua IC555 voi chan ngat ngoai cua
Arduino
// Khi co xung canh len, timer se chay vao chuong trinh ngat ISR_2 de dem thoi
gian cua chu ky xung T
attachInterrupt(digitalPinToInterrupt(2), ISR_2, RISING);

// 9. Doan chuong trinh chinh cua Arduino


void loop() {
// 11. Hien thi gia tri tan so xung len LCD
lcd.clear(); // Xoa ma hinh LCD
lcd.print("PWM Freq (Hz):"); // In len dong thu 1 cua LCD dong chu PWM Freq (Hz):
lcd.setCursor(0, 1);
lcd.print(PWM_f_Hz); // Hien thi tan so xung (Hz)
delay(500); // Dung man hinh 200 mili s de de quan sat LCD
delay(500);
}

You might also like