Skip to content

Instantly share code, notes, and snippets.

@kichMan
Created June 18, 2017 13:22
Show Gist options
  • Save kichMan/f4cbed2054c429face091f05f1a884a5 to your computer and use it in GitHub Desktop.
Save kichMan/f4cbed2054c429face091f05f1a884a5 to your computer and use it in GitHub Desktop.
Attiny13A UART TX
/*
* 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_ */
/*
* 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++);
}
}
/*
* 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_ */
@kichMan
Copy link
Author

kichMan commented Jun 18, 2017

@todo Слишком жирно получилось, нужно накатить на asm avr

@kichMan
Copy link
Author

kichMan commented Jul 6, 2017

Написал тут, почти в 70 байт уложился.
Надо попробовать в инлайновый асм перевести

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment