AVR-Playground/main.c

85 lines
2.1 KiB
C
Raw Normal View History

2024-02-09 02:14:45 +01:00
/*
* This code utilizes the AVR libraries for I2C and UART communication.
* Make sure to connect the SDA and SCL pins of the ATmega328P to the
* corresponding pins of your I2C temperature sensor. Additionally, you might
* need pull-up resistors for the I2C lines.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
#define LED_PIN PB5 // Define the pin connected to the LED
#define TEMP_SENSOR_ADDR 0x48
#define TEMP_REG_ADDR 0x00
2024-03-23 21:27:59 +01:00
#include "MPU6050.h"
#include "i2c.h"
#include "uart.h"
2024-02-09 02:14:45 +01:00
float readTemperature() {
uint16_t temperature;
// Send start condition
I2C_start();
// Send device address with write operation
I2C_write((TEMP_SENSOR_ADDR << 1) | 0);
// Send temperature register address
I2C_write(0x00);
// Repeat start
I2C_start();
// Send device address with read operation
I2C_write((TEMP_SENSOR_ADDR << 1) | 1);
// Read temperature data MSB
temperature = (uint16_t)(I2C_read(1)) << 8;
// Read temperature data LSB
temperature |= I2C_read(0);
// Send stop condition
I2C_stop();
// Convert raw data to temperature in Celsius
return (float)temperature * 0.0625;
}
void blink() {
// Set the LED pin as output
DDRB |= (1 << LED_PIN);
while (1) {
// Turn on the LED by setting the pin high
PORTB |= (1 << LED_PIN);
// Delay for 500 milliseconds
_delay_ms(500);
// Turn off the LED by setting the pin low
PORTB &= ~(1 << LED_PIN);
// Delay for 500 milliseconds
_delay_ms(500);
}
}
2024-03-23 04:28:03 +01:00
int main(void) {
// initI2C();
initUART();
while(1) {
UART_println("Hello, World!");
_delay_ms(1000);
}
// float temperature;
// while (1) {
// temperature = readTemperature();
// // Print temperature over UART
// UART_println("Temperature: ");
// UART_transmit((uint8_t)(temperature / 10.0) + '0');
// UART_transmit((uint8_t)fmod(temperature, 10.0) + '0');
// UART_println(" °C");
// _delay_ms(1000); // Delay for 1 second
// }
// return 0;
}