Embedded-Software-Projects

Log | Files | Refs | README | LICENSE

commit 83c147177e643f671c0c1ade3a5b863eae099759
parent 063a2d796f339baab608227d908d0d9f842d3ee9
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed,  9 Oct 2024 19:47:40 +0100

Added initialization of adc0.

Diffstat:
M.gitignore | 5+++--
MProject1/.vs/Project1/v14/.atsuo | 0
MProject1/Project1/main.c | 139+++++++++++++++++++++++++++++++------------------------------------------------
3 files changed, 57 insertions(+), 87 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -52,4 +52,6 @@ Mkfile.old dkms.conf # Debug directory -**/Debug/* -\ No newline at end of file +**/Debug/* +Project1/.vs/Project1/v14/.atsuo +Project1/.vs/Project1/v14/.atsuo diff --git a/Project1/.vs/Project1/v14/.atsuo b/Project1/.vs/Project1/v14/.atsuo Binary files differ. diff --git a/Project1/Project1/main.c b/Project1/Project1/main.c @@ -5,8 +5,6 @@ * Authors: William Lindholm, Thomas Berry */ -#define BINARY_SETTINGS 1 - #include <avr/io.h> #include <avr/interrupt.h> #include <avr/cpufunc.h> @@ -23,100 +21,80 @@ struct LED_BITS LED_Array[10] = { {&PORTC, PIN5_bm}, {&PORTC, PIN4_bm}, {&PORTA, PIN0_bm}, {&PORTF, PIN5_bm}, {&PORTC, PIN6_bm}, {&PORTB, PIN2_bm}, {&PORTF, PIN4_bm}, {&PORTA, PIN1_bm}, {&PORTA, PIN2_bm}, {&PORTA, PIN3_bm} }; -void CLOCK_init (void); -void InitialiseLED_PORT_bits(void); -void Set_Clear_Ports(uint8_t set); -void TCA0_init_bits(void); +void clock_init (void); +void set_leds_output(void); +void set_clr_leds(bool set); +void configure_TCA0(void); +void configure_ADC0(void); +int main(void) +{ + clock_init(); + set_leds_output(); + set_clr_leds(0); // ensure all leds are turned off by default + configure_TCA0(); + configure_ADC0(); + + sei(); // Global interrupts enable + + for (;;){} +} -void CLOCK_init (void) +/* Set the CPU frequency to 20Mhz, default 6Mhz */ +void clock_init (void) { - /* Disable CLK_PER Prescaler */ + // Disable CLK_PER Prescaler ccp_write_io( (void *) &CLKCTRL.MCLKCTRLB , (0 << CLKCTRL_PEN_bp)); - /* If set from the fuses during device programming, the CPU will now run at 20MHz (default is /6) */ } -void InitialiseLED_PORT_bits() +/* Set all led pins to output mode */ +void set_leds_output() { - PORTC.DIR = PIN6_bm | PIN5_bm | PIN4_bm; /*(1<<6) | (1<<5) | (1<<4); 0x70;*/ /* PC4-UNO D1 (TXD1), PC5-UNO D0 (RXD1), PC6 - UNO D4 */ - PORTA.DIR = PIN3_bm | PIN2_bm | PIN1_bm | PIN0_bm; /*(1<<1) | (1<<0); 0x0f; */ /* PA1-UNO D7, PA0 - UNO D2, PA2- LED8, PA3 - LED9 */ - PORTB.DIR = PIN2_bm; /*0x04;*/ /* PB2 - UNO D5 */ - PORTF.DIR = PIN5_bm | PIN4_bm; /*(1<<5) | (1<<4); 0x30; */ /* PF5 - UNO D3, PF4 UNO D6 */ - /* Later use PIN6_bm etc */ + PORTC.DIR = PIN6_bm | PIN5_bm | PIN4_bm; + PORTA.DIR = PIN3_bm | PIN2_bm | PIN1_bm | PIN0_bm; + PORTB.DIR = PIN2_bm; + PORTF.DIR = PIN5_bm | PIN4_bm; } -/* Function to set or clear all LED port bits, 1 - set, 0 - clear */ -void Set_Clear_Ports(uint8_t set) { - - uint8_t i; +/* Enable and configure TCA0 */ +void configure_TCA0(void) +{ + TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm; //Counter overflow interrupt option + TCA0.SINGLE.CTRLB = 0x00; // Normal Mode selected - TOP value in PER register + TCA0.SINGLE.EVCTRL = 0x00; // TCA0 can count events from the EVENT module - disable this option - for (i = 0; i <= 9; i += 1) - { - if (set) - LED_Array[i].LED_PORT->OUTSET = LED_Array[i].bit_mapping; - else - LED_Array[i].LED_PORT->OUTCLR = LED_Array[i].bit_mapping; - } + TCA0.SINGLE.PER = 9766; // The TCA0 counter will overflow when it reaches the PER value + TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1024_gc | TCA_SINGLE_ENABLE_bm; // Prescale set to /1024 and enable TCA0 (start count) } -void Toggle_Ports(void) +/* Enable and configure ADC0 */ +void configure_ADC0() { - uint8_t i; - - for (i = 0; i <= 9; i += 1) - { - LED_Array[i].LED_PORT->OUTTGL = LED_Array[i].bit_mapping; - } + ADC0.CTRLA = ADC_RESSEL_8BIT_gc | ADC_FREERUN_bm; // Set 8 bit resolution and freerun mode + ADC0.CTRLC = VREF_AC0REFSEL_AVDD_gc | ADC_PRESC_DIV128_gc; // Set reference voltage to 5V and set ADC prescaler to div 128 + ADC0.MUXPOS = ADC_MUXPOS_AIN3_gc; // set input to Analog in 3 + ADC0.CTRLA |= ADC_ENABLE_bm; // Enable ADC } -int main(void) -{ - CLOCK_init(); +/* Set or clear all LED port bits, 1 - set, 0 - clear */ +void set_clr_leds(bool set) { - /* set UNO D0-D7 to all outputs, also LED8 and LED9 */ - InitialiseLED_PORT_bits(); - - Set_Clear_Ports(0); - - TCA0_init_bits(); - - sei(); - - while (1) + uint8_t i; + for (i = 0; i <= 9; i += 1) { + if (set) + { + LED_Array[i].LED_PORT->OUTSET = LED_Array[i].bit_mapping; + } + else + { + LED_Array[i].LED_PORT->OUTCLR = LED_Array[i].bit_mapping; + } } } -void TCA0_init_bits(void) -{ - - #ifdef BINARY_SETTINGS - - TCA0.SINGLE.INTCTRL = 0b00000001; /* Counter overflow interrupt option */ - TCA0.SINGLE.CTRLB = 0b00000000; /* Normal Mode selected - TOP value in PER register */ - TCA0.SINGLE.EVCTRL = 0b00000000; /* TCA0 can count events from the EVENT module - disable this option */ - - /* Now set the PER register to 9766, our top value */ - /* This is because 20MHz/1024 gives TCA0clk period = 51.2us and 51.2us*9766 = 0.5000192s */ - - TCA0.SINGLE.PER = 9766; /* The TCA0 counter will overflow when it reaches the PER value */ - TCA0.SINGLE.CTRLA = 0b00001111; /* Prescale set to /1024 and enable TCA0 (start count) */ - - #else - - /* Same implementation using explicit bit naming: */ - TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm; //Counter overflow interrupt option - TCA0.SINGLE.CTRLB = 0x00; // Normal Mode selected - TOP value in PER register - TCA0.SINGLE.EVCTRL = 0x00; // TCA0 can count events from the EVENT module - disable this option - - - TCA0.SINGLE.PER = 9766; // The TCA0 counter will overflow when it reaches the PER value - TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1024_gc | TCA_SINGLE_ENABLE_bm; // Prescale set to /1024 and enable TCA0 (start count) - - #endif - -} +/* Interrupt for cylon animation */ ISR(TCA0_OVF_vect) { static uint8_t i = 0; // Index of the current LED @@ -136,17 +114,8 @@ ISR(TCA0_OVF_vect) } if (i >= 9 || i <= 0) { - direction = !direction; + direction = !direction; // reverse direction } - //Toggle_Ports(); - /* The interrupt flag has to be cleared manually */ - - #ifdef BINARY_SETTINGS - TCA0.SINGLE.INTFLAGS = 0b00000001; /* Writing 1 to the flag bit clears it */ - #else - /* Same implementation using explicit bit naming: */ TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm; - #endif - } \ No newline at end of file