diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-05-01 11:05:40 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-05-01 11:05:40 +0800 |
| commit | 57e9d1a5c00dc7dc5cf3149c35a8004b5f7aab7f (patch) | |
| tree | 7d45b11513f09f9f4bfb488d167a5dd30691e4a1 /batchk | |
| parent | d11b0f733b293c768f1bca6bc54ae88997d691a2 (diff) | |
| download | smart-home-57e9d1a5c00dc7dc5cf3149c35a8004b5f7aab7f.tar.gz | |
Measure vcc.
Diffstat (limited to 'batchk')
| -rw-r--r-- | batchk/Makefile | 43 | ||||
| -rw-r--r-- | batchk/main.c | 37 | ||||
| -rw-r--r-- | batchk/uart.c | 37 | ||||
| -rw-r--r-- | batchk/uart.h | 8 |
4 files changed, 125 insertions, 0 deletions
diff --git a/batchk/Makefile b/batchk/Makefile new file mode 100644 index 0000000..a735c42 --- /dev/null +++ b/batchk/Makefile @@ -0,0 +1,43 @@ +CC = avr-gcc +MCU = atmega328p +PORT = /dev/cuaU0 +TARGET = sleep + +SRC = main.c uart.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P $(PORT) +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex diff --git a/batchk/main.c b/batchk/main.c new file mode 100644 index 0000000..a925aa1 --- /dev/null +++ b/batchk/main.c @@ -0,0 +1,37 @@ +#include <stdlib.h> + +#include <avr/io.h> +#include <util/delay.h> + +#include "uart.h" + +int main(void) +{ + char s[6]; + unsigned long v; + + uart_init(); + + ADMUX |= (1 << REFS0); /* AVCC as reference */ + ADMUX |= (1 << MUX3) | (1 << MUX2) | (1 << MUX1); /* measure 1.1V VBG */ + ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS0); /* prescaler 1/32 */ + + _delay_us(500); + + for (;;) { + ADCSRA |= (1 << ADSC); + while (ADCSRA & (1 << ADSC)) + ; + v = (1100UL * 1023 / ADC); + + for (int i = 0; i < 6; i++) + s[i] = 0; + itoa(v, s, 10); + + uart_write("voltage: "); + uart_write_line(s); + _delay_ms(1500); + } + + return 0; +} diff --git a/batchk/uart.c b/batchk/uart.c new file mode 100644 index 0000000..a6d6674 --- /dev/null +++ b/batchk/uart.c @@ -0,0 +1,37 @@ +#include <avr/io.h> +#include <util/setbaud.h> + +#include "uart.h" + +void uart_init(void) +{ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= (1 << U2X0); +#else + UCSR0A &= ~(1 << U2X0); +#endif + UCSR0B = (1 << TXEN0) | (1 << RXEN0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); +} + +static inline void uart_write_char(char c) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = c; +} + +void uart_write(const char *s) +{ + for (; *s; s++) + uart_write_char(*s); +} + +void uart_write_line(const char *s) +{ + uart_write(s); + uart_write_char('\r'); + uart_write_char('\n'); +} diff --git a/batchk/uart.h b/batchk/uart.h new file mode 100644 index 0000000..a88a3c6 --- /dev/null +++ b/batchk/uart.h @@ -0,0 +1,8 @@ +#ifndef UART_H +#define UART_H + +void uart_init(void); +void uart_write(const char *s); +void uart_write_line(const char *s); + +#endif /* UART_H */ |
