void _Delay_ms(unsigned int Time); Main.h #define _Main_h_ #include <REGX52.H>
#include <Delay.h> //khai bao chan ket noi led on //sbit LED_1 = P1^0; #define LED_1 P1_0 Bài 2 Thực hành nhúng điều khiển đóng mở đèn điện khi trời sáng, dùng cảm biến ánh sáng dùng PIC và ARDUINO
int sensorPin = A0; int ledPin = 13;
// select the input pin for the potentiometer
// select the pin for the LED
int sensorValue=0; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); delay(500); Serial.println("THU THAP GIA TRI CAM BIEN ANH SANG!"); Serial.println("DUOC THUC HIEN BOI LILAMA2!"); } 3
lcd.setCursor(0,0); lcd.print("GIAO TIEP DHT11"); } void loop() { // put your main code here, to run repeatedly: while(1) { _DHT11.DHT_GetTemHumi(HNhietDo,LNhietDo,HDoAm,LDoAm);//ham do nhiet do do am moi if(HNhietDo{ digitalWrite(DEN,1); digitalWrite(DEN_DO,1); } else if(HNhietDo>=NhietDoMax) { digitalWrite(DEN,0); digitalWrite(DEN_DO,0); } if(HDoAm{ digitalWrite(MAY_BOM,1); digitalWrite(DEN_XANH,1); 6
digitalWrite(DEN_XANH,0); } lcd.setCursor(0,1); lcd.print("N/D="); lcd.print(HNhietDo); lcd.print("; "); lcd.print("D/A="); lcd.print(HDoAm); delay(50); } } Bài 4 Tự động đóng mở thiết bị điện trong tòa nhà thông minh dung từ xa thông qua internet (cửa, đèn, máy bơm, điều hòa…)
#define DEBUG true #define RELAY_1 2 #define RELAY_2 3 char x; void setup() { Serial.begin(9600); Serial1.begin(115200); // chu y phai cung toc do voi ESP 8266 7
//guidulieu("AT+CIOBAUD=9600\r\n", 1000, DEBUG); // Chọn chức năng client cho esp //Serial1.begin(9600); // chu y phai cung toc do voi ESP 8266 pinMode(13, OUTPUT); // tuong tu chan 12 digitalWrite(13, LOW); pinMode(RELAY_1,OUTPUT);
pinMode(RELAY_2,OUTPUT); digitalWrite(RELAY_1, LOW); digitalWrite(RELAY_2, LOW); //========================gui tap lenh AT cho ESP 8266 de cai dat cac tham so cho WiFi================================ guidulieu("AT+RST\r\n", 2000, DEBUG); // reset module guidulieu("AT+CWMODE=1\r\n", 1000, DEBUG); // Chọn chức năng client cho esp guidulieu("AT+CWQAP\r\n", 1000, DEBUG); guidulieu("AT+CWJAP=\"KSTH\",\"kysuthuchanh\"\r\n", 10000, DEBUG); // Kết nối với wifi //guidulieu("AT+CIPSTA=\"192.168.0.178\"\r\n", 5000, DEBUG); // fix ip guidulieu("AT+CIFSR\r\n", 5000, DEBUG); // xem ip là bn guidulieu("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections guidulieu("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // Mở cổng 80 //delay(1000); //guidulieu("AT+PING=\"google.com\"\r\n", 1000, DEBUG); Serial.println("KHOI DONG XONG!"); digitalWrite(13, HIGH); } void loop() { while (Serial1.available()) { IncomingChar(Serial1.read()); } if (x) //neu doc xong du lieu tu web gui xuong { // Đây là giao diện web page viết dưới dạng html...các bạn có thể dễ dàng edit
String html = "<HTML>" "<HEAD><TITLE>DIEU KHIEN THIET BI QUA WIFI</TITLE>"
{ case '\r': //Cái này là xuống dòng...cho linux break; case '\n': // xuống dòng cho window...( kết thúc bảng tin ) InLine[Position] = 0; ProcessCommand(String(InLine)); Position = 0; break; 9
default: InLine[Position++] = InChar; } } void ProcessCommand(String InLine) { Serial.println("InLine: " + InLine); if (InLine.startsWith("+IPD,")) { x = 1; } // lện String.indexOf(kytu)...chả về vị trí của kytu trong chuỗi String...Nếu giá trị trả về là -1...tức là kytu không xuất hiện trong chuỗi String if (InLine.indexOf("TAT1") != -1) { digitalWrite(RELAY_1, LOW); } else if (InLine.indexOf("BAT1") != -1) {
digitalWrite(RELAY_1, HIGH); } else if (InLine.indexOf("BAT2") != -1) { digitalWrite(RELAY_2, HIGH); } else if (InLine.indexOf("TAT2") != -1) { digitalWrite(RELAY_2, HIGH); } } String guidulieu(String lenh, const int thoigian, boolean debug) { String chuoigiatri = ""; Serial1.print(lenh); // send the read character to the Serial1 long int time = millis(); 10
while ((time + thoigian) > millis()) { while (Serial1.available()) { // The esp has data so display its output to the serial window char c = Serial1.read(); // read the next character. chuoigiatri += c; } } if (debug) { Serial.print(chuoigiatri);
} return chuoigiatri; }
11
Bài 5 Điều khiển động cơ dung IC L298-V3 và Arduino
//khai bao ket noi L298 #define IN_1 3 #define IN_2 4 //khai bao ket noi cac nut nhan #define TANG
5
#define GIAM
6
//chuong trinh cai dat void setup() { // put your setup code here, to run once: Serial.begin(9600); while(!Serial);//cho ket noi UART Serial.println("KHOI DONG UART XONG!"); pinMode(IN_1,OUTPUT);
pinMode(IN_2,OUTPUT); pinMode(TANG,INPUT_PULLUP);//thiet lap tro keo len ben trong chan nay pinMode(GIAM,INPUT_PULLUP);//thiet lap tro keo len ben trong chan nay } //chuong trinh chinh 12
void loop() { // put your main code here, to run repeatedly: //khai bao bien va cai dat them int TocDo=128; analogWrite(IN_1,(byte)TocDo); digitalWrite(IN_2,LOW); Serial.print("TOC DO HIEN TAI LA:"); Serial.println(TocDo); while(1)//vong lap vo han { if(digitalRead(TANG)==0) { while(digitalRead(TANG)==0);//cho phim nhan duoc tha ra TocDo=TocDo+10; if(TocDo>255) { TocDo=128; } analogWrite(IN_1,(byte)TocDo); digitalWrite(IN_2,LOW); Serial.print("TOC DO HIEN TAI LA:");
Serial.println(TocDo); } if(digitalRead(GIAM)==0) { while(digitalRead(GIAM)==0);//cho phim nhan duoc tha ra TocDo=TocDo-10; if(TocDo<0) { TocDo=128; } analogWrite(IN_1,(byte)TocDo); 13
digitalWrite(IN_2,LOW); Serial.print("TOC DO HIEN TAI LA:"); Serial.println(TocDo); } } } Bài 6 Điều khiển led sáng tắt Arduino
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care of use the correct LED pin whatever is the board used. If you want to know what pin the on-board LED is connected to on your Arduino model, check
the Technical Specs of your board at />This example code is in the public domain. modified 8 May 2014 by Scott Fitzgerald modified 2 Sep 2016 by Arturo Guadalupi */ long int MyDelay = 80;
14
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(MyDelay);
// wait for a second
digitalWrite(10, LOW);
// turn the LED off by making the voltage LOW
digitalWrite(11, LOW);
// turn the LED off by making the voltage LOW
digitalWrite(12, LOW);
// turn the LED off by making the voltage LOW
digitalWrite(13, LOW);
// turn the LED off by making the voltage LOW
delay(MyDelay);
// wait for a second
} Bài 7 Điều khiển led sáng dồn Arduino
//khai bao cac chan ket noi led don #define Led_1 0 #define Led_2 1 15
#define Led_3 2 #define Led_4 3 #define Led_5 4 #define Led_6 5 #define Led_7 6 #define Led_8 7 #define MyTime 300 void _bat_led(byte Led); void _tat_led(byte Led); void _bat_het(void); void _tat_het(void); byte _bit_test (byte Data,byte BitThuMay); void _ghi_led(byte MaDieuKhien); void setup() { // put your setup code here, to run once: pinMode(Led_1,OUTPUT); pinMode(Led_2,OUTPUT); pinMode(Led_3,OUTPUT); pinMode(Led_4,OUTPUT); pinMode(Led_5,OUTPUT); pinMode(Led_6,OUTPUT); pinMode(Led_7,OUTPUT); pinMode(Led_8,OUTPUT); } void loop() { // put your main code here, to run repeatedly: byte i,j;
} else { _tat_led(i+1); } } } Bài 8 Đếm bốn số trên led 7 đoạn Arduino
#include "SevenSeg.h" SevenSeg _SevenSeg(true); void setup() { // put your setup code here, to run once: _SevenSeg.cau_hinh(); } void loop() { // put your main code here, to run repeatedly: byte SO1,SO2,SO3,SO4; long Dem; _SevenSeg.tat_quet_cot(); while(1) { ///_SevenSeg.delay_hien_thi(So[0],So[1],So[2],So[3],400); for(Dem=3333;Dem<3444;Dem++) { _SevenSeg.tach_so(Dem,&SO1,&SO2,&SO3,&SO4); _SevenSeg.delay_hien_thi(SO1,SO2,SO3,SO4,70);
} } }
19
Bài 9 Led đơn dung AT 89C51
Main.c #include <Main.h> //------------CHUONG TRINH CHINH--------------------------------------------------void main(void) { //khai bao bien cuc bo o day while(1) { LED_1=0; _Delay_ms(500); LED_1=1; _Delay_ms(500); } } Delay.c #include <Main.h> void _Delay_ms(unsigned int Time) { unsigned int Dem_1,Dem_2; for(Dem_1=0;Dem_1{