0% found this document useful (0 votes)
2 views13 pages

Filewordvdk 1

The document contains multiple code snippets for microcontroller programming, primarily using the C language. It includes various functions for controlling hardware components such as LEDs, motors, and LCD displays. The code demonstrates functionalities like blinking LEDs, handling button inputs, and displaying messages on an LCD screen.

Uploaded by

nttacb9
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)
2 views13 pages

Filewordvdk 1

The document contains multiple code snippets for microcontroller programming, primarily using the C language. It includes various functions for controlling hardware components such as LEDs, motors, and LCD displays. The code demonstrates functionalities like blinking LEDs, handling button inputs, and displaying messages on an LCD screen.

Uploaded by

nttacb9
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/ 13

Bài2

#include P2_6 = 0; P2_1 = 0; P2_2 = P2_5 = 0;


<REGX52.H> delay(20000); P2_6 delay(20000); P2_1 delay(20000); P2_2
= 1; = 1; =P2_5 = 1;
void delay(int time)
P2_7 = 0; P2_0 = 0; P2_1 = P2_6 = 0;
{
delay(20000); P2_7 delay(20000); P2_0 delay(20000); P2_1
while(time--); = 1; = 1; =P2_6 = 1;

} } } P2_0 = P2_7 = 0;
delay(20000); P2_0
#define sang = 0 } }
=P2_7 = 1;
#define tat = 1 void void blink( char
}
phaisangtrai(char lanlap)
void lanlap) }
traisangphai( char {
lanlap) { void main()
char i;
{ char i; {
for(i=0;i<lanlap;i++)
char i; for(i=0;i<lanlap;i++) while(1)
{
for(i=0;i<lanlap;i++) { {
P2 = 0;
{ P2_7 = 0; if(P3_0==1){}
delay(10000);
delay(20000); P2_7
P2_0 = 0; if(P3_0==0)
= 1; P2 = 0xff;
delay(20000); P2_0 {traisangphai(1);}
= 1; P2_6 = 0; delay(10000);
if(P3_1==1){}
delay(20000); P2_6
P2_1 = 0; = 1; }
if(P3_1==0)
delay(20000); P2_1
} {phaisangtrai(1);}
= 1; P2_5 = 0;
delay(20000); P2_5 void if(P3_2==1){}
P2_2 = 0; = 1; giuasanghaiben( ch
delay(20000); P2_2 if(P3_2==0)
P2_4 = 0; ar lanlap)
= 1; {blink(1);}
delay(20000); P2_4 {
P2_3 = 0; = 1; if(P3_3==1){}
delay(20000); P2_3 char i;
P2_3 = 0; if(P3_3==0)
= 1;
delay(20000); P2_3 for(i=0;i<lanlap;i++) {giuasanghaiben(1);
P2_4 = 0; = 1; }
delay(20000); P2_4 {
P2_2 = 0; }
= 1; P2_3 = P2_4 = 0;
delay(20000); P2_2 delay(20000); P2_3 }
P2_5 = 0; = 1;
delay(20000); P2_5 =P2_4 = 1;
= 1;
Bài3

#include TR0=0; while(1) led=tat;


<REGX52.H>
} { }
#define led P0
} int x; if(nut==0xfb)
#define nut P3
void hienthi (int if(nut == 0xfd) {
sbit lsa= P2^2; dem)
{ int j;
sbit lsb= P2^3; {
int j; for(j=0;j<100;j++)
sbit lsc= P2^4; int j;
x=x+1; {
unsigned char code for(j=0;j<10;j++){
if(x>99) x=0; hienthi(j);hienthi(j);
so[10]={0x3f,0x06,0
lsa=0;
x5b,0x4f,0x66,0x6d, for(j=0;j<5;j++) }
0x7d,0x07,0x7f,0x6f led=so[dem%10];
}; { led=tat;
lsb=0;
unsigned char code hienthi(x); }
tat=0x00; delay(5);
} if (nut==0xf7)
void delay (int t) lsa=1;
led=tat; {
{ led=so[dem/10];
} int j;
int i; lsb=0;
if(nut == 0xfe) for(j=0;j<100;j++)
for (i=0;i<=t;i++) delay(5);
{ {
{ }
int j; hienthi(99-
TMOD=0x01; } j);hienthi(99-j);
x=x-1;
TH0=0xFC; void main () }
if(x<0) x=99;
TL0=0x18; { led=tat;
for(j=0;j<5;j++)
TF0=0; lsa=0; }
{
TR0=1; lsb=0; }
hienthi(x);
while (!TF0); lsc=0; }
}
Bài4

#include rs=0; for(i=0;str[i]!=0;i++) lcd_yc (0x80);


<REGX52.H>
rw=0; { }
#define lcd_data P0
en=1; LCD_Char (str[i]); void main()
sbit rs=P2^6;
delay(1); } {
sbit rw=P2^5;
en=0; } unsigned char i;
sbit en=P2^7;
delay(5); void LCD_String_xy delay(5);
void delay (int t) (char row, char pos,
} LCD_Init();
char *str)
{
void LCD_Char while(1)
{
int i; (unsigned char
{delay(500);
char_data) if (row == 0)
for(i=0;i<=t;i++)
lcd_yc(0x01);
{ lcd_yc((pos &
{
0x0F)|0x80); LCD_String_xy(0,15,
lcd_data=char_data
TMOD=0x01; "KHAMLA
; else if (row == 1)
KHANTHILAD");
TH0=0xFC;
rs=1; lcd_yc((pos &
LCD_String_xy(1,15,
TL0=0x18; 0x0F)|0xC0);
rw=0; "4551170136");
TF0=0; LCD_String(str);
en=1; for(i=0;i<sizeof("KH
TR0=1; } AMLA
delay(1);
KHANTHILAD");i++)
while (!TF0); void LCD_Init (void)
en=0;
{
TR0=0; {
delay(5);
lcd_yc(0x18);
} delay(20);
}
delay(300);
} lcd_yc (0x38);
void LCD_String
}
void lcd_yc (unsigned char *str) lcd_yc (0x0C);
(unsigned char yc) lcd_yc(0x01);
{ lcd_yc (0x06);
{ }
int i; lcd_yc (0x01);
lcd_data= yc; }
Bài8

#include <reg51.h> { while(1) if(OFF_BTN == 0)

sbit ON_BTN = unsigned int i, j; { {


P3^1;
for(i=0; i<ms; i++) if(ON_BTN == 0) MOTOR = 0;
sbit OFF_BTN =
for(j=0; j<112; j++); { while(OFF_BTN ==
P3^2;
0);
} MOTOR = 1;
sbit MOTOR =
}
P1^0; void main() while(ON_BTN ==
0); }
void {
delay_ms(unsigned } }
int ms) MOTOR = 0;

Bài9

#include TR0 = 1; { delay(mucthap);


<regx51.h>
while (!TF0); unsigned int }
void delay(unsigned muccao, mucthap;
TF0 = 0; void main()
int t)
muccao = (x *
TR0 = 0; {
{ dutycycle) / 100;
t--; while (1)
TMOD = 0x01; mucthap = x -
} muccao; {
while (t > 0)
} P1_0 = 1; pwm(90);
{
unsigned int x = 10; delay(muccao); }
TH0 = 0xFC;
void pwm(unsigned P1_0 = 0; }
TL0 = 0x18;
char dutycycle)
Bài10 IrValue[5] = IrValue[2] & 0x0F;

#include <reg51.h> if (IrValue[2] != 0 && IrValue[2] != lastCode) {

#define uchar unsigned char lastCode = IrValue[2];

#define uint unsigned int if (IrValue[2] == 0x07) { // Nút xóa

#define lcd P0 LcdWriteCom(0x01);

sbit E = P2^7; delayhn(5);

sbit RW = P2^5; lcd_pos = 0x80;

sbit RS = P2^6; } else if (IrValue[2] == 0x44) { // Nút t?o kho?ng


tr?ng
sbit IRIN = P3^2;
LcdWriteCom(lcd_pos);
unsigned char code maIR[10] = { 0x16, 0x0C,
0x18, 0x5E, 0x08, 0x1C, 0x5A, 0x42, 0x52, LcdWriteData(' ');
0x4A };
lcd_pos++;
unsigned char IrValue[6] = {0};
if (lcd_pos == 0x90) lcd_pos = 0xC0;
unsigned char Time;
else if (lcd_pos == 0xD0) lcd_pos = 0x80;
void delay(uint c);
} else {
void LcdWriteCom(uchar com);
for (i = 0; i < 10; i++) {
void LcdWriteData(uchar dat);
if (IrValue[2] == maIR[i]) {
void khoitaolcd(void);
LcdWriteCom(lcd_pos);
void delayhn(uint x);
LcdWriteData(i + '0');
void IrInit(void);
lcd_pos++;
void ReadIr(void);
if (lcd_pos == 0x90) lcd_pos = 0xC0;
void main() {
else if (lcd_pos == 0xD0) lcd_pos = 0x80;
uchar i;
break;
uchar lcd_pos = 0x80;
}
uchar lastCode = 0xFF;
}
IrInit();
}
khoitaolcd();
}
while (1) {
if (IrValue[2] == 0) {
IrValue[4] = IrValue[2] >> 4;
lastCode = 0xFF;
} RS = 1;

delayhn(100); RW = 0;

} lcd = dat & 0xF0;

} delay(1);

void delay(uint c) { E = 1;

uchar a, b; delay(5);

while (c--) { E = 0;

for (b = 199; b > 0; b--) { lcd = (dat << 4) & 0xF0;

for (a = 1; a > 0; a--); delay(1);

} E = 1;

} delay(5);

} E = 0;

void LcdWriteCom(uchar com) { }

E = 0; void khoitaolcd() {

RS = 0; LcdWriteCom(0x32);

RW = 0; LcdWriteCom(0x28);

lcd = com & 0xF0; LcdWriteCom(0x0C);

delay(1); LcdWriteCom(0x06);

E = 1; LcdWriteCom(0x01);

delay(5); LcdWriteCom(0x80);

E = 0; }

lcd = (com << 4) & 0xF0; void delayhn(uint x) {

delay(1); uchar i;

E = 1; while (x--) {

delay(5); for (i = 0; i < 13; i++) {}

E = 0; }

} }

void LcdWriteData(uchar dat) { void IrInit() {

E = 0; IT0 = 1;
EX0 = 1; while ((IRIN == 1) && (err--)) {

EA = 1; delayhn(1);

IRIN = 1; Time++;

} if (Time > 30) {

void ReadIr(void) interrupt 0 { EX0 = 1;

uchar j, k; return;

uint err; }

Time = 0; }

delayhn(70); IrValue[k] >>= 1;

if (IRIN == 0) { if (Time >= 8) IrValue[k] |= 0x80;

err = 1000; Time = 0;

while ((IRIN == 0) && (err--)) delayhn(1); }

if (IRIN == 1) { }

err = 500; if (IrValue[2] != ~IrValue[3]) {

while ((IRIN == 1) && (err--)) delayhn(1); IrValue[2] = 0;

for (k = 0; k < 4; k++) { return;

for (j = 0; j < 8; j++) { }

err = 60; }

while ((IRIN == 0) && (err--)) delayhn(1); }

err = 500; }

Bài6

#include <REGX52.H> {

#include <string.h> int j;

#define ledmatran P0 for (j = tg; j = 0; j--);

sbit ds = P3^4; }

sbit st = P3^5; void hangled(char x)

sbit sh = P3^6; {

void delay(int tg) int i;


for (i = 0; i < 8; i++) {0x7C, 0xFE, 0x92, 0x92, 0xF2, 0x64, 0x00,
0x00}, // 6
{
{0x06, 0xC6, 0xE2, 0x32, 0x1E, 0x06, 0x00,
if (x & (1 << i))
0x00}, // 7
{
{0x6C, 0xFE, 0x92, 0x92, 0xFE, 0x6C, 0x00,
ds = 1; 0x00}, // 8

} else {0x4C, 0xDE, 0x92, 0x92, 0xFE, 0x7C, 0x00,


0x00}, // 9
{
{0xF8, 0x2C, 0x26, 0x2E, 0xFC, 0xF0, 0x00,
ds = 0; 0x00}, // A
} {0xFE, 0xFE, 0x92, 0x92, 0xFC, 0x60, 0x00,
sh = 1; 0x00}, // B

sh = 0; {0x7C, 0xFE, 0xCE, 0x86, 0x86, 0x4C, 0x00,


0x00}, // C
}
{0x82, 0xFE, 0x82, 0x82, 0xFE, 0x7C, 0x00,
} 0x00}, // D
void cotled(char x) {0xFE, 0xFE, 0x92, 0xBA, 0x82, 0xC6, 0x00,
{ 0x00}, // E

ledmatran = x; {0xFE, 0xFE, 0x92, 0x12, 0x12, 0x06, 0x00,


0x00}, // F
}
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
char code chu[][8] = { // Empty
{0x7C, 0xFE, 0x82, 0x82, 0x7C, 0x00, 0x00, };
0x00}, // 0
int i, j;
{0x00, 0x84, 0xFE, 0xFE, 0x80, 0x00, 0x00,
0x00}, // 1 void hienchu(unsigned char* chaychu)

{0x8C, 0xCE, 0xC6, 0xE6, 0xBE, 0x9C, 0x00, {


0x00}, // 2 for (i = 0; i < 8; i++)
{0x46, 0xC2, 0x92, 0x92, 0xD6, 0x6C, 0x00, {
0x00}, // 3
st = 0;
{0x30, 0x38, 0x2C, 0x26, 0xFE, 0x20, 0x00,
0x00}, // 4 hangled(chaychu[i]);

{0x5E, 0x5A, 0x8A, 0x8A, 0xDA, 0x72, 0x00, cotled((0x01 << i));
0x00}, // 5 delay(80);

st = 1;
} if (!(P1 & (1 << cot)))

} {

void chay1chu(unsigned int tocdo, unsigned while (!(P1 & (1 << cot)));
char* chaychu)
return 15 - (hang * 4 + cot);
{
}
int w;
}
for (w = 0; w < tocdo; w++)
}
{
}
hienchu(chaychu);
return 0xFF;
}
}
}
void main()
unsigned char quetnut()
{
{
unsigned char nh;
unsigned char hang, cot;
while (1)
for (hang = 0; hang < 4; hang++)
{
{
nh = quetnut();
P1 = 0xFF;
if (nh < 16)
P1 &= ~(0x10 << hang);
{
delay(2);
chay1chu(100, chu[nh]);
if ((P1 & 0x0F) != 0x0F)
}
{
}
for (cot = 0; cot < 4; cot++)
}
{
Bài7

#include <reg51.h> LCD = com; void khoitaolcd()

#include <stdio.h> delay(1); {

#define uchar unsigned char E = 1; LcdWriteCom(0x32);

#define uint unsigned int delay(5); LcdWriteCom(0x28);

#define LCD P0 E = 0; LcdWriteCom(0x0c);

sbit E = P2^7; LCD = com << 4; LcdWriteCom(0x06);

sbit RW = P2^5; delay(1); LcdWriteCom(0x01);

sbit RS = P2^6; E = 1; LcdWriteCom(0x80);

void delay( unsigned int t) delay(5); }

{ E = 0; sbit DQ = P3^7;

while(t>0) } void Delay1ms(unsigned int


y)
{ void LcdWriteData(uchar
dat) {
TMOD = 0x01;
{ unsigned int x;
TH0 = 0xFC;
E = 0; for (y; y > 0; y--)
TL0 = 0x18;
RS = 1; for (x = 110; x > 0; x--);
TR0 =1;
RW = 0; }
while(!TF0);
LCD = dat; unsigned char
TF0=0;
khoitaocambien()
delay(1);
TR0=0;
{
E = 1;
t--;
unsigned int i;
delay(5);
}
DQ = 0;
E = 0;
}
i = 70;
LCD = dat << 4;
void LcdWriteCom(uchar
while (i--);
com) delay(1);
DQ = 1;
{ E = 1;
i = 0;
E = 0; delay(5);
while (DQ)
RS = 0; E = 0;
{
RW = 0; }
i++; i++; unsigned char tmh, tml;

if (i > 5000) DQ = 1; Ds18b20ChangTemp();

return 0; i++; Ds18b20ReadTempCom();

} i++; tml = Ds18b20ReadByte();

return 1; bi = DQ; tmh = Ds18b20ReadByte();

} byte = (byte >> 1) | (bi << 7); temp = tmh;

void guilenh(unsigned char i = 4; temp <<= 8;


dat)
while (i--); temp |= tml;
{
} return temp;
unsigned int i, j;
return byte; }
for (j = 0; j < 8; j++)
} void LcdDisplay(int temp)
{
void Ds18b20ChangTemp() {
DQ = 0;
{ int i;
i++;
khoitaocambien(); char buffer[16];
DQ = dat & 0x01;
Delay1ms(1); float tp;
i = 6;
guilenh(0xcc); if (temp < 0)
while (i--);
guilenh(0x44); {
DQ = 1;
} temp = temp - 1;
dat >>= 1;
void temp = ~temp;
} Ds18b20ReadTempCom()
tp = temp * 0.0625;
} {
sprintf(buffer, "-%.2f", tp);
unsigned char khoitaocambien();
}
Ds18b20ReadByte()
Delay1ms(1);
else
{
guilenh(0xcc);
{
unsigned char byte = 0, bi;
guilenh(0xbe);
tp = temp * 0.0625;
unsigned int i, j;
}
sprintf(buffer, "+%.2f", tp);
for (j = 8; j > 0; j--)
int Ds18b20ReadTemp()
}
{
{
LcdWriteCom(0x80);
DQ = 0;
int temp = 0;
for (i = 0; "nhiet do:"[i] != '\ LcdWriteData(buffer[i]); LcdWriteData('C');
0'; i++)
} while (1)
{
} {
LcdWriteData("nhiet do:"[i]);
void main() LcdDisplay(Ds18b20ReadTem
} p());
{
for (i = 0; buffer[i] != '\0'; i++) }
khoitaolcd();
{ }
LcdWriteCom(0x88);

Bài5

#include <REGX51.H> for (k = 16; k > 0; k--) { for (n=0;n<8;n++)

void send_data_cot(unsigned for (j = 0; j < 10; j++) { {


char _data);
for (i = 0; i < 8; i++) { tam =_data&0x80;
void delay(unsigned int ms);
P0 = a[i+k]; _data = _data<<1;
sbit SH_CP = P3^4;
send_data_cot(b[i]); if (tam==0x80)
sbit DS = P3^5;
delay(5); {
sbit ST_CP = P3^6;
send_data_cot(0x00); DS=1;
unsigned char a[24] = {
} }
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
} else
0xFF, 0xFF, 0xFF,
} {
0xFF, 0x00, 0x76, 0x76, 0x76,
0x76, 0x76, 0xFF, } DS=0;
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, } }
0xFF, 0xFF, 0xFF
void send_data_cot(unsigned SH_CP=1;
}; char _data)
SH_CP=0;
unsigned char i, j, k; {
}
unsigned char b[8] = {0x01, unsigned char n,tam;
0x02, 0x04, 0x08, 0x10, 0x20, ST_CP=1;
0x40, 0x80}; ST_CP=0;
ST_CP=0;
int main() { SH_CP=0;
}
while (1) { DS=0;
void delay(unsigned int ms) {
unsigned int i, j;

for (i = 0; i < ms; i++)

for (j = 0; j < 50; j++);

You might also like