[mw_shl_code=c,true]int divider[6] = {0, 1, 8, 64, 256, 1024};
int prescaler = 5;
double count = 0;
ISR(TIMER1_OVF_vect)
{
if (prescaler < 4)
{
prescaler++;
}
}
void sei()
{
count = TCNT1;
TCNT1 = 0x000;
TCCR1B = prescaler;
attachInterrupt(0, sei, FALLING);
}
void setup()
{
Serial.begin(115200);
TIMSK1 = 0x01;
TCCR1A = 0x00; attachInterrupt(0,sei, RISING);
}
void loop()
{
static int s = -1;
static int pin = 13;
static int setpoint;
static double kp=6.2, ki=4.7;
int val = 0;
int agv = 0;
int dgv = 0;
double rad = 16000000.0*60/ 334 / divider[prescaler] / count;
unsigned long lastTime;
double errSum, lastErr;
double Output;
int out;
if (Serial.available() >0) {
val = Serial.read();
switch (s)
{
case -1:
if (val>47 && val<57)
{
s=10*(val-48);
}
else
{
s=-1;
}
break;
case 0:
if (val>98 && val<167)
{
pin=val-97;
s=1;
}
else
{
s=-1;
}
break;
case 1:
if (val>47 && val<50)
{
if (val==48)
{
if((0<=pin)&&(pin<=7))
DDRD&=~(1<<pin);
if((8<=pin)&&(pin<=13))
DDRB&=~(1<<(pin-8));
}
else
{
if((0<=pin)&&(pin<=7))DDRD|=(1<<pin);
if((8<=pin)&&(pin<=13))
DDRB|=(1<<(pin-8));
}
}
s=-1;
break;
case 10:
if (val>98 && val<167) {
pin=val-97;
if((0<=pin)&&(pin<=7))
{
if(PIND&(1<<pin)) dgv=1;
else dgv=0;
}
if((8<=pin)&&(pin<=13))
{
int pin1;
pin1=pin-8;
if(PINB&(1<<pin1)) dgv=1;
else dgv=0;
}
Serial.println(dgv);
}
s=-1;
break;
case 20:
if (val>98 && val<167)
{
pin=val-97;
s=21;
}
else
{
s=-1;
}
break;
case 21:
if (val>47 && val<50)
{
dgv=val-48;
if((0<=pin)&&(pin<=7))
{
if(pin==6) TCCR0A&=~(1<<COM0A1);
if(pin==5) TCCR0A&=~(1<<COM0B1);
if(pin==3) TCCR2A&=~(1<<COM2B1);
DDRD|=(1<<pin);
switch(dgv)
{
case 1ORTD|=(1<<pin);
break;
case 0ORTD&=~(1<<pin);
break;
}
}
if((8<=pin)&&(pin<=13))
{
if(pin==11) TCCR2A&=~(1<<COM2A1);
if(pin==9) TCCR1A&=~(1<<COM1A1);
if(pin==10) TCCR1A&=~(1<<COM1B1);
int pin1;
pin1=pin-8;
switch(dgv)
{
case 1ORTB|=(1<<pin1);
break;
case 0ORTB&=~(1<<pin1);
break;
}
}
}
s=-1;
break;
case 30:
if (val>96 && val<113)
{
unsigned int low,high;
pin=val-97;
ADMUX = (1<< 6) | ((pin-1) & 0x07);
ADCSRA|=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|
(1<<ADPS1)|(1<<ADPS0);
while(ADSC==0);
low = ADCL;
high = ADCH;
agv= (high << 8) | low;
Serial.println(agv);
}
s=-1;
break;
case 40:
if (val>98 && val<167)
{
pin=val-97;
s=41;
}
else
{
s=-1;
}
break;
case 41:
{
setpoint = val;
if(pin==6)
{
DDRD|=(1<<pin);
PORTD&=~(1<<pin);
TCCR0A|=(1<<COM0A1)|(1<<WGM00);
TCCR0B|=(1<<CS00);
OCR0A=val;
}
if(pin==5)
{
DDRD|=(1<<pin);
PORTD&=~(1<<pin);
TCCR0A|=(1<<COM0B1)|(1<<WGM00);
TCCR0B|=(1<<CS00);
OCR0B=val;
}
if(pin==3)
{
DDRD|=(1<<pin);
PORTD&=~(1<<pin);
TCCR2A|=(1<<COM2B1)|(1<<WGM00);
TCCR2B|=(1<<CS00);
OCR2B=val;
}
if(pin==11)
{
DDRB|=(1<<3);
PORTB&=~(1<<3);
TCCR2A|=(1<<COM2A1)|(1<<WGM00);
TCCR2B|=(1<<CS00);
OCR2A=val;
}
if(pin==9)
{
DDRB|=(1<<1);
PORTD&=~(1<<1);
TCCR1A|=(1<<COM1A1)|(1<<WGM10);
TCCR1B|=(1<<CS10);
OCR1A=val;
}
if(pin==10)
{
DDRB|=(1<<2);
PORTB&=~(1<<2);
TCCR1A|=(1<<COM1B1)|(1<<WGM10);
TCCR0B|=(1<<CS10);
OCR1B=val;
}
s=-1;
break;
}
case 50:
Serial.println(16000000.0*60/ 334 / divider[prescaler] / count);
if (prescaler > 1)
{
prescaler--;
}
s=-1;
break;
case 60:
if (val==60)
{
Serial.println(3);
}
s=-1;
break;
case 70:
kp=val-97;
s=-1;
break;
case 80:
ki=val-97;
s=-1;
break;
default:
s=-1;
}
}
{
unsigned long now = millis();
double timeChange = (now - lastTime)/1000;
float sampletime = 0.1;
double set = setpoint * 335 / 51;
if( timeChange>= sampletime)
{
double error = set - rad;
errSum +=( ki*error* timeChange);
Output = ( kp * error + errSum ) * 51 / 335;
out = int(Output);
DDRD|=(1<<3);
PORTD&=~(1<<3);
TCCR2A|=(1<<COM2B1)|(1<<WGM00);
TCCR2B|=(1<<CS00);
OCR2B=out;
lastErr = error;
lastTime = now;
}
}
}[/mw_shl_code]
代码如上,求大神指点 |