argens
Newbie level 6
Hello,
after a long brake I got it finally to learn a bit more about coding.
My next step is to learn, how to write a command via USART (I use Tera Term) which will turn on or off a given led.
I know, how to deal with single letters (example: letter "A" turns on led no. 1), but not how to do it with a sentence, like "led1on".
The code, I use is as follows:
For now I after each time I press a key on my keyboard, I get the answer from the "else" part: "Unknown communicate".
Please give me a hint (but not a solution).
Thanks for the help in advance!
after a long brake I got it finally to learn a bit more about coding.
My next step is to learn, how to write a command via USART (I use Tera Term) which will turn on or off a given led.
I know, how to deal with single letters (example: letter "A" turns on led no. 1), but not how to do it with a sentence, like "led1on".
The code, I use is as follows:
Code:
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
char t_user[] = "";
int buffer_user;
void send_char(char c)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, c);
}
void send_string(const char* s)
{
while (*s)
send_char(*s++);
}
int main(void)
{
GPIO_InitTypeDef gpio;
USART_InitTypeDef uart;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_2;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
USART_StructInit(&uart);
uart.USART_BaudRate = 115200;
USART_Init(USART2, &uart);
gpio.GPIO_Pin = GPIO_Pin_13;
gpio.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &gpio);
GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|
GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &gpio);
USART_Cmd(USART2, ENABLE);
while (1)
{
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE))
{
char c = USART_ReceiveData(USART2);
t_user[buffer_user] = c;
if (strncmp (t_user, "led1on", 6) == 0)
{
send_string("Received communicate: Led1on!\r\n");
GPIO_SetBits(GPIOC, GPIO_Pin_0);
}
else if (strncmp (t_user, "led1off", 7)== 0)
{
send_string("Received communicate: led1off!\r\n");
GPIO_ResetBits(GPIOC, GPIO_Pin_0);
}
else if (strncmp (t_user, "led2on", 6) == 0)
{
send_string("Received communicate: led2on!\r\n");
GPIO_SetBits(GPIOC, GPIO_Pin_1);
}
else if (strncmp (t_user, "led2off", 7) == 0)
{
send_string("Received communicate: led2off!\r\n");
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
}
else
{
send_string("Unknown communicate:(\r\n");
}
buffer_user++;
//c = 0;
}
}
}
For now I after each time I press a key on my keyboard, I get the answer from the "else" part: "Unknown communicate".
Please give me a hint (but not a solution).
Thanks for the help in advance!