Outils pour utilisateurs

Outils du site


robotics:computing:communication_serie_atmega

Ceci est une ancienne révision du document !


Introduction

Nous allons faire communiquer deux atmega de chez ATMEL via la communication série

Protocole

Transmission synchrone

Dans une transmission synchrone, on transmet l'information et l'horloge qui synchronise la transmission. Ce mode de transmission est plus rapide car il n'est pas nécessaire d'ajouter des bits de contrôle à l'information. Cependant, il est plus couteux car il faut réserver 2 lignes (information + horloge)

Transmission asynchrone

Dans une transmission asynchrone, on ne transmet pas d'horloge au recepteur. Cela implique que l'emetteur et le recepteur doivent avoir la meme configuration (vitesse de transmission et longueur de l'information). Il est également nécessaire d'ajouter des bits de contrôle à l'information. Ce mode est plus lent (car la taille de l'information est réduite) mais plus efficace pour les longues distances. BIT START - 5 à 9 BIT INFORMATION - BIT PARITE - 1 ou 2 BIT STOP

/* * serial.c * * Created on: 13 avr. 2010 * Author: ldo */ /* ATMEGA48 @20MHz * LOW FUSE : F7 * CKDIV8=1 no divided clock by 8 * CKOUT=1 no clock output on PORTB * SUT1..0=11 slowly rising power * CKSEL3..0=0111 full swing crystal oscillator */ #include<avr/io.h> #include<avr/interrupt.h> volatile unsigned char receiveChar; receiver interrupt ISR(USART_RX_vect) { unsigned char dustbin; If frame error or parity error or data overRun if 1) || (UCSR0A & (1 « UPE0)) || (UCSR0A & (1 « DOR0))) { dustbin = UDR0; } no error else { receiveChar = UDR0; } } void portInitialise(void) { /* configuration des ports : '1' pour sortie */ DDRB = (1 « PORTB0); /* PB0 en sortie */ PORTB = 0x00; /* PORTB a 0 */ } void usartInitialise(void) { set baud rate : 9600 bps UBRR0H = 0; UBRR0L = 129;103; @16MHz enable receiver and transmiter and RX complete interrupt UCSR0B = (1 « RXCIE0) | (1 « RXEN0) | (1 « TXEN0); set frame format : asynchronous mode 8 data, 1 stop bit, even parity UCSR0C = (0 « UMSEL01) | (0 « UMSEL00) | (1 « UPM01) | (0 « UPM00) | (0 « USBS0) | (1 « UCSZ01) | (1 « UCSZ00); } transmit a char void usartTransmit(unsigned char data) { wait for empty transmit buffer while (!(UCSR0A & (1 « UDRE0))) ; put data info buffer, sends the data UDR0 = data; } receive a char unsigned char usartReceive(void) { wait for data to be received while (!(UCSR0A & (1 « RXC0))) ; get and return received data from buffer return UDR0; } int main(void) { portInitialise(); usartInitialise(); SREG = (1 « SREG_I); /* The Global Interrupt Enable bit must be set for the interrupts to be enabled */ while (1) { usartTransmit(22); if (receiveChar == 55) { PORTB = 0xFF; } } } int main(void) { portInitialise(); usartInitialise(); SREG = (1 « SREG_I); /* The Global Interrupt Enable bit must be set for the interrupts to be enabled */ while (1) { usartTransmit(22); receiveChar = usartReceive(); if (receiveChar == 55) { PORTB = 0xFF; } } }

1) UCSR0A & (1 « FE0
robotics/computing/communication_serie_atmega.1271252835.txt.gz · Dernière modification: 2012/09/20 10:52 (modification externe)