-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutines.c
71 lines (61 loc) · 1.54 KB
/
routines.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* routines.c
*
* Created on: 19. 12. 2018
* Author: DESKTOP
*/
#include "routines.h"
#include <util/delay.h>
#include <avr/io.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/interrupt.h>
#define USART_BAUDRATE 9600
#define UBRR_VALUE ((F_CPU / (USART_BAUDRATE * 16UL)) - 1)
// Read the 8 most significant bits
// of the AD conversion result
uint16_t ReadADC(uint8_t adc_input) {//read voltages in 0,01V
ADMUX = adc_input | (1 << REFS0);
// Delay needed for the stabilization of the ADC input voltage
_delay_us(100);//default was 10us
// Start the AD conversion
ADCSRA |= (1 << ADSC); // Start conversion
// Wait for the AD conversion to complete
while (ADCSRA & (1 << ADSC))
;
ADCSRA |= 0x10;
return ADCW;
}
void USARTInit(void)
{
UBRR0H = (uint8_t)(UBRR_VALUE>>8); // shift the register right by 8 bits
UBRR0L = (uint8_t)UBRR_VALUE; // set baud rate
UCSR0B|= (1<<TXEN0)|(1<<RXEN0); // enable receiver and transmitter
//UCSR0C|= (1<<UCSZ00)|(1<<UCSZ01); // 8bit data format
}
/*
* Send character c down the UART Tx, wait until tx holding register
* is empty.
*/
int
uart_putchar(char c, FILE *stream)
{
if (c == '\a')
{
fputs("*ring*\n", stderr);
return 0;
}
if (c == '\n')
uart_putchar('\r', stream);
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
void USART_Transmit( uint8_t data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}