Atmega8 UART String Empfangen


Elektronik Forum
Atmega8 UART String Empfangen
Author: Flecto 02.06.2016-09:48
Sample Code für den AtMega8

Auf electronicsplanet gibt es viele nützliche Sample Codes für den ATmega8. Wie sieht es auf mit Codes für die UART?

Ich möchte gerne ein Zeichen empfangen und dann eine LED anschalten. Wie geht das?

Danke für eure Hilfe

Flecto

Re: Atmega8 UART String Empfangen
Author: Simba 02.06.2016-11:12
Hallo du

Versuch mal diesen Code:

#ifndef F_CPU
/* In the new version of the WinAVR/Mfile Makefile guideline One can defined F_CPU in the Makefile, a repeated definition here would lead to a compiler warning . therefore "prevention" through    #ifndef/#endif This "Prevention" can lead to Debugger, if AVRStudio use a another, not the hardware fitting Clock rate:  Then the    following definition doesn't use, but instead the default value (1 MHz?) of AVRStudio - hence the Output of a warning if F_CPU yet does not define:*/
#warning "F_CPU was not defined yet, now make up with 3686400"
#define F_CPU 3686400L   // Systemtakt in Hz -
#endif


#define BAUD 9600L
#define UBRR_VAL ((F_CPU+BAUD * 8)/(BAUD*16)-1)     //clever round
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))         //real baud rate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD-1000)     //Error per thousand


#if ((BAUD_ERROR>10)||(BAUD_ERROR<-10))
#error Systematic error in the baud rate more than 1% and thus too high!
#endif

#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>


void USART_Init()
{
   // Set baud rate
   UBRRH = (unsigned char)(UBRR_VAL>>8);
   UBRRL = (unsigned char)UBRR_VAL;
   // Enable Receiver and Transmitter
   UCSRB = (1<<RXEN)|(1<<TXEN);
   // Set frame format: 8data, 1stop bit
   UCSRC = (1<<URSEL) | (1<<UCSZ1)|(1<<UCSZ0);  
}

void USART_Transmit( unsigned char data )
{
   // Wait for empty transmit buffer
   while ( !( UCSRA & (1<<UDRE)) ){
   }
   // Put data into buffer, sends the data
   UDR = data;
}

void UART_Puts (char *string)
{
   while( *string != '\0' )  //as long as *string != '\0'
       USART_Transmit(*string);
       string++;
   }
}//end USART_Puts()

unsigned char USART_Receive( void )
{
   // Wait for data to be received
   while ( !(UCSRA & (1<<RXC)) );
   // Get and return received data from buffer
   return UDR;
}

int main(void){
   USART_Init();
   DDRB = 0xFF;
   PORTB = 0x00;
   unsigned char test;
   while(1){
 test = USART_Receive();
 UART_Puts("\r\n");
 //USART_Transmit(test);  

       if(test == '1'){
           PORTB = 4;
     _delay_ms(10);
       }
       else if(test == '0'){
           PORTB = 8;
     _delay_ms(10);
       }
       //PORTB = 1;
   }
}

Re: Atmega8 UART String Empfangen
Author: Simba 02.06.2016-11:13
Mit nettem Gruss

Simba

Re: Atmega8 UART String Empfangen
Author: Flecto 03.06.2016-12:44
Danke, wird ich ausprobieren!

Antwort schreiben
Name:

Betreff:

Spamschutz, Welche Farbe hat ein rotes Herz?

Beitrag:

Elektronik Forum
          
Grundlagen Spannungsteiler Widerstandsreihen Widerstandsfarbcode Farbcode Quiz Servos Standard Transistoren Steckerbelegungen Elektronik Bücher EMV Lektion 1 Elektronik Forum
Schaltungssammlung CD Laser ersetzen Servo Hacking Meine Roboter
ATmega8 ADC ATmega8 Timer0 ATmega8 Timer1 ATmega8 Ext. Int. ATmega16 Timer0 ATmega16 Timer1 ATmega16 ADC ATmega16 Ext. Int. ATmega32 Timer0 ATmega32 Timer1 ATmega32 ADC ATmega32 Ext. Int. AVR Tutorial C
Feedback Würfelaugen