BÀI THỰC HÀNH NHÚNG (CODE) BÀI 1 Thực hành nhúng đấu nối, chạy demo mạch 8 led đơn sang dồn dùng PIC và ARDUINO
Main.c #include <Main.h> //COPYRIGHT BY XUAN KIEN 2017 //------------CHUONG TRINH CHINH--------------------------------------------------void main(void) { //khai bao bien cuc bo o day unsigned char MaDieuKhien=0x01, LuuTam=0x00,Gan; signed char i,j; while(1) { P1=0x00; LuuTam=0x00; _Delay_ms(300); for(i=8;i>=1;i--) { 1
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; // select the input pin for the potentiometer
int ledPin = 13;
// 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 NGUYEN XUAN KIEN!"); } void loop() { unsigned long LayTong=0; int Time=0; while(Time<500) { Time=Time+1; 3
LiquidCrystal lcd(2,3,4,5,6,7); DHT11 _DHT11; void setup() { // put your setup code here, to run once: pinMode(MAY_BOM,OUTPUT); pinMode(DEN,OUTPUT); pinMode(DEN_XANH,OUTPUT); pinMode(DEN_DO,OUTPUT); 5
digitalWrite(MAY_BOM,0); digitalWrite(DEN,0); digitalWrite(DEN_XANH,0); digitalWrite(DEN_DO,0); lcd.begin(16,2); 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(HDoAm>=DoAmMax) { digitalWrite(MAY_BOM,0); 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>" 8
"<input type=\"submit\" value=\"Submit\">" "</form>" "</BODY></HTML>"; String cipsend = "AT+CIPSEND=0,"; //gui chuoi data qua Wifi cipsend += html.length(); cipsend += "\r\n"; guidulieu(cipsend, 1000, DEBUG); guidulieu(html, 1000, DEBUG); guidulieu("AT+CIPCLOSE=0\r\n", 1000, DEBUG); x = 0; } } void IncomingChar(const byte InChar) { static char InLine[300]; //Hạn chế ký tự static unsigned int Position = 0; switch (InChar) { 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 15
#define Led_2 1 #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; byte MaDieuKhien=0x01,LuuTam=0x00,Gan; _ghi_led(0x00);
{ byte i; for(i=0;i<8;i++) { if(_bit_test(MaDieuKhien,i)==1) { _bat_led(i+1); } 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;
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{ for(Dem_2=0;Dem_2<123;Dem_2++) { ; } } }
20
Bài 10 Đếm 1 số trên led 7 đoạn AT 89C51
Main.c #include <Main.h> //------------CHUONG TRINH CHINH--------------------------------------------------void main(void) { //khai bao bien cuc bo o day char Dem;