define IN_1 3 define IN_2 4 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 ...
Trang 1LẠI MINH HỌC
BÀI LẬP TRÌNH HỆ THỐNG NHÚNG (CÓ CODE)
PIC, ARDUINO, AT89…
Trang 2BÀI THỰC HÀNH Bài 1
Đi u khi n đ ng c dung IC L298-V3 và Arduinoều khiển động cơ dung IC L298-V3 và Arduino ển động cơ dung IC L298-V3 và Arduino ộng cơ dung IC L298-V3 và Arduino ơ 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}
Trang 3//chuong trinh chinh
void loop()
{
// put your main code here, to run repeatedly:
//khai bao bien va cai dat them
Trang 4Turns 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 5// the setup function runs once when you press reset or power the boardvoid setup()
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ều khiển động cơ dung IC L298-V3 và Arduino ển động cơ dung IC L298-V3 và Arduino ồn Arduino
Trang 6//khai bao cac chan ket noi led don
void _bat_led(byte Led);
void _tat_led(byte Led);
Trang 11}
}
Trang 12Dem=Dem+1;
Trang 13Dem=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 14//0b11000011Tam=Data & (0x80>>SoLanDich);//gui bit cao di truocif(Tam!=0)
{
DS=1;
}
Trang 15DS=0;
}SH_CP=0;
;}
//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);
void _DenVang_1(void);
Trang 16signed 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 17Bài 12
Led đ nơ dung IC L298-V3 và Arduino
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
{
output_high(PIN_B0);//bat led
Trang 18_Delay(10);//tao tre 1soutput_low(PIN_B0);//tat led_Delay(10);//tao tre 1s}
}
/****************************************************************//****************************************************************/void _Delay(char time)
Bài 13
Led sang d nầ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 1void _SangDan_C2(void);//ham dieu khien led sang dang theo cach 2void _SangDan_C3(void);
Trang 20output_b(0b00111111);//bat led thu sau
unsigned int8 temp,reset=0x00,init=0x01,move;
output_b(reset);//dua trang thai led ve trang thai ban dau la tat hetoutput_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 21output_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
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
output_c(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
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 22value=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
//value=0b00000010
Trang 23//sau lenh value|temp thi ta duoc//move=0b00000011
Trang 24//value=0b00000001output_b(value);//nap gia tri vao ngo ra_Delay(3);//tao tre 0.3s
output_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 25#use delay(clock=4M)
/*****************************************************/
//khai bao bien toan cuc
/*****************************************************/
//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){
Trang 26_To_Right();//dong quay phai}
lcd_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 27=[PR2+1]*4*(1/Fosc)*(TMR2 Prescale Value)hay T(pwm)=[99+1]*4*(1/4)*1
=100*10^(-6)=10^(-4)=10khzvay 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 laithi bi off
Trang 28SETUP_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 laithi bi off