75 lines
2.0 KiB
C
75 lines
2.0 KiB
C
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
#define F_CPU 8000000UL
|
|
#define I2C_FREQ 100000UL
|
|
|
|
#include "i2c.h"
|
|
|
|
#define PWM_ADDRESS 0x50
|
|
#define BUTTONS_ADDRESS 0x40
|
|
|
|
#define PWM_ENABLE_CMD 0x0001
|
|
#define PWM_DISABLE_CMD 0x0002
|
|
#define PWM_INCREASE_FREQ_CMD 0x0011
|
|
#define PWM_DECREASE_FREQ_CMD 0x0012
|
|
#define PWM_INCREASE_DUTY_CYCLE_CMD 0x0021
|
|
#define PWM_DECREASE_DUTY_CYCLE_CMD 0x0022
|
|
|
|
struct PWM pwm;
|
|
struct I2C buttons_i2c;
|
|
|
|
void setup() {
|
|
i2c_begin(&buttons_i2c);
|
|
i2c_beginTransmission(&buttons_i2c, BUTTONS_ADDRESS);
|
|
i2c_write(&buttons_i2c, 0x00);
|
|
i2c_endTransmission(&buttons_i2c);
|
|
|
|
pwm.pin = PB3;
|
|
pwm.frequency = 1000;
|
|
pwm_begin(&pwm);
|
|
}
|
|
|
|
void loop() {
|
|
i2c_beginTransmission(&buttons_i2c, BUTTONS_ADDRESS);
|
|
i2c_read(&buttons_i2c, buttons_i2c.data, 1);
|
|
i2c_endTransmission(&buttons_i2c);
|
|
|
|
uint16_t command = ((uint16_t)(buttons_i2c.data[0]) << 8) | buttons_i2c.data[1];
|
|
|
|
if (command == PWM_ENABLE_CMD) {
|
|
pwm_increase_speed(&pwm);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 1);
|
|
} else if (command == PWM_DISABLE_CMD) {
|
|
pwm_decrease_speed(&pwm);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 0);
|
|
} else if (command == PWM_INCREASE_FREQ_CMD) {
|
|
pwm.frequency += round(pwm.frequency * 0.25);
|
|
pwm_set_frequency(&pwm, pwm.frequency);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 1);
|
|
} else if (command == PWM_DECREASE_FREQ_CMD) {
|
|
pwm.frequency -= round(pwm.frequency * 0.2);
|
|
pwm_set_frequency(&pwm, pwm.frequency);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 0);
|
|
} else if (command == PWM_INCREASE_DUTY_CYCLE_CMD) {
|
|
pwm_increase_duty_cycle(&pwm);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 1);
|
|
} else if (command == PWM_DECREASE_DUTY_CYCLE_CMD) {
|
|
pwm_decrease_duty_cycle(&pwm);
|
|
i2c_sendByte(&buttons_i2c, PWM_ADDRESS, 0);
|
|
}
|
|
|
|
_delay_ms(100);
|
|
}
|
|
|
|
int main() {
|
|
setup();
|
|
while (true) {
|
|
loop();
|
|
}
|
|
return 0;
|
|
} |