diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2022-01-05 02:18:16 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2022-01-05 02:18:16 +0100 |
commit | d9b9b3e71397a2da9e16cbee75c4954105237363 (patch) | |
tree | 79d4be62e5357868b5af75301017553d3e8b692c | |
parent | 01227e76785845dae377a8808146ff51209f727a (diff) | |
download | analog_instruments-d9b9b3e71397a2da9e16cbee75c4954105237363.tar.gz |
p command working to change PWM
refactgored pwm code
-rw-r--r-- | src/cmd.c | 20 | ||||
-rw-r--r-- | src/cmd.h | 13 | ||||
-rw-r--r-- | src/helpers.c | 27 | ||||
-rw-r--r-- | src/helpers.h | 16 | ||||
-rw-r--r-- | src/main.c | 44 | ||||
-rw-r--r-- | src/pwm.c | 51 | ||||
-rw-r--r-- | src/pwm.h | 21 |
7 files changed, 145 insertions, 47 deletions
@@ -5,6 +5,8 @@ */ #include "cmd.h" +#include "helpers.h" +#include <stdint.h> 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[] ) { @@ -10,6 +10,8 @@ #include <stdint.h> #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 <himself@jonasgunz.de> + * 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 <himself@jonasgunz.de> + * License: All rights reserved. + */ + +#ifndef _HELPERS_H_ +#define _HELPERS_H_ + +#include <stdint.h> + +uint8_t hex_to_byte(char c[]); + +uint8_t hex_to_halfbyte(char c); + +#endif /* _HELPERS_H_ */ @@ -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<<PINB0); - else - PORTB |= (1<<PINB0); - - sei(); -} - -ISR(TIMER2_OVF_vect) { - cli(); - pb0_thresh ++; - sei(); -} +#include "pwm.h" int main(void) { - char c; - cli(); - /* Pins */ - DDRB |= (1<<PINB0); - - /* Enable TIMER0 with interrupt, no prescaler */ - TCCR0 |= (1<<CS00); - TIMSK |= (1<<TOIE0); - - /* Enable TIMER2 with interrupt, Clk divide by 1024 */ - /* - TCCR2 |= (7<<CS20); - TIMSK |= (1<<TOIE2); - */ - - /* Uart */ uart_init(); - cmd_init(); + pwm_init(); /* Go! */ sei(); @@ -64,6 +25,7 @@ int main(void) { uart_putstring("START\r\n"); while(1){ + char c; if( uart_getchar(&c) ) continue; diff --git a/src/pwm.c b/src/pwm.c new file mode 100644 index 0000000..787fcae --- /dev/null +++ b/src/pwm.c @@ -0,0 +1,51 @@ +/* + * src/pwm.c + * (c) 2022 Jonas Gunz <himself@jonasgunz.de> + * 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<<PINB0); + else + PORTB |= (1<<PINB0); + + sei(); +} + +ISR(TIMER2_OVF_vect) { + cli(); + pb0_thresh ++; + sei(); +} + +void pwm_init() { + /* Pins */ + DDRB |= (1<<PINB0); + + /* Enable TIMER0 with interrupt, no prescaler */ + TCCR0 |= (1<<CS00); + TIMSK |= (1<<TOIE0); + + /* Enable TIMER2 with interrupt, Clk divide by 1024 */ + /* + TCCR2 |= (7<<CS20); + TIMSK |= (1<<TOIE2); + */ +} + +void pwm_set_pin(uint8_t _pin, uint8_t _duty) { + if(_pin == 0) + pb0_thresh = _duty; +} diff --git a/src/pwm.h b/src/pwm.h new file mode 100644 index 0000000..822e76d --- /dev/null +++ b/src/pwm.h @@ -0,0 +1,21 @@ +/* + * src/pwm.h + * (c) 2022 Jonas Gunz <himself@jonasgunz.de> + * License: All rights reserved. + */ + +#ifndef _PWM_H_ +#define _PWM_H_ + +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +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_ */ |