#include <90s8515.h>
#include //for debugging using printf, etc
#include
#include
//timeout values for each task
#define t1 1
#define t2 2500
#define t6 15
#define t3 700
#define t4 100
#define t5 20
#define t7 5000
#define kgC 1770
#define koC 29
#define kgL 1590
#define koL 69
#define kgR 1590
#define koR 69
//PWM speed options:
#define full 0x3ff
#define speed2 0x380;
#define speed3 0x1ff;
#define speed4 0xff;
#define speed0 0x00;
//the subroutines
void sensor(void); //start a new conversion
void initialize(void); //all the usual mcu stuff
void centralcontrol(void);
unsigned char reload; //timer 0 reload to set 1 mSec
int time1,time2,time3,time4,time5,time6,time7; //task scheduling timeout counter
unsigned char count,flag,Cdis,Rdis,Ldis,index,indexR,indexL,Ct[8],Lt[8],Rt[8],sensorstart;
float Ccm,Lcm,Rcm;
char C[6],L[6],R[6];
char Crange, Lrange, Rrange;
void initialize(void)
{
//serial setop for debugging using printf, etc.
UCR = 0x10 + 0x08 ;
UBRR = 25 ;
DDRB = 0xff;
PORTB = 0xff;
DDRC.0=1; //output on PA0 and input on PA1
PORTC.0=1; //PA0 = 1;
DDRC.1=0;
PORTC.1=0;
DDRC.2=0;
PORTC.2=0;
DDRC.3=0;
PORTC.3=0;
//Now I use portA for the direction control
DDRA=0xff; //output
PORTA=0xff; //turn on pull-up resistors
DDRD = 0xff;
PORTD = 0xff;
reload = 206;
TCNT0= reload;
TCCR0 = 2;
TIMSK = 2;
/********************/
//set up timer 1
//TIMSK=TIMSK | 0x40; //turn on timer 1 compare match interrupt
TCCR1B = 0; //disable timer 1 until song starts
TCNT1 = 0; //and zero the timer
//PWM Stuffs:
TCCR1A = TCCR1A | 0x03;
TCCR1A = TCCR1A | 0x80;
TCCR1A = TCCR1A | 0x20;
TCCR1B = 0;
TCCR1B = TCCR1B | 0x04;
/*************************/
time1 = t1;
time2 = t2;
time3 = t3;
time4 = t4;
time5 = t5;
time6 = t6;
time7 = t7;
flag = 0;
count = 0;
Cdis = 0;
for(index=0;index<8;index++){
Ct[index]=0;
Lt[index]=0;
Rt[index]=0;
}
index = 0;
indexL=0;
indexR=0;
sensorstart=1;
/****Initialisations for the centrolcontrol**/
Crange = 3;
Lrange = 3;
Rrange = 3;
PORTA.0=1;
PORTA.1=1;
/*********************************************/
#asm
sei
#endasm
}
interrupt[TIM0_OVF] void timer0_overflow(void)
{
TCNT0=reload;
if(time3 > 0) --time3; //70ms
if(time4 > 0) --time4; //10ms
if(time5 > 0) --time5; //2ms
if(time1 > 0) --time1;
if(time7 > 0) --time7; //500ms for the centralcontrol
// if(time2 > 0) --time2;
}
void main(void)
{
initialize();
// printf("init");
// printf("vout=%x",PINA.1);
while(1) {
if((time4==0)&&(flag==0)&&(sensorstart==1)) {sensor(); }
if((time3==0)&&(flag==1)) {flag=2;}
if((time1==0)&&(flag==2)) {sensor();}
if((time5==0)&&(flag==3)) {sensor();}
if (time7==0) {centralcontrol();}
// if(time2==0) {time2=t2; PORTB=~PORTB;}
//if(time1==0) {time1=t1;printf("PINC.0=%d flag=%d\r\n",PINC.0,flag);}
}
}
void sensor(void)
{
time1=t1;
if((flag==2)) {
// printf("count=%d",count);
// printf("PINC.0=%d flag=%d\r\n",PINC.0,flag);
count++;
PORTC.0 = !PORTC.0;
if((count>2)&&(PORTC.0==1))
{ //printf("PINA.1=%x",PINA.1);
Ct[index++]=PINC.1;
Lt[indexL++] = PINC.2;
Rt[indexR++] = PINC.3;
}
}
if(count==17) {//printf("turnoff,count=9");
flag=3;time5=t5;count=0;index=0;indexL=0,indexR=0;PORTC.0=1;
//printf("PINC.0OFF=%d",PINC.0);
}
if((flag==0)&&(sensorstart=1))
{
//printf("turnon");
PORTC.0 = 0; //turn on sensor for 70ms
//printf("FLAGPINC.0=%d\r\n",PINC.0);
time3=t3;
time4=t4;
flag=1;
}
if(flag==3)
{ //PINC.0 = 0;
Cdis = (Ct[0]*128)+(Ct[1]*64)+(Ct[2]*32)+(Ct[3]*16)+(Ct[4]*8)+(Ct[5]*4)+(Ct[6]*2)+(Ct[7]*1);
Ldis = (Lt[0]*128)+(Lt[1]*64)+(Lt[2]*32)+(Lt[3]*16)+(Lt[4]*8)+(Lt[5]*4)+(Lt[6]*2)+(Lt[7]*1);
Rdis = (Rt[0]*128)+(Rt[1]*64)+(Rt[2]*32)+(Rt[3]*16)+(Rt[4]*8)+(Rt[5]*4)+(Rt[6]*2)+(Rt[7]*1);
// for(index=0;index<8;index++){
// printf("%x",Ct[index]);
// }
// printf("\r\n%d\r\n",Cdis);
// for(index=0;index<8;index++){
// Ct[index]=0;
// }
// printf("\r\n%d\r\n",Ldis);
// for(indexL=0;indexL<8;indexL++){
// Lt[indexL]=0;
// }
// printf("\r\n%d\r\n",Ldis);
// for(indexL=0;indexL<8;indexL++){
// Lt[indexL]=0;
// }
Ccm = kgC/(Cdis-koC);
Lcm = kgL/(Ldis-koL);
Rcm = kgR/(Rdis-koR);
ftoa(Ccm,3,C);
printf("C=%s ",C);
ftoa(Lcm,3,L);
printf("L=%s ",L);
ftoa(Rcm,3,R);
printf("R=%s\r\n",R);
index = 0;
indexL = 0;
indexR = 0;
time3=t3;
time4=t4;
flag = 0;
count = 0;
sensorstart=1;
}
}
//*************Central Control Block**********/
void centralcontrol(void)
{
time7 = t7;
printf ("c\r\n");
//define ranges:
//1:close: 10 - 20 cm
//2:far: 20 - 30 cm
//3:infinite: > 30cm
if ((Ccm >= 7) && (Ccm < 20)) Crange = 1;
else if ((Ccm >= 20) && (Ccm < 30)) Crange = 2;
else if ((Ccm >=30) || (Ccm<7)) Crange = 3;
if ((Lcm >= 7) && (Lcm < 20)) Lrange = 1;
else if ((Lcm >= 20) && (Lcm < 30)) Lrange = 2;
else if ((Lcm >= 30) || (Lcm<7)) Lrange = 3;
if ((Rcm >= 7) && (Rcm < 20)) Rrange = 1;
else if ((Rcm >= 20) && (Rcm < 30)) Rrange = 2;
else if ((Rcm >=30) || (Rcm<7)) Rrange = 3;
//Rrange=1;
//Lrange=1;
//ftoa(Ccm,3,C);
//printf("C=%s ",C);
//ftoa(Lcm,3,L);
//printf("L=%s ",L);
//ftoa(Rcm,3,R);
//printf("R=%s\r\n",R);
//***********THE CASES***********//
if ((Crange==3) && (Lrange==3) && (Rrange==3))
{//straight//
OCR1A = full; PORTA.0=1; //Have to ask Dave which Port I can use
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==3) && (Lrange==2) && (Rrange==3))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==3) && (Lrange==1) && (Rrange==3))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==2) && (Lrange==3) && (Rrange==3))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==2) && (Lrange==2) && (Rrange==3))
{//turn R//
OCR1A = full; PORTA.0=1;
OCR1B = speed3; PORTA.1=1;
printf ("R3\r\n");
}
else if ((Crange==2) && (Lrange==1) && (Rrange==3))
{//turn R//
OCR1A = full; PORTA.0=1;
OCR1B = speed2; PORTA.1=1;
printf ("R2\r\n");
}
else if ((Crange==1) && (Lrange==3) && (Rrange==3))
{//turn around//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=0;
printf ("turn\r\n");
}
else if ((Crange==1) && (Lrange==2) && (Rrange==3))
{//turn R//
OCR1A = full; PORTA.0=1;
OCR1B = speed0; PORTA.1=1;
printf ("R0\r\n");
}
else if ((Crange==1) && (Lrange==1) && (Rrange==3))
{//turn R//
OCR1A = full; PORTA.0=1;
OCR1B = speed0; PORTA.1=1;
printf ("R0\r\n");
}
/**************************/
else if ((Crange==3) && (Lrange==3) && (Rrange==2))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==3) && (Lrange==3) && (Rrange==1))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==2) && (Lrange==3) && (Rrange==3))
{//straight//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("str\r\n");
}
else if ((Crange==2) && (Lrange==3) && (Rrange==2))
{//turn L//
OCR1A = speed3; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("L3\r\n");
}
else if ((Crange==2) && (Lrange==3) && (Rrange==1))
{//turn L//
OCR1A = speed2; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("L2\r\n");
}
else if ((Crange==1) && (Lrange==3) && (Rrange==2))
{//turn L//
OCR1A = speed0; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("L0\r\n");
}
else if ((Crange==1) && (Lrange==3) && (Rrange==1))
{//turn L//
OCR1A = speed0; PORTA.0=1;
OCR1B = full; PORTA.1=1;
printf ("L0\r\n");
}
//What if it is not one of the case?//
else if ((Crange!=3) && (Lrange!=3) && (Rrange!=3))
{//turn around//
OCR1A = full; PORTA.0=1;
OCR1B = full; PORTA.1=0;
printf ("?\r\n");
}
}