Nguyentrungkien - BT Timer 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1. Dùng ngắt timer 0 để quét led, thời gian delay mỗi led là 2 ms.

Sử dụng:
Setup_timer_0(RTCC_DIV_BY_4|RTCC_INTERNAL)
Viết chương trình thực hiện hiển thị số đếm trên 4 LED, giá trị ban đầu là 0, và:
- Nhấn SW1: nếu số đếm < 9000: sodem = sodem+10
- Nhấn SW2: nếu số đếm > 0: sodem = sodem-1

#include <16F877A.h>

#FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP

#use delay(clock=8000000)

#define D1 PIN_B5

#define D2 PIN_C0

#define SW1 PIN_C1

#define SW2 PIN_B4

//========================

unsigned char led[10]= {0x03, 0x9f, 0x25, 0x0d, 0x99, 0x49, 0x41,
0x1f, 0x01, 0x09};  

unsigned char nghin,tram,chuc,dv;         

unsigned int16 x=0;            

unsigned char k; 

void led()            

nghin=x/1000; 

tram=(x%1000)/100; 

chuc=((x%1000)%100)/10; 

dv =((x%1000)%100)%10; 

void qled() 

 { output_d(0xff);qled();  

Void main()

{
enable_interrupts(GLOBAL);// -----

enable_interrupts(INT_RTCC);// -------------

Setup_timer_0(RTCC_DIV_BY_4|RTCC_INTERNAL);//-----------

while(TRUE);

                      

 { output_d(0xf7);output_b(led[nghin]);delay_ms(2); 

output_d(0xfb);output_b(led[tram]);delay_ms(2); 

output_d(0xfd); output_b(led[chuc]);delay_ms(2);

output_d(0xfe); output_b(led[dv]);delay_ms(2); 

if(!input(SW1));k++;k<9000;k=k+10 beak;

if(!input(SW1));k++;k>0;k=k-1 beak;}}}

2.Viết chương trình đọc bàn phím HEX và hiển thị giá trị HEX trên led 7 đoạn
a) Quét phím trong chương trình chính
b) Quét phím dùng ngắt timer1
c) Mô phỏng mạch dùng Proteus
Setup_timer_1(T1_INTERNAL| T1_DIV_BY_1)
HƯỚNG DẪN:
Cấu hình PORT C với RC0-RC3 là ngõ vào; RC4-RC7 là ngõ ra. Xuất lần lượt
giá trị 0 ra các cột và kiểm tra hàng tương ứng để biết phím nào đang nhấn

#include <16F877A.h>
#FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT,
NOLVP
#use delay(clock=8000000)
CONST unsigned char LED7S[17] = {0x03,
0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
#define col1 pin_c4
#define col2 pin_c5
#define col3 pin_c6
#define col4 pin_c7
#define row1 pin_c0
#define row2 pin_c1
#define row3 pin_c2
#define row4 pin_c3
//-------CH??NG TRÌNH CHÍNH---------
Void main()
{int8 phim = 16;
output_d(0xff); output_low(pin_d3);
while(true)
{
output_low(col1);
output_high(col2);
output_high(col3);
output_high(col4);
if(!input(row1))phim=0;
if(!input(row2))phim=1;
if(!input(row3))phim=2;
if(!input(row4))phim=3;
//======================================
output_low(col2);
output_high(col1);
output_high(col3);
output_high(col4);
if(!input(row1))phim=4;
if(!input(row2))phim=5;
if(!input(row3))phim=6;
if(!input(row4))phim=7;
//======================================
output_low(col3);
output_high(col1);
output_high(col2);
output_high(col4);
if(!input(row1))phim=8;
if(!input(row2))phim=9;
if(!input(row3))phim=0x0a;
if(!input(row4))phim=0x0b;
//==========================================
output_b(LED7s[phim]);
}}

You might also like