init module06
This commit is contained in:
1
module06/ex00/Makefile
Normal file
1
module06/ex00/Makefile
Normal file
@@ -0,0 +1 @@
|
|||||||
|
include ../../Makefile
|
||||||
27
module06/ex00/adc.c
Normal file
27
module06/ex00/adc.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
// 24.2 : The ADC generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL
|
||||||
|
|
||||||
|
void adc_init(uint8_t prescaler_value) {
|
||||||
|
ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin
|
||||||
|
ADMUX |= (1 << ADLAR); // 24.9.1 : result is left adjusted, meaning the first 8 bits values are readable in ADCH, (24.9.3.2 : ADC data register is not updated util ADCH is read)
|
||||||
|
ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC
|
||||||
|
ADCSRA |= (1 << ADATE); // 24.9.2 : enable Auto Trigger -> it will start a conversion on the selected channel in ADMUX when the selected source (in ADCSRB) is triggered
|
||||||
|
ADCSRA |= (1 << ADIE); // 24.9.2 : enable ADC Interrupt
|
||||||
|
ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC
|
||||||
|
|
||||||
|
ADCSRB = ADC_TRIGGER_TIMER_1_COMPARE_B; // Table 24-6 : ADC Auto Trigger Source
|
||||||
|
ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel 0, (Table 14-6 : alternate function for RV1 on PC0 -> ADC0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void adc_print_hex(uint8_t value) {
|
||||||
|
char buffer[3] = {0};
|
||||||
|
int_to_hex_string(value, buffer, 2);
|
||||||
|
uart_printstr_endl(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
||||||
|
uint8_t value = ADCH; // 24.9.3.2 : read ADCH 8 bits precision
|
||||||
|
adc_print_hex(value);
|
||||||
|
}
|
||||||
41
module06/ex00/header.h
Normal file
41
module06/ex00/header.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef HEADER_H
|
||||||
|
#define HEADER_H
|
||||||
|
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
#include "bitmanip.h"
|
||||||
|
#include "interrupt.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include "adc.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
// GLOBAL
|
||||||
|
//
|
||||||
|
extern volatile uint8_t adc_channel;
|
||||||
|
|
||||||
|
//
|
||||||
|
// PROTOTYPES
|
||||||
|
//
|
||||||
|
// main.c
|
||||||
|
// timer.c
|
||||||
|
void timer_1B_init();
|
||||||
|
// math.c
|
||||||
|
void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits);
|
||||||
|
// adc.c
|
||||||
|
void adc_init(uint8_t prescaler_value);
|
||||||
|
uint16_t adc_read(uint8_t channel);
|
||||||
|
// uart.c
|
||||||
|
void uart_init();
|
||||||
|
void uart_tx(char c);
|
||||||
|
void uart_printstr(const char* str);
|
||||||
|
void uart_printstr_endl(const char* str);
|
||||||
|
|
||||||
|
//
|
||||||
|
// MACROS
|
||||||
|
//
|
||||||
|
|
||||||
|
#endif // HEADER_H
|
||||||
29
module06/ex00/main.c
Normal file
29
module06/ex00/main.c
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
// 1.1.7 : AVCC is the supply voltage pin for the A/D Converter, PC3:0, and ADC7:6
|
||||||
|
// 1.1.8 : AREF is the analog reference pin for the A/D Converter
|
||||||
|
// 24.4 : frequency needs to be between 50kHz and 200kHz, for less than 10bits accuracy it can be >200kHz
|
||||||
|
// -> Frequence_ADC = Frequance_CPU / Prescaler (Fadc = Fcpu/P):
|
||||||
|
// - P = 2 -> Fadc = 16,000,000 / 2 = 8,000,000 = 8MHz
|
||||||
|
// - P = 4 -> Fadc = 16,000,000 / 4 = 4,000,000 = 4MHz
|
||||||
|
// - P = 8 -> Fadc = 16,000,000 / 8 = 2,000,000 = 2MHz
|
||||||
|
// - P = 16 -> Fadc = 16,000,000 / 16 = 1,000,000 = 1MHz
|
||||||
|
// - P = 32 -> Fadc = 16,000,000 / 32 = 500,000 = 500KHz
|
||||||
|
// - P = 64 -> Fadc = 16,000,000 / 64 = 250,000 = 250KHz -> OK
|
||||||
|
// - P = 128 -> Fadc = 16,000,000 / 128 = 125,000 = 125KHz -> OK
|
||||||
|
|
||||||
|
#define ADC_PRESCALER 64 // Table 24-5 : can only be 2, 4, 8, 16, 32, 64, or 128
|
||||||
|
|
||||||
|
volatile uint8_t adc_channel = 0;
|
||||||
|
|
||||||
|
// read potentiometer value and print it in uart as hexadecimal 2-number
|
||||||
|
int main() {
|
||||||
|
char buffer[4];
|
||||||
|
SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable
|
||||||
|
uart_init();
|
||||||
|
adc_init(ADC_PRESCALER);
|
||||||
|
|
||||||
|
timer_1B_init();
|
||||||
|
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
9
module06/ex00/math.c
Normal file
9
module06/ex00/math.c
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // num_digits : number of digit of the output, ex 2 for 3FF (1023) -> FF
|
||||||
|
for (uint8_t i = 0; i < num_digits; ++i) {
|
||||||
|
uint8_t shift = (num_digits - 1 - i) * 4;
|
||||||
|
out[i] = INT_TO_HEX_CHAR((value >> shift) & 0x0F);
|
||||||
|
}
|
||||||
|
out[num_digits] = '\0';
|
||||||
|
}
|
||||||
22
module06/ex00/timer.c
Normal file
22
module06/ex00/timer.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
// Set up Timer1 in CTC mode to trigger every 20ms
|
||||||
|
// With 16MHz clock and prescaler of 64, and OCR1A = 49999:
|
||||||
|
// 16000000/64/50000 = 20ms period
|
||||||
|
#define T1B_PRESCALE_VALUE 64
|
||||||
|
void timer_1B_init() {
|
||||||
|
TCCR1A = 0; // 16.11.1 : Normal operation, OC1A/OC1B disconnected
|
||||||
|
TCCR1B = CTC_TOP_OCR1A_IN_TCCR1B; // 16.11.2 : CTC mode top OCR1A
|
||||||
|
TCCR1B |= T1_PRESCALE_SET(T1B_PRESCALE_VALUE); // 16.11.2 : prescaler
|
||||||
|
|
||||||
|
OCR1A = TIME_MS(20, T1B_PRESCALE_VALUE); // 16.11.5 : Compare match value for register A
|
||||||
|
// OCR1B = ; // 16.11.6 : Compare match value for register B, since not defined, should default to 0
|
||||||
|
|
||||||
|
TIMSK1 = (1 << OCIE1B); // 16.11.8 : Enable Timer1 Compare B Match Interrupt
|
||||||
|
|
||||||
|
// ADCSRA |= (1 << ADSC); // 24.9.2 : start first conversion
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(TIMER1_COMPB_vect) {
|
||||||
|
// Empty ISR to ensure proper flag clearing or something like that (p145 : "OCF1B is automatically cleared when the Output Compare Match B Interrupt Vector is executed")
|
||||||
|
}
|
||||||
77
module06/ex00/uart.c
Normal file
77
module06/ex00/uart.c
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
// MACROS
|
||||||
|
// USART
|
||||||
|
#define USART_BAUDRATE 115200
|
||||||
|
#define INPUT_SIZE 7
|
||||||
|
|
||||||
|
// GLOBAL VARIABLES
|
||||||
|
|
||||||
|
//
|
||||||
|
// FUNCTIONS
|
||||||
|
//
|
||||||
|
void uart_init() {
|
||||||
|
UBRR0H = (unsigned char) (BAUD_PRESCALER(USART_BAUDRATE) >> 8); // 20.11.5 : UBRRnL and UBRRnH – USART Baud Rate Registers
|
||||||
|
UBRR0L = (unsigned char) BAUD_PRESCALER(USART_BAUDRATE);
|
||||||
|
UCSR0C = DATA_EIGHT_BIT; // 20.11.4 : set Frame Format
|
||||||
|
UCSR0B = TRANSMITTER_ENABLED; // 20.11.3 : enable Receiver, Transmitter, and interrupts
|
||||||
|
}
|
||||||
|
|
||||||
|
// char uart_rx(void) {
|
||||||
|
// while (!TEST(UCSR0A, RXC0)); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data)
|
||||||
|
// return UDR0; // 20.11.1 : get data in buffer, UDRn – USART I/O Data Register (read and write)
|
||||||
|
// }
|
||||||
|
|
||||||
|
void uart_tx(char c) {
|
||||||
|
while (!TEST(UCSR0A, UDRE0)); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty)
|
||||||
|
UDR0 = (unsigned char) c; // 20.11.1 : Put data into buffer, UDRn – USART I/O Data Register (read and write)
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart_printstr(const char* str) {
|
||||||
|
while (*str) {
|
||||||
|
uart_tx(*str);
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart_printstr_endl(const char* str) {
|
||||||
|
uart_printstr(str);
|
||||||
|
uart_printstr("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ISR(USART_RX_vect) { // Table 12-6 : we select the code for USART Receive
|
||||||
|
// char received_char = UDR0; // read received character
|
||||||
|
// if (received_char == '\b' || received_char == 127) { // if backspace is received
|
||||||
|
// if (input_index <= 0) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (input_index >= INPUT_SIZE) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// remove_last_character();
|
||||||
|
// input_index--;
|
||||||
|
// uart_tx('\b'); // Move cursor back
|
||||||
|
// uart_tx(' '); // Erase the character on screen
|
||||||
|
// uart_tx('\b'); // Move cursor back again
|
||||||
|
// } else if (received_char == '\n' || received_char == '\r') { // if enter is received
|
||||||
|
// if (input_index != INPUT_SIZE) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// set_color((char *)color_input);
|
||||||
|
// reset_input();
|
||||||
|
// } else { // any other character
|
||||||
|
// if (input_index >= INPUT_SIZE) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (!is_valid_color_input(received_char)) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// fill_str(received_char);
|
||||||
|
// uart_tx(received_char);
|
||||||
|
// input_index++;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ISR(USART_TX_vect) { // Table 12-6 : we select the code for USART Transmit
|
||||||
|
// char received_char = UDR0; // read received character
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user