From 7bd019cad977ee821ebfc8885bb03f0171b061c4 Mon Sep 17 00:00:00 2001 From: zloihach <72695485+zloihach@users.noreply.github.com> Date: Tue, 27 Jun 2023 00:54:44 +0300 Subject: [PATCH] add gpio module --- gpio.c | 22 ++++++++++++++++++++++ gpio.h | 12 ++++++++++++ i2c.c | 15 +++++---------- main.c | 5 +++-- 4 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 gpio.c create mode 100644 gpio.h diff --git a/gpio.c b/gpio.c new file mode 100644 index 0000000..97f997a --- /dev/null +++ b/gpio.c @@ -0,0 +1,22 @@ +#include "gpio.h" + +void setupGPIO() { + DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); + PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); +} + +void setPinOutput(uint8_t pin) { + DDRB |= (1 << pin); +} + +void setPinInput(uint8_t pin) { + DDRB &= ~(1 << pin); +} + +void setPinHigh(uint8_t pin) { + PORTB |= (1 << pin); +} + +void setPinLow(uint8_t pin) { + PORTB &= ~(1 << pin); +} \ No newline at end of file diff --git a/gpio.h b/gpio.h new file mode 100644 index 0000000..615d23b --- /dev/null +++ b/gpio.h @@ -0,0 +1,12 @@ +#ifndef GPIO_H +#define GPIO_H + +#include + +void setupGPIO(); +void setPinOutput(uint8_t pin); +void setPinInput(uint8_t pin); +void setPinHigh(uint8_t pin); +void setPinLow(uint8_t pin); + +#endif \ No newline at end of file diff --git a/i2c.c b/i2c.c index 8154c08..f961642 100644 --- a/i2c.c +++ b/i2c.c @@ -6,33 +6,28 @@ void i2c_init() { void i2c_start() { TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); - while (!(TWCR & (1 << TWINT))) - ; + while (!(TWCR & (1 << TWINT))); } void i2c_stop() { TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); - while (TWCR & (1 << TWSTO)) - ; + while (TWCR & (1 << TWSTO)); } void i2c_write(uint8_t data) { TWDR = data; TWCR = (1 << TWINT) | (1 << TWEN); - while (!(TWCR & (1 << TWINT))) - ; + while (!(TWCR & (1 << TWINT))); } uint8_t i2c_read_ack() { TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); - while (!(TWCR & (1 << TWINT))) - ; + while (!(TWCR & (1 << TWINT))); return TWDR; } uint8_t i2c_read_nack() { TWCR = (1 << TWINT) | (1 << TWEN); - while (!(TWCR & (1 << TWINT))) - ; + while (!(TWCR & (1 << TWINT))); return TWDR; } \ No newline at end of file diff --git a/main.c b/main.c index 44adfd6..0b7c100 100644 --- a/main.c +++ b/main.c @@ -2,13 +2,14 @@ #include #include #include "pwm.h" +#include "i2c.h" +#include "gpio.h" const uint8_t PWM_SLAVE_ADDR = 9; void setup() { i2c_init(); - DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); - PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); + setupGPIO(); Serial.begin(9600); Serial.println("PWM Controller started!"); sendCommand(0x01, 0.0);