Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//# include "E:\mikroC PRO for PIC\include\built_in.h"
#define move_time 10// miliseconds
#define rad 8// radius of the axel in cm
#define roam_time 20000// in miliseconds
/*#define PORTB.FO PIN_B0
#define PORTB.F1 PIN_B1
#define PORTB.F2 PIN_B2
#define PORTB.F3 PIN_B3
#define PORTB.F4 PIN_B4
#define PORTB.F5 PIN_B5
#define PORTB.F6 PIN_B6
#define PORTB.F7 PIN_B7
#define PORTC.F0 PIN_C0
#define PORTC.F1 PIN_C1
#define PORTC.F2 PIN_C2
#define PORTC.F3 PIN_C3
#define PORTC.F4 PIN_C4
#define PORTC.F5 PIN_C5
#define PORTC.F6 PIN_C6
#define PORTC.F7 PIN_C7*/
void forward();
void right();
void left();
void right_straight();
void left_straight();
void reverse();
void stop();
void main()
{ /*
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
*/
// TODO: USER CODE!!
int count=0;
int decition_left=0;
int decition_right=0;
//Pwm_Init(4000000);
//Pwm_change_duty(192);
//Pwm_Start();
TRISB=0xFF;
TRISC=0x00;
while(1){
if(PORTB.F3==0 && PORTB.F4==0){
if(PORTB.F0==1 && PORTB.F1==1 && PORTB.F2==1 && PORTB.F5==1 && PORTB.F6==1 && PORTB.F7==1){
forward();
delay_ms(move_time);
continue;
}
else if(PORTB.F1==0 && PORTB.F2==0){
decition_right=1;
decition_left=0;
continue;
}
else if(PORTB.F5==0 && PORTB.F6==0){
decition_left=1;
decition_right=0;
continue;
}
else if(((~(PORTB.F1|PORTB.F2))==0 && PORTB.F0==0)||((~(PORTB.F5|PORTB.F6))==0 && PORTB.F7==0)){
if(decition_left==1&& decition_right==0){
do{
left_straight();
delay_ms(5);
}while(PORTB.F4==0 || PORTB.F5==0);
continue;
}
if(decition_left==0 && decition_right==1){
do{
right_straight();
delay_ms(5);
}while(PORTB.F4==0 || PORTB.F5==0);
continue;
}
if(decition_left==0 && decition_right==0){
forward();
delay_ms(move_time);
continue;
}
}
else{
continue;
}
}
else
if(PORTB.F3==0 && PORTB.F4==1){
do{
left();
delay_ms(5);
}while(PORTB.F3==0 && PORTB.F4==0);
continue;
}
else
if(PORTB.F3==1 && PORTB.F4==0){
do{
right();
delay_ms(5);
}while(PORTB.F3==0 && PORTB.F4==0);
continue;
}
else
if(PORTB.F3==1 && PORTB.F4==1){
if(count==1){
do{
forward();
delay_ms(roam_time);
right();
delay_ms(roam_time);
}while(PORTB.F3==0 || PORTB.F4==0);
}
else{
forward();
delay_ms(roam_time);
if(PORTB.F3==0 || PORTB.F4==0){
continue;
}
else{
reverse();
delay_ms(roam_time);
count++;
continue;
}
}
}
else{
stop();
}
}
// Pwm_Stop();
}
void forward(){
PORTC.F1=1;//OUTPUT_HIGH(PIN_C1);
PORTC.F2=1;//OUTPUT_HIGH(PIN_C2);
PORTC.F7=1;//OUTPUT_HIGH(PIN_C7);
PORTC.F6=0;//OUTPUT_LOW(PIN_C6);
PORTC.F5=1;//OUTPUT_HIGH(PIN_C5);
PORTC.F4=0;//OUTPUT_HIGH(PIN_C4);
PORTC.F0=1;//OUTPUT_HIGH(PIN_C0);
PORTC.F3=1;//OUTPUT_HIGH(PIN_C3);
return ;
}
void right(){
PORTC.F1=1;//OUTPUT_HIGH(PIN_C1);
PORTC.F2=0;//OUTPUT_LOW(PIN_C2);
PORTC.F7=1;//OUTPUT_HIGH(PIN_C7);
PORTC.F6=0;//OUTPUT_LOW(PIN_C6);
PORTC.F5=1;//OUTPUT_HIGH(PIN_C5);
PORTC.F4=1;//OUTPUT_HIGH(PIN_C4);
PORTC.F0=1;//OUTPUT_HIGH(PIN_C0);
PORTC.F3=0;//OUTPUT_LOW(PIN_C3);
return ;
}
void left(){
PORTC.F1=0;//OUTPUT_LOW(PIN_C1);
PORTC.F2=1;//OUTPUT_HIGH(PIN_C2);
PORTC.F7=1;//OUTPUT_HIGH(PIN_C7);
PORTC.F6=0;//OUTPUT_LOW(PIN_C6);
PORTC.F5=1;//OUTPUT_HIGH(PIN_C5);
PORTC.F4=0;//OUTPUT_HIGH(PIN_C4);
PORTC.F0=0;//OUTPUT_LOW(PIN_C0);
PORTC.F3=1;//OUTPUT_HIGH(PIN_C3);
return ;
}
void left_straight(){
PORTC.F1=1;//OUTPUT_HIGH(PIN_C1);
PORTC.F2=1;//OUTPUT_HIGH(PIN_C2);
PORTC.F7=0;//OUTPUT_HIGH(PIN_C7);
PORTC.F6=1;//OUTPUT_LOW(PIN_C6);
PORTC.F4=0;//OUTPUT_HIGH(PIN_C4);
PORTC.F5=1;//OUTPUT_HIGH(PIN_C5);
PORTC.F0=1;//OUTPUT_HIGH(PIN_C0);
PORTC.F3=1;//OUTPUT_HIGH(PIN_C3);
return;
}
void right_straight(){
PORTC.F1=1;//OUTPUT_HIGH(PIN_C1);
PORTC.F2=1;//OUTPUT_HIGH(PIN_C2);
PORTC.F6=0;//OUTPUT_HIGH(PIN_C6);
PORTC.F7=1;//OUTPUT_LOW(PIN_C7);
PORTC.F5=0;//OUTPUT_HIGH(PIN_C5);
PORTC.F4=1;//OUTPUT_HIGH(PIN_C4);
PORTC.F0=1;//OUTPUT_HIGH(PIN_C0);
PORTC.F3=1;//OUTPUT_HIGH(PIN_C3);
return;
}
void reverse(){
PORTC.F1=1;//OUTPUT_HIGH(PIN_C1);
PORTC.F2=1;//OUTPUT_HIGH(PIN_C2);
PORTC.F6=1;//OUTPUT_HIGH(PIN_C6);
PORTC.F7=0;//OUTPUT_LOW(PIN_C7);
PORTC.F4=1;//OUTPUT_HIGH(PIN_C4);
PORTC.F5=0;//OUTPUT_HIGH(PIN_C5);
PORTC.F0=1;//OUTPUT_HIGH(PIN_C0);
PORTC.F3=1;//OUTPUT_HIGH(PIN_C3);
return;
}
void stop(){
PORTC.F1=0;//OUTPUT_LOW(PIN_C1);
PORTC.F2=0;//OUTPUT_LOW(PIN_C2);
PORTC.F7=1;//OUTPUT_HIGH(PIN_C7);
PORTC.F6=1;//OUTPUT_LOW(PIN_C6);
PORTC.F5=1;//OUTPUT_HIGH(PIN_C5);
PORTC.F4=1;//OUTPUT_HIGH(PIN_C4);
PORTC.F0=1;//OUTPUT_LOW(PIN_C0);
PORTC.F3=1;//OUTPUT_LOW(PIN_C3);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.