quarta-feira, 8 de setembro de 2010

Usando serial com atmega8 - avr studio

//************************************************************************


// usando a porta serial
// Version : 1.0
// microcontrolador : AVR ATMega8 UART
// Autor : Aguivone
// descrição : exemplo de uso da serial
//
//************************************************************************

#include <avr/io.h>
#define F_CPU 1000000UL // 1 MHz deve vir antes das interrupçoes
#include <avr/interrupt.h>
#include <stdio.h>
///////////////////////////////////////////////funçoes usadas////////////////////////////////////////////////

int uart_putchar(char c, FILE *stream)

{
if (c == '\n')
uart_putchar('\r', stream);
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}

int uart_getch(FILE *stream)
{
unsigned char ch;
while (!(UCSRA & (1<<RXC)));
ch=UDR;
return ch;
}

/* assimila I/O stream á UART */

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getch, _FDEV_SETUP_RW);

void ini_serial(int BAUD_RATE)
{
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // calcula o ubrr da comunicação
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
UCSRB = (1<<RXEN)
(1<<TXEN); //habilita tx e rx

UCSRC = (1<<URSEL)
(1<<UCSZ1)
(1<<UCSZ0);//programa para 8bit ,sem paridade,1 stop bit
sei(); // habilita interrupçoes geral
}

void Delay(unsigned int count) // conta tempo

{
while(count>0)
{
count--;
}
}



//////////////////////////////////////////função principal///////////////////////////////////

int main(void)
{
stdout = stdin = &uart_str; /* inicializa a UART */
ini_serial(4800); /* inicializa serial para 4800 bps */
DDRD = 0xFF; // configura o portd
PORTD = 0; // inicializa o portd

for(;;)
{
printf("teste de serial \n\r");
Delay(12000);
}

}

///////////////////////////////////////////fim//////////////////////////////////////////////

Nenhum comentário :

Postar um comentário

olá,digite aqui seu comentário!