Add command and play around with shit. Change atoi() with strtol() because it is safer.

§
This commit is contained in:
Marco 2022-05-12 18:25:38 +02:00
parent 6d8e00b162
commit ee8f6387ad
4 changed files with 44 additions and 18 deletions

View File

@ -25,8 +25,6 @@ void cmd_collect_char(char c)
if (c == 0xD) { if (c == 0xD) {
printf("\n\r");
/* Null-terminate the string */ /* Null-terminate the string */
command.buf[command.index-1] = '\0'; command.buf[command.index-1] = '\0';
cmd_handle(&command); cmd_handle(&command);
@ -48,15 +46,41 @@ void cmd_handle(cmd_t *command)
void cmd_handle_set(char *op1, char *op2) void cmd_handle_set(char *op1, char *op2)
{ {
int val; long int val;
char *end;
if (0 == strcmp(op1, "t0_top")) { if (0 == strcmp(op1, "t0_top")) {
val = atoi(op2); val = strtol(op2, &end, 10);
if (val) { if (op2 != end) {
OCR0A = val; OCR0A = val;
printf("Setting Timer0 TOP to %i\r\n", val); printf("Setting Timer0 TOP to %li\r\n", val);
} }
else else
printf("No valid value for t0_top given\n\r"); printf("No valid value for t0_top given\n\r");
} }
else if (0 == strcmp(op1, "t0_ps")) {
val = strtol(op2, &end, 10);
TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02));
switch (val)
{
case 0:
TCCR0B |= (1 << CS00);
break;
case 8:
TCCR0B |= (1 << CS01);
break;
case 64:
TCCR0B |= (1 << CS00) | (1 << CS01);
break;
case 256:
TCCR0B |= (1 << CS02);
break;
case 1024:
TCCR0B |= (1 << CS02) | (1 << CS00);
break;
default:
printf("No valid prescaler value.\n\r");
break;
}
}
} }

View File

@ -20,7 +20,7 @@ int main()
while (1) { while (1) {
if ((PORTB & (1<<PB2))>1) { if ((PORTB & (1<<PB2))>1) {
_delay_us(10); _delay_us(1);
PORTB &= ~(1<<PB2); PORTB &= ~(1<<PB2);
} }
} }
@ -33,10 +33,3 @@ ISR(TIMER0_COMPA_vect)
PORTB |= (1 << PB2); PORTB |= (1 << PB2);
} }
ISR(USART1_RX_vect)
{
char u;
u = UDR1;
cmd_collect_char(u);
}

View File

@ -9,8 +9,8 @@ void initCtcTimer0(void)
/* Enable Counter0 Compare Match A Interrupt */ /* Enable Counter0 Compare Match A Interrupt */
TIMSK0 |= (1 << OCIE0A); TIMSK0 |= (1 << OCIE0A);
/* Select clock. Prescaler of 1024 */ /* Select clock. Prescaler of 8 */
TCCR0B |= (1 << CS02) | (1 << CS00); TCCR0B |= (1 << CS01);
/* Use CTC Mode */ /* Use CTC Mode */
TCCR0A |= (1 << WGM01); TCCR0A |= (1 << WGM01);
@ -18,7 +18,7 @@ void initCtcTimer0(void)
/* /*
* OCR0A contains TOP value for counter: * OCR0A contains TOP value for counter:
*/ */
OCR0A = 78; OCR0A = 25;
} }
void initOverflowTimer0(void) void initOverflowTimer0(void)

View File

@ -1,6 +1,8 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h>
#include "include/uart.h" #include "include/uart.h"
#include "include/commands.h"
static FILE stream = FDEV_SETUP_STREAM(uart_printf, NULL, _FDEV_SETUP_WRITE); static FILE stream = FDEV_SETUP_STREAM(uart_printf, NULL, _FDEV_SETUP_WRITE);
@ -40,3 +42,10 @@ int uart_printf(char var, FILE *stream)
return 0; return 0;
} }
ISR(USART1_RX_vect)
{
char u;
u = UDR1;
cmd_collect_char(u);
}