You are on page 1of 7

Bài 2: CCS COMPILER

9. Viết chương trình đồng hồ đếm giây: dòng 1 tại (1,1) hiển thị
“DONG HO DEM GIAY”, dòng 2 tai (4,2) hiển thị “00:00”, và khi:
- Nhấn SW1: bắt đầu đếm “giây : phần trăm giây”
- Nhấn SW2: dừng đếm
- Nhấn giữ SW2 hơn 5 giây sẽ xóa số đếm về 0

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
#include < lcd.c >
# define SW1 PIN_C3
# define SW2 PIN_C4
Void main ()
{int8 giay=0, phantramgiay=0;
lcd_init ();
set_tris_d(0x00);
output_d(0x00);
lcd_gotoxy (1,1);
 printf (lcd_putc,” DONG HO DEM GIAY”);
 While (1)
{lcd_gotoxy (4,2);
Printf (lcd_putc, “%02u : %02u”, giay, phantramgiay);
If (!input (SW1)) SW=1;
If (!input (SW2)) SW=2;
If (!input (SW2)) SW=3;
Switch (SW)
{case 1: {phantramgiay ++;
if (phantramgiay == 100) {phantramgiay=0; giay++;} lcd_gotoxy
(4,2);
printf (lcd_putc, “%02u : %02u”, giay, phantramgiay); break;}
case 2: {break;}
case 3:
{
delay_ms (5000); 
giay=0; phantramgiay=0;
lcd_gotoxy (4,2);
printf (lcd_putc, “%02u : %02u”,giay, phantramgiay); break;
}}}
10. Viết chương trình thực hiện: hiển thị chữ “DONG HO” trên dòng
1 tại (4,1)
và hiển thị “giờ:phút:giây” tại (6,2); thời gian bắt đầu là “00:00:00”
và nếu:
- Nhấn SW1: cho phép chỉnh giờ (0-23)
- Nhấn SW2: cho phép chỉnh phút (0-59)

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M) 
#include < lcd.c >
# define SW1 PIN_C3
# define SW2 PIN_C4
Void main ()
{int8 giay=0, phut=0, gio=0; 
lcd_init (); set_tris_d(0x00);
output_d(0x00);
lcd_gotoxy (4,1);
 Printf (lcd_putc, “DONG HO”);
 While (1)
{lcd_gotoxy (6,2);
printf (lcd_putc, “%02u : %02u : %02u”, gio, phut, giay);
 If (!input(SW1)) gio ++;
If (gio==24) gio=0;
If (!input (SW2)) phut ++; 
if (phut==60) phut=0;
lcd_gotoxy (6,2);
printf (lcd_putc, “%02u : %02u : %02u”, gio, phut, giay);
}}

11. Viết chương trình hiển thị các LED đơn sáng dần từ D1 đến D8,
delay 250 ms, lặp lại 8 lần, sau đó tắt dần từ D1 đến D8, delay 300
ms, lặp lại 20 lần.

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Void main ()
{for (int8 a=0; a<8; a++)
{Unsigned char maled1 [9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f,
0x7f, 0xff};
For (int8 i=0; i<9; i++)
{output_c(maled1[i]);
delay_ms (250);
}}
for (int8 b=0; b<20; b++)
{Unsigned char maled2 [9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0,
0x80, 0x00};
For (int8 k=0; k<9; k++)
{output_c(maled2[k]);
delay_ms (300);
}}}

12. Viết chương trình hiển thị các LED đơn sáng dần từ D8 đến D1,
delay 450 ms, sau đó tắt dần từ D8 đến D1, delay 500 ms, lặp lại hai
quá trình trên 10 lần.

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Void main ()
{for (int8 a=0; a<11; a++)
{Unsigned char maled1 [9] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe,
0xff};
For (int8 i=0; i<9; i++)
{output_c(maled1[i]);
delay_ms (450);
}
Unsigned char maled2 [9] = {0xff, 0xef, 0xcf, 0x8f, 0x0f, 0x07, 0x03,
0x01, 0x00};
For (int8 k=0; k<9; k++)
{output_c(maled2[k]);
delay_ms (500);
}}}

ĐỀ KHÁC
1.
a. Sáng dần từ  D1 đến D8; lặp 8 lần

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] =
{0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};Void main (){For (int8
k=0; k<8; k++)
{For (int8 i=0; i<9; i++)
{output_c (maled [i]);
delay_ms (300);
}}}

b. Tắt dần từ D8 đến D1; Lặp lại 10 lần

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] =
{0x77,0x67,0x47,0x07,0x87,0x8f,0x8d,0x8c,0x88};
Void main ()
{For (int8 k=0; k<10; k++)
{For (int8 i=0; i<9; i++)
{output_b (maled [i]);
delay_ms (300);
}}}
 
c. Một LED sáng dịch từ  D1 –  D8; Lặp lại vô hạn lần

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] =
{0x80,0x81,0x84,0x82,0x00,0x88,0x90,0xa0,0xc0};
Void main ()
{While (1)
{For (int8 i=0; i<9; i++)
{output_d (maled [i]);
delay_ms (300);
}}}

d) Sáng dần từ  trong ra ngoài; lặp lại 15 lần


#include < 16f877A.h >
# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [5] = {0x0c,0x0f,0x3f,0x77,0xf3};
Void main ()
{For (int8 k=0; k<15; k++)
{For (int8 i=0; i<5; i++)
{output_c (maled [i]);
delay_ms (300);
}}}

2. Viết chương trình hiển thị các LED đơn sáng dần từ  D8 đến
D1,delay 450 ms, sau đó tắt dần từ  D8 đến D1, delay 500 ms, lặp lại
hai quá trình trên 10 lần.

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Void main ()
{for (int8 a=0; a<11; a++)
{Unsigned char maled1 [9] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe,
0xff};
For (int8 i=0; i<9; i++)
{output_c(maled1[i]);
delay_ms (450);
}
Unsigned char maled2 [9] = {0xff, 0xef, 0xcf, 0x8f, 0x0f, 0x07, 0x03,
0x01, 0x00};
For (int8 k=0; k<9; k++)
{output_c(maled2[k]);
delay_ms (500);
}}}

3. Viết chương trình hiển thị các led sáng dần từ  D1 đến D8 sau đó
tắt dần từ  D8 đến D1 lặp lại quá trình 10 lần (delay 200ms)

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)Unsigned char maled [9] =
{0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
Void main ()
{For (int8 i=0; i<10; i++)
{For (int8 k=0; k<9; k++)
{output_c (maled [k]);
delay_ms (200);
}
For (int8 j=0; j<9; j++)
{output_c (maled [9-j]);
delay_ms (200);}}}

4. Viết chương trình hiển thị các led tắt dần từ  D8 đến D1 sau đó
sáng dần từ  D1 đến D8 lặp lại quá trình 8 lần (delay 350ms)

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03,
0x01, 0x00};
Void main ()
{For (int8 i=0; i<8; i++)
{For (int8 k=0; k<9; k++)
{output_c (maled [k]);
delay_ms (350);
}
For (int8 j=0; j<9; j++)
{output_c (maled [9-j]);
delay_ms (350);
}}}

5. Viết chương trình hiển thị các led tắt dần từ  D1 đến D8 sau đó
sáng dần từ  D8 đến D1 lặp lại quá trình 47 lần (delay 400ms)

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0,
0x80,0x00};
Void main ()
{For (int8 i=0; i<47; i++)
{For (int8 k=0; k<9; k++)
{output_c (maled [k]);
delay_ms (400);
}
For (int8 j=0; j<9; j++)
{output_c (maled [9-j]);
delay_ms (400);
}}}

6. Viết chương trình hiển thị các led tắt dần từ  D8 đến D1 sau đó
sáng/tắt 8led 3 lần và lặp lại quá trình 17 lần (delay 300ms)

#include < 16f877A.h >


# FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG,
NOBROWNOUT, NOLVP
# use delay (clock=8M)
Unsigned char maled [9] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03,
0x01, 0x00};
Void main ()
{For (int8 i=0; i<17; i++)
{For (int8 k=0; k<9; k++)
{output_c (maled [k]);
delay_ms (300);
}
For (int8 j=0; j<3; j++)
{output_c (0xff);
output_c (0x00);
delay_ms (300);
}}}

You might also like