pwm in MCU added
This commit is contained in:
parent
5f48849c4e
commit
1f53117577
|
@ -21,7 +21,7 @@ HEX = b15f.hex
|
|||
MCU = atmega1284p
|
||||
CFLAGS = -Wall -Wextra -std=c++14 -O3 -mmcu=$(MCU) -DF_CPU=$(F_CPU) $(DEBUG)
|
||||
LDFLAGS =
|
||||
OBJECTS = main.o spi.o mcp23s17.o tlc5615.o adu.o selftest.o global_vars.o usart.o requests.o interrupts.o
|
||||
OBJECTS = main.o spi.o mcp23s17.o tlc5615.o adu.o selftest.o global_vars.o usart.o requests.o interrupts.o pwm.o
|
||||
|
||||
|
||||
COMPILE = $(COMPILER_PATH) $(CFLAGS)
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
# Name: Makefile
|
||||
# Project: B15F (board15 Famulus Edition)
|
||||
# Author: Tristan Krause
|
||||
# Creation Date: 2019-03-22
|
||||
|
||||
# Umgebungseinstellungen
|
||||
COMPILER_PATH = C:\avr8-gnu-toolchain-win32_x86\bin\avr-g++.exe
|
||||
OBJCOPY_PATH = C:\avr8-gnu-toolchain-win32_x86\bin\avr-objcopy.exe
|
||||
|
||||
|
||||
OUTPUT = B15F.elf
|
||||
HEX = B15F.hex
|
||||
CFLAGS = -Wall -Wextra -std=c++14 -O3 -mmcu=atmega1284 -DF_CPU=20000000
|
||||
LDFLAGS =
|
||||
OBJECTS = main.o spi.o mcp23s17.o tlc5615.o adu.o selftest.o global_vars.o usart.o requests.o
|
||||
|
||||
|
||||
COMPILE = $(COMPILER_PATH) $(CFLAGS)
|
||||
|
||||
B15F: $(OBJECTS)
|
||||
@echo "Linking..."
|
||||
$(COMPILE) $(OBJECTS) -o $(OUTPUT) $(LDFLAGS)
|
||||
|
||||
$(OBJCOPY_PATH) -O ihex -R .eeprom -R .fuse -R .lock -R .signature $(OUTPUT) $(HEX)
|
||||
|
||||
help:
|
||||
@echo "This Makefile has the following rules:"
|
||||
@echo "make B15F .... to compile (default)"
|
||||
@echo "make clean ... to delete objects, elf and hex file"
|
||||
|
||||
clean:
|
||||
@echo "Cleaning..."
|
||||
rm -f $(OBJECTS) $(OUTPUT) $(HEX)
|
||||
|
||||
.cpp.o:
|
||||
$(COMPILE) -c $< -o $@
|
|
@ -6,6 +6,7 @@
|
|||
#include "selftest.h"
|
||||
#include "requests.h"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// deactivate WDT ENTIRELY!
|
||||
|
@ -28,8 +29,6 @@ int main()
|
|||
usart.init();
|
||||
usart.initRX();
|
||||
|
||||
|
||||
|
||||
// Hauptschleife, Verarbeitung der Requests
|
||||
while(1)
|
||||
{
|
||||
|
|
18
firmware/pwm.cpp
Normal file
18
firmware/pwm.cpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include "pwm.h"
|
||||
|
||||
void pwmSetFrequency(uint32_t freq)
|
||||
{
|
||||
TCCR0A = _BV(COM0B1) | _BV(WGM00) | _BV(WGM01);
|
||||
DDRB |= _BV(PB4);
|
||||
|
||||
uint16_t p_ideal = ceil(float(F_CPU) / (freq * 0x100));
|
||||
for(int8_t i = PWM_PRESCALER_COUNT - 1; i >= 0 && PWM_PRESCALERS[i] >= p_ideal; i--)
|
||||
TCCR0B = _BV(WGM02) | i; // set prescaler
|
||||
if(p_ideal)
|
||||
OCR0A = (uint8_t) (float(F_CPU) / (freq * PWM_PRESCALERS[TCCR0B & 0x07]));
|
||||
}
|
||||
|
||||
void pwmSetValue(uint8_t value)
|
||||
{
|
||||
OCR0B = value;
|
||||
}
|
13
firmware/pwm.h
Normal file
13
firmware/pwm.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef PWM_H
|
||||
#define PWM_H
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <math.h>
|
||||
|
||||
const uint16_t PWM_PRESCALERS[] = {0, 1, 8, 64, 256, 1024};
|
||||
const uint8_t PWM_PRESCALER_COUNT = sizeof(PWM_PRESCALERS) / sizeof(uint16_t);
|
||||
|
||||
void pwmSetFrequency(uint32_t);
|
||||
void pwmSetValue(uint8_t);
|
||||
|
||||
#endif // PWM_H
|
|
@ -216,3 +216,13 @@ void rqAdcDacStroke()
|
|||
usart.writeByte(USART::MSG_OK);
|
||||
usart.flush();
|
||||
}
|
||||
|
||||
void rqSetPwm()
|
||||
{
|
||||
usart.initTX();
|
||||
uint16_t value = usart.readByte();
|
||||
OCR0A = value;
|
||||
|
||||
usart.writeByte(USART::MSG_OK);
|
||||
usart.flush();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "global_vars.h"
|
||||
#include "selftest.h"
|
||||
#include "boardinfo.h"
|
||||
#include "pwm.h"
|
||||
|
||||
constexpr static uint8_t RQ_DISC = 0;
|
||||
constexpr static uint8_t RQ_TEST = 1;
|
||||
|
@ -20,6 +21,7 @@ constexpr static uint8_t RQ_AA0 = 10;
|
|||
constexpr static uint8_t RQ_AA1 = 11;
|
||||
constexpr static uint8_t RQ_ADC = 12;
|
||||
constexpr static uint8_t RQ_ADC_DAC_STROKE = 13;
|
||||
constexpr static uint8_t RQ_SET_PWM = 14;
|
||||
|
||||
uint8_t const rq_len[] = {
|
||||
/* RQ_DISC */ 1,
|
||||
|
@ -35,7 +37,8 @@ uint8_t const rq_len[] = {
|
|||
/* RQ_AA0 */ 3,
|
||||
/* RQ_AA1 */ 3,
|
||||
/* RQ_ADC */ 2,
|
||||
/* RQ_ADC_DAC_STROKE */ 9
|
||||
/* RQ_ADC_DAC_STROKE */ 9,
|
||||
/* RQ_SET_PWM */ 2
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -58,5 +61,6 @@ void rqAnalogWrite0(void);
|
|||
void rqAnalogWrite1(void);
|
||||
void rqAnalogRead(void);
|
||||
void rqAdcDacStroke(void);
|
||||
void rqSetPwm(void);
|
||||
|
||||
#endif // REQUESTS_H
|
||||
|
|
Loading…
Reference in a new issue