Chào các bạn trong bài đăng này mình sẽ hướng dẫn các bạn cách điều khiển động cơ quay trái, quay phải bằng PWM sử dụng PIC16F877A.
Ảnh mô phỏng protues.
Code chương trình.
#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
#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
/*****************************************************/
/*****************************************************/
//chuong trinh chinh
void main(void)
{
//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
_To_Right();//dong quay phai
}
}
}
/*****************************************************/
/*****************************************************/
void _Delay(char time)
{
char i;
for(i=0;i<time;i++)//xay dung vong lap for
{
delay_ms(100);//tao tre 0.1s
}
}
void _Init_Input(void)
{
PORT_B_PULLUPS(0b00000011);//ta thiet lap dien tro keo len cho chan PIN_B0
}
void _Init_lcd(void)
{
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)
=[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)
*/
SET_PWM1_DUTY(0);//thiet lap chu ki bon phan ban dau la 0 => ngung quay dong co(neu dong co dang chay)
SET_PWM2_DUTY(0);
}
void _To_Left(void)//dieu khien dong co quay trai
{
/*
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
SET_PWM2_DUTY(0);
lcd_gotoxy(1,1);
printf(lcd_putc,"QUAY TRAI ");
}
void _To_Right(void)//dieu khien dong co quay phai
{
/*
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 ");
}
Link download project.
http://adf.ly/11512871/sensor
Ảnh mô phỏng protues.
Code chương trình.
#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
#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
/*****************************************************/
/*****************************************************/
//chuong trinh chinh
void main(void)
{
//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
_To_Right();//dong quay phai
}
}
}
/*****************************************************/
/*****************************************************/
void _Delay(char time)
{
char i;
for(i=0;i<time;i++)//xay dung vong lap for
{
delay_ms(100);//tao tre 0.1s
}
}
void _Init_Input(void)
{
PORT_B_PULLUPS(0b00000011);//ta thiet lap dien tro keo len cho chan PIN_B0
}
void _Init_lcd(void)
{
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)
=[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)
*/
SET_PWM1_DUTY(0);//thiet lap chu ki bon phan ban dau la 0 => ngung quay dong co(neu dong co dang chay)
SET_PWM2_DUTY(0);
}
void _To_Left(void)//dieu khien dong co quay trai
{
/*
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
SET_PWM2_DUTY(0);
lcd_gotoxy(1,1);
printf(lcd_putc,"QUAY TRAI ");
}
void _To_Right(void)//dieu khien dong co quay phai
{
/*
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 ");
}
Link download project.
http://adf.ly/11512871/sensor
EmoticonEmoticon