You are on page 1of 5

Bài 1 kiểm tra 1.

4
Câu 1.4. Tạo hiệu ứng 8 led đơn sáng giữa ra dần 2 bên:
Thầy yêu cầu: cho led chạy từ 2 led ngoài chạy vô,rồi chạy
ra lại
#include <AT89X52.H>
void delay_ms(int ms){
int x,y;
for(x=0;x<ms;x++){
for(y=0;y<125;y++){
}
}
}
void main(){
unsigned char led=0x81;
while(1){
P0= ~0x18;
delay_ms(250);
P0= ~0x24;
delay_ms(250);
P0=~0x42;
delay_ms(250);
P0=~0x81;
delay_ms(250);
}
}

1.5. Chạy lần lượt từng hiệu ứng ở dịch trái, dịch phải, giữ ra 2 bên:
Thầy yêu cầu: thầy yêu cầu gì quên mất kk
#include <AT89X52.H>
int i;
void delay_ms(int ms){
int x,y;
for (x=0;x<ms;x++){
for (y=0;y<125;y++){
}
}
}
void main (){
unsigned char led = 0x01;
while(1){
for( i=0; i<7; i++){
P0 = ~led ;
delay_ms(250);
led=led<<1;
}
for (i=0; i<7;i++){
P0 = ~led ;
delay_ms(250);
led=led>>1;
}
for (i=0; i<7;i++){
P0= ~0x18;
delay_ms(250);
P0= ~0x24;
delay_ms(250);
P0=~0x42;
delay_ms(250);
P0=~0x81;
delay_ms(250);
}
}
}
BÀI 2: LẬP TRÌNH NÚT NHẤN

1. 1 nút nhấn nhấn nút đảo trạng thái đèn:


Thầy yêu cầu: nhấn nút đèn tắt xong sáng lại
#include<AT89X52.H>
#define LED P0_0
#define NUTNHAN P1_0

void delay_ms(int ms)


{
int x, y;
for (x = 0; x < ms; x++)
for (y = 0; y < 125; y++);
}
void main ()
{ NUTNHAN = 1;
while (1)
{
if (NUTNHAN == 0)
{ delay_ms (20);
if (NUTNHAN == 0)
{ LED =~ LED;
while(NUTNHAN == 0)
}
}
}
}

2. Viết chương trình 8 nút nhấn cùng 8 led:


Thầy yêu cầu: nhấn nút đèn tắt thì phải câu ni không nhớ
#include<AT89X52.H>
#define LED1 P0_0
#define NUTNHAN1 P1_0
#define LED2 P0_1
#define NUTNHAN2 P1_1
#define LED3 P0_2
#define NUTNHAN3 P1_2
#define LED4 P0_3
#define NUTNHAN4 P1_3
#define LED5 P0_4
#define NUTNHAN5 P1_4
#define LED6 P0_5
#define NUTNHAN6 P1_5
#define LED7 P0_6
#define NUTNHAN7 P1_6
#define LED8 P0_7
#define NUTNHAN8 P1_7

void delay_ms(int ms)


{
int x, y;
for (x = 0; x < ms; x++)
for (y = 0; y < 125; y++);
}

void main(){
P1_0 = 1;
P1_1 = 1;
P1_2 = 1;
P1_3 = 1;
P1_4 = 1;
P1_5 = 1;
P1_6 = 1;
P1_7 = 1;
while(1){
if(P1_0==0)
{delay_ms(20);
if(P1_0==0)
{LED1=~LED1;
while(P1_0==0);
}
}
if(P1_1==0)
{delay_ms(20);
if(P1_1==0)
{LED2=~LED2;
while(P1_1==0);
}
}
if(P1_2==0)
{delay_ms(20);
if(P1_2==0)
{LED3=~LED3;
while(P1_2==0);
}
}
if(P1_3==0)
{delay_ms(20);
if(P1_3==0)
{LED4=~LED4;
while(P1_3==0);
}
}
if(P1_4==0)
{delay_ms(20);
if(P1_4==0)
{LED5=~LED5;
while(P1_4==0);
}
}
if(P1_5==0)
{delay_ms(20);
if(P1_5==0)
{LED6=~LED6;
while(P1_5==0);
}
}
if(P1_6==0)
{delay_ms(20);
if(P1_6==0)
{LED7=~LED7;
while(P1_6==0);
}
}
if(P1_7==0)
{delay_ms(20);
if(P1_7==0)
{LED8=~LED8;
while(P1_7==0);
}
}
}
}

3. Viết nút nhấn hiệu ứng dịch trái, dịch phải, giữ ra 2 bên:
Thầy yêu cầu: Nhấn nút 1: hiệu ứng 3
Nhấn nút 2: không thay đổi
Nhấn nút 3: hiệu ứng 1
#include <regx52.h>
#define Led P0
sbit Nutnhan = P1^2;
void Delay_ms(unsigned int t)
{
unsigned int x, y;
for(x = 0; x < t; x++)
for(y = 0; y < 113 ; y++);
}

unsigned char cnt = 0;


void main()
{
unsigned char i;

EA = EX0 = IT0 = 1;
while(1)
{

// Khi khong nhan nut


if(cnt == 0)
{
Led = 0x00;
Delay_ms(200);
for(i = 0; i<8;i++)
{
Led = Led | (0x01 << i);
Delay_ms(200);
}

Led = 0x00;
for(i=0;i<8;i++)
{
Led = Led | (0x80>>i);
Delay_ms(200);
}

Led = 0x00;
for(i=0;i<4;i++)
{
Led = Led |(0x08>>i) | Led |(0x10<<i) ;
Delay_ms(200);

}
}
// Khi nhan nut 1 lan
if(cnt == 1)
{
Led = 0x00;
Delay_ms(200);
for(i = 0; i<8;i++)
{
Led = Led | (0x01 << i);
Delay_ms(200);
}
}
//Khi nhan nut 2 lan
if(cnt == 2)
{
Led = 0x00;
Delay_ms(200);
for(i=0;i<8;i++)
{
Led = Led | (0x80>>i);
Delay_ms(200);
}
}
// Khi nhan nut 3 lan
if(cnt == 3)
{
Led = 0x00;
Delay_ms(200);
for(i=0;i<4;i++)
{
Led = Led |(0x08>>i) | Led |(0x10<<i) ;
Delay_ms(200);
}
}
}
}

void ISR_Button() interrupt 0


{
Delay_ms(20);
if(Nutnhan == 0)
{
cnt++;
if(cnt >= 4)
cnt = 0;
}
}

You might also like