Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Created: 4/23/2019 3:50:01 PM
- * Author: dspuser
- */
- #include <avr/io.h>
- #include "Serial.h"
- #include <util/delay.h>
- int main(){
- uart_init();
- uart_transmit('0');
- while(1){
- char receive = uart_receive();
- _delay_ms(300);
- uart_transmit(receive);
- }
- }
- --------
- /*
- * Serial.c
- *
- * Created: 4/23/2019 3:49:18 PM
- * Author: dspuser
- */
- #include <avr/io.h>
- #include "Serial.h"
- void uart_init(){
- UCSRB = (1 << 3) | (1 << 4);//activam 3 si 4 corespunzator pentru TXEN respectiv RXEN, adica activam receptia+transm
- UCSRC&=~(1 << URSEL);//il punem pe 0 pentru a avea acces la UBRRH
- UBRRH = _UBRR >> 8;
- UBRRL = _UBRR;
- UCSRC = (1 << 2) | (1 << 1) | (1 << 7);//Activam UCSZ0,UCSZ1, iar UCSZ2 ramane pe 0 => 8 bits per character
- PORTD|=0x2;
- }
- void uart_transmit(char character){
- // De asemenea, bitii 4 si 5 sunt pe 0 pentru ca nu vrem paritate(UPM0 si UPM1) iar bitul 3 pentru pentru a avea un bit de stop, iar bitul 2 asemenea pt mod asincron
- while((UCSRA&(1<<UDRE))==0);//asteptam pentru un transmit buffer gol
- UDR=character;//punem in UDR ce vrem sa transmitem
- }
- char uart_receive(){
- while (!(UCSRA & (1<<RXC)));//asteptam primirea datelor
- return UDR;//returnam data primita de la buffer
- }
- ---
- /*
- * Serial.h
- *
- * Created: 4/23/2019 3:49:35 PM
- * Author: dspuser
- */
- #include <avr/io.h>
- #ifndef F_CPU
- #define F_CPU 14745600UL
- #ifndef _BAUDRATE
- #define _BAUDRATE 9600
- #ifndef _UBRR
- #define _UBRR 0x5F //folosim formula din lab UBRR=(Fosc/(16*BaudRate))-1 => 95 => hex: 0x5F
- void uart_init();
- void uart_transmit(char character);
- char uart_receive();
- #endif
- #endif
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement