From d9b9b3e71397a2da9e16cbee75c4954105237363 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Wed, 5 Jan 2022 02:18:16 +0100 Subject: p command working to change PWM refactgored pwm code --- src/cmd.c | 20 ++++++++++++++------ src/cmd.h | 13 +++++++++++++ src/helpers.c | 27 +++++++++++++++++++++++++++ src/helpers.h | 16 ++++++++++++++++ src/main.c | 44 +++----------------------------------------- src/pwm.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/pwm.h | 21 +++++++++++++++++++++ 7 files changed, 145 insertions(+), 47 deletions(-) create mode 100644 src/helpers.c create mode 100644 src/helpers.h create mode 100644 src/pwm.c create mode 100644 src/pwm.h (limited to 'src') diff --git a/src/cmd.c b/src/cmd.c index 43b9bfc..6e03c08 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -5,6 +5,8 @@ */ #include "cmd.h" +#include "helpers.h" +#include struct { uint8_t state; @@ -18,7 +20,7 @@ const struct { uint8_t argc; } cmd[_CMD_CNT] = { {0,cmd_err,0}, - {'p',cmd_set_pwm,2} + {'p',cmd_set_pwm,3} }; void cmd_init() { @@ -31,18 +33,22 @@ void cmd_tick(char _c) { if(_c == '\r') { /* if cmd is not cmd_err and argc do not match */ - if( cmd_state.cmd && cmd[cmd_state.cmd].argc != cmd_state.state - 1 ) { + if( cmd_state.cmd && ( cmd[cmd_state.cmd].argc != cmd_state.state - 1 ) ) { /* set cmd_err to _ERR_ARGC */ cmd_state.cmd = 0; cmd_state.argv[0] = _ERR_ARGC; } (*cmd[cmd_state.cmd].fkt)(cmd_state.argv); - cmd_state.state = 0; - cmd_state.cmd = 0; + cmd_state.state = 0; + cmd_state.cmd = 0; + cmd_state.argv[0] = 0; return; } + if(_c == '\n') + return; + if(!cmd_state.state) { cmd_state.state = 1; for( i=0; i<_CMD_CNT; i++ ) { @@ -67,8 +73,10 @@ void cmd_tick(char _c) { } void cmd_set_pwm(uint8_t _argv[]) { - uart_putchar(_argv[0]); - uart_putchar(_argv[1]); + uint8_t duty = hex_to_byte((char*)&(_argv[1])); + uint8_t pin = _argv[0] - 48; + + pwm_set_pin(pin, duty); } void cmd_err( uint8_t _argv[] ) { diff --git a/src/cmd.h b/src/cmd.h index c547f6e..e85093f 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -10,6 +10,8 @@ #include #include "uart.h" +#include "pwm.h" +#include "helpers.h" #define _CMD_CNT 2 #define _CMD_MAX_ARGC 4 @@ -21,8 +23,19 @@ void cmd_init(); void cmd_tick(char _c); +/** + * argc=3 + * + * 0: Pin Num + * 1-2: Dutycycle in HEX (00-FF) + */ void cmd_set_pwm(uint8_t _argv[]); +/** + * argc=1 + * + * 0: Error type + */ void cmd_err(uint8_t _argv[]); #endif diff --git a/src/helpers.c b/src/helpers.c new file mode 100644 index 0000000..b8c3373 --- /dev/null +++ b/src/helpers.c @@ -0,0 +1,27 @@ +/* + * src/helpers.c + * (c) 2022 Jonas Gunz + * License: All rights reserved. + */ + +#include "helpers.h" + +uint8_t hex_to_byte(char c[]) { + uint8_t ret = hex_to_halfbyte(c[0]); + ret += 16 * hex_to_halfbyte(c[1]); + + return ret; +} + +uint8_t hex_to_halfbyte(char c) { + uint8_t ret = 0; + + if ( c >= 48 && c <= 57) + ret = c-48; + else if (c >= 65 && c <= 70) + ret = c-55; + else if (c >= 97 && c <= 102) + ret = c-87; + + return ret; +} diff --git a/src/helpers.h b/src/helpers.h new file mode 100644 index 0000000..6395abc --- /dev/null +++ b/src/helpers.h @@ -0,0 +1,16 @@ +/* + * src/helpers.h + * (c) 2022 Jonas Gunz + * License: All rights reserved. + */ + +#ifndef _HELPERS_H_ +#define _HELPERS_H_ + +#include + +uint8_t hex_to_byte(char c[]); + +uint8_t hex_to_halfbyte(char c); + +#endif /* _HELPERS_H_ */ diff --git a/src/main.c b/src/main.c index 131b483..7e1939c 100644 --- a/src/main.c +++ b/src/main.c @@ -10,53 +10,14 @@ #include "uart.h" #include "cmd.h" - -uint8_t t0_ovf_cnt = 0; - -uint8_t pb0_thresh = 128; - -ISR(TIMER0_OVF_vect) { - cli(); - /*TCNT0 = (1<<7);*/ /* Hack-increase Interrupt trigger freq */ - - t0_ovf_cnt ++; - - if ( t0_ovf_cnt >= pb0_thresh ) - PORTB &= ~(1< + * License: All rights reserved. + */ + +#include "pwm.h" + +uint8_t t0_ovf_cnt = 0; + +uint8_t pb0_thresh = 128; + +ISR(TIMER0_OVF_vect) { + cli(); + /*TCNT0 = (1<<7);*/ /* Hack-increase Interrupt trigger freq */ + + t0_ovf_cnt ++; + + if ( t0_ovf_cnt >= pb0_thresh ) + PORTB &= ~(1< + * License: All rights reserved. + */ + +#ifndef _PWM_H_ +#define _PWM_H_ + +#include +#include +#include + +extern uint8_t t0_ovf_cnt; +extern uint8_t pb0_thresh; + +void pwm_init(); + +void pwm_set_pin(uint8_t _pin, uint8_t _duty); + +#endif /* _PWM_H_ */ -- cgit v1.2.3