Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Cortex M-3 sending commands to switch leds

Status
Not open for further replies.

argens

Newbie level 6
Joined
Sep 19, 2014
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,388
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:

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!
 

Hi,

Please give me a hint (but not a solution).

Now you compare a single byte as command.

But "led1on" is mord than a single byte....

Klaus
 

@KlausST: Thanks for the quick reply.

I got to this point, that is why I try to receive the single char(s) into a receiver buffer and afterwards compare the buffer to the word ("led1on"):

Code:
if (strncmp (t_user, "led1on", 6) == 0)

to


Code:
if (strncmp (buffer_user, "led1on", 6) == 0)

But changing the t_user into buffer_user isn't helping.

I am also aware, that the command "string" is not working in this case :/
 

Hi,

"Led1on" is not received immediately.
Expect it to be received byte by byte:
"L"
"Le"
"Led"
"Led1"
"Led1o"
"Led1on"
...and expect that garbage is trailing like "ztrled1o" (you see there is still one byte of the message missing..

Often one uses special bytes to indicate that the message is finished.
Like [Cr][Lf].

Enough hints?

Klaus
 
  • Like
Reactions: argens

    argens

    Points: 2
    Helpful Answer Positive Rating
any data receiving proccess should be done in interrupt or dma. Poling method not applicable.
 

@klausSt: I tried to do it as you showed and expect the code to work but unfortunatly nothing else, then the "else" is not showing up.

@Easyrider83: thanks for the hint, will try it out, when I get my lousy code up and working :).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top