moved voltage fram main to c file

This commit is contained in:
Inamr 2024-03-12 13:37:29 +01:00
parent 18ea74bbb6
commit 4105c1ff04
4 changed files with 40 additions and 57 deletions

View File

@ -4,7 +4,8 @@
* *
* Created on March 6, 2024, 12:34 PM * Created on March 6, 2024, 12:34 PM
*/ */
#include "header.h" #include "voltage.h"
#include "uart.h"
#define RTC_PERIOD (511) #define RTC_PERIOD (511)
#define DELAY_TIME 1000 #define DELAY_TIME 1000
#include <avr/io.h> #include <avr/io.h>
@ -15,50 +16,6 @@
#define F_CPU 4E6 #define F_CPU 4E6
#include <stdio.h>
#include <stdlib.h>
#include "uart.h"
#include <util/delay.h>
void sensor_init(void) {
/* Disable digital input buffer */
}
void ADC0_init(void) {
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
/* Select ADC channel */
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
}
uint16_t ADC0_read(void) {
/* Start ADC conversion */
ADC0.COMMAND = ADC_STCONV_bm;
/* Wait until ADC conversion done */
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
;
}
/* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
bool ADC0_conversationDone(void) {
if (ADC0.INTFLAGS && ADC_RESRDY_bm) {
return true;
} else {
return false;
}
}
int main() { int main() {
sensor_init(); sensor_init();
ADC0_init(); ADC0_init();
@ -66,7 +23,7 @@ int main() {
stdout = &USART_stream; stdout = &USART_stream;
while (1) { while (1) {
adcVal = ADC0_read(); uint16_t adcVal = ADC0_read();
VREF.ADC0REF = VREF_REFSEL_VDD_gc; VREF.ADC0REF = VREF_REFSEL_VDD_gc;
printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal); printf("The values: \n%u , %u\n",VREF_REFSEL_VDD_gc , adcVal);
} }

View File

@ -5,7 +5,7 @@
displayName="Header Files" displayName="Header Files"
projectFiles="true"> projectFiles="true">
<itemPath>uart.h</itemPath> <itemPath>uart.h</itemPath>
<itemPath>header.h</itemPath> <itemPath>voltage.h</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="ExternalFiles" <logicalFolder name="ExternalFiles"
displayName="Important Files" displayName="Important Files"
@ -21,6 +21,7 @@
projectFiles="true"> projectFiles="true">
<itemPath>main.c</itemPath> <itemPath>main.c</itemPath>
<itemPath>uart.c</itemPath> <itemPath>uart.c</itemPath>
<itemPath>voltage.c</itemPath>
</logicalFolder> </logicalFolder>
</logicalFolder> </logicalFolder>
<projectmakefile>Makefile</projectmakefile> <projectmakefile>Makefile</projectmakefile>

29
prosjekt.X/voltage.c Normal file
View File

@ -0,0 +1,29 @@
#include "voltage.h"
void sensor_init(void) {
/* Disable digital input buffer */
}
void ADC0_init(void) {
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc;
VREF.ADC0REF = VREF_REFSEL_VDD_gc; /* Internal reference */
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
/* Select ADC channel */
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
}
uint16_t ADC0_read(void) {
/* Start ADC conversion */
ADC0.COMMAND = ADC_STCONV_bm;
/* Wait until ADC conversion done */
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
;
}
/* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}

View File

@ -44,19 +44,15 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
void sensor_init(void);
// TODO If C++ is being used, regular C code needs function names to have C void ADC0_init(void);
// linkage so the functions can be used by the c code. void ADC0_start(void);
uint16_t ADC0_read(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* XC_HEADER_TEMPLATE_H */ #endif /* XC_HEADER_TEMPLATE_H */
void sensor_init(void);
void ADC0_init(void);
void ADC0_start(void);
bool ADC0_conversationDone(void);
uint16_t ADC0_read(void);
uint16_t adcVal;