Ctrl + A hoặc Home : chuyển con trỏ về đầu dòng. Ctrl + E hoặc End: Chuyển con trỏ đến cuối dòng. BackSpace: Xoá ký tự bên trái con trỏ. Esc: xoá dòng lệnh. Ctrl + K : Xoá từ vị trí con trỏ đến cuối dòng. Ctrl + C : Dừng chương trình đang thực hiện. Clc : lệnh xoá màn hình. Clf: Lệnh xoá màn hình đồ hoạ. Input: lệnh nhập dữ liệu vào từ bàn phím. Demo: lệnh cho phép xem các chương trình mẫu. Help: lệnh cho phép xem phần trợ giúp. Ctrl – c: Dừng chương trình khi nó bị rơi vào trạng thái lặp không kết thúc.
Trang 1//khai bao bien cuc bo o day
unsigned char MaDieuKhien=0x01, LuuTam=0x00,Gan;
signed char i,j;
Trang 22
//LuuTam=0b11000000 Gan=MaDieuKhien|LuuTam;
P1=Gan;
_Delay_ms(300);
for(j=1;j<i;j++) {
} }
; } }
}
Delay.h
#define _Delay_h_
Trang 3int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
Serial.println("THU THAP GIA TRI CAM BIEN ANH SANG!");
Serial.println("DUOC THUC HIEN BOI LILAMA2!");
}
Trang 8guidulieu("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
Trang 99
"<form action=\"\" method=\"get\">"
"<input type=\"radio\" name=\"LED\" value=\"BAT1\">BAT 1" "<br/>"
"<input type=\"radio\" name=\"LED\" value=\"TAT1\">TAT 1<br>" "<br/>"
"<input type=\"radio\" name=\"LED\" value=\"BAT2\">BAT 2" "<br/>"
"<input type=\"radio\" name=\"LED\" value=\"TAT2\">TAT 2<br>" "<br/>"
static char InLine[300]; //Hạn chế ký tự
static unsigned int Position = 0;
Trang 10Serial1.print(lenh); // send the read character to the Serial1
long int time = millis();
Trang 1212
Bài 5
Điều khiển động cơ dung IC L298-V3 và Arduino
//khai bao ket noi L298
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
Trang 1313
void loop()
{
// put your main code here, to run repeatedly:
//khai bao bien va cai dat them
Trang 14Turns 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 https://www.arduino.cc/en/Main/Products
This example code is in the public domain
Trang 15digitalWrite(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
Trang 16void _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);
Trang 20; } }
Trang 2222
P0=Dem;
if(TANG==0) {
while(TANG==0);
Dem=Dem+1;
if(Dem>9) {
Dem=0;
} }
else if(GIAM==0) {
while(GIAM==0);
Dem=Dem-1;
if(Dem<0) {
Dem=9;
} }
for(Dem_2=0;Dem_2<123;Dem_2++) {
; }
Trang 24//0b11000011 Tam=Data & (0x80>>SoLanDich);//gui bit cao di truoc if(Tam!=0)
{
DS=1;
} else {
DS=0;
} SH_CP=0;
Trang 2525
for(Dem_2=0;Dem_2<123;Dem_2++) {
; } }
}
MAIN.C
#include <Main.h>
#define DELAY 500
//khai bao cac chuong trinh con
void _TachSo(char Dem, char *H1, char *H2);
void _HienThiLed(char H1, char H2, char H3, char H4, char H5, char H6, char H7, char H8); void _DelayScan(char H1, char H2, char H3, char H4, char H5, char H6, char H7, char H8, int t); void _DenXanh_1(void);
signed char DenXanh_1=20,Den_Vang_1=3,Den_Do_1;
signed char DenXanh_2=15,Den_Vang_2=3,Den_Do_2;
signed char Dem=0;
Trang 2727
bit bit_test (unsigned char Data,char SoDichBit);
Bài 12
Led đơn
H BAT TAT LED DON GIAN VOI PIC 16F887A - DAO DONG THACH ANH LA 4MHZ
#include<16F877A.H> //khai bao con pic se su dung
#include<math.h> //khai bao thu vien chua cac ham con se duoc su dung
//cau hinh dao dong thach anh 4MHZ
//khai bao cac ham chuon trinh con
void _Delay(char time);
//khai bao bien cuc bo
while(1)//vong lap vo han
{
Trang 2828
output_high(PIN_B0);//bat led _Delay(10);//tao tre 1s
output_low(PIN_B0);//tat led _Delay(10);//tao tre 1s }
}
/****************************************************************/ /****************************************************************/ void _Delay(char time)
Bài 13
Led sang dần
//CHUONG TRINH DIEU KHIEN LED SANG DAN
#include<16F877A.H>//khai bao con pic se duoc su dung
#include<math.h>//khai bao thu vien chua cac ham se duoc su dung
//cau hinh dao dong thach anh 4MHZ
//khai bao cac ham chuong trinh con
void _Delay(char time);//tao thoi gian tre
void _SangDan_C1(void);//ham dieu khien led sang dang theo cach 1
void _SangDan_C2(void);//ham dieu khien led sang dang theo cach 2
Trang 30unsigned int8 temp,reset=0x00,init=0x01,move;
output_b(reset);//dua trang thai led ve trang thai ban dau la tat het output_c(reset);
_Delay(5);//tao tre 0.5s
move=init;//nap gia tri khoi tao ban dau cho gia tri ngo ra
for(i=0;i<8;i++)//xay dung vong lap for
Trang 3131
{
output_b(move);//nap gia tri cho ngo ra temp=move;//luu gia tri ngo ra vao bien tam(buffer) _Delay(5);//tao tre 0.5s
move=move<<1;//dich 1 bit ve phia ben trai //0b00000001
//sau lenh move<<1 thi ta duoc //0b00000010
//sau lenh move<<1 thi ta duoc //0b00000100
move=move|temp;//or hai gia tri lai voi nhau //move=0b00000010 do sau khi dich di mot bit //temp=0b00000001
//sau lenh move|temp thi ta duoc //move=0b00000011
move=move<<1;//dich 1 bit ve phia ben trai //0b00000001
//sau lenh move<<1 thi ta duoc //0b00000010
//sau lenh move<<1 thi ta duoc //0b00000100
move=move|temp;//or hai gia tri lai voi nhau //move=0b00000010 do sau khi dich di mot bit //temp=0b00000001
//sau lenh move|temp thi ta duoc //move=0b00000011
Trang 32value=value|temp;//or hai gia tri voi nhau //temp=0b00000001
//value=0b00000010 //sau lenh value|temp thi ta duoc //move=0b00000011
value=value|temp;//or hai gia tri voi nhau //temp=0b00000001
Trang 3333
//value=0b00000010 //sau lenh value|temp thi ta duoc //move=0b00000011
Trang 34output_c(value);//nap gia tri vao ngo ra _Delay(3);//tao tre 0.3s
}
}
Bài 14
CHUONG TRINH DIEU KHIEN DONG CO DC
#include<16F877A.H>//cau hinh dong pic su dung
#include<math.h>//chen thu vien chua cac ham se duoc su dung
#include<lcd.h>//chen thu vien chua cac ham de giao tiep va tuong tac voi lcd
#define en_pwm PIN_C0//cho phep dong co dung hay chay
#define direction PIN_B0//thay doi chieu quay cua dong co
//cau hinh dao dong thach anh 4M
#fuses HS,NOWDT,NOPROTECT,NOLVP
Trang 35//khai bao cac ham chuong trinh con se duoc su dung
void _Delay(char time);//ham tao thoi gian tre
void _Init_Input(void);//ham cai dat dien tro keo len cho porb b
//voi ham nay ta khong can mac them dien tro nao o ngo vao
void _Init_lcd(void);//ham khoi tao lcd
void _Init_PWM(void);//ham khoi tao pwm
void _To_Left(void);//quay trai
void _To_Right(void);//quay phai
//khai bao bien cuc bo
_Init_Input();//cai dat dien tro keo len
_Init_lcd();//ham khoi tao lcd
_Init_PWM();//ham khoi tao PWM
_To_Right();//mac dinh la dong co quay phai
while(1)//xay dung vong lap vo han
{
if(INPUT(PIN_B0)==0) {
while(INPUT(PIN_B0)==0);//cho doi toi khi nut nhan duoc tha ra _To_Left();//dong co quay trai
} else if(INPUT(PIN_B1)==0) {
while(INPUT(PIN_B1)==0);//cho doi toi khi nut nhan duoc tha ra
Trang 36lcd_init();//ham khoi tao lcd
lcd_putc('\f');//ham xoa trang man hinh lcd
}
void _Init_PWM(void)
{
OUTPUT_HIGH(en_PWM);//cho phep dong co duoc phep hoat dong
SETUP_CCP1(CCP_PWM);//cai dat ccp1 ve che do pwm
SETUP_CCP2(CCP_PWM);//cai dat ccp2 ve che do pwm
SETUP_TIMER_2(T2_DIV_BY_1,99,1);//T2_DIV_BY_1 dung de dinh ti le timer2 la 1:1 //99 la PR2(chu ky PWM/TIMER2), 1 la so ngat cua timer2 la 1
/*
ap dung cong thuc chu ky PWM ta co:
T(pwm)=[PR2+1]*4*(Tosc)*(TMR2 Prescale Value)
Trang 3737
=[PR2+1]*4*(1/Fosc)*(TMR2 Prescale Value) hay T(pwm)=[99+1]*4*(1/4)*1
=100*10^(-6)=10^(-4)=10khz vay tan so de dieu khien dong co la 10(khz)
SETUP_TIMER_2(T2_DIV_BY_1,99,1);//T2_DIV_BY_1 dung de dinh ti le timer2 la 1:1
99 la PR2(chu ky PWM/TIMER2), 1 la so ngat cua timer2 la 1
ap dung cong thuc tinh chu ky bon phan
Ton=(x/T)*100%
thi ta duoc
60%=(x/99)=>x=59;
trong do: 60% la gia tri chu ky bon phan ma ta muon cai dat
nghia la trong chu ky pwm la 99 thi 60% thoi gian chu ky cua no lun duoc on, con lai thi bi off
| -60% -| 40% |
*/
SET_PWM1_DUTY(59);
//khi ta muon dong co quay theo chieu nao thi ta chi nap gia tri duty vao chan do
//va nap gia tri 0 cho chan con lai
Trang 3838
SETUP_TIMER_2(T2_DIV_BY_1,99,1);//T2_DIV_BY_1 dung de dinh ti le timer2 la 1:1
99 la PR2(chu ky PWM/TIMER2), 1 la so ngat cua timer2 la 1
ap dung cong thuc tinh chu ky bon phan
Ton=(x/T)*100%
thi ta duoc
60%=(x/99)=>x=59;
trong do: 60% la gia tri chu ky bon phan ma ta muon cai dat
nghia la trong chu ky pwm la 99 thi 60% thoi gian chu ky cua no lun duoc on, con lai thi bi off
| -60% -| 40% |
*/
SET_PWM1_DUTY(0);
//khi ta muon dong co quay theo chieu nao thi ta chi nap gia tri duty vao chan do
//va nap gia tri 0 cho chan con lai
SET_PWM2_DUTY(59);
lcd_gotoxy(1,1);
printf(lcd_putc,"QUAY PHAI ");
Trang 39void _QuetCot(char TenCot);
char _ScanDelay(char H1,H2,H3,H4,H5,H6,H7,H8,unsigned int Time);
Trang 40_TachSo(Gio,&H1,&H2);
for(Phut=PhutCai;Phut<=59;Phut++) {
_TachSo(Phut,&H4,&H5);
for(Giay=0;Giay<=59;Giay++) {
_TachSo(Giay,&H7,&H8);
_ScanDelay(H1,H2,10,H4,H5,10,H7,H8,150); if(MENU==0)
{
while(MENU==0);
Option=Option+1;
if(Option>4) {
Option=0;
} goto Setup;
} if(Gio==GioHen && Phut==PhutHen) {
THIET_BI=0;
} }
} }
Trang 4141
Setup:
while(Option==1) {
_TachSo(GioCai,&H7,&H8);
_ScanDelay(11,11,11,11,11,11,H7,H8,400);
if(TANG==0) {
while(TANG==0);
GioCai=GioCai+1;
if(GioCai>23) {
GioCai=0;
} } else if(GIAM==0) {
while(GIAM==0);
GioCai=GioCai-1;
if(GioCai<0) {
GioCai=23;
} } else if(MENU==0) {
while(MENU==0);
Option=Option+1;
if(Option>4) {
Option=0;
} }
Trang 4242
} while(Option==2) {
_TachSo(PhutCai,&H7,&H8);
_ScanDelay(11,11,11,11,11,11,H7,H8,400);
if(TANG==0) {
while(TANG==0);
PhutCai=PhutCai+1;
if(PhutCai>59) {
PhutCai=0;
} } else if(GIAM==0) {
while(GIAM==0);
PhutCai=PhutCai-1;
if(PhutCai<0) {
PhutCai=59;
} } else if(MENU==0) {
while(MENU==0);
Option=Option+1;
if(Option>4) {
Option=0;
} }
Trang 4343
} while(Option==3) {
_TachSo(GioHen,&H7,&H8);
_ScanDelay(11,11,11,11,11,11,H7,H8,400);
if(TANG==0) {
while(TANG==0);
GioHen=GioHen+1;
if(GioHen>23) {
GioHen=0;
} } else if(GIAM==0) {
while(GIAM==0);
GioHen=GioHen-1;
if(GioHen<0) {
GioHen=23;
} } else if(MENU==0) {
while(MENU==0);
Option=Option+1;
if(Option>4) {
Option=0;
} } }
Trang 4444
while(Option==4) {
_TachSo(PhutHen,&H7,&H8);
_ScanDelay(11,11,11,11,11,11,H7,H8,400);
if(TANG==0) {
while(TANG==0);
PhutHen=PhutHen+1;
if(PhutHen>59) {
PhutHen=0;
} } else if(GIAM==0) {
while(GIAM==0);
PhutHen=PhutHen-1;
if(PhutHen<0) {
PhutHen=59;
} } else if(MENU==0) {
while(MENU==0);
Option=Option+1;
if(Option>4) {
Option=0;
} }
Trang 4646
if(MENU==0||TANG==0||GIAM==0) return 1; }
{
COT_2=0;
break;
} case 2:
{
COT_3=0;
break;
} case 3:
{
COT_4=0;
break;
} case 4:
{
COT_5=0;
break;
Trang 4747
} case 5:
{
COT_6=0;
break;
} case 6:
{
COT_7=0;
break;
} case 7:
{
COT_8=0;
break;
} default:
{
; break;
} }
return 0;
}
Trang 48for(Dem_2=0;Dem_2<123;Dem_2++) {
; } }