Mjnh đang tự làm con robot dò đường dùng pic 16f877a nhưng cai code ko hiểu sao lại ko chạy dc ai pro vào xem hộ mìn cái !
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=24000000)
#define dc_t(x) set_pwm1_duty(x)
#define dc_p(x) set_pwm2_duty(x)
#define dir_t_l output_low(pin_c2) // dc_1 muc thap
#define dir_t_h output_high(pin_c3) // dc_1 muc cao
#define dir_p_l output_low(pin_c0) // dc_2 muc thap
#define dir_p_h output_high(pin_c1) // dc_2 muc cao
/////////////////////////////////////////////
INT8 SENSOR=0xfc;
INT1 LEFT,RIGHT;
INT8 SPEED; //toc do dong co
int8 nguong = 100; // nguong cam bien /////////////////////////////////////////////////////////////////////////
INT8 DELTA_P=0;
INT8 DELTA_T=0;
VOID scan()
{
int8 k;
sensor=0;
for(k=0;k<=5;k++)
{
set_adc_channel(k);
delay_ms(10);
if(read_adc()>150) bit_set(sensor,k);
}
}
VOID TU_DONG_TIEN()
{
if (sensor==0b00100000){dc_t(10);dc_p(60);}
if (sensor==0b00010000){dc_t(10);dc_p(50);}
if (sensor==0b00001000){dc_t(10);dc_p(40);}
if (sensor==0b00001100){dc_t(50);dc_p(50);}
if (sensor==0b00000100){dc_t(60);dc_p(10);}
if (sensor==0b00000010){dc_t(50);dc_p(10);}
if (sensor==0b00000001){dc_t(40);dc_p(0);}
}
void main()
{
setup_timer_2(T2_div_by_4,100,1);
setup_ccp1(ccp_pwm);
setup_ccp2(ccp_pwm);
//Dinh huong cong vao ra
set_tris_a(255); // Cong A vao
set_tris_c(0); // Cong C ra
//set_timer0(0xFFFE);
while(1)
{
scan();
//sensor=0b00000001
void tu_dong_tien();/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//sensor=0b00000000;
}
}
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=24000000)
#define dc_t(x) set_pwm1_duty(x)
#define dc_p(x) set_pwm2_duty(x)
#define dir_t_l output_low(pin_c2) // dc_1 muc thap
#define dir_t_h output_high(pin_c3) // dc_1 muc cao
#define dir_p_l output_low(pin_c0) // dc_2 muc thap
#define dir_p_h output_high(pin_c1) // dc_2 muc cao
/////////////////////////////////////////////
INT8 SENSOR=0xfc;
INT1 LEFT,RIGHT;
INT8 SPEED; //toc do dong co
int8 nguong = 100; // nguong cam bien /////////////////////////////////////////////////////////////////////////
INT8 DELTA_P=0;
INT8 DELTA_T=0;
VOID scan()
{
int8 k;
sensor=0;
for(k=0;k<=5;k++)
{
set_adc_channel(k);
delay_ms(10);
if(read_adc()>150) bit_set(sensor,k);
}
}
VOID TU_DONG_TIEN()
{
if (sensor==0b00100000){dc_t(10);dc_p(60);}
if (sensor==0b00010000){dc_t(10);dc_p(50);}
if (sensor==0b00001000){dc_t(10);dc_p(40);}
if (sensor==0b00001100){dc_t(50);dc_p(50);}
if (sensor==0b00000100){dc_t(60);dc_p(10);}
if (sensor==0b00000010){dc_t(50);dc_p(10);}
if (sensor==0b00000001){dc_t(40);dc_p(0);}
}
void main()
{
setup_timer_2(T2_div_by_4,100,1);
setup_ccp1(ccp_pwm);
setup_ccp2(ccp_pwm);
//Dinh huong cong vao ra
set_tris_a(255); // Cong A vao
set_tris_c(0); // Cong C ra
//set_timer0(0xFFFE);
while(1)
{
scan();
//sensor=0b00000001
void tu_dong_tien();/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//sensor=0b00000000;
}
}
Comment