//Distributed under the GPL v 2.0 //by fenn 03/10/07 //industrial servo control with PID algorithm demo //this code was written for an attiny26 and is still buggy #include #include #include #define SETBIT(PORT, BIT) PORT |= (1< bound ) { n = bound; } else if(n < -bound ) { n = -bound; } return n; } //for a delay of 1 second, n = about 23000 void delay(unsigned long int n) { unsigned long int a; for(a=0;a to reverse pwm polarity/direction if(n<0){ OCR1A=(uint8_t) -n; OCR1B=0; } else { OCR1A=0; OCR1B=(uint8_t) n; } } int main(void) { //set port i/o direction DDRB= (0<>8; //Ko pid_total = boundscheck(pid_total, 0xFF); /*//set max pwm value at ~95% of 2^8 (change this to whatever you like) if ( temp_pid_sum > 0xFC ) pid_total = 0xFC; else if ( temp_pid_sum < 0 ) pid_total = 0; else pid_total = temp_pid_sum; */ pwm(pid_total); /* end PID section */ //PORTB=~PORTB; old_pos = enc0_pos; delay(100); delta = enc0_pos - old_pos; // PORTA=(enc0_pos<<4 | (PORTA & 0x0F)); //show position in binary on PA4-PA7 PORTA=((uint8_t)enc0_pos<<4 | (PORTA & 0x0F)); //show setpoint in binary on PA4-PA7 PORTA ^=_BV(LED); //toggle the led } }