Compare commits

...

6 commits

Author SHA1 Message Date
Imbus
2c4a1b1565 Restructure DHT22 code, move defines to header, remove redundant includes 2025-02-05 14:16:54 +01:00
Imbus
be4b394bed Pull trigger on formatting 2025-02-05 14:14:14 +01:00
Imbus
a770213f69 Clarify that F_CPU is define as a compiler flag 2025-02-05 14:12:32 +01:00
Imbus
f43efe2079 Format target 2025-02-05 14:12:07 +01:00
Imbus
84cd6bf99e More formatting options 2025-02-05 14:08:35 +01:00
Imbus
07ed9f1dcf Formatter + gitignore 2025-02-05 13:57:38 +01:00
13 changed files with 308 additions and 298 deletions

12
.clang-format Normal file
View file

@ -0,0 +1,12 @@
BasedOnStyle: LLVM
IndentWidth: 4 # Use 4 spaces for indentation
TabWidth: 4 # Tab width is also 4 spaces
UseTab: Never # Always use spaces instead of tabs
ColumnLimit: 80 # Wrap lines after 80 characters
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
BreakConstructorInitializers: BeforeComma
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: AcrossEmptyLines
AlignConsecutiveBitFields: AcrossEmptyLines
AlignConsecutiveDeclarations: AcrossEmptyLines

2
.gitignore vendored
View file

@ -10,12 +10,14 @@
# Build directories # Build directories
build/ build/
bin/ bin/
.cache
# IDE and editor files # IDE and editor files
.vscode/ .vscode/
.idea/ .idea/
*.sublime-project *.sublime-project
*.sublime-workspace *.sublime-workspace
compile_commands.json
# Dependency directories # Dependency directories
lib/ lib/

View file

@ -1,7 +1,7 @@
#include "config.h" #include "config.h"
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include <util/delay.h>
// Pin Definitions // Pin Definitions
#define ENCODER_A_PIN PD6 #define ENCODER_A_PIN PD6
@ -20,13 +20,17 @@ void prepare_interrupt() {
// Encoder Module // Encoder Module
void encoder_init() { void encoder_init() {
// Set A, B, C, D, and DA pins as inputs // Set A, B, C, D, and DA pins as inputs
DDRD &= ~((1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) | (1 << ENCODER_C_PIN) | (1 << ENCODER_D_PIN) | (1 << ENCODER_DA_PIN)); DDRD &=
~((1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) | (1 << ENCODER_C_PIN) |
(1 << ENCODER_D_PIN) | (1 << ENCODER_DA_PIN));
// Set OE as output // Set OE as output
DDRD |= (1 << ENCODER_OE_PIN); DDRD |= (1 << ENCODER_OE_PIN);
// Enable internal pull-up resistors for A, B, C, D, and OE pins // Enable internal pull-up resistors for A, B, C, D, and OE pins
PORTD |= (1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) | (1 << ENCODER_C_PIN) | (1 << ENCODER_D_PIN) | (1 << ENCODER_DA_PIN); PORTD |= (1 << ENCODER_A_PIN) | (1 << ENCODER_B_PIN) |
(1 << ENCODER_C_PIN) | (1 << ENCODER_D_PIN) |
(1 << ENCODER_DA_PIN);
} }
uint8_t encoder_read() { uint8_t encoder_read() {
@ -46,6 +50,4 @@ uint8_t encoder_read() {
return encoder_data; return encoder_data;
} }
bool encoder_available() { bool encoder_available() { return (PIND & (1 << ENCODER_DA_PIN)); }
return (PIND & (1 << ENCODER_DA_PIN));
}

38
DHT22.c
View file

@ -1,23 +1,10 @@
#include "config.h" #include "DHT22.h"
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include <util/delay.h>
#include "GDM1602K.h"
#define DATAPIN PC0
#define PORT PORTC
#define PIN PINC
#define DDR DDRC
bool is_high() {
return (PIN & (1 << DATAPIN));
}
// Function to read data from the DHT22 sensor // Function to read data from the DHT22 sensor
int DHT22_Read(int *temperature, int *humidity) int DHT22_Read(int *temperature, int *humidity) {
{
// Set the data pin as output // Set the data pin as output
DDR |= (1 << DATAPIN); DDR |= (1 << DATAPIN);
PORT |= (1 << DATAPIN); // Set the pin high, if not already pulled up PORT |= (1 << DATAPIN); // Set the pin high, if not already pulled up
@ -33,25 +20,24 @@ int DHT22_Read(int *temperature, int *humidity)
_delay_us(20); // Wait for 40 microseconds _delay_us(20); // Wait for 40 microseconds
// Sensor should have pulled low here // Sensor should have pulled low here
if(PIN & (1 << DATAPIN)) return -2; if (PIN & (1 << DATAPIN))
return -2;
_delay_us(80); // Wait for 80 microseconds _delay_us(80); // Wait for 80 microseconds
// Sensor should have pulled high here // Sensor should have pulled high here
if(!(PIN & (1 << DATAPIN))) return -1; if (!(PIN & (1 << DATAPIN)))
return -1;
// Read data from the sensor // Read data from the sensor
uint8_t data[5] = {0, 0, 0, 0, 0}; uint8_t data[5] = {0, 0, 0, 0, 0};
uint8_t bitMask = 0x80; // All zeroes with MSB high uint8_t bitMask = 0x80; // All zeroes with MSB high
uint8_t byteIndex = 0; uint8_t byteIndex = 0;
for (byteIndex = 0; byteIndex < 5; byteIndex++) for (byteIndex = 0; byteIndex < 5; byteIndex++) {
{ for (bitMask = 0x80; bitMask != 0; bitMask >>= 1) {
for (bitMask = 0x80; bitMask != 0; bitMask >>= 1)
{
// Wait for the data bit to go low // Wait for the data bit to go low
while (!(PIN & (1 << PC0))) while (!(PIN & (1 << PC0)));
;
// Wait for the data bit to go high // Wait for the data bit to go high
_delay_us(20); _delay_us(20);
@ -59,8 +45,7 @@ int DHT22_Read(int *temperature, int *humidity)
data[byteIndex] |= bitMask; data[byteIndex] |= bitMask;
// Wait for the data bit to go low (end of bit) // Wait for the data bit to go low (end of bit)
while (PIN & (1 << PC0)) while (PIN & (1 << PC0));
;
} }
} }
@ -75,4 +60,3 @@ int DHT22_Read(int *temperature, int *humidity)
return 0; return 0;
} }

View file

@ -1,6 +1,11 @@
#ifndef DHT22_H #ifndef DHT22_H
#define DHT22_H #define DHT22_H
#define DATAPIN PC0
#define PORT PORTC
#define PIN PINC
#define DDR DDRC
int DHT22_Read(int *temperature, int *humidity); int DHT22_Read(int *temperature, int *humidity);
void DHT22_Init(); void DHT22_Init();

View file

@ -1,8 +1,8 @@
#include "config.h" #include "config.h"
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h> #include <stdbool.h>
#include <util/delay.h>
#define LCD_DATA_PORT PORTB #define LCD_DATA_PORT PORTB
#define LCD_DATA_DDR DDRB #define LCD_DATA_DDR DDRB
@ -39,7 +39,8 @@ void lcd_send(bool rs, uint8_t data) {
_delay_us(1); _delay_us(1);
// If rs (actual text data), set RS pin high // If rs (actual text data), set RS pin high
if(rs == true) LCD_DATA_PORT |= (1 << LCD_RS_PIN); if (rs == true)
LCD_DATA_PORT |= (1 << LCD_RS_PIN);
// Pulse enable // Pulse enable
_delay_ms(2); _delay_ms(2);
@ -71,16 +72,14 @@ void lcd_init() {
_delay_ms(100); _delay_ms(100);
lcd_4bit_init(); lcd_4bit_init();
//lcd_send(1, 0b01111000); // lcd_send(1, 0b01111000);
lcd_send(0, 0x01); // Clear screen lcd_send(0, 0x01); // Clear screen
lcd_send(0, 0x02); // Return home lcd_send(0, 0x02); // Return home
lcd_send(0, 0b00001100); // Display on, no cursor lcd_send(0, 0b00001100); // Display on, no cursor
lcd_send(0, 0b00101000); lcd_send(0, 0b00101000);
} }
void lcd_clear() { void lcd_clear() { lcd_send(0, 0x01); }
lcd_send(0, 0x01);
}
void test_leds() { void test_leds() {
DDRB |= 0xFF; DDRB |= 0xFF;
@ -103,19 +102,21 @@ void test_leds() {
void lcd_set_cursor(uint8_t row, uint8_t col) { void lcd_set_cursor(uint8_t row, uint8_t col) {
uint8_t position = 0x80; uint8_t position = 0x80;
if (row == 1) position += 0x40; if (row == 1)
position += 0x40;
position += col; position += col;
lcd_send(0, position); lcd_send(0, position);
} }
// Takes a string and splits it over two lines if needed. Max length is 32 characters. // Takes a string and splits it over two lines if needed. Max length is 32
void lcd_write_string(const char* string) { // characters.
void lcd_write_string(const char *string) {
uint8_t i = 0; uint8_t i = 0;
while(string[i]) { while (string[i]) {
if(i == 16) { if (i == 16) {
lcd_set_cursor(1, 0); lcd_set_cursor(1, 0);
while(string[i] == ' ') i++; // Skip leading spaces on the new line while (string[i] == ' ') i++; // Skip leading spaces on the new line
} }
lcd_send(1, string[i]); lcd_send(1, string[i]);
i++; i++;
@ -123,7 +124,7 @@ void lcd_write_string(const char* string) {
} }
// Takes two separate strings and prints them on their corresponding row // Takes two separate strings and prints them on their corresponding row
void lcd_write_strings(const char* top, const char* bottom) { void lcd_write_strings(const char *top, const char *bottom) {
uint8_t i = 0; uint8_t i = 0;
while (top[i]) { while (top[i]) {
@ -133,9 +134,8 @@ void lcd_write_strings(const char* top, const char* bottom) {
i = 0; i = 0;
lcd_set_cursor(1, 0); lcd_set_cursor(1, 0);
while(bottom[i]) { while (bottom[i]) {
lcd_send(1, bottom[i]); lcd_send(1, bottom[i]);
i++; i++;
} }
} }

View file

@ -1,8 +1,8 @@
#ifndef GDM1602K_H_ #ifndef GDM1602K_H_
#define GDM1602K_H_ #define GDM1602K_H_
void lcd_write_strings(const char* top, const char* bottom); void lcd_write_strings(const char *top, const char *bottom);
void lcd_write_string(const char* string); void lcd_write_string(const char *string);
void lcd_init(); void lcd_init();
void lcd_clear(); void lcd_clear();

14
LM335.c
View file

@ -1,10 +1,9 @@
#include "LM335.h"
#include "config.h" #include "config.h"
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include "LM335.h"
void ADC_Init() void ADC_Init() {
{
// Set AVCC as the voltage reference with external capacitor at AREF pin // Set AVCC as the voltage reference with external capacitor at AREF pin
ADMUX = (1 << REFS0); ADMUX = (1 << REFS0);
@ -15,8 +14,7 @@ void ADC_Init()
ADCSRA |= (1 << ADEN); // | (1 << ADIE); ADCSRA |= (1 << ADEN); // | (1 << ADIE);
} }
uint16_t ADC_Read(uint8_t channel) uint16_t ADC_Read(uint8_t channel) {
{
// Select ADC channel // Select ADC channel
ADMUX = (ADMUX & 0xF8) | (channel & 0x07); ADMUX = (ADMUX & 0xF8) | (channel & 0x07);
@ -24,15 +22,13 @@ uint16_t ADC_Read(uint8_t channel)
ADCSRA |= (1 << ADSC); ADCSRA |= (1 << ADSC);
// Wait for ADC conversion to complete // Wait for ADC conversion to complete
while (ADCSRA & (1 << ADSC)) while (ADCSRA & (1 << ADSC));
;
// Read ADC result // Read ADC result
return ADC; return ADC;
} }
float LM335_ReadTemperature() float LM335_ReadTemperature() {
{
// Read ADC value from LM335 pin (PA0) // Read ADC value from LM335 pin (PA0)
uint16_t adcValue = ADC_Read(0x00); uint16_t adcValue = ADC_Read(0x00);

View file

@ -1,6 +1,7 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
//#define F_CPU 1600000UL // This is passed as a compiler flag (CFLAGS += -DF_CPU=16000000UL)
// #define F_CPU 1600000UL
#endif // CONFIG_H #endif // CONFIG_H

41
main.c
View file

@ -2,15 +2,15 @@
#include <avr/io.h> #include <avr/io.h>
#include <stdbool.h> #include <stdbool.h>
#include <util/delay.h>
#include <stdio.h> #include <stdio.h>
#include <util/delay.h>
#include "74C922.h" #include "74C922.h"
#include "DHT22.h" #include "DHT22.h"
#include "GDM1602K.h" #include "GDM1602K.h"
#include "LM335.h" #include "LM335.h"
enum DisplayMode {TEMP, AVG}; enum DisplayMode { TEMP, AVG };
bool b = false; bool b = false;
@ -36,24 +36,27 @@ int main() {
long loops = 0; long loops = 0;
while(1) { while (1) {
// If encoder has data available // If encoder has data available
if(encoder_available() == true) { if (encoder_available() == true) {
loops = 0xFFFF; loops = 0xFFFF;
if(!btn_just_pressed) { if (!btn_just_pressed) {
btn_just_pressed = true; btn_just_pressed = true;
button_data = encoder_read(); button_data = encoder_read();
sprintf(btn_string, "Knapp %d", button_data); sprintf(btn_string, "Knapp %d", button_data);
lcd_clear(); lcd_clear();
if(button_data == 11) { if (button_data == 11) {
if(temp_unit == 'C') temp_unit = 'F'; if (temp_unit == 'C')
else if(temp_unit == 'F') temp_unit = 'K'; temp_unit = 'F';
else if(temp_unit == 'K') temp_unit = 'C'; else if (temp_unit == 'F')
temp_unit = 'K';
else if (temp_unit == 'K')
temp_unit = 'C';
loops = 0; loops = 0;
} }
if(button_data == 3) { if (button_data == 3) {
sprintf(celc_str, "10h Tmp: %s", temp_with_unit); sprintf(celc_str, "10h Tmp: %s", temp_with_unit);
sprintf(hum_str, "10h Hum: %.1f%%", humidity-1); sprintf(hum_str, "10h Hum: %.1f%%", humidity - 1);
lcd_write_strings(celc_str, hum_str); lcd_write_strings(celc_str, hum_str);
} }
} }
@ -61,25 +64,27 @@ int main() {
btn_just_pressed = false; btn_just_pressed = false;
} }
if(loops == 0) { if (loops == 0) {
loops = 0x2FFFF; loops = 0x2FFFF;
int result = DHT22_Read(&temp, &hum); int result = DHT22_Read(&temp, &hum);
lcd_clear(); lcd_clear();
if(result == 0) { if (result == 0) {
celcius = ((float)temp)/10.0; celcius = ((float)temp) / 10.0;
humidity = ((float) hum)/10.0; humidity = ((float)hum) / 10.0;
switch(temp_unit) { switch (temp_unit) {
case 'C': case 'C':
sprintf(temp_with_unit, "%.1f %c", celcius, temp_unit); sprintf(temp_with_unit, "%.1f %c", celcius, temp_unit);
break; break;
case 'F': case 'F':
sprintf(temp_with_unit, "%.1f %c", celcius*9/5+32, temp_unit); sprintf(temp_with_unit, "%.1f %c", celcius * 9 / 5 + 32,
temp_unit);
break; break;
case 'K': case 'K':
sprintf(temp_with_unit, "%.1f %c", celcius + 273.15, temp_unit); sprintf(temp_with_unit, "%.1f %c", celcius + 273.15,
temp_unit);
break; break;
} }
sprintf(celc_str, "Temp: %s", temp_with_unit); sprintf(celc_str, "Temp: %s", temp_with_unit);

View file

@ -43,6 +43,9 @@ flash: $(TARGET).hex
qemu: $(TARGET).elf qemu: $(TARGET).elf
qemu-system-avr -machine $(QEMU_MACHINE_NAME) -bios $(TARGET).elf qemu-system-avr -machine $(QEMU_MACHINE_NAME) -bios $(TARGET).elf
format:
find . -type f \( -name "*.c" -o -name "*.h" \) -exec clang-format -i {} +
# Clean # Clean
clean: clean:
rm -f $(OBJS) $(TARGET).elf $(TARGET).hex rm -f $(OBJS) $(TARGET).elf $(TARGET).hex