Created
June 9, 2016 23:12
-
-
Save anonymous/22741ce93b86362f9a44cc9c7a488532 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* ADC.hpp | |
* | |
* Created on: 06/06/2016 | |
* Author: puc | |
*/ | |
#include <avr/io.h> | |
enum adc_reference{ | |
AREF = 0, AVCC = 1, INTERNAL_1_1 = 3 | |
}; | |
enum adc_result_adjust{ | |
RIGHT_ADJUST = 0, LEFT_ADJUST = 1 | |
}; | |
void enable(bool b){ | |
if(b){ | |
ADCSRA |= (1 << 7); | |
}else{ | |
ADCSRA &= ~(1 << 7); | |
} | |
} | |
void enable_interrupt_adc(){ | |
SREG |= (1 << 7); | |
ADCSRA |= (1 << 3); | |
} | |
void start(){ | |
ADCSRA |= ~(1 << 4); | |
ADCSRA |= (1 << 6) | (1 << 5); | |
} | |
void select_reference(adc_reference ref){ | |
if(ref == AREF){ | |
ADMUX &= ~(3<<6); | |
} | |
else if(ref == AVCC){ | |
ADMUX &= ~(1<<7); | |
ADMUX |= (1<<6); | |
} | |
else if(ref == INTERNAL_1_1){ | |
ADMUX |= (3<<6); | |
} | |
} | |
void set_result_adjust(adc_result_adjust adj){ | |
if(adj == RIGHT_ADJUST){ | |
ADMUX &= ~(1<<5); | |
} | |
else if(adj == LEFT_ADJUST){ | |
ADMUX |= (1<<5); | |
} | |
} | |
bool isLeftAdjust(){ | |
return (ADMUX & (1 << ADLAR)); | |
} | |
void set_analog_channel(uint8_t ch){ | |
if(ch == 0){ | |
ADMUX &= 0b11110000; | |
} | |
else if(ch == 1){ | |
ADMUX &= 0xF0; | |
ADMUX |= 0b0000001; | |
} | |
else if(ch == 2){ | |
ADMUX &= 0xF0; | |
ADMUX |= 0b0000010; | |
} | |
else if(ch == 3){ | |
ADMUX &= 0xF0; | |
ADMUX |= 0b0000011; | |
} | |
else if(ch == 4){ | |
ADMUX &= 0xF0; | |
ADMUX |= 0b0001001; | |
} | |
else if(ch == 5){ | |
ADMUX &= 0xF0; | |
ADMUX |= 0b0000101; | |
} | |
} | |
void set_prescaler(){ | |
ADCSRA |= 7; | |
} | |
void enable_input(uint8_t pin){ | |
DIDR0 &= 0x00; | |
DIDR0 |= pin; | |
} | |
This file contains hidden or 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
#include"serial.hpp" | |
#include"ADC.hpp" | |
#include<avr/io.h> | |
#include<util/delay.h> | |
#include<avr/interrupt.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char op; | |
float temp; | |
void setup(void){ | |
DDRB = 0xFF; | |
/* Configurar o USART */ | |
// Configurar modo de operação: MODE_ASYNCHRONOUS | |
set_usart_mode(MODE_ASYNCHRONOUS); | |
// Configurar tamanho do caractere: CHAR_SIZE_8 | |
set_character_size(CHAR_SIZE_9); | |
// Configurar stop bit: STOP_BIT_ONE | |
set_stop_bit(STOP_BIT_TWO); | |
// Configurar baud rate: 9600 | |
set_baud_rate(9600); | |
// Habilitar RX | |
enable_rx(true); | |
// Habilitar TX | |
enable_tx(true); | |
//Tensão de referência | |
select_reference(INTERNAL_1_1); | |
//Entrada analógica ADC0; | |
set_analog_channel(0); | |
//Divisão do clock da CPU 128 | |
set_prescaler(); | |
enable_interrupt_adc(); | |
enable(true); | |
ADCSRB = 0x00; | |
//ADLAR = 0 | |
set_result_adjust(RIGHT_ADJUST); | |
enable_input(0); | |
//Modo de auto disparo ativo | |
start(); | |
} | |
void loop(void){ | |
char str_temp[6]; | |
dtostrf(temp/10, 4, 2, str_temp); | |
println(str_temp); | |
_delay_ms(1000); | |
} | |
ISR(ADC_vect){ | |
temp = ADC + (ADC*19)/256; | |
} | |
int main(){ | |
setup(); | |
while(true){ | |
loop(); | |
} | |
return 0; | |
} | |
This file contains hidden or 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
#include <avr/io.h> | |
#ifndef SERIAL_HPP_ | |
#define SERIAL_HPP_ | |
#define fosc 16000000 | |
enum usart_mode { | |
MODE_ASYNCHRONOUS = 0, MODE_SYNCHRONOUS = 1, MODE_MASTER_SPI = 3 | |
}; | |
enum usart_parity { | |
PARITY_DISABLED = 0, PARITY_EVEN = 1, PARITY_ODD = 3 | |
}; | |
enum usart_stop_bit { | |
STOP_BIT_ONE = 0, STOP_BIT_TWO = 1 | |
}; | |
enum usart_character_size{ | |
CHAR_SIZE_5 = 0, | |
CHAR_SIZE_6 = 1, | |
CHAR_SIZE_7 = 2, | |
CHAR_SIZE_8 = 3, | |
CHAR_SIZE_9 = 7 | |
}; | |
enum usart_clock_polarity { | |
CLK_POL_RISING_EDGE = 0, CLK_POL_FALLING_EDGE = 1 | |
}; | |
/* Seleciona o modo de operação */ | |
void set_usart_mode(usart_mode mode) { | |
if (mode == MODE_ASYNCHRONOUS) { | |
UCSR0C &= ~(3 << 6); | |
} else { | |
if (mode == MODE_SYNCHRONOUS) { | |
UCSR0C &= ~(1 << 6); | |
UCSR0C |= (1 << 5); | |
} else { | |
if (mode == MODE_MASTER_SPI) { | |
UCSR0C |= (3 << 6); | |
} | |
} | |
} | |
} | |
/* Configura o tamanho do dado a ser transmitido */ | |
void set_character_size(usart_character_size chs) { | |
if (chs == CHAR_SIZE_5) { | |
UCSR0C &= ~(3 << 1); | |
UCSR0B &= ~(1 << 2); | |
} else { | |
if (chs == CHAR_SIZE_6) { | |
UCSR0C &= ~(1 << 2); | |
UCSR0C |= (1 << 1); | |
UCSR0B &= ~(1 << 2); | |
} else { | |
if (chs == CHAR_SIZE_7) { | |
UCSR0B &= ~(1 << 2); | |
UCSR0C |= (1 << 2); | |
UCSR0C &= ~(1 << 1); | |
} else { | |
if (chs == CHAR_SIZE_8) { | |
UCSR0B &= ~(1 << 2); | |
UCSR0C |= (3 << 1); | |
} else { | |
if (chs == CHAR_SIZE_9) { | |
UCSR0B |= (1 << 2); | |
UCSR0C |= (3 << 1); | |
} | |
} | |
} | |
} | |
} | |
} | |
/* Configura o tamanho do stop bit */ | |
void set_stop_bit(usart_stop_bit b) { | |
if (b == STOP_BIT_TWO) { | |
UCSR0C |= (1 << 3); | |
} else { | |
UCSR0C &= ~(1 << 3); | |
} | |
} | |
/* Configura a taxa de transmissão */ | |
void set_baud_rate(uint16_t baud) { | |
uint16_t ubrr = (16000000 / (16.0 * baud)) - 1.0; | |
UBRR0H = (ubrr >> 8); //contém os quatro bits mais significativos | |
UBRR0L = ubrr; //contém os oito bits menos significativos. | |
UCSR0A &= ~(1 << U2X0); // Velocidade da transmissao U2xn | |
} | |
/* Habilita/desabilita o RX */ | |
void enable_rx(bool x) { | |
if (x){ | |
UCSR0B |= (1 << 4); | |
} | |
else{ | |
UCSR0B &= ~(1 << 4); | |
} | |
} | |
/* Habilita/desabilita o TX*/ | |
void enable_tx(bool x) { | |
if (x) { | |
UCSR0B |= (1 << 3); | |
} else { | |
UCSR0B &= ~(1 << 3); | |
} | |
} | |
/* Verifica se existe algum dado a ser lido*/ | |
inline bool isListening() { | |
return (UCSR0A & (1 << 7)); | |
} | |
/* Verifica se o buffer de transmissão está pronto para receber novos dados */ | |
inline bool isReady() { | |
return (UCSR0A & (1 << 5)); | |
} | |
/* Leitura de um dado*/ | |
uint8_t read() { | |
while (!isListening()) | |
; | |
return UDR0; | |
} | |
/* Escrita de um dado (caracter) */ | |
void write(uint8_t byte) { | |
while (!isReady()) | |
; | |
UDR0 = byte; | |
} | |
/* Escrita de uma cadeia de caracteres */ | |
void print(const char * str) { | |
int ch = 0; | |
while (str[ch] != 0) { | |
write(str[ch]); | |
++ch; | |
} | |
} | |
//Quebra a linha | |
void laneBreak() { | |
write('\n'); | |
} | |
/* Escrita de uma cadeia de caracteres com quebra de linha*/ | |
void println(const char * str) { | |
print(str); | |
laneBreak(); | |
} | |
void clear(){ | |
for(int i = 0;i < 25;i++){ | |
laneBreak(); | |
} | |
} | |
#endif /* SERIAL_HPP_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment