30-08-2013, 11:04 AM
|
#11
|
Đệ tử 1 túi
Tham gia ngày: May 2013
Bài gửi: 20
:
|
Trích:
Nguyên văn bởi quanghuy_125
Hoàn toàn được bạn ạ.
Bạn tham khảo ví dụ nha:
#include <16f877a.h>
#include <def_877a.h>
#device *=16 adc=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=200000000)
int8 P=0;
void init()
{
// cai PWM bo dieu che xung cho dong co
setup_ccp1(CCP_PWM); // Bat che do PWM cho RC1
// setup_ccp2(CCP_PWM); // Bat che do PWM cho RC2
setup_timer_2(T2_DIV_BY_4,156,1);//Ta co PR2=199,prescale=1
//Tpwm=[PR2+1]*4*1/clock*prescale=[199+1]*4/20000000*1=40 us
//Fpwm=1/Tpwm=25 kHZ
// set_tris_c(0b00000000);
set_pwm1_duty (10);
}
void main()
{
init();
// Khoi tao che do cho bo ADC
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
delay_us(10);
// Lay mau nhiet do lan dau tien
P=Read_ADC(ADC_START_AND_READ); //Lire Vin
restart_wdt(); // Woof !
delay_ms (10);
TRISA = 0x3F;
TRISB = 0x00;
TRISC = 0x0B;
TRISE = 0x07;
TRISD=0x03;
PORTB=0x00;
PORTC=0x0B;
PORTD=0x00;
while (true)
{
if(RA3==0||RD0==0){
if(RA3==0){
if(RA1==0||RA2==0){
if(RA1==0){RC5=1;RD2=0;
delay_ms(3);RC4=RD3=1;
};
if(RA2==0){RD2=1;RC5=0;
delay_ms(3);RC4=RD3=1;
};
}
else {RC5=RD2=0;RD3=1;RC4=1;delay_us(100);};
};
if(RD0==0){
if(RA1==0||RA2==0){
if(RA1==0){RC5=0;RD2=1;
delay_ms(3);RC4=RD3=1;
};
if(RA2==0){RD2=0;RC5=1;
delay_ms(3);RC4=RD3=1;
};
}
else {RC5=RD2=1;delay_ms(3);RC4=RD3=1;};
};
} else {RD3=RC4=RC5=RD2=0;};
if((RA1==0||RA2==0)&&RA3==1&&RD0==1){
if(RA1==0){ RC5=1;RD2=0;
delay_ms(3);RC4=1;RD3=0;
};
if(RA2==0){ RC5=0;RD2=1;
delay_ms(3);RC4=0;RD3=1;
};
};
delay_ms(1);
P=Read_ADC(ADC_START_AND_READ); //Lire Vin
restart_wdt(); // Woof !
delay_ms(4);
P=P/1.57;
set_pwm1_duty (P);
delay_ms (2);
/*
set_pwm2_duty(10);//100%
delay_ms(50);
set_pwm2_duty(25);//80%
delay_ms(100);
set_pwm2_duty(50);//50%
delay_ms(100);
set_pwm2_duty(100);
delay_ms(100);
set_pwm2_duty(120);
delay_ms(100);*/
}
}
|
cảm ơn anh nhiều em thử lại nhé có gì anh tiếp tục chỉ giáo, cảm ơn anh nhiều
|
|
|