generated from TDD-Templates/cmake_cpputest_template_avr
Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
|
b377eab7c0 | |
|
43d15247ad | |
|
a29e16963f | |
|
d3d69ca180 | |
|
05b6de17d0 | |
|
b2a3dcbed4 | |
|
49d8ca037b | |
|
c2ed2cead0 | |
|
4d229a973c | |
|
cc57692a1d |
|
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
|
|||
|
||||
# Use the fancy version substitution
|
||||
project(main
|
||||
VERSION 0.2.2
|
||||
VERSION 0.2.4
|
||||
DESCRIPTION "template for cmake + cpputest"
|
||||
LANGUAGES C CXX
|
||||
)
|
||||
|
@ -71,6 +71,7 @@ include_directories(
|
|||
./inc
|
||||
/usr/local/avr/include/avr
|
||||
/usr/local/avr/include
|
||||
/usr/local/avr/avr/include #for Linux
|
||||
)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
|
|
@ -15,9 +15,11 @@ set(AVR_MCU attiny13a)
|
|||
#set(AVR_MCU avr64dd28) # Newer DX series, avrxmega2
|
||||
|
||||
#set(F_CPU 16000000UL)
|
||||
#set(F_CPU 8000000)
|
||||
set(F_CPU 9600000)
|
||||
#set(F_CPU 4800000)
|
||||
#set(F_CPU 8000000UL)
|
||||
#set(F_CPU 9600000UL)#AVR without prescaler
|
||||
set(F_CPU 1200000UL) #AVR (9.6MHz) with prescaler 8DIV
|
||||
#set(F_CPU 4800000UL) #AVR without prescaler
|
||||
#set(F_CPU 600000UL) #AVR (4.8MHz) with prescaler 8DIV
|
||||
|
||||
add_compile_definitions(F_CPU=${F_CPU})
|
||||
# add_compile_definitions(MCU=atmega328p)
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
#define SPEED_PIN PB2 // Pin 7/ADC1
|
||||
#define BUTTON_PIN PB4 // Pin 3 - Button input
|
||||
|
||||
#define MOTOR_PULSE 1 //uS motor base pulse
|
||||
#define MOTOR_PULSE 6 //uS motor base pulse
|
||||
|
||||
|
||||
/*The timing of "ticks" is dependent on the AVR timer's counter register
|
||||
|
|
|
@ -3,7 +3,7 @@ add_executable(${PROJECT_NAME}
|
|||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
#RegEdit
|
||||
RegEdit
|
||||
#timer
|
||||
)
|
||||
|
||||
|
@ -36,10 +36,11 @@ add_custom_target(bin ALL
|
|||
endif()
|
||||
|
||||
|
||||
# Setup for default 9.6MHz
|
||||
if(NOT TARGET upload)
|
||||
# Upload command (adjust according to your programmer)
|
||||
add_custom_target(upload ALL
|
||||
COMMAND avrdude -c ${PROGRAMMER} -P ${PORT} -p ${AVR_MCU} -B 125kHz -U flash:w:${CMAKE_PROJECT_NAME}.hex
|
||||
COMMAND avrdude -c ${PROGRAMMER} -P ${PORT} -p ${AVR_MCU} -B 125kHz -U lfuse:w:0x6a:m -U hfuse:w:0xFF:m -U lock:w:0xFF:m -U flash:w:${CMAKE_PROJECT_NAME}.hex
|
||||
DEPENDS hex
|
||||
)
|
||||
endif()
|
||||
|
|
26
src/main.c
26
src/main.c
|
@ -1,8 +1,10 @@
|
|||
#ifndef F_CPU
|
||||
#define F_CPU 4800000UL
|
||||
#error "F_CPU not defined: defaulting to 9.6MHz"
|
||||
#define F_CPU 1200000UL
|
||||
#endif
|
||||
|
||||
#include "main.h"
|
||||
//#include "ADC.h"
|
||||
#include <avr/eeprom.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
@ -49,6 +51,7 @@ int main() {
|
|||
while (1) {
|
||||
UpdateButtonOutput(&btn1);
|
||||
UpdateButtonInput(&btn1);
|
||||
//MotorMoveTo(0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -85,6 +88,7 @@ void InitProg(void) {
|
|||
|
||||
uint8_t ReadSpeed(void) {
|
||||
// Initialize ADC
|
||||
|
||||
ADMUX = (0 << MUX1) | (1 << MUX0); // Select ADC1 (PB2)
|
||||
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
|
||||
|
||||
|
@ -94,10 +98,8 @@ uint8_t ReadSpeed(void) {
|
|||
|
||||
uint8_t val = (uint8_t)(ADC >> 2);
|
||||
|
||||
// We want to set a minimum acceptable speed.
|
||||
if (val < 32) {
|
||||
val = 32;
|
||||
}
|
||||
//Map the speed value to the 100%-50% duty cycle range.
|
||||
//val = (uint8_t)( (float)val / 255.0 ) * 100;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -139,11 +141,12 @@ uint8_t MotorGetSavedPos(void) {
|
|||
|
||||
void MotorMoveTo(uint8_t target) {
|
||||
|
||||
uint8_t on_delay = ReadFader();
|
||||
uint8_t off_delay = 255 - on_delay;
|
||||
uint8_t on_delay = ReadSpeed();
|
||||
uint8_t pos = (uint8_t)(ReadFader() >> 2);
|
||||
uint8_t idx =0;
|
||||
|
||||
while (diff(target, pos) > 8) {
|
||||
on_delay = ReadSpeed();
|
||||
pos = (uint8_t)(ReadFader() >> 2);
|
||||
if (target > pos) {
|
||||
MotorMove(1);
|
||||
|
@ -152,12 +155,12 @@ void MotorMoveTo(uint8_t target) {
|
|||
}
|
||||
|
||||
// The delay ratio controlls the PWM waveforms.
|
||||
for(uint8_t i = 0; i < on_delay; i++){
|
||||
_delay_us(MOTOR_PULSE);
|
||||
for (idx = 0; idx < on_delay; idx++) {
|
||||
_delay_us(MOTOR_PULSE);
|
||||
}
|
||||
MotorCoast();
|
||||
for(uint8_t i = 0; i < off_delay; i++){
|
||||
_delay_us(MOTOR_PULSE);
|
||||
for (;idx < 255; idx++) {
|
||||
_delay_us(MOTOR_PULSE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,6 +168,7 @@ void MotorMoveTo(uint8_t target) {
|
|||
}
|
||||
|
||||
// Using the compatable bool type.
|
||||
// The motor being used seems to stop working below a 50% duty cycle.
|
||||
void MotorMove(uint8_t fwd) {
|
||||
if (fwd) {
|
||||
PORTB |= (1 << PWM_PIN1);
|
||||
|
|
|
@ -13,5 +13,6 @@ target_link_libraries(test_ADC
|
|||
#Needed for the tests to function
|
||||
include_directories(
|
||||
/usr/local/avr/include/avr
|
||||
#/usr/local/avr/avr/include #for Linux
|
||||
#/usr/lib/avr/include/avr
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue