So im still pretty new at this, but im trying to make sure im learning to understand datasheets..and basically being able to implement them in the simplest way possible. UART was the first "communication protocol" I learned on the AVR. It seems pretty simple (at least so far, im sure im missing something). From my understanding:
UART: Hardware for sending RS232 Signals
RS232: Standard for Communicating through serial.
The UART is responsible for adjusting the voltage levels to RS232 levels correct...or can it only send TTL Serial Levels?
From my understanding TTL Serial is 0 to Vcc (so 5v or 3.3v), and we would use a MAX232 to actually adjust from a 5v level to 232v level (MAX does this through charge pumps, which im not sure how "precisely" these work...but I understand the general concept). Otherwise the MAX232 is essentially just a shift register right? Taking a 5v level, turning it into +/- ~12V level (Although I think it's inverted from my reading, IE: 0v TTL is +12V and 5v TTL is -12V?). Or does it literally just convert logic levels and thats it. (Maybe im thinking that the UART is essentially a shift register....anyways)
So anyways assuming I have the idea of serial transmission (hopefully im not super far off) I was trying to figure out the UART on the Atmega328p (along with the EM-18 RFID Reader). From my understanding from the UART Initialization I would need to do something like this:
(Assume my Clock is running at 1MHz, default chip setup)
/* Set Baud Rate */ /* Pretending I want a Baud of 9600 Formula's taken from Datasheet: F_CPU = 1000000UL I assume? UBRRN = F_CPU/((9600*16)-1) (So like around 6.51, datasheet mentions 6 for 1MHz, so I guess thats close?) Sidequestion: Why does the Datasheet also literally have a BAUD calculation? */ void init_usart0(void) { // Don't need to touch UCSR0C Since Async Operation #define UBRRN F_CPU/((9600*16)-1) //So 00000110 (6 right?) UCSR0B = (1 << RXEN0); /* Im only receiving RFID */ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00) /* 8 bits ASCII data according to EM-18 */ // 1 Stop bit is default, so no need to set USBS0 //UCPOL0 not needed since synchronous UBRR0L = UBRRN // 00000110 UBRR0H = UBRRN >> 4 //Says it needs upper most 4 bits? and the rest zero? (I feel like im wrong here somehow) /* Done Initializing */ } int main(void) { char letter; // -------- Inits --------- // init_usart0(); /* --- Event Loop --- */ while (1) { loop_until_bit_is_set(UCSR0A, RXC0); /* wait until done */ letter = UDR0; /* Pull in Byte from UART */ LCDPrint(letter_hex_or_ascii); // Pretend I have a LCD hooked up w/driver and have converted it } return 0; }
Ok it's not glorious code, but it's what i've gathered from just looking at the datasheet. Im a bit confused on the `UDR0` reading part......does `RXC0` get reset when we read from `UDR0`. Based off the datasheet that seems to be correct. Is this really all there is? (as far as reading the data). It seems pretty simple (which scares me, since im still new to a lot of this).
From the EM-18 datasheet it's sending 10 bytes (ascii characters that are 8 bits). haven't quite figured out how i'll convert them, but this is just SUPER SIMPLE basic usage of just reading a RFID chip/device. Is this accurate hopefully for a basic setup? Has anyone used the EM-18 before (I see other RFID readers/writers but they seem super complex).
I hate just using a library without understanding how it works, so hopefully im close.
Thanks all!