Created
June 18, 2017 13:22
-
-
Save kichMan/f4cbed2054c429face091f05f1a884a5 to your computer and use it in GitHub Desktop.
Attiny13A UART TX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* uart_const.h | |
* | |
* Created: 18.06.2017 14:45:02 | |
* Author: kich | |
*/ | |
#ifndef UART_CONST_H_ | |
#define UART_CONST_H_ | |
//USES | |
#ifndef F_CPU | |
#define F_CPU 9600000UL | |
#endif | |
#define UART_BAUDRATE 9600 | |
#define TxD PB4 | |
//\USES | |
//магическая константа из учета делителя на 8, в данном случае делитель отключен | |
#define BAUD_C (((F_CPU / (UART_BAUDRATE * 8UL))) - 1) //baud = 123 | |
#define T_START TCCR0B = (1 << CS01) // F_CPU/8 | |
#define T_STOP TCCR0B = 0 | |
#define T_RESET TCNT0 = 0 | |
#endif /* UART_CONST_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* uart_tx.c | |
* | |
* Created: 18.06.2017 14:16:04 | |
* Author: kich | |
*/ | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include "uart_const.h" | |
uint8_t temp, count, start; | |
volatile uint8_t c; | |
ISR(TIM0_COMPA_vect){ | |
OCR0A = BAUD_C; | |
c = 1; | |
T_RESET; | |
} | |
void uart_init () { | |
DDRB |= (1 << TxD); | |
TIMSK0 = (1 << OCIE0A); | |
sei(); | |
} | |
void send(uint8_t data) { | |
if (count >= 8) { | |
PORTB |= (1<<TxD); | |
start = 0; temp = 0; c = 0; count = 0; | |
T_STOP; | |
OCR0A = 0; | |
return; | |
} | |
if(c == 1) { | |
if (start == 0) { | |
temp = 0x80; | |
start = 1; | |
count--; | |
} else { | |
temp = data; | |
temp = temp >> count; | |
temp = temp << 7; | |
} | |
switch(temp) { | |
case 0x80 : PORTB &= ~(1 << TxD); break; | |
case 0 : PORTB |= (1 << TxD); break; | |
} | |
count++; | |
c = 0; | |
} | |
} | |
void uart_ch(uint8_t data){ | |
uint8_t f; | |
data = ~data; | |
T_START; | |
for(f = 0; f < 10; f++){ | |
while(c == 0); | |
send(data); | |
} | |
} | |
void uart_str(char *text){ | |
while(*text) { | |
uart_ch(*text++); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* uart_tx.h | |
* | |
* Created: 18.06.2017 14:15:53 | |
* Author: kich | |
* @title Реализация UART на ATtiny13a, только с отправкой (TX) | |
* @todo Слишком жирно получилось, нужно накатить на asm avr | |
*/ | |
#ifndef UART_TX_H_ | |
#define UART_TX_H_ | |
extern void uart_init(); | |
extern void uart_ch(uint8_t data); | |
extern void uart_str(char *text); | |
#endif /* UART_TX_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@todo Слишком жирно получилось, нужно накатить на asm avr