USART Transmit

USART Transmit
Çeşme:


C
1. https://www.youtube.com/watch?v=EN0nUflF-FU&t=3s
2. https://www.youtube.com/watch?v=3_omxGIL0kw

Assembler
https://github.com/pearsonalan/arduino-serial-asm/blob/master/serial-alpha.asm
  1. Arduino UNO
#define F_CPU 16000000UL #include <avr/io.h> #include <stdio.h> #include <util/delay.h> #include <string.h> #define BAUD 9600 void USART_setup(void); void USART_TransmitString(char str[]); void USART_TransmitChar(unsigned char data); int main(void) { USART_setup(); int i = 0; char temp[50]; while (1) { sprintf(temp, "%i %s\n", i++, "Welcome to Turkmenistan!"); USART_TransmitString(temp); } return 0; } void USART_TransmitChar(unsigned char data) { // If UDRE0 is one, the buffer is empty, and therefore ready to be written. while (!(UCSR0A & (1<<UDRE0))) {} UDR0 = data; // USART Transmit / Receive Data Buffer } void USART_TransmitString(char str[]) { for (uint8_t i = 0; i < strlen(str); i++) { USART_TransmitChar(str[i]); } } void USART_setup(void) { int speed = ((F_CPU/16/BAUD)-1); // set boud rate UBRR0H = (speed>>8); UBRR0L = speed; UCSR0B = (1<<TXEN0); // Bit 3 – TXEN0: Transmitter Enable 0 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // USART Character Size / Data Order = 8-bit }
.nolist .include "./m328Pdef.inc" .list ;***** Speed setting ***** .equ F_CPU = 16000000 .equ BAUD = 9600 .equ speed = F_CPU / 16 / BAUD - 1 .def temp = r16 .def data = r19 .org 0x0000 rjmp start .org 0x100 home: .db 'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'T', 'u', 'r', 'k', 'm', 'e', 'n', 'i', 's', 't', 'a', 'n', '!', $0D, $0A start: rcall USART_Init while: ldi ZL, low(home << 1) ldi ZH, high(home << 1) echo: lpm data, Z+ rcall USART_Transmit cpi data, $0A breq PC+2 rjmp echo rcall delay_500ms rjmp while USART_Init: ; these values are for 9600 Baud with a 16MHz clock ; Set baud rate clr temp sts UBRR0H, temp ldi temp, speed sts UBRR0L, temp ; Enable transmitter ldi temp, (1<<TXEN0) sts UCSR0B, temp ; Set frame format: Async, no parity, 8 data bits, 1 stop bit ldi temp, (1<<UCSZ01) | (1<<UCSZ00) sts UCSR0C, temp ret USART_Transmit: ; wait for empty transmit buffer lds temp, UCSR0A sbrs temp, UDRE0 rjmp USART_Transmit ; Put data (r19) into buffer, sends the data sts UDR0, data ret delay_500ms: ; the subroutine: ldi temp, 31 ; load temp with 31 OUTER_LOOP: ; outer loop label ldi r24, 0xfd ; load registers r24:r25 with 1021, our new ; init value ldi r25, 0x03 ; the loop label DELAY_LOOP: ; "add immediate to word": r24:r25 are ; incremented adiw r24, 1 ; if no overflow ("branch if not equal"), go ; back to "delay_loop" brne DELAY_LOOP dec temp ; decrement temp brne OUTER_LOOP ; and loop if outer loop not finished ret ; return from subroutine