Compare commits
20
Commits
6d2cb5af62
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebc47a79ee | ||
|
|
7bd019cad9 | ||
|
|
afa8ca5d02 | ||
|
|
bace9ac2d2 | ||
|
|
2aee2330c6 | ||
|
|
a7310eef77 | ||
|
|
3e466f6e48 | ||
|
|
f498e9b0cc | ||
|
|
d133eb9dd9 | ||
|
|
96295c6d50 | ||
|
|
20b8dc9d5d | ||
|
|
fe558d57e8 | ||
|
|
d1ae25e83a | ||
|
|
a0d085a0d0 | ||
|
|
46a811ea4e | ||
|
|
993ae6b624 | ||
|
|
eea13c46bf | ||
|
|
ae7b4fffba | ||
|
|
9d65806d8b | ||
|
|
df75af19c2 |
+326
-1
@@ -1,4 +1,4 @@
|
||||
### JetBrains template
|
||||
### CLion+iml template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
@@ -77,6 +77,173 @@ fabric.properties
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### JetBrains+all template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### C++ template
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
### C template
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
### CLion+all template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
@@ -156,3 +323,161 @@ fabric.properties
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### CLion template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### JetBrains+iml template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
|
||||
Generated
-8
@@ -1,8 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
Generated
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/pmw.iml" filepath="$PROJECT_DIR$/.idea/pmw.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
-2
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -10,8 +10,8 @@
|
||||
- #### Выключить ШИМ.
|
||||
- #### Увеличить частоту на 25% от текущего.
|
||||
- #### Уменьшить частоту на 20% от текущего.
|
||||
- #### Увеличить скважность.
|
||||
- #### Уменьшить скважность.
|
||||
- #### Увеличить скважность на 10% от текущего.
|
||||
- #### Уменьшить скважность на 10% от текущего.
|
||||
|
||||
### Пояснения к заданию
|
||||
|
||||
@@ -22,13 +22,13 @@
|
||||
### Набор команд
|
||||
|
||||
| Команда | Описание |
|
||||
|:--------|:--------------------------------------|
|
||||
| TODO: | Включить ШИМ. |
|
||||
| TODO: | Выключить ШИМ. |
|
||||
| TODO: | Увеличить частоту на 25% от текущего. |
|
||||
| TODO: | Уменьшить частоту на 20% от текущего. |
|
||||
| TODO: | Увеличить скважность. |
|
||||
| TODO: | Уменьшить скважность. |
|
||||
|:--------|:------------------------------------------|
|
||||
| 0x01: | Включить ШИМ. |
|
||||
| 0x02: | Выключить ШИМ. |
|
||||
| 0x03: | Увеличить частоту на 25% от текущего. |
|
||||
| 0x04: | Уменьшить частоту на 20% от текущего. |
|
||||
| 0x05: | Увеличить скважность на 10% от текущего |
|
||||
| 0x06: | Уменьшить скважность. на 10% от текущего |
|
||||
|
||||
### Описание регистров для I2C
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
#define I2C_FREQ 100000UL
|
||||
|
||||
void i2c_begin(uint8_t address) {
|
||||
TWBR = ((F_CPU / I2C_FREQ) - 16) / 2; // Расчет предделителя для заданной частоты
|
||||
TWAR = (address << 1); // Установка адреса устройства
|
||||
}
|
||||
|
||||
void i2c_endTransaction() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); // Отправка условия STOP
|
||||
}
|
||||
|
||||
void i2c_beginTransmission(uint8_t address) {
|
||||
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); // Отправка условия START
|
||||
while (!(TWCR & (1 << TWINT))); // Ожидание завершения START
|
||||
|
||||
TWDR = (address << 1); // Отправка адреса устройства
|
||||
TWCR = (1 << TWINT) | (1 << TWEN); // Отправка адреса
|
||||
while (!(TWCR & (1 << TWINT))); // Ожидание завершения передачи адреса
|
||||
}
|
||||
|
||||
|
||||
void i2c_endTransmission() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); // Отправка условия STOP
|
||||
}
|
||||
|
||||
void i2c_write(uint8_t data) {
|
||||
TWDR = data; // Запись данных
|
||||
TWCR = (1 << TWINT) | (1 << TWEN); // Отправка данных
|
||||
while (!(TWCR & (1 << TWINT))); // Ожидание завершения передачи данных
|
||||
}
|
||||
|
||||
void i2c_read(uint8_t *data, uint8_t ack) {
|
||||
if (ack) {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); // Отправка ACK
|
||||
} else {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN); // Отправка NACK
|
||||
}
|
||||
while (!(TWCR & (1 << TWINT))); // Ожидание завершения приема данных
|
||||
*data = TWDR; // Чтение данных
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
#define F_CPU 16000000UL
|
||||
#define I2C_FREQ 100000UL
|
||||
#define I2C_PRESCALER 1
|
||||
#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER)
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
float pwm_frequency = 1.0;
|
||||
uint8_t pwm_duty_cycle = 50;
|
||||
|
||||
volatile uint8_t pwm_enabled = 0;
|
||||
volatile uint8_t pwm_changed = 0;
|
||||
|
||||
void i2c_init() {
|
||||
TWBR = I2C_BITRATE;
|
||||
}
|
||||
|
||||
void i2c_start() {
|
||||
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
}
|
||||
|
||||
void i2c_stop() {
|
||||
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
|
||||
while (TWCR & (1 << TWSTO))
|
||||
;
|
||||
}
|
||||
|
||||
void i2c_write(uint8_t data) {
|
||||
TWDR = data;
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
}
|
||||
|
||||
uint8_t i2c_read_ack() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
return TWDR;
|
||||
}
|
||||
|
||||
uint8_t i2c_read_nack() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)))
|
||||
;
|
||||
return TWDR;
|
||||
}
|
||||
|
||||
const uint8_t PWM_SLAVE_ADDR = 9;
|
||||
|
||||
void pwm_enable() {
|
||||
pwm_enabled = 1;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void pwm_disable() {
|
||||
pwm_enabled = 0;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void setPWMFrequency(float frequency) {
|
||||
if (frequency < 0 || frequency > 50000.0) {
|
||||
return; // Недопустимая частота
|
||||
}
|
||||
pwm_frequency = frequency;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void setPWMDutyCycle(float duty_cycle) {
|
||||
if (duty_cycle < 1 || duty_cycle > 100) {
|
||||
return; // Недопустимый коэффициент заполнения
|
||||
}
|
||||
pwm_duty_cycle = duty_cycle;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void pwm_check_state() {
|
||||
if (pwm_enabled) {
|
||||
// Включаем пин ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("PWM enabled. Frequency: ");
|
||||
Serial.print(pwm_frequency);
|
||||
Serial.print(" Hz, duty cycle: ");
|
||||
Serial.println(pwm_duty_cycle);
|
||||
} else {
|
||||
// Выключаем пин ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.println("PWM disabled");
|
||||
}
|
||||
}
|
||||
|
||||
void pwm_set_frequency(float frequency) {
|
||||
// Устанавливаем частоту ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("Setting PWM frequency to: ");
|
||||
Serial.println(frequency);
|
||||
}
|
||||
|
||||
void pwm_set_duty_cycle(float duty_cycle) {
|
||||
// Устанавливаем коэффициент заполнения ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("Setting PWM duty cycle to: ");
|
||||
Serial.println(duty_cycle);
|
||||
}
|
||||
|
||||
void enablePWM() {
|
||||
pwm_enable();
|
||||
// Включаем ШИМ
|
||||
sendCommand(0x01, 0.0);
|
||||
}
|
||||
|
||||
void disablePWM() {
|
||||
// Выключаем ШИМ
|
||||
sendCommand(0x02, 0.0);
|
||||
pwm_disable();
|
||||
}
|
||||
|
||||
void increaseFrequency() {
|
||||
setPWMFrequency(pwm_frequency * 1.25);
|
||||
// Выключаем ШИМ, отправляем команду и включаем обратно
|
||||
disablePWM();
|
||||
sendCommand(0x03, pwm_frequency);
|
||||
enablePWM();
|
||||
}
|
||||
|
||||
void decreaseFrequency() {
|
||||
setPWMFrequency(pwm_frequency * 0.8);
|
||||
// Выключаем ШИМ, отправляем команду и включаем обратно
|
||||
disablePWM();
|
||||
sendCommand(0x04, pwm_frequency);
|
||||
enablePWM();
|
||||
}
|
||||
|
||||
void increaseDutyCycle() {
|
||||
if (pwm_duty_cycle < 100) {
|
||||
pwm_duty_cycle+=10;
|
||||
setPWMDutyCycle(pwm_duty_cycle);
|
||||
// Выключаем ШИМ, отправляем команду и включаем обратно
|
||||
disablePWM();
|
||||
sendCommand(0x05, pwm_duty_cycle);
|
||||
enablePWM();
|
||||
} else {
|
||||
Serial.println("Maximum duty cycle reached!");
|
||||
}
|
||||
}
|
||||
|
||||
void decreaseDutyCycle() {
|
||||
|
||||
if (pwm_duty_cycle > 1) {
|
||||
pwm_duty_cycle-=10;
|
||||
setPWMDutyCycle(pwm_duty_cycle);
|
||||
// Выключаем ШИМ, отправляем команду и включаем обратно
|
||||
disablePWM();
|
||||
sendCommand(0x06, pwm_duty_cycle);
|
||||
enablePWM();
|
||||
} else {
|
||||
Serial.println("Minimum duty cycle reached!");
|
||||
}
|
||||
}
|
||||
|
||||
void checkButton(uint8_t pin, const char* message, void (*command)()) {
|
||||
if (bit_is_clear(PINC, pin)) {
|
||||
Serial.println(message);
|
||||
delay(200);
|
||||
command();
|
||||
}
|
||||
|
||||
if (pwm_changed) {
|
||||
pwm_changed = 0;
|
||||
pwm_check_state();
|
||||
if (pwm_enabled) {
|
||||
pwm_set_frequency(pwm_frequency);
|
||||
pwm_set_duty_cycle(pwm_duty_cycle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
Serial.begin(9600);
|
||||
Serial.println("PWM Controller started!");
|
||||
sendCommand(0x01, 0.0);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
checkButton(0, "Turn on PWM!", enablePWM);
|
||||
checkButton(1, "Turn off PWM!", disablePWM);
|
||||
checkButton(2, "Increase frequency by 25%!", increaseFrequency);
|
||||
checkButton(3, "Decrease frequency by 20%!", decreaseFrequency);
|
||||
checkButton(4, "Increase duty cycle by 10%!", increaseDutyCycle);
|
||||
checkButton(5, "Decrease duty cycle by 10%!", decreaseDutyCycle);
|
||||
}
|
||||
|
||||
|
||||
void sendCommand(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value;
|
||||
|
||||
switch (cmd) {
|
||||
case 0x01:
|
||||
case 0x02:
|
||||
sendCommandOneByte(cmd);
|
||||
break;
|
||||
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
sendCommandThreeBytes(cmd, value);
|
||||
break;
|
||||
|
||||
case 0x05:
|
||||
case 0x06:
|
||||
sendCommandTwoBytes(cmd, value);
|
||||
break;
|
||||
|
||||
default:
|
||||
Serial.println("Unknown command");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void sendCommandOneByte(uint8_t cmd) {
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.println(cmd, HEX);
|
||||
}
|
||||
|
||||
void sendCommandTwoBytes(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value = round(value);
|
||||
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_write(cmd_value & 0xFF);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", value: ");
|
||||
Serial.print(value);
|
||||
Serial.print(", cmd value: ");
|
||||
Serial.print(cmd_value);
|
||||
Serial.print(", data bytes: 0x");
|
||||
Serial.println(cmd_value & 0xFF, HEX);
|
||||
}
|
||||
|
||||
void sendCommandThreeBytes(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value = round(value * 16); // приводим к 12 бит и 4 бита дробной части
|
||||
uint8_t data_bytes[2] = {(cmd_value >> 8) & 0xFF, cmd_value & 0xFF};
|
||||
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_write(data_bytes[0]);
|
||||
i2c_write(data_bytes[1]);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", value: ");
|
||||
Serial.print(value);
|
||||
Serial.print(", cmd value: ");
|
||||
Serial.print(cmd_value);
|
||||
Serial.print(", data bytes: 0x");
|
||||
Serial.print(data_bytes[0], HEX);
|
||||
Serial.print(" ");
|
||||
Serial.println(data_bytes[1], HEX);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "i2c.h"
|
||||
|
||||
void i2c_init() {
|
||||
TWBR = I2C_BITRATE;
|
||||
}
|
||||
|
||||
void i2c_start() {
|
||||
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
}
|
||||
|
||||
void i2c_stop() {
|
||||
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
|
||||
while (TWCR & (1 << TWSTO));
|
||||
}
|
||||
|
||||
void i2c_write(uint8_t data) {
|
||||
TWDR = data;
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
}
|
||||
|
||||
uint8_t i2c_read_ack() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
return TWDR;
|
||||
}
|
||||
|
||||
uint8_t i2c_read_nack() {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
return TWDR;
|
||||
}
|
||||
@@ -1,111 +1,18 @@
|
||||
//
|
||||
// Created by FSB-PC on 22.05.2023.
|
||||
//
|
||||
#ifndef I2C_H
|
||||
#define I2C_H
|
||||
|
||||
#ifndef INC_8_PMW_I2C_H
|
||||
#define INC_8_PMW_I2C_H
|
||||
#include <avr/io.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t address; // адрес устройства на шине I2C
|
||||
uint8_t data[32]; // буфер для передачи и приема данных
|
||||
uint8_t length; // длина передаваемых данных
|
||||
};
|
||||
#define F_CPU 16000000UL
|
||||
#define I2C_FREQ 100000UL
|
||||
#define I2C_PRESCALER 1
|
||||
#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER)
|
||||
|
||||
void i2c_begin(i2c_device_t *i2c){
|
||||
TWBR = ((F_CPU / i2c->speed) - 16) / 2;
|
||||
TWAR = (i2c->address << 1);
|
||||
}
|
||||
void i2c_init();
|
||||
void i2c_start();
|
||||
void i2c_stop();
|
||||
void i2c_write(uint8_t data);
|
||||
uint8_t i2c_read_ack();
|
||||
uint8_t i2c_read_nack();
|
||||
|
||||
void i2c_beginTransmission(i2c_device_t *i2c, uint8_t address){
|
||||
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
|
||||
TWDR = (address << 1);
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
i2c->address = address;
|
||||
}
|
||||
|
||||
void i2c_write(i2c_device_t *i2c, uint8_t data){
|
||||
TWDR = data;
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
}
|
||||
|
||||
void i2c_read(i2c_device_t *i2c, uint8_t *data, uint8_t ack){
|
||||
if (ack){
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
||||
} else {
|
||||
TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
}
|
||||
while (!(TWCR & (1 << TWINT)));
|
||||
*data = TWDR;
|
||||
}
|
||||
|
||||
void i2c_endTransmission(i2c_device_t *i2c){
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
|
||||
}
|
||||
|
||||
void i2c_sendByte(i2c_device_t *i2c, uint8_t address, uint8_t data){
|
||||
i2c_beginTransmission(i2c, address);
|
||||
i2c_write(i2c, data);
|
||||
i2c_endTransmission(i2c);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif //INC_8_PMW_I2C_H
|
||||
|
||||
//void i2c_begin(I2C *i2c) {
|
||||
// TWBR = ((F_CPU / i2c->speed) - 16) / 2;
|
||||
// TWAR = (i2c->address << 1);
|
||||
//}
|
||||
//
|
||||
//void i2c_beginTransmission(I2C *i2c, uint8_t address) {
|
||||
// TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
|
||||
// while (!(TWCR & (1 << TWINT)));
|
||||
//
|
||||
// TWDR = (address << 1);
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
// while (!(TWCR & (1 << TWINT)));
|
||||
// i2c->address = address;
|
||||
//}
|
||||
//
|
||||
//void i2c_write(I2C *i2c, uint8_t data) {
|
||||
// TWDR = data;
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
// while (!(TWCR & (1 << TWINT)));
|
||||
//}
|
||||
//
|
||||
//void i2c_read(I2C *i2c, uint8_t *data, uint8_t ack) {
|
||||
// if (ack) {
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
|
||||
// } else {
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN);
|
||||
// }
|
||||
// while (!(TWCR & (1 << TWINT)));
|
||||
// *data = TWDR;
|
||||
//}
|
||||
//
|
||||
//void i2c_endTransmission(I2C *i2c) {
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
|
||||
//}
|
||||
//
|
||||
//void sendByte(uint8_t data) {
|
||||
// sendByteRaw(data);
|
||||
// _writes++;
|
||||
// if (_writes >= 16) {
|
||||
// endTransm();
|
||||
// beginData();
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void sendByteRaw(uint8_t data) {
|
||||
// i2c_write(data);
|
||||
//}
|
||||
//
|
||||
//void sendCommand(uint8_t cmd1) {
|
||||
// beginOneCommand();
|
||||
// sendByteRaw(cmd1);
|
||||
// i2c_endTransmission();
|
||||
//}
|
||||
#endif
|
||||
@@ -1,5 +1,120 @@
|
||||
#define I2C_FREQ 100000UL
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
#include "pwm.h"
|
||||
#include "i2c.h"
|
||||
#include "pmw.h"
|
||||
#include "gpio.h"
|
||||
|
||||
const uint8_t PWM_SLAVE_ADDR = 9;
|
||||
|
||||
void setup() {
|
||||
i2c_init();
|
||||
setupGPIO();
|
||||
Serial.begin(9600);
|
||||
Serial.println("PWM Controller started!");
|
||||
sendCommand(0x01, 0.0);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
checkButton(0, "Turn on PWM!", enablePWM);
|
||||
checkButton(1, "Turn off PWM!", disablePWM);
|
||||
checkButton(2, "Increase frequency by 25%!", increaseFrequency);
|
||||
checkButton(3, "Decrease frequency by 20%!", decreaseFrequency);
|
||||
checkButton(4, "Increase duty cycle by 10%!", increaseDutyCycle);
|
||||
checkButton(5, "Decrease duty cycle by 10%!", decreaseDutyCycle);
|
||||
}
|
||||
|
||||
void checkButton(uint8_t pin, const char* message, void (*command)()) {
|
||||
if (bit_is_clear(PINC, pin)) {
|
||||
Serial.println(message);
|
||||
delay(200);
|
||||
command();
|
||||
}
|
||||
|
||||
if (pwm_changed) {
|
||||
pwm_changed = 0;
|
||||
pwm_check_state();
|
||||
if (pwm_enabled) {
|
||||
pwm_set_frequency(pwm_frequency);
|
||||
pwm_set_duty_cycle(pwm_duty_cycle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sendCommand(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value;
|
||||
|
||||
switch (cmd) {
|
||||
case 0x01:
|
||||
case 0x02:
|
||||
sendCommandOneByte(cmd);
|
||||
break;
|
||||
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
sendCommandThreeBytes(cmd, value);
|
||||
break;
|
||||
|
||||
case 0x05:
|
||||
case 0x06:
|
||||
sendCommandTwoBytes(cmd, value);
|
||||
break;
|
||||
|
||||
default:
|
||||
Serial.println("Unknown command");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void sendCommandOneByte(uint8_t cmd) {
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.println(cmd, HEX);
|
||||
}
|
||||
|
||||
void sendCommandTwoBytes(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value = round(value);
|
||||
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_write(cmd_value & 0xFF);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", value: ");
|
||||
Serial.print(value);
|
||||
Serial.print(", cmd value: ");
|
||||
Serial.print(cmd_value);
|
||||
Serial.print(", data bytes: 0x");
|
||||
Serial.println(cmd_value & 0xFF, HEX);
|
||||
}
|
||||
|
||||
void sendCommandThreeBytes(uint8_t cmd, float value) {
|
||||
uint16_t cmd_value = round(value * 16); // приводим к 12 бит и 4 бита дробной части
|
||||
uint8_t data_bytes[2] = {(cmd_value >> 8) & 0xFF, cmd_value & 0xFF};
|
||||
|
||||
i2c_start();
|
||||
i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
i2c_write(cmd);
|
||||
i2c_write(data_bytes[0]);
|
||||
i2c_write(data_bytes[1]);
|
||||
i2c_stop();
|
||||
|
||||
Serial.print("Sent command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", value: ");
|
||||
Serial.print(value);
|
||||
Serial.print(", cmd value: ");
|
||||
Serial.print(cmd_value);
|
||||
Serial.print(", data bytes: 0x");
|
||||
Serial.print(data_bytes[0], HEX);
|
||||
Serial.print(" ");
|
||||
Serial.println(data_bytes[1], HEX);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "pwm.h"
|
||||
|
||||
float pwm_frequency = 1.0;
|
||||
uint8_t pwm_duty_cycle = 50;
|
||||
|
||||
volatile uint8_t pwm_enabled = 0;
|
||||
volatile uint8_t pwm_changed = 0;
|
||||
|
||||
void pwm_enable() {
|
||||
pwm_enabled = 1;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void pwm_disable() {
|
||||
pwm_enabled = 0;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void setPWMFrequency(float frequency) {
|
||||
if (frequency < 0 || frequency > 50000.0) {
|
||||
return; // Недопустимая частота
|
||||
}
|
||||
pwm_frequency = frequency;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void setPWMDutyCycle(float duty_cycle) {
|
||||
if (duty_cycle < 1 || duty_cycle > 100) {
|
||||
return; // Недопустимый коэффициент заполнения
|
||||
}
|
||||
pwm_duty_cycle = duty_cycle;
|
||||
pwm_changed = 1;
|
||||
}
|
||||
|
||||
void pwm_check_state() {
|
||||
if (pwm_enabled) {
|
||||
// Включаем пин ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("PWM enabled. Frequency: ");
|
||||
Serial.print(pwm_frequency);
|
||||
Serial.print(" Hz, duty cycle: ");
|
||||
Serial.println(pwm_duty_cycle);
|
||||
} else {
|
||||
// Выключаем пин ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.println("PWM disabled");
|
||||
}
|
||||
}
|
||||
|
||||
void pwm_set_frequency(float frequency) {
|
||||
// Устанавливаем частоту ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("Setting PWM frequency to: ");
|
||||
Serial.println(frequency);
|
||||
}
|
||||
|
||||
void pwm_set_duty_cycle(float duty_cycle) {
|
||||
// Устанавливаем коэффициент заполнения ШИМ - реализация зависит от аппаратной платформы
|
||||
Serial.print("Setting PWM duty cycle to: ");
|
||||
Serial.println(duty_cycle);
|
||||
}
|
||||
@@ -1,70 +1,33 @@
|
||||
//
|
||||
// Created by FSB-PC on 22.05.2023.
|
||||
//
|
||||
#ifndef PWM_H
|
||||
#define PWM_H
|
||||
|
||||
#ifndef INC_8_PMW_PMW_H
|
||||
#define INC_8_PMW_PMW_H
|
||||
#include <avr/io.h>
|
||||
#include "i2c.h"
|
||||
|
||||
extern float pwm_frequency;
|
||||
extern uint8_t pwm_duty_cycle;
|
||||
|
||||
struct PWM {
|
||||
uint8_t pin; // номер пина
|
||||
uint16_t frequency; // частота ШИМ сигнала
|
||||
uint8_t duty_cycle; // коэффициент заполнения
|
||||
};
|
||||
extern volatile uint8_t pwm_enabled;
|
||||
extern volatile uint8_t pwm_changed;
|
||||
|
||||
void pwm_begin(struct PWM *pwm) {
|
||||
TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01);
|
||||
TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
OCR0A = 0;
|
||||
pwm->duty_cycle = 0;
|
||||
}
|
||||
void pwm_enable();
|
||||
void pwm_disable();
|
||||
void setPWMFrequency(float frequency);
|
||||
void setPWMDutyCycle(float duty_cycle);
|
||||
void pwm_check_state();
|
||||
void pwm_set_frequency(float frequency);
|
||||
void pwm_set_duty_cycle(float duty_cycle);
|
||||
|
||||
void pwm_set_duty_cycle(struct PWM *pwm, uint8_t duty_cycle) {
|
||||
pwm->duty_cycle = duty_cycle;
|
||||
OCR0A = (duty_cycle * 255) / 100;
|
||||
}
|
||||
void enablePWM();
|
||||
void disablePWM();
|
||||
void increaseFrequency();
|
||||
void decreaseFrequency();
|
||||
void increaseDutyCycle();
|
||||
void decreaseDutyCycle();
|
||||
void checkButton(uint8_t pin, const char* message, void (*command)());
|
||||
void sendCommand(uint8_t cmd, float value);
|
||||
void sendCommandOneByte(uint8_t cmd);
|
||||
void sendCommandTwoBytes(uint8_t cmd, float value);
|
||||
void sendCommandThreeBytes(uint8_t cmd, float value);
|
||||
|
||||
void pwm_increase_speed(struct PWM *pwm) {
|
||||
pwm->duty_cycle += 25;
|
||||
TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
}
|
||||
|
||||
void pwm_decrease_speed(struct PWM *pwm) {
|
||||
pwm->duty_cycle -= 20;
|
||||
TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
}
|
||||
|
||||
void pwm_end(struct PWM *pwm) {
|
||||
TCCR0B = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//void pmw_begin(PMW *pmw) {
|
||||
// TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01);
|
||||
// TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
// OCR0A = 0;
|
||||
// pmw->speed = 0;
|
||||
// pmw->duty_cycle = 0;
|
||||
//}
|
||||
//
|
||||
//void pmw_set_duty_cycle(PMW *pmw, uint8_t duty_cycle) {
|
||||
// pmw->duty_cycle = duty_cycle;
|
||||
// OCR0A = (duty_cycle * 255) / 100;
|
||||
//}
|
||||
//
|
||||
//void pmw_increase_speed(PMW *pmw) {
|
||||
// pmw->speed += 25;
|
||||
// TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
//}
|
||||
//
|
||||
//void pmw_decrease_speed(PMW *pmw) {
|
||||
// pmw->speed -= 20;
|
||||
// TCCR0B = (1 << CS00) | (1 << CS01);
|
||||
//}
|
||||
//
|
||||
//void pmw_end(PMW *pmw) {
|
||||
// TCCR0B = 0;
|
||||
//}
|
||||
|
||||
#endif //INC_8_PMW_PMW_H
|
||||
#endif
|
||||
+290
@@ -0,0 +1,290 @@
|
||||
// #include <avr/io.h>
|
||||
// #include <avr/interrupt.h>
|
||||
|
||||
// #define SLAVE_ADDRESS 9 // адрес устройства slave
|
||||
// #define COMMAND_1 0x01 //команда включения шим
|
||||
// #define COMMAND_2 0x02 //команда выключения шим
|
||||
// #define COMMAND_3 0x03 //команда изменения частоты шим
|
||||
// #define COMMAND_4 0x04 //команда изменения скважности шим
|
||||
// #define COMMAND_5 0x05 //команда изменения скважности шим
|
||||
|
||||
|
||||
// uint16_t frequency = 1 << 4;
|
||||
// uint8_t dutycycle = 50;
|
||||
// uint8_t buffer[32]; // буфер для полученных данных
|
||||
// uint8_t cnt = 0; // счетчик байтов
|
||||
// bool tran_end = false; //флаг окончания передачи
|
||||
|
||||
// void i2c_init() {
|
||||
// TWAR = SLAVE_ADDRESS << 1; // задаем адрес устройства
|
||||
// TWSR = 0; // частота шины 100 кГц (скорость передачи)
|
||||
// TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // включаем slave reciever
|
||||
// }
|
||||
|
||||
// // обработчик прерывания TWI
|
||||
// ISR(TWI_vect) {
|
||||
// switch (TWSR) {
|
||||
// case 0x60: // SLA+W received, ACK returned
|
||||
// //cnt = 0; // сброс счетчика
|
||||
// //tran_end = false;
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // включаем прерывание и устанавливаем бит ACK
|
||||
// break;
|
||||
// case 0x80: // data byte received, ACK returned
|
||||
// buffer[cnt++] = TWDR; // сохраняем принятые данные
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
// break;
|
||||
// case 0x88: // last data byte received, NACK returned
|
||||
// buffer[cnt++] = TWDR; // сохраняем последний байт данных
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
// break;
|
||||
// case 0xA0: // STOP or repeated START received
|
||||
// tran_end = true;
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
// break;
|
||||
// default:
|
||||
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// void timer1_init() {
|
||||
// PORTB = 0;
|
||||
// DDRB |= (1 << PORTB1); //OC1A
|
||||
// DDRB |= (1 << PORTB2); //OC1B
|
||||
// TCCR1A = 0; // обнуляем регистры управления
|
||||
// TCCR1B = 0;
|
||||
// TCNT1 = 0; // и счетный регистр
|
||||
// }
|
||||
|
||||
// //частота приходит в 16 раз больше
|
||||
// void timer1_on(uint16_t frequency, uint8_t dutycycle) {
|
||||
// // Неинверсный режим работы
|
||||
// TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (1 << COM1B1) | (0 << COM1B0);
|
||||
|
||||
// //Fast PWM (Быстрый ШИМ)
|
||||
// TCCR1A |= (1 << WGM11) | (1 << WGM10);
|
||||
// TCCR1B |= (1 << WGM13) | (1 << WGM12);
|
||||
|
||||
// //Предделитель - 1024
|
||||
// TCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
|
||||
|
||||
// //Частота
|
||||
// OCR1A = F_CPU / 1024 * 16 / frequency;
|
||||
|
||||
// //Скважность
|
||||
// OCR1B = (F_CPU / 1024 * 16 / frequency) * dutycycle / 100;
|
||||
// }
|
||||
|
||||
// void timer1_off() {
|
||||
// // TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0));
|
||||
// //Fast PWM (Быстрый ШИМ)
|
||||
// TCCR1A |= (0 << WGM11) | (0 << WGM10);
|
||||
// TCCR1B |= (0 << WGM13) | (0 << WGM12);
|
||||
// TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //шим отключен, нормальная работа порта
|
||||
// TCCR1B = 0;
|
||||
|
||||
// // OCR1A = 0;
|
||||
// // OCR1B = 0;
|
||||
// }
|
||||
|
||||
// void setup(void) {
|
||||
// i2c_init(); //инициализация i2c
|
||||
// timer1_init();
|
||||
// Serial.begin(9600);
|
||||
// Serial.println("PWM Slave started!");
|
||||
// sei(); // включаем прерывания
|
||||
// }
|
||||
|
||||
|
||||
// void loop() {
|
||||
// if (tran_end) {
|
||||
// tran_end = false;
|
||||
// for(int i = 0;i<100;i++){
|
||||
// }
|
||||
// if (buffer[0] == COMMAND_1) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// timer1_on(frequency, dutycycle);
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX); // Включение ШИМ
|
||||
// } else if (buffer[0] == COMMAND_2) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// timer1_off();
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX); // Выключение ШИМ
|
||||
// } else if (buffer[0] == COMMAND_3) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// frequency = (buffer[1] << 8) + buffer[2];
|
||||
// Serial.print("frequency value: ");
|
||||
// Serial.print(frequency);
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX);
|
||||
// } else if (buffer[0] == COMMAND_4) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// frequency = (buffer[1] << 8) + buffer[2];
|
||||
// Serial.print("frequency value: ");
|
||||
// Serial.print(frequency);
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX);
|
||||
// } else if (buffer[0] == COMMAND_5) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// dutycycle = buffer[1];
|
||||
// Serial.print("dytycycle value: ");
|
||||
// Serial.println(dutycycle);
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX);
|
||||
// } else if (buffer[0] == COMMAND_5) {
|
||||
// Serial.print("cmd: 0x");
|
||||
// Serial.println(buffer[0]);
|
||||
// dutycycle = buffer[1];
|
||||
// Serial.print("dytycycle value: ");
|
||||
// Serial.println(dutycycle);
|
||||
// Serial.print(", data bytes: ");
|
||||
// Serial.print(buffer[1], HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(buffer[2], HEX);
|
||||
// }
|
||||
// cnt = 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#define SLAVE_ADDRESS 9 // адрес устройства slave
|
||||
#define COMMAND_1 0x01 //команда включения шим
|
||||
#define COMMAND_2 0x02 //команда выключения шим
|
||||
#define COMMAND_3 0x03 //команда изменения частоты шим
|
||||
#define COMMAND_4 0x04 //команда изменения скважности шим
|
||||
#define COMMAND_5 0x05 //команда изменения скважности шим
|
||||
#define COMMAND_6 0x06 //команда изменения скважности шим
|
||||
|
||||
|
||||
|
||||
uint16_t frequency = 1 << 4;
|
||||
uint8_t dutycycle = 50;
|
||||
uint8_t buffer[32]; // буфер для полученных данных
|
||||
uint8_t cnt = 0; // счетчик байтов
|
||||
bool tran_end = false; //флаг окончания передачи
|
||||
|
||||
void i2c_init() {
|
||||
TWAR = SLAVE_ADDRESS << 1; // задаем адрес устройства
|
||||
TWSR = 0; // частота шины 100 кГц (скорость передачи)
|
||||
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // включаем slave reciever
|
||||
}
|
||||
|
||||
// обработчик прерывания TWI
|
||||
ISR(TWI_vect) {
|
||||
switch (TWSR) {
|
||||
case 0x60: // SLA+W received, ACK returned
|
||||
//cnt = 0; // сброс счетчика
|
||||
//tran_end = false;
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // включаем прерывание и устанавливаем бит ACK
|
||||
break;
|
||||
case 0x80: // data byte received, ACK returned
|
||||
buffer[cnt++] = TWDR; // сохраняем принятые данные
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
break;
|
||||
case 0x88: // last data byte received, NACK returned
|
||||
buffer[cnt++] = TWDR; // сохраняем последний байт данных
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
break;
|
||||
case 0xA0: // STOP or repeated START received
|
||||
tran_end = true;
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
break;
|
||||
default:
|
||||
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA) | (1 << TWIE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void timer1_init() {
|
||||
PORTB = 0;
|
||||
DDRB |= (1 << PORTB1); //OC1A
|
||||
DDRB |= (1 << PORTB2); //OC1B
|
||||
TCCR1A = 0; // обнуляем регистры управления
|
||||
TCCR1B = 0;
|
||||
TCNT1 = 0; // и счетный регистр
|
||||
}
|
||||
|
||||
//частота приходит в 16 раз больше
|
||||
void timer1_on(uint16_t frequency, uint8_t dutycycle) {
|
||||
// Неинверсный режим работы
|
||||
TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (1 << COM1B1) | (0 << COM1B0);
|
||||
|
||||
//Fast PWM (Быстрый ШИМ)
|
||||
TCCR1A |= (1 << WGM11) | (1 << WGM10);
|
||||
TCCR1B |= (1 << WGM13) | (1 << WGM12);
|
||||
|
||||
//Предделитель - 1024
|
||||
TCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
|
||||
|
||||
//Частота
|
||||
OCR1A = F_CPU / 1024 * 16 / frequency;
|
||||
|
||||
//Скважность
|
||||
OCR1B = (F_CPU / 1024 * 16 / frequency) * dutycycle / 100;
|
||||
}
|
||||
|
||||
void timer1_off() {
|
||||
// TCCR1A &= ~((1 << COM1A1) | (1 << COM1A0));
|
||||
//Fast PWM (Быстрый ШИМ)
|
||||
TCCR1A |= (0 << WGM11) | (0 << WGM10);
|
||||
TCCR1B |= (0 << WGM13) | (0 << WGM12);
|
||||
TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //шим отключен, нормальная работа порта
|
||||
TCCR1B = 0;
|
||||
|
||||
// OCR1A = 0;
|
||||
// OCR1B = 0;
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
i2c_init(); //инициализация i2c
|
||||
timer1_init();
|
||||
Serial.begin(9600);
|
||||
Serial.println("PWM Slave started!");
|
||||
sei(); // включаем прерывания
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
if (tran_end) {
|
||||
tran_end = false;
|
||||
Serial.println(" ");
|
||||
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
Serial.print(buffer[i], HEX); // вывод байтов в шестнадцатеричном формате
|
||||
Serial.print(" ");
|
||||
}
|
||||
if (buffer[0] == COMMAND_1) {
|
||||
timer1_on(frequency, dutycycle);
|
||||
} else if (buffer[0] == COMMAND_2) {
|
||||
timer1_off();
|
||||
} else if (buffer[0] == COMMAND_3) {
|
||||
frequency = (buffer[1] << 8) + buffer[2];
|
||||
} else if (buffer[0] == COMMAND_4) {
|
||||
frequency = (buffer[1] << 8) + buffer[2];
|
||||
} else if (buffer[0] == COMMAND_5) {
|
||||
dutycycle = buffer[1];
|
||||
} else if (buffer[0] == COMMAND_6) {
|
||||
dutycycle = buffer[1];
|
||||
}
|
||||
cnt = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user