#include<16F648A.h>
#fuses NOLVP,NOMCLR,INTRC,NOPROTECT,NOCPD
#use delay(clock=4000000)
static int counter_left=0,counter_right=0;
static int Tableindex1=0,Tableindex2=0;
static int const Lookuptable1[8]={0x03,0x02,0x06,0x04,0x0c,0x08,0x09,0x01};
static int const Lookuptable2[8]={0x03,0x02,0x06,0x04,0x0c,0x08,0x09,0x01};
int left_speed=-1;
int right_speed=-1;
struct pin{
int sensor:2;
int unused:4;
boolean unused1;
boolean unused2;
int portA:4;//A for left motor
int portB:4;//B for right motor
};
struct pin pin_map;
struct pin pin_dir;
#byte pin_map=0x05
#byte pin_dir=0x85
#int_RTCC
void Timer0_isr()
{
if(counter_left<left_speed&&counter_right<right_speed)
{
counter_left++;
counter_right++;
}
else if (counter_left==left_speed&&counter_right<right_speed)
{
Tableindex1=(Tableindex1+1)%8;
pin_map.portA=Lookuptable1[Tableindex1];
counter_left=0;
counter_right++;
}
else if(counter_left<left_speed&&counter_right==right_speed)
{
Tableindex2=(Tableindex2+1)%8;
pin_map.portB=Lookuptable2[Tableindex2];
counter_right=0;
counter_left++;
}
else if(counter_left==left_speed&&counter_right==right_speed)
{
Tableindex1=(Tableindex1+1)%8;
pin_map.portA=Lookuptable1[Tableindex1];
counter_left=0;
Tableindex2=(Tableindex2+1)%8;
pin_map.portB=Lookuptable2[Tableindex2];
counter_right=0;
}
else
{
counter_left++;
counter_right++;
}
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
pin_dir.sensor=0b11;
pin_dir.portA=0x0;
pin_dir.portB=0x0;
while(1)
{
switch(pin_map.sensor)
{
case 0b00:
left_speed=30;
right_speed=30;
break;
case 0b10:
left_speed=3;
right_speed=1;
break;
case 0b01:
left_speed=1;
right_speed=3;
break;
case 0b11:
left_speed=3;
right_speed=3;
break;
default:
left_speed=30;
right_speed=30;
break;
}
// delay_ms(1);
}
}
这个是我边写的 代码,测试过基本上好使, 但是每次改开关的时候(例如从11 改为10,状态有时候会停住,用proteus仿真也是如此,不知道为什么会停住啊,只是有时候,有时候又好使)是不是跳变的问题啊~~还是什么问题, 求大神解答啊~~~~ |