Skip to main content

Using PIC16F887 microcontroller to control 8 single leds on and off using timer interrupt T1

  • Requirements: Use a PIC16F887 microcontroller to control 8 single leds on and off using the timer interrupt T1 with a delay period of 262ms.
  •  Circuit diagram on Proteus simulation software 
Figure 1. Controlling 8 LEDs to turn on and off using interrupts

  • Main program:

File: main.c

___________________

#INCLUDE <16F887.H>

#FUSES NOWDT,PUT,HS,NOPROTECT,NOLVP

#USE DELAY(CLOCK=20M)

UNSIGNED INT8 X;

#int_timer1

void interrupt_timer1()

{

   X=~X;

   OUTPUT_D(X);

}

VOID MAIN()

   SET_TRIS_D(0x00); X=0X00; OUTPUT_D(X);

   SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);

   SET_TIMER1(0);

   ENABLE_INTERRUPTS(GLOBAL); ENABLE_INTERRUPTS(INT_TIMER1);

   WHILE(TRUE) {}

}

___________________

Link download: https://drive.google.com/file/d/1e_8QOGfguNwGeuFN-T0MR9-vJyvMLAmz/view

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