hier ist noch einmal ein beispiel-code aus dem "avr-forum":
Code:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#define Pin1 0
#define Pin2 1
#define Pin3 2
#define Pin4 3
#define Pin5 4
#define Pin6 5
#define Pin7 6
#define Pin8 7
#define READ 1
#define WRITE 2
#define F_CPU 8000000
#define USART_BAUD_RATE 9600
#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16l)-1)
//static unsigned char changed = 0;
//static unsigned char data = 0;
volatile unsigned char changed = 0;
volatile unsigned char data = 0;
void usart_init(int Enable, int Interupts)
{
if (Enable & READ) UCSRB = (1<<RXEN);
if (Enable & WRITE) UCSRB |= (1<<TXEN);
if (Interupts & READ) UCSRB |= (1<<RXCIE);
if (Interupts & WRITE) UCSRB |= (1<<TXCIE);
UBRRL = (unsigned char) USART_BAUD_SELECT;
}
void usart_writeChar(unsigned char c)
{
while (!(UCSRA & (1<<UDRE))) {}
UDR = c;
while(!(UCSRA & (1<<TXC))) {}
}
unsigned char usart_readChar(void)
{
while(!(UCSRA & (1<<RXC))) {}
return UDR;
}
void usart_writeString(unsigned char *string) {
while (!(UCSRA & (1<<UDRE))) {}
while ( *string)
usart_writeChar(*string++);
}
SIGNAL (SIG_UART_RECV)
{
data = UDR;
changed = 1;
usart_writeChar('[');
usart_writeChar(data);
usart_writeChar(']');
}
int main (void)
{
usart_init( (READ + WRITE) , READ);
DDRD = 0xFF;
PORTD = 0x00;
changed = 0;
usart_writeString("\nHello World \'l\'->left \'r\'->right \'s\'->stop \n");
sei();
while (1)
{
if (changed == 1)
{
data |= 0x20;
if (data == 'l')
{
PORTD = (1 << Pin6);
usart_writeString("[Motor left]\n");
}
else
{
if (data == 'r')
{
PORTD = (1 << Pin7);
usart_writeString("[Motor right]\n");
}
else
{
if (data == 's')
{
PORTD = 0x00;
usart_writeString("[Motor stop]\n");
}
else
{
usart_writeChar(data);
}
}
}
changed = 0;
}
}
}
mfg pebisoft
Lesezeichen