0% found this document useful (0 votes)
29 views23 pages

Bùi Quang Tấn - Huỳnh Quốc Thắng

The document contains a series of programming exercises related to microcontroller applications, specifically using C language for embedded systems. It includes code examples for controlling LEDs, traffic lights, a perpetual calendar, and remote control functionality. Each section demonstrates different functionalities and hardware interactions, showcasing practical applications of programming in an educational context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

Bùi Quang Tấn - Huỳnh Quốc Thắng

The document contains a series of programming exercises related to microcontroller applications, specifically using C language for embedded systems. It includes code examples for controlling LEDs, traffic lights, a perpetual calendar, and remote control functionality. Each section demonstrates different functionalities and hardware interactions, showcasing practical applications of programming in an educational context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

HO CHI MINH CITY UNIVERSITY OF

TECHNOLOGY AND EDUCATION

THỰC TẬP KIẾN TRÚC VÀ TỔ


CHỨC MÁY TÍNH

Class ID: COOL325364_10

GVHD: ThS. Đậu Trọng Hiển

Bùi Quang Tấn - 22119228


Huỳnh Quốc Thắng - 22119230

November/2024
Bài 1: Dịch Led
#include <main.h>
#include <stdio.h>
#include < REGX52.h>
#define LED_PORT P1

void Delay (unsigned int t)


{
unsigned int x,y;
for(x=0 ; x<t ; x++)
{
for (y=0 ; y < 123 ; y++);
}
}
void main()
{
unsigned char i ;
while(1)
{
LED_PORT = 0x00;
for(i=0 ; i<8 ; i++)
{

LED_PORT = (LED_PORT << 1) | 0x01; // Dich tat ca cac


bitt 1 luc
Delay(200);
}
for(i=0 ; i<8 ; i++)
{
LED_PORT = LED_PORT << 1;
Delay(200);
}
}
}

Bài 2 : Đèn giao thông ( proteus và board như nhau)


#include <REGX52.H>
sbit DO = P1^0;
sbit VANG = P1^1;
sbit XANH = P1^2;
sbit DO1 = P1^3;
sbit VANG1 = P1^4;
sbit XANH1 = P1^5;
#define Button P3_1
#define Control P3_0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned DisplayData[8];
int t, k, l, j, s, o;
unsigned char chuc, donvi, chuc1, donvi1;
unsigned char code so[] = {0x3f, 0x06, 0x5b, 0x4F, 0x66, 0x6d, 0x7d, 0x07, 0x7f ,
0x6f};
void delay_ms(unsigned t)
{
int x,y;
for(x=0;x<t;x++)
{
for(y=0;y<123;y++);
}
}

void DigDisplay()
{ int i;
DisplayData[0] = l%10; //lay so hang don vi den ben trai
DisplayData[1] = l/10; //lay so hang chuc

DisplayData[2] = t%10; //lay so hang don vi den ben phai


DisplayData[3] = t/10; //lay so hang chuc

for(i=0;i<30;i++)
{
if(Button==1){

LSA=1;LSB=1;LSC=1; // su dung 74hc138 de dieu khien LED 7 doan , LSC LSB LSA ,
111 là LED ben trai ngoai cung
P0 = so[DisplayData[1]];
delay_ms(5);

LSA=0;LSB=1;LSC=1;
P0 = so[DisplayData[0]];
delay_ms(5);

LSA=1;LSB=0;LSC=0;
P0 = so[DisplayData[3]];
delay_ms(5);

LSA=0;LSB=0;LSC=0;
P0 = so[DisplayData[2]];
delay_ms(5);

}
else
{
P0=0X00;;
}
}
}
void main ()
{
while (Button == 0 ){

if(Control == 0){
DO = 0;
VANG = 1;
XANH = 1;
DO1= 1;
VANG1=1;
XANH1=0;
}
else
{
DO= 1;
VANG=1;
XANH=0;
DO1=0;
VANG1=1;
XANH1=1;

}
}
while(Button==1)
{
t=20; l=15; s=4; o=20;
for(k=20; k>0; k--)
{
t--&l--&o--;
if(l<0) l = s;
if(o<=5) {
DO = 0;
VANG = 1;
XANH = 1; // Truong hop ben trai den do ben phai
den vang
DO1 = 1;
VANG1 = 0;
XANH1 = 1; }
else {
DO = 0;
VANG = 1;
XANH = 1;
DO1 = 1; // Truong hop ben trai den do ben phai den
xanh , den do 20s den xanh 15s , den vang 5s
VANG1 = 1;
XANH1 = 0; }
DigDisplay();
}
break;
}
while(Button==1)
{
t=15; l=20; s=4; o=20;
for(k=20; k>0; k--)
{
t--&l--&o--;
if(t<0) t = s;
if(o<=5) {
DO = 1;
VANG = 0;
XANH = 1; // truong hop ben phai den do ben trai
mau vang
DO1 = 0;
VANG1 = 1;
XANH1 = 1; }
else {
DO = 1;
VANG = 1;
XANH = 0;
DO1 = 0; // truong hop ben phai den do ben trai
mau xanh
VANG1 = 1;
XANH1 = 1; }
DigDisplay();
}
break;
}
}
Bài 3 Lịch vạn niên proteus

/*--------------------------------------------------------------------------
bit DS1307_Read_Time(unsigned int * hour, unsigned int * minute, unsigned int *
second)
{
unsigned int h_tmp, m_tmp, s_tmp, *mode;
bit am_pm;
I2C_Start(); gui 1 xung den ds1307
I2C_Write(0xD0); chon thanh ghi
I2C_Write(0x00); dia chi chua giay phut gio
I2C_Start();
I2C_Write(0xD1); doc thanh ghi
s_tmp = I2C_Read(1); ACK de doc du lieu tiep theo
m_tmp = I2C_Read(1); ACK
h_tmp = I2C_Read(0); NACK
I2C_Stop();
--------------------------------------------------------------------------*/
#include <REGX51.H>
#include <Real_Time_DS1307.H>

#define button P2_0


#define SCL P3_6
#define SDA P3_7
#define Xung_Data2 P1_0
#define Data_Data2 P1_1
#define Chot_Data2 P1_2
#define Xung_Control2 P3_0
#define Data_Control2 P3_1
#define Chot_Control2 P3_2
unsigned second, minute, hour, d;
unsigned day, date, month, year;

unsigned e;
int ma7doan2[10] = {0XC0, 0XF9, 0XA4, 0XB0, 0X99, 0X92, 0X82, 0XF8, 0X80,
0X90};
int ma7doancham[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00,
0x10};

void delay(unsigned t)
{
int x,y;
for(x=0;x<t;x++)
{
for(y=0;y<123;y++);
}
}

void Send_Data2(int dulieu) // gui du lieu den ma led 7 seg 74hc595


{
int i, tach[8], dich=0X01;

for(i=0; i<8; i++) // tach du lieu dau vao thanh cac bits
{
if(dulieu&dich) tach[i] = 1;
else tach[i] = 0;
dich = dich << 1;
}
for(i=7; i>=0; i--) // Ghi va dich du lieu
{
Data_Data2 = tach[i];
Xung_Data2 = 0;
Xung_Data2 = 1;
}
Chot_Data2 = 0;
Chot_Data2 = 1;
}

void Send_Control2 (int dulieu) // hien thi len LED 7 seg 74hc595
{
int i, tach[8], dich=0X01;

for(i=0; i<8; i++) // tach du lieu dau vao thanh cac bits
{
if(dulieu&dich) tach[i] = 1;
else tach[i] = 0;
dich = dich << 1;
}
for(i=7; i>=0; i--) // Ghi va dich du lieu , i di tu 7 den 0 la de dich cac bit co
trong so cao truoc
{
Data_Control2 = tach[i];
Xung_Control2 = 0;
Xung_Control2 = 1;
}
Chot_Control2 = 0;
Chot_Control2 = 1;
}

void main()
{
DS1307_Init(); // goi ds1307 de lay du lieu
while(1)
{

DS1307_Read_Time(&hour,&minute,&second); // lay thoi gian


DS1307_Read_Date(&day,&date,&month,&year); // lay ngay thang
if(day <=1)
{
day = 0x08 ;}

if(button == 1)
{
Send_Control2(2);
Send_Data2(ma7doancham[day%10]);
delay(10);
Send_Control2(1);
Send_Data2(ma7doan2[day/10]);
delay(8);
Send_Control2(8);
Send_Data2(ma7doancham[date%10]);
delay(6);
Send_Control2(4);
Send_Data2(ma7doan2[date/10]);
delay(4);
Send_Control2(32);
Send_Data2(ma7doancham[month%10]);
delay(4);
Send_Control2(16);
Send_Data2(ma7doan2[month/10]);
delay(2);
Send_Control2(64);
Send_Data2(ma7doan2[year/10]);
delay(2);
Send_Control2(128);
Send_Data2(ma7doan2[year%10]);
}
else
{
Send_Control2(2);
Send_Data2(ma7doan2[hour%10]); //don vi gio
delay(10);
Send_Control2(1);
Send_Data2(ma7doan2[hour/10]); //hang chuc gio
delay(8);
Send_Control2(4);
Send_Data2(0xbf); // dau gach ngang
delay(6);
Send_Control2(16);
Send_Data2(ma7doan2[minute%10]);
delay(4);
Send_Control2(32);
Send_Data2(0xbf); // dau gach ngang
delay(4);
Send_Control2(8);
Send_Data2(ma7doan2[minute/10]);
delay(2);
Send_Control2(128);
Send_Data2(ma7doan2[second%10]);
delay(1);
Send_Control2(64);
Send_Data2(ma7doan2[second/10]);
}
}
}

Bài 4. Lịch vạn niên Board


#include<reg51.h>
#include "ds1302.h"
#define DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit BUTTON=P3^2;
unsigned char code
DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Num=0;
unsigned int disp[8];
unsigned char showDate = 0;
void Timer0Configuration();

void delay(unsigned t)
{
unsigned x,y;
for(x=0;x<t;x++)
{
for(y=0;y<123;y++);
}
}
void main()
{
SetInitialTime();
Ds1302Init();
Timer0Configuration();
while(1)
{
if(BUTTON == 0) {
showDate = !showDate;
while(BUTTON == 0);
}

if(showDate) {
Ds1302ReadTime();
disp[7] = DIG_CODE[TIME[6]&0x0f];
disp[6] = DIG_CODE[TIME[6]>>4];
disp[5] = 0X80; //dau cham
disp[4] = DIG_CODE[TIME[4]&0x0f];
disp[3] = DIG_CODE[TIME[4]>>4];
disp[2] = 0X80;
disp[1] = DIG_CODE[TIME[3]&0x0f];
disp[0] = DIG_CODE[TIME[3]>>4];
} else {
Ds1302ReadTime();
disp[7] = DIG_CODE[TIME[0]&0x0f]; // BCD TIME[0] = 0x23 ,
TIME[0] & 0x0f = 0x03
disp[6] = DIG_CODE[TIME[0]>>4]; //giay
disp[5] = 0X40; // dau gach ngang
disp[4] = DIG_CODE[TIME[1]&0x0f]; // phut
disp[3] = DIG_CODE[TIME[1]>>4];
disp[2] = 0X40;
disp[1] = DIG_CODE[TIME[2]&0x0f];
disp[0] = DIG_CODE[TIME[2]>>4]; // gio
}
}
}
void Timer0Configuration()
{
TMOD = 0x02; // Cau hinh timer 0 auto reload 8bit
TH0 = 0x9C; // Giá tri tra lai cho TH0
TL0 = 0x9C; // 0x9c9c cho den 0xffff thi kich hoat ngat
ET0 = 1; // Bat ngat Timer0
EA = 1; // Bat ngat toàn cuc
TR0 = 1; // Bat Timer0
}

void DigDisplay() interrupt 1 //timer 0 su dung


{
DIG=0;
switch(Num) //chon Led de hien thi
{
case(7):
LSA=0;LSB=0;LSC=0; break;
case(6):
LSA=1;LSB=0;LSC=0; break;
case(5):
LSA=0;LSB=1;LSC=0; break;
case(4):
LSA=1;LSB=1;LSC=0; break;
case(3):
LSA=0;LSB=0;LSC=1; break;
case(2):
LSA=1;LSB=0;LSC=1; break;
case(1):
LSA=0;LSB=1;LSC=1; break;
case(0):
LSA=1;LSB=1;LSC=1; break;
}
DIG=disp[Num]; // hien thi
Num++; // tang bien num de quet het 8 led
if(Num>7)
Num=0;
}
Bài 5. Điều khiển từ xa IR
#include<reg51.h>

sbit LED0 = P2^0;


sbit LED1 = P2^1;
sbit LED2 = P2^2;
sbit IRIN = P3^2;

unsigned char IrValue[6];


unsigned char Time;
void IrInit();
void DelayMs(unsigned int );
void main()
{
unsigned char i;
IrInit();
while(1)
{
if (IrValue[2]==0x0c)
LED0=~LED0;
if (IrValue[2]==0x18)
LED1=~LED1;
if (IrValue[2]==0x5e)
LED2=~LED2;
IrValue[2]=0;
}
}

void DelayMs(unsigned int x) //tao tre 10us


{
unsigned char i;
while(x--)
{
for (i = 0; i<13; i++) //for mat 13 µs.
{}
}
}

void IrInit()
{
IT0=1;
EX0=1;
EA=1;
IRIN=1;
}

//Theo giao th?c NEC:


//Start bit: M?c th?p kéo dài 9ms, sau dó là m?c cao 4.5ms.
void ReadIr() interrupt 0
{
unsigned char j,k;
unsigned int err;
Time=0;
DelayMs(70);

if(IRIN==0)
{

err=1000; //1000*10us=10ms

while((IRIN==0)&&(err>0)) //tao xung 10ms d


{
DelayMs(1);
err--;
}
if(IRIN==1)
{
err=500;
while((IRIN==1)&&(err>0)) //tao xung 4.5ms de
kiem ra tin hieu da tat chua
{
DelayMs(1);
err--;
}
for(k=0;k<6;k++)
{
for(j=0;j<8;j++)
{

err=60;
while((IRIN==0)&&(err>0))

{
DelayMs(1);
err--;
}
err=500;
while((IRIN==1)&&(err>0))
{
DelayMs(1);
Time++;
err--;
if(Time>30)
{

return;
}
}
IrValue[k]>>=1;
if(Time>=8)
{
IrValue[k]|=0x80;
}
Time=0;
}
}
}
if(IrValue[2]!=~IrValue[3])
{
return;
}
}
}

Bài 6. Smart home


#include <REGX52.H>
#include "ds1302.h"
#include "XPT2046.h"
#include "intrins.h"
#include "stdutils.h"

#define DIG P0
//7seg
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
// quang
sbit LED=P2^0;
//ir
sbit LED0=P2^5;
sbit LED1=P2^6;
sbit LED2=P2^7;
sbit LEDX =P2^1;

sbit IRIN = P3^2;

unsigned char code


DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Num=0;
unsigned int disp[8];
unsigned int temp;
unsigned char IrValue[6];
unsigned char l;
void Timer0Configuration();
void IrInit();
void DelayMs(unsigned int );
void dk();
void SetInitialTime();

void main() {
IrInit();
SetInitialTime();
Ds1302Init();
Timer0Configuration();
while (1) {
Ds1302ReadTime();
disp[7] = DIG_CODE[TIME[0] & 0x0f];
disp[6] = DIG_CODE[TIME[0] >> 4]; // giây
disp[5] = 0x40;
disp[4] = DIG_CODE[TIME[1] & 0x0f]; // phút
disp[3] = DIG_CODE[TIME[1] >> 4];
disp[2] = 0x40;
disp[1] = DIG_CODE[TIME[2] & 0x0f];
disp[0] = DIG_CODE[TIME[2] >> 4]; // gi?
dk();
temp = Read_AD_Data(0xA4);
if (temp > 200) {
LED = 1;
} else {
LED = 0;
}
if (IrValue[2] == 0x0c) {
LED0 = ~LED0;
}
if (IrValue[2] == 0x18) {
LED1 = ~LED1;
}
if (IrValue[2] == 0x5e) {
LED2 = ~LED2;
}
IrValue[2] = 0;
}
}
/*void dk()
{
if(TIME[2] == 0x16 && TIME[1] == 0x00 && TIME[0] == 0x00)
LEDX =0;
else if (TIME[2] == 0x16 && TIME[1] == 0x00 && TIME[0] == 0x05)
LEDX=1;
}*/
void dk()
{
if( TIME[0] > 0x30)
LEDX=0;
else
LEDX=1;
}
void Timer0Configuration() {
TMOD = 0x02;
TH0 = 0x9C;
TL0 = 0x9C;
ET0 = 1;
EA = 1;
TR0 = 1;
}
// hien thi
void DigDisplay() interrupt 1
{
DIG=0;
switch(Num)
{
case(7):
LSA=0;LSB=0;LSC=0; break;
case(6):
LSA=1;LSB=0;LSC=0; break;
case(5):
LSA=0;LSB=1;LSC=0; break;
case(4):
LSA=1;LSB=1;LSC=0; break;
case(3):
LSA=0;LSB=0;LSC=1; break;
case(2):
LSA=1;LSB=0;LSC=1; break;
case(1):
LSA=0;LSB=1;LSC=1; break;
case(0):
LSA=1;LSB=1;LSC=1; break;
}
DIG=disp[Num];
Num++;
if(Num>7)
Num=0;
}

void DelayMs(unsigned int x)


{
unsigned char i;
while(x--)
{
for (i = 0; i<13; i++)
{}
}
}

void IrInit()
{
IT0=1;//ϽµÑØ´¥·¢
EX0=1;//´ò¿ªÖжÏ0ÔÊÐí
EA=1; //´ò¿ª×ÜÖжÏ
IRIN=1;//³õʼ»¯¶Ë¿Ú
}
void ReadIr() interrupt 0
{
unsigned char j,k;
unsigned int err;
l=0;
DelayMs(70);

if(IRIN==0) //È·ÈÏÊÇ·ñÕæµÄ½ÓÊÕµ½ÕýÈ·µÄÐźÅ
{

err=1000;
//1000*10us=10ms,³¬¹ý˵Ã÷½ÓÊÕµ½´íÎóµÄÐźÅ

while((IRIN==0)&&(err>0)) //µÈ´ýÇ°Ãæ9msµÄµÍµçƽ¹ýÈ¥

{
DelayMs(1);
err--;
}
if(IRIN==1) //Èç¹ûÕýÈ·µÈµ½9msµÍµçƽ
{
err=500;
while((IRIN==1)&&(err>0)) //µÈ
´ý4.5msµÄÆðʼ¸ßµçƽ¹ýÈ¥
{
DelayMs(1);
err--;
}
for(k=0;k<6;k++) //¹²ÓÐ4×éÊý¾Ý
{
for(j=0;j<8;j++) //½ÓÊÕÒ»×éÊý¾Ý
{

err=60;
while((IRIN==0)&&(err>0))//µÈ
´ýÐźÅÇ°ÃæµÄ560usµÍµçƽ¹ýÈ¥

{
DelayMs(1);
err--;
}
err=500;
while((IRIN==1)&&(err>0))
{
DelayMs(1);
l++;
err--;
if(l>30)
{
return;
}
}
IrValue[k]>>=1;
if(l>=8)
{
IrValue[k]|=0x80;
}
l=0;

}
}
}
if(IrValue[2]!=~IrValue[3])
{
return;
}
}

}
Bài 7.Ran san moi
#include <matrix.h>
#include <INTRINS.h>
#include <REG51.h>
#define HANG P0

// Define button pins


sbit RIGHT = P3^3;
sbit LEFT = P3^2;
sbit UP = P3^1;
sbit DOWN = P3^0;

unsigned char snake[64];


unsigned char food;
unsigned char direction = 0; // 3: Up, 2: Down, 0: Left, 1: Right
unsigned char snake_length = 1;
unsigned char display_buffer[8];
bit game_over = 0;

// Ham tao so ngay nhien


unsigned char rand() {
static unsigned int seed = 0xACE1; // Seed ban dau
seed = (seed * 25173 + 13849) & 0xFFFF; // & voi FFFF de gioi han 16bit
return (unsigned char)(seed % 64); // chia 64 de ra tu 0-63
}

// ham khoi tao tro choi


void init_game() {
unsigned char i;
// vong lap de set gia tri ban dau cua led ma tran tat het
for (i = 0; i < 64; i++) {
snake[i] = 0;
}
snake[0] = 5; // vi tri bat dau
food = rand(); // vi tri moi ngau nhien
snake_length =1;
game_over = 0; // Reset co game over
}

// cap nhat vi tri ran


void update_snake() {
unsigned char i;
// vong lap de ran di chuyen
for (i = snake_length; i > 0; i--) {
snake[i] = snake[i-1]; // gan cho phan sau bang phan truoc
}
if (direction == 0) {
if (snake[0] < 8) snake[0] += 56;
else snake[0] -= 8;
} else if (direction == 1) {
if (snake[0] >= 56) snake[0] -= 56;
else snake[0] += 8;
} else if (direction == 2) {
if (snake[0] % 8 == 0) snake[0] += 7;
else snake[0] -= 1;
} else if (direction == 3) {
if (snake[0] % 8 == 7) snake[0] -= 7;
else snake[0] += 1;
}
}

// Kiem tra va cham


void check_collision() {
// so sanh dau ran voi than ran
unsigned char i;
for (i = 1; i < snake_length; i++) {
if (snake[0] == snake[i]) {
game_over = 1; // Ðánh dau trang thai game over khi va cham
return;
}
}
if (snake[0] == food) {
if (snake_length < 64) {
snake_length++;
}
// S? d?ng hàm rand() d? t?o v? trí m?i ng?u nhiên
do {
food = rand();
} while (food == snake[0]); // dam bao moi khong trung voi ran
}
}

// Hi?n th? vi?n màu d? khi game_over


void show_border() {
unsigned char i;
for (i = 0; i < 8; i++) {
display_buffer[i] = 0xFF; // T?t c? các dèn LED trong hàng d?u sáng
}
}

// C?p nh?t buffer hi?n th?


void update_display_buffer() {
unsigned char i, row, col;

// Xóa display_buffer
for (i = 0; i < 8; i++) {
display_buffer[i] = 0x00;
}

if (game_over) {
show_border(); // Hi?n th? vi?n n?u game_over
} else {
// Hi?n th? r?n và th?c an
for (i = 0; i < snake_length; i++) {
row = snake[i] / 8;
col = snake[i] % 8;
display_buffer[row] |= (1 << col); // dich bit sang trai or voi row
}
row = food / 8;
col = food % 8;
display_buffer[row] |= (1 << col);
}
}

void main() {
unsigned char tab;
unsigned int i;

init_game();

while(1) {
if (game_over) {
show_border(); // Hien thi do man hinh
update_display_buffer();
for(i = 0; i < 300; i++) {
for(tab = 0; tab < 8; tab++) {
Hc595SendByte(0x00);
HANG = TAB[tab];
Hc595SendByte(display_buffer[tab]);
delay(1);
}
}
init_game(); // Reset game sau khi hien thi vien
continue; // Quay lai dau vong lap chinh
}

if (UP == 0 && direction != 2) {


direction = 3;
delay(20);
}
if (DOWN == 0 && direction != 3) {
direction = 2;
delay(20);
}
if (LEFT == 0 && direction != 1) {
direction = 0;
delay(20);
}
if (RIGHT == 0 && direction != 0) {
direction = 1;
delay(20);
}

update_snake();
check_collision();
update_display_buffer();

for(i = 0; i < 30; i++) {


for(tab = 0; tab < 8; tab++) {
Hc595SendByte(0x00);
HANG = TAB[tab];
Hc595SendByte(display_buffer[tab]);
delay(1);
}
}
delay(1);
}
}

You might also like