Motor-PWM, code formatting
This commit is contained in:
1 parent
2d50676a5e
commit
0860162c4f
2 files changed
+69
-45
No files matched your search
@@ -1,12 +1,13 @@
|
|||||||
|
//All CVs are 8-bit numbers (range 0 - 255)
|
||||||
#define CV_1 5 //Address - Basic Address
|
#define CV_1 5 //Address - Basic Address
|
||||||
#define CV_2 0 //V_min - Minimum Speed
|
#define CV_2 0 //V_min - Minimum Speed
|
||||||
#define CV_3 5 //Accelaration Rate - Acceleration formula: (CV_3*0.896)/(number of speed steps in use)
|
#define CV_3 5 //Acceleration Rate - Acceleration formula: (CV_3*0.896)/(number of speed steps in use)
|
||||||
#define CV_4 5 //Deceleration Rate - Deceleration formula: (CV_4*0.896)/(number of speed steps in use)
|
#define CV_4 5 //Deceleration Rate - Deceleration formula: (CV_4*0.896)/(number of speed steps in use)
|
||||||
#define CV_5 255 //V_max - Maximum Speed
|
#define CV_5 255 //V_max - Maximum Speed
|
||||||
#define CV_6 127 //V_mid - Middle Speed
|
#define CV_6 127 //V_mid - Middle Speed
|
||||||
#define CV_7 13 //Version No. - Decoder-Software version
|
#define CV_7 13 //Version No. - Decoder-Software version
|
||||||
#define CV_8 13 //Manufacturer - Manufacturer Identification (13 == "Public Domain & Do-It-Yourself Decoders").
|
#define CV_8 13 //Manufacturer - Manufacturer Identification (13 == "Public Domain & Do-It-Yourself Decoders").
|
||||||
#define CV_9 5 //PWM-Period - Defines the Motor PWM-Period
|
#define CV_9 0 //PWM-Period - Defines the Motor PWM-Period
|
||||||
const uint8_t CV_FUNCTION_ARRAY [256] = {
|
const uint8_t CV_FUNCTION_ARRAY [256] = {
|
||||||
// 31 30 29 28 - 27 26 25 24 -- 23 22 21 20 - 19 18 17 16 -- 15 14 13 12 - 11 10 9 8 -- 7 6 5 4 - 3 2 1 0 <- GPIO
|
// 31 30 29 28 - 27 26 25 24 -- 23 22 21 20 - 19 18 17 16 -- 15 14 13 12 - 11 10 9 8 -- 7 6 5 4 - 3 2 1 0 <- GPIO
|
||||||
// byte_3 -- byte_2 -- byte_1 -- byte_0
|
// byte_3 -- byte_2 -- byte_1 -- byte_0
|
||||||
|
|||||||
+66
-43
@@ -13,8 +13,59 @@
|
|||||||
#define SIZE_BYTE_ARRAY 5
|
#define SIZE_BYTE_ARRAY 5
|
||||||
#define SIZE_ACTIVE_FUNCTIONS 69
|
#define SIZE_ACTIVE_FUNCTIONS 69
|
||||||
#define DCC_INPUT_PIN 17
|
#define DCC_INPUT_PIN 17
|
||||||
|
#define MOTOR_PWM_FORWARD 21
|
||||||
|
#define MOTOR_PWM_REVERSE 22
|
||||||
|
#define PACKAGE_3_BYTES 0b11111111110000000000000000000000000001
|
||||||
|
#define PACKAGEMASK_3_BYTES 0b11111111111000000001000000001000000001
|
||||||
|
#define PACKAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
|
||||||
|
#define PACKAGEMASK_4_BYTES 0b11111111111000000001000000001000000001000000001
|
||||||
|
#define PACKAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
|
||||||
|
#define PACKAGEMASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
||||||
bool active_functions[SIZE_ACTIVE_FUNCTIONS] = {false};
|
bool active_functions[SIZE_ACTIVE_FUNCTIONS] = {false};
|
||||||
bool direction = 1;
|
bool direction = 1;
|
||||||
|
uint64_t last_bits = 0;
|
||||||
|
|
||||||
|
int8_t check_for_package() //function returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned
|
||||||
|
{
|
||||||
|
uint64_t package3Masked = last_bits & PACKAGEMASK_3_BYTES;
|
||||||
|
if (package3Masked == PACKAGE_3_BYTES) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
uint64_t package4Masked = last_bits & PACKAGEMASK_4_BYTES;
|
||||||
|
if (package4Masked == PACKAGE_4_BYTES) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
uint64_t package5Masked = last_bits & PACKAGEMASK_5_BYTES;
|
||||||
|
if (package5Masked == PACKAGE_5_BYTES) {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool readBit() {
|
||||||
|
busy_wait_us_32(87);
|
||||||
|
return !gpio_get(DCC_INPUT_PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeLastBit(bool bit) {
|
||||||
|
last_bits <<= 1;
|
||||||
|
last_bits |= bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
//start of transmission -> byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission
|
||||||
|
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
|
||||||
|
for (uint8_t i = 0; i < number_of_bytes; i++) {
|
||||||
|
byte_array[i] = last_bits >> (i * 9 + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
void init_pwm(uint gpio, uint8_t speed) {
|
||||||
|
uint32_t period = 126*500;
|
||||||
|
uint slice_num = pwm_gpio_to_slice_num(gpio);
|
||||||
|
pwm_set_wrap(slice_num, period);
|
||||||
|
pwm_set_gpio_level(gpio,0);
|
||||||
|
pwm_set_enabled(slice_num, true);
|
||||||
|
}
|
||||||
|
|
||||||
void set_speed(uint8_t speed) {
|
void set_speed(uint8_t speed) {
|
||||||
if (speed == 255) //Emergency Break
|
if (speed == 255) //Emergency Break
|
||||||
@@ -23,6 +74,16 @@ void set_speed(uint8_t speed) {
|
|||||||
} else //normal speed control ranges from 0 to 126
|
} else //normal speed control ranges from 0 to 126
|
||||||
{
|
{
|
||||||
printf("Speed: %u - Direction: %u\n", speed, direction);
|
printf("Speed: %u - Direction: %u\n", speed, direction);
|
||||||
|
uint16_t duty_cycle = speed*500;
|
||||||
|
if (direction)
|
||||||
|
{
|
||||||
|
pwm_set_gpio_level(MOTOR_PWM_REVERSE,1);
|
||||||
|
pwm_set_gpio_level(MOTOR_PWM_FORWARD,duty_cycle);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
pwm_set_gpio_level(MOTOR_PWM_FORWARD,1);
|
||||||
|
pwm_set_gpio_level(MOTOR_PWM_REVERSE,duty_cycle);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,11 +114,6 @@ void set_outputs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readBit() {
|
|
||||||
busy_wait_us_32(87);
|
|
||||||
return !gpio_get(DCC_INPUT_PIN);
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_active_functions(uint8_t function_number, uint8_t input_byte, uint8_t count) {
|
void update_active_functions(uint8_t function_number, uint8_t input_byte, uint8_t count) {
|
||||||
uint8_t mask = 0b00000001;
|
uint8_t mask = 0b00000001;
|
||||||
for (uint8_t i = 0; i < count; i++) {
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
@@ -198,47 +254,9 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[])
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set_outputs();
|
set_outputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t last_bits = 0;
|
|
||||||
#define PACKAGE_3_BYTES 0b11111111110000000000000000000000000001
|
|
||||||
#define PACKAGEMASK_3_BYTES 0b11111111111000000001000000001000000001
|
|
||||||
#define PACKAGE_4_BYTES 0b11111111110000000000000000000000000000000000001
|
|
||||||
#define PACKAGEMASK_4_BYTES 0b11111111111000000001000000001000000001000000001
|
|
||||||
#define PACKAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
|
|
||||||
#define PACKAGEMASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
|
|
||||||
|
|
||||||
int8_t check_for_package() {
|
|
||||||
uint64_t basepacketMasked = last_bits & PACKAGEMASK_3_BYTES;
|
|
||||||
if (basepacketMasked == PACKAGE_3_BYTES) {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
uint64_t package4Masked = last_bits & PACKAGEMASK_4_BYTES;
|
|
||||||
if (package4Masked == PACKAGE_4_BYTES) {
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
uint64_t package5Masked = last_bits & PACKAGEMASK_5_BYTES;
|
|
||||||
if (package5Masked == PACKAGE_5_BYTES) {
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeLastBit(bool bit) {
|
|
||||||
last_bits <<= 1;
|
|
||||||
last_bits |= bit;
|
|
||||||
}
|
|
||||||
|
|
||||||
//start of transmission -> byte_n(address byte) -> ... -> byte_0(error detection byte) -> end of transmission
|
|
||||||
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
|
|
||||||
for (uint8_t i = 0; i < number_of_bytes; i++) {
|
|
||||||
byte_array[i] = last_bits >> (i * 9 + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void gpio_callback_rise(unsigned int gpio, long unsigned int events) {
|
void gpio_callback_rise(unsigned int gpio, long unsigned int events) {
|
||||||
writeLastBit(readBit());
|
writeLastBit(readBit());
|
||||||
int8_t number_of_bytes = check_for_package();
|
int8_t number_of_bytes = check_for_package();
|
||||||
@@ -262,6 +280,11 @@ int main() {
|
|||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
gpio_init(DCC_INPUT_PIN);
|
gpio_init(DCC_INPUT_PIN);
|
||||||
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
|
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
|
||||||
|
sleep_ms(1000);
|
||||||
|
gpio_set_function(MOTOR_PWM_FORWARD, GPIO_FUNC_PWM);
|
||||||
|
gpio_set_function(MOTOR_PWM_REVERSE, GPIO_FUNC_PWM);
|
||||||
|
init_pwm(MOTOR_PWM_FORWARD, 50);
|
||||||
|
init_pwm(MOTOR_PWM_REVERSE, 100);
|
||||||
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise);
|
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise);
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user