Skip to main content

Use PWM of PIC 16F887 to control 1 led

  • Requirements: Use PWM of PIC 16F887 to control 1 led. Let the frequency of the quartz capacitor be 20MHz. Given a PWM period of 0.8ms. Let's calculate the parameters and write a program to control the led light with level 1 equal to 1 tenth of the maximum brightness. 
  •  Circuit diagram on Proteus simulation software:
  • Figure 1. The control circuit changes the light intensity of the lamp using PWM

    • Main program:
    File: main.c
    ___________________
    #INCLUDE <16F887.H>
    #FUSES NOWDT,PUT,HS,NOPROTECT,NOLVP
    #USE DELAY(CLOCK=20M)
    UNSIGNED INT16 BIEN_TOC_DO;
    VOID MAIN()
    {
       SET_TRIS_C(0X00);
       SETUP_CCP1(CCP_PWM);
       SETUP_TIMER_2(T2_DIV_BY_16,249,1);
       BIEN_TOC_DO=500;
       SET_PWM1_DUTY(BIEN_TOC_DO);
       WHILE(TRUE){}
    }
    ___________________

    Comments

    Popular posts from this blog

    Privacy Policy

    Introduce We always strive to protect our customers' personal information. Customer Data The application fully commits not to collect and share data with third parties.

    Bài 2: Dùng vi điều khiển 16F887 điều khiển 8 led đơn sáng dần tắt dần từ phải sang trái mô phỏng trên Proteus

    Sơ đồ mạch:   Hình 1.  Sơ đồ mạch  điều khiển 8 led đơn sáng dần tắt dần từ phải sang trái Chương trình: #INCLUDE <16F887.H> #FUSES NOWDT,PUT,HS,NOPROTECT,NOLVP #USE DELAY(CLOCK=20M) UNSIGNED INT8 I, X; VOID MAIN() {      SET_TRIS_D(0x00); X=0X00; OUTPUT_D(X); DELAY_MS(500);      WHILE(TRUE)      {            FOR (I=0;I<8;I++)           {                 X = (X<<1)+0X01; OUTPUT_D(X); DELAY_MS(500);           }           FOR (I=0;I<8;I++)           {                 X = (X<<1); OUTPUT_D(X); DELAY_MS(500);           }      } } Giải thích chương trình: Khởi tạo portD là xuất dữ liệu, gán biến X bằng 0x00, xuất giá trị của X ra portD làm 8 led tắt, delay. Vòng lặp for thứ nhất thực hiện 8 lần: tiến hành xoay trái dữ liệu của X và cộng với 0x01. Khi xoay trái dữ liệu thì số 0 được đẩy vào, cộng với 0x01 là để đẩy số 1 vào X. Dữ liệu biến X được xuất ra portD điều khiển led sáng dần, sau 8 lần thì X sẽ bằng 1111_1111 – 8 led sáng hết. Vòng lặp for thứ hai thực hiện