add gpio module
This commit is contained in:
parent
afa8ca5d02
commit
7bd019cad9
22
gpio.c
Normal file
22
gpio.c
Normal file
@ -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);
|
||||
}
|
12
gpio.h
Normal file
12
gpio.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef GPIO_H
|
||||
#define GPIO_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
void setupGPIO();
|
||||
void setPinOutput(uint8_t pin);
|
||||
void setPinInput(uint8_t pin);
|
||||
void setPinHigh(uint8_t pin);
|
||||
void setPinLow(uint8_t pin);
|
||||
|
||||
#endif
|
15
i2c.c
15
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;
|
||||
}
|
5
main.c
5
main.c
@ -2,13 +2,14 @@
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
#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);
|
||||
|
Loading…
Reference in New Issue
Block a user