Ich bekomme das UART einfach nicht zum laufen hier ist mal der Code:
Code:
/*
* SMARTINIUS.c
*
* Created: 26.07.2011 00:17:22
* Author: Martinius
*/
#define F_CPU 16000000
#define BAUD 9600UL // Baudrate
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
char s[7];
const int FWD = 1; // Definition der Richtungswerte
const int STOP = 0;
const int BWD = 2;
const int ON = 1; //andere Werte
const int OFF = 0;
const int Left = 2;
const int Right = 1;
const int Enable = 1;
const int Disable = 0;
const int Reset = 2;
const float A = 11.925;
const float BL = 0.660;
const float BR = 0.610;
const float ADCconst = 00.04883;
void Setup(void){ // Stellt den Controller für das nachfolgende Programm ein
// muss immer als erstes aufgerufen werden.
UBRRH = UBRR_VAL >> 8;
UBRRL = UBRR_VAL & 0xFF;
UCSRB |= (1<<TXEN); // UART TX einschalten
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchron 8N1
DDRC |= (1<<PC7); // Status LED
DDRB |= (1<<PB0); // IR_Eable
DDRA &= ~(1<<PA0); // IR_Left
DDRA &= ~(1<<PA1); // IR_Right
ADMUX |= (1<<REFS0);//Reverrenzspannungsquelle des ADC wird festgelegt
ADCSRA = (1<<ADPS1) | (1<<ADPS2); // Frequenzvorteiler
ADCSRA |= (1<<ADEN); // ADC aktivieren
ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten
}
void Status_LED(uint8_t Status){
if(Status == 1){
PORTC |= (1<<PC7);
}
if(Status == 0){
PORTC &= ~(1<<PC7);
}
}
int uart_putc(unsigned char c)
{
while (!(UCSRA & (1<<UDRE))) /* warten bis Senden moeglich */
{
}
UDR = c; /* sende Zeichen */
return 0;
}
void uart_puts (char *s)
{
while (*s)
{ /* so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" */
uart_putc(*s);
s++;
}
}
uint16_t get_Ir_Distance (uint8_t Dir){ // Distance in mm
if(Dir==1){//Right
float x;
ADMUX = (ADMUX & ~(0x1F)) | (1 & 0x1F);
ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten
x = ADCW;
x = x*ADCconst;
x = x-BL;
x = A/x;
x = x*10
uart_puts( itoa( x, s, 10 ) );
return x; // ADC auslesen und zurückgeben
}
if(Dir==2){//Left
float x;
ADMUX = (ADMUX & ~(0x1F)) | (0 & 0x1F);
ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion"
while (ADCSRA & (1<<ADSC) ) {} // auf Abschluss der Konvertierung warten
x = ADCW;
x = x*ADCconst;
x = x-BL;
x = A/x;
x = x*10;
uart_puts( itoa( x, s, 10 ) );
return x;
}
return 0;
}
void Ir_Enable(uint8_t Status){
if (Status == 1){
PORTB |= (1<<PB0);
}
if(Status == 0){
PORTB &= ~(1<<PB0);
}
}
int main(void)
{
Setup();
Ir_Enable(ON);
while(1){
drive_ahead();
if(get_Ir_Distance(Right)<2000){
Status_LED(ON);
}
if(get_Ir_Distance(Left)< 2000){
Status_LED(ON);
}
}
}
Lesezeichen