KR-500
28.08.2009, 08:10
Hallo liebes Forum
ich hab jetzt mal den Code für Servos aus dem RN-Wissen auf 10 Stück umgeschrieben, im Moment benutze ich einen atmega32 mit 16mhz. Später will ich den code für einen attiny2313 umschreiben. Ich wollte jetzt fragen ob es noch verbesserungs Vorschläge/Kritik gibt. Danke.
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000
#define SERVOPIN 7
#define SERVOPORT PORTD
#define DDRSERVO DDRD
#define USART_BAUD_RATE 9600
#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16L)-1)
#define Low_Servo0 PORTD&=~(1<<PD7)
#define High_Servo0 PORTD|=(1<<PD7)
#define Low_Servo1 PORTD&=~(1<<PD6)
#define High_Servo1 PORTD|=(1<<PD6)
#define Low_Servo2 PORTD&=~(1<<PD5)
#define High_Servo2 PORTD|=(1<<PD5)
#define Low_Servo3 PORTD&=~(1<<PD4)
#define High_Servo3 PORTD|=(1<<PD4)
#define Low_Servo4 PORTD&=~(1<<PD3)
#define High_Servo4 PORTD|=(1<<PD3)
#define Low_Servo5 PORTD&=~(1<<PD2)
#define High_Servo5 PORTD|=(1<<PD2)
#define Low_Servo6 PORTC&=~(1<<PC4)
#define High_Servo6 PORTC|=(1<<PC4)
#define Low_Servo7 PORTC&=~(1<<PC5)
#define High_Servo7 PORTC|=(1<<PC5)
#define Low_Servo8 PORTC&=~(1<<PC6)
#define High_Servo8 PORTC|=(1<<PC6)
#define Low_Servo9 PORTC&=~(1<<PC7)
#define High_Servo9 PORTC|=(1<<PC7)
void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<USBS)|(3<<UCSZ0) | (1<<URSEL);
}
void USART_Transmit( unsigned char data )
{
while ( !( UCSRA & (1<<UDRE)) );
UDR = data;
}
unsigned char USART_Receive( void )
{
while ( !(UCSRA & (1<<RXC)) );
return UDR;
}
volatile unsigned char servopos[10] = {100,100,100,100,100,100,100,100,100,100};
volatile static int count;
volatile unsigned char buffer=0,servonr=0,rx_count=0;
void servo_init()
{
TIMSK|=(1<<OCIE2);
TCCR2 |= (1<<WGM21) | (1<<CS20); //Prescale=1, CTC mode
OCR2 = F_CPU/100000; //alle 10µS ein IRQ
DDRD=0xFF;
DDRC=0xFF;
};
int main (void) {
servo_init();
USART_Init(USART_BAUD_SELECT);
sei();
while (1) {
//------------------------------------------------------------------------------------------------------------------------------
if(count>servopos[0])Low_Servo0;//SERVOPORT&=~(1<<SERVOPIN);
else High_Servo0;//SERVOPORT|=(1<<SERVOPIN);
if(count>servopos[1])Low_Servo1;//PORTD&=~(1<<PD6);
else High_Servo1;//PORTD|=(1<<PD6);
if(count>servopos[2])Low_Servo2;
else High_Servo2;
if(count>servopos[3])Low_Servo3;
else High_Servo3;
if(count>servopos[4])Low_Servo4;
else High_Servo4;
if(count>servopos[5])Low_Servo5;
else High_Servo5;
if(count>servopos[6])Low_Servo6;
else High_Servo6;
if(count>servopos[7])Low_Servo7;
else High_Servo7;
if(count>servopos[8])Low_Servo8;
else High_Servo8;
if(count>servopos[9])Low_Servo9;
else High_Servo9;
//------------------------------------------------------------------------------------------------------------------------------
TCCR2 |= (1<<WGM21) | (1<<CS20);
}
return 0;
}
ISR(TIMER2_COMP_vect)
{
//if(count<2000+servopos)count++;
if(count<1800)count++;
else count=0;
TCCR2 = 0x00;
};
ISR (USART_RXC_vect) {
buffer = UDR;
switch (rx_count){
case 0: if(buffer > 9){
rx_count = 0;
buffer = 0;
}
else {
servonr = buffer;
rx_count = 1;
}
break;
case 1: servopos[servonr]=buffer;
rx_count = 0;
break;
default:rx_count = 0;
break;
}
}
KR-500
ich hab jetzt mal den Code für Servos aus dem RN-Wissen auf 10 Stück umgeschrieben, im Moment benutze ich einen atmega32 mit 16mhz. Später will ich den code für einen attiny2313 umschreiben. Ich wollte jetzt fragen ob es noch verbesserungs Vorschläge/Kritik gibt. Danke.
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000
#define SERVOPIN 7
#define SERVOPORT PORTD
#define DDRSERVO DDRD
#define USART_BAUD_RATE 9600
#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16L)-1)
#define Low_Servo0 PORTD&=~(1<<PD7)
#define High_Servo0 PORTD|=(1<<PD7)
#define Low_Servo1 PORTD&=~(1<<PD6)
#define High_Servo1 PORTD|=(1<<PD6)
#define Low_Servo2 PORTD&=~(1<<PD5)
#define High_Servo2 PORTD|=(1<<PD5)
#define Low_Servo3 PORTD&=~(1<<PD4)
#define High_Servo3 PORTD|=(1<<PD4)
#define Low_Servo4 PORTD&=~(1<<PD3)
#define High_Servo4 PORTD|=(1<<PD3)
#define Low_Servo5 PORTD&=~(1<<PD2)
#define High_Servo5 PORTD|=(1<<PD2)
#define Low_Servo6 PORTC&=~(1<<PC4)
#define High_Servo6 PORTC|=(1<<PC4)
#define Low_Servo7 PORTC&=~(1<<PC5)
#define High_Servo7 PORTC|=(1<<PC5)
#define Low_Servo8 PORTC&=~(1<<PC6)
#define High_Servo8 PORTC|=(1<<PC6)
#define Low_Servo9 PORTC&=~(1<<PC7)
#define High_Servo9 PORTC|=(1<<PC7)
void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<USBS)|(3<<UCSZ0) | (1<<URSEL);
}
void USART_Transmit( unsigned char data )
{
while ( !( UCSRA & (1<<UDRE)) );
UDR = data;
}
unsigned char USART_Receive( void )
{
while ( !(UCSRA & (1<<RXC)) );
return UDR;
}
volatile unsigned char servopos[10] = {100,100,100,100,100,100,100,100,100,100};
volatile static int count;
volatile unsigned char buffer=0,servonr=0,rx_count=0;
void servo_init()
{
TIMSK|=(1<<OCIE2);
TCCR2 |= (1<<WGM21) | (1<<CS20); //Prescale=1, CTC mode
OCR2 = F_CPU/100000; //alle 10µS ein IRQ
DDRD=0xFF;
DDRC=0xFF;
};
int main (void) {
servo_init();
USART_Init(USART_BAUD_SELECT);
sei();
while (1) {
//------------------------------------------------------------------------------------------------------------------------------
if(count>servopos[0])Low_Servo0;//SERVOPORT&=~(1<<SERVOPIN);
else High_Servo0;//SERVOPORT|=(1<<SERVOPIN);
if(count>servopos[1])Low_Servo1;//PORTD&=~(1<<PD6);
else High_Servo1;//PORTD|=(1<<PD6);
if(count>servopos[2])Low_Servo2;
else High_Servo2;
if(count>servopos[3])Low_Servo3;
else High_Servo3;
if(count>servopos[4])Low_Servo4;
else High_Servo4;
if(count>servopos[5])Low_Servo5;
else High_Servo5;
if(count>servopos[6])Low_Servo6;
else High_Servo6;
if(count>servopos[7])Low_Servo7;
else High_Servo7;
if(count>servopos[8])Low_Servo8;
else High_Servo8;
if(count>servopos[9])Low_Servo9;
else High_Servo9;
//------------------------------------------------------------------------------------------------------------------------------
TCCR2 |= (1<<WGM21) | (1<<CS20);
}
return 0;
}
ISR(TIMER2_COMP_vect)
{
//if(count<2000+servopos)count++;
if(count<1800)count++;
else count=0;
TCCR2 = 0x00;
};
ISR (USART_RXC_vect) {
buffer = UDR;
switch (rx_count){
case 0: if(buffer > 9){
rx_count = 0;
buffer = 0;
}
else {
servonr = buffer;
rx_count = 1;
}
break;
case 1: servopos[servonr]=buffer;
rx_count = 0;
break;
default:rx_count = 0;
break;
}
}
KR-500