lol
Puoi partire da pag 170 del datasheet (http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf) e studiarti come funzionano i registri interni della seriale e la sua inizializzazione.
La velocità massima mi pare sia 2.5Mbs a 20Mhz.
Ho capito come settare alcuni registri ma non ho capito bene come settare il frame da inviare a 7 bit perchè il codice ascii da inviare è un 7 bit se non sbaglio. Grazie mille in anticipo
Questo è il codice della lib serial di Arduino
// Define config for Serial.begin(baud, config);
#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E
void HardwareSerial::begin(unsigned long baud, byte config)
{
// Try u2x mode first
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
*_ucsra = 1 << U2X0;
// hardcoded exception for 57600 for compatibility with the bootloader
// shipped with the Duemilanove and previous boards and the firmware
// on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot
// be > 4095, so switch back to non-u2x mode if the baud rate is too
// low.
if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095))
{
*_ucsra = 0;
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
// assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting;
_written = false;
//set the data bits, parity, and stop bits
#if defined(__AVR_ATmega8__)
config |= 0x80; // select UCSRC register (shared with UBRRH)
#endif
*_ucsrc = config;
sbi(*_ucsrb, RXEN0);
sbi(*_ucsrb, TXEN0);
sbi(*_ucsrb, RXCIE0);
cbi(*_ucsrb, UDRIE0);
}
L'ultima parte riguarda la configurazione, mentre nella prima ci sono i define.
Dopo questo cosa devo fare?
Questo è quello che ho saputo "scrivere" per ora e che mi funziona. Ora dovrei fare il programma vero e proprio ma come posso fare?
#define F_CPU 16000000UL //Says to the compiler which is our clock frequency, permits the delay functions to be very accurate
#define FOSC 1843200 // Clock Speed
#define BAUD 9600 //Baud rate
#define MYUBRR FOSC/16/BAUD-1
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1) //The formula that does all the required maths
#include <avr/io.h> //General definitions of the registers values
#include <util/delay.h> //This is where the delay functions are located
int main(void){
return 0;
}
void USART_Init( unsigned int ubrr)
{
UBRR0H = (unsigned char)(ubrr>>8); // Setto il baud rate
UBRR0L = (unsigned char)ubrr; // Setto il baud rate
UCSR0B = (1<<RXEN0)|(1<<TXEN0); // Abilito la trasmissione e la ricezione
UCSR0C = (1<<USBS0)|(3<<UCSZ00); // Setto il formato del frame in questo caso: 8data, 2 stop bits
}
void USART_send( unsigned char data){
while(!(UCSR0A & (1<<UDRE0))); //Aspetto che il buffer sia libero per caricarci il dato
UDR0 = data; //Carico il dato nel buffer
}
unsigned char USART_receive(void){
while(!(UCSR0A & (1<<RXC0))); //Aspetto che il receiver buffer sia pieno per poter scaricare il dato
return UDR0; //Ritorno il dato ricevuto
}
void USART_putstring(char* StringPtr){ // Questa è una funzione che mi permette di inviare una stringa
while(*StringPtr != 0x00){ //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
USART_send(*StringPtr); //Using the simple send function we send one char at a time
StringPtr++;} //We increment the pointer so we can read the next char
}
Per programmare puoi usare l'IDE di Arduino o Atmel AVR Studio.
Aggiungi un for(;;); nel main altrimenti esce subito dal programma in esecuzione.