SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * main.c
  3.  *
  4.  * Created: 4/23/2019 3:50:01 PM
  5.  *  Author: dspuser
  6.  */
  7. #include <avr/io.h>
  8. #include "Serial.h"
  9. #include <util/delay.h>
  10. int main(){
  11.     uart_init();
  12.     uart_transmit('0');
  13.     while(1){
  14.         char receive = uart_receive(); 
  15.         _delay_ms(300);
  16.         uart_transmit(receive);
  17.     }
  18. }
  19.  
  20.  
  21. --------
  22. /*
  23.  * Serial.c
  24.  *
  25.  * Created: 4/23/2019 3:49:18 PM
  26.  *  Author: dspuser
  27.  */
  28. #include <avr/io.h>
  29. #include "Serial.h"
  30. void uart_init(){
  31.     UCSRB = (1 << 3) | (1 << 4);//activam 3 si 4 corespunzator pentru TXEN respectiv RXEN, adica activam receptia+transm
  32.     UCSRC&=~(1 << URSEL);//il punem pe 0 pentru a avea acces la UBRRH
  33.     UBRRH = _UBRR >> 8;
  34.     UBRRL = _UBRR;
  35.     UCSRC = (1 << 2) | (1 << 1) | (1 << 7);//Activam UCSZ0,UCSZ1, iar UCSZ2 ramane pe 0 => 8 bits per character
  36.     PORTD|=0x2;
  37. }
  38.  
  39. void uart_transmit(char character){
  40.     // 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
  41.     while((UCSRA&(1<<UDRE))==0);//asteptam pentru un transmit buffer gol
  42.     UDR=character;//punem in UDR ce vrem sa transmitem
  43. }
  44.  
  45. char uart_receive(){
  46.     while (!(UCSRA & (1<<RXC)));//asteptam primirea datelor
  47.     return UDR;//returnam data primita de la buffer
  48.    
  49. }
  50.  
  51. ---
  52. /*
  53.  * Serial.h
  54.  *
  55.  * Created: 4/23/2019 3:49:35 PM
  56.  *  Author: dspuser
  57.  */
  58. #include <avr/io.h>
  59. #ifndef F_CPU
  60. #define F_CPU 14745600UL
  61. #ifndef _BAUDRATE
  62. #define _BAUDRATE 9600
  63. #ifndef _UBRR
  64. #define _UBRR 0x5F //folosim formula din lab UBRR=(Fosc/(16*BaudRate))-1 => 95 => hex: 0x5F
  65. void uart_init();
  66. void uart_transmit(char character);
  67. char uart_receive();
  68. #endif
  69. #endif
  70. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top