Professional Documents
Culture Documents
Đại Học Bách Khoa Hà Nội: Viện Điện Tử - Viễn Thông
Đại Học Bách Khoa Hà Nội: Viện Điện Tử - Viễn Thông
Nhm 21
Sinh vin: TRN XUN TRNG 20092932
MC LC
V. iu xung (PWM) iu khin ng c
Gin xung
Vi tn s xc nh c l f = 1/(ln.C1.(R1+2R2) nn ch cn iu
chnh R2 l c th thay i rng xung d dng. Ngoi 555 ra cn
rt nhiu cc IC to xung vung khc.
V tc ng c DC t l vi duty cycle nn tc ng c t
tng ng 80% tc ti a.
Delay_ms(Toff);
Tn s iu xung PWM:
f = fosc / [ 4*mode*(period+1) ]
o setup_ccp1(mode) v setup_ccp2(mode)
mode:
CCP_PWM: chn ch PWM.
CCP_OFF: tt ch PWM.
o set_pwm1_duty(value) v set_pwm2_duty(value)
Nu value l gi tr kiu int 8bit:
duty_cycle = value / ( period+1 )
Nu value l gi tr long int 16bit:
duty_cycle = value&1023 /
[4*( period+1 )]
Nu khng cn iu xung qu mn th nn iu xung gi tr
value 8bit cho n gin.
V d: Ta mun iu xung PWM vi tn s 10kHz vi tn s thch
anh (fosc) s dng l 20MHz (value 8bit).
f=fosc/[4*mode*(period+1)] <=> 10000 =20000000/
[ 4*mode*(period+1) ] <=> mode(period+1) = 500
Vi mode = [1,4,16] v period = 0-255 ta c th chn:
o mode = 4; period = 124
o mode = 16; period = 32
Code:
setup_timer_2(T2_DIV_BY_4,124,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(30);
setup_ccp2(CCP_PWM);
set_pwm2_duty(value);
}
void right_motor_forward(int value)
{
MOTOR_RIGHT_DIR=0;
setup_timer_2(T2_DIV_BY_4,124,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(value);
}
void left_motor_reverse(int value)
{
MOTOR_LEFT_DIR=1;
setup_timer_2(T2_DIV_BY_4,124,1);
setup_ccp2(CCP_PWM);
set_pwm2_duty(value);
}
void right_motor_reverse(int value)
{
MOTOR_RIGHT_DIR=1;
setup_timer_2(T2_DIV_BY_4,124,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(value);
}
void left_motor_stop()
{
setup_ccp1(CCP_OFF);
}
void right_motor_stop()
{
setup_ccp1(CCP_OFF);
}
// Chng trnh x l tc 2 ng c
// 0:Stop,100:FORWARD 100%,-100:Reverse 100%
void speed (signed int left_motor_speed, signed int
right_motor_speed)
{
int left_pwm_value=0,right_pwm_value=0;
/* Left motor */
if( left_motor_speed >= 0 )
{
left_pwm_value = 1.25*left_motor_speed; //
(125*left_motor_speed/100)
left_motor_forward(left_pwm_value);
}
else
{
left_motor_speed = -left_motor_speed;
left_pwm_value = 1.25*left_motor_speed; //
(125*left_motor_speed/100)
left_motor_reverse(left_pwm_value);
}
/* Right motor */
if( right_motor_speed >= 0 )
{
right_pwm_value = 1.25*right_motor_speed; //
(125*left_motor_speed/100)
right_motor_forward(right_pwm_value);
}
else
{
right_motor_speed = -right_motor_speed;
right_pwm_value = 1.25*right_motor_speed; //
(125*left_motor_speed/100)
right_motor_reverse(right_pwm_value);
}
}
4.PWM trong iu khin ng c
Trong ng c : iu m chng ta d nhn thy rng l PWM rt
hay c s dng trong ng c iu khin ng c nh l
nhanh , chm, thun ,nghch v n nh tc cho n. Ci ny c
ng dng nhiu trong iu khin ng c 1 chiu. v s nguyn
l ca mch iu khin ng c DC l :