cancel
Showing results for 
Search instead for 
Did you mean: 

how to send two strings via usart

adeemkazi92
Associate II
Posted on September 15, 2015 at 14:43

i am currently working on the usart example of stm32f07

however this example sends only one string

i wanted to know how to send two strings at different press of buttons

#stm-32f07
16 REPLIES 16
adeemkazi92
Associate II
Posted on September 30, 2015 at 12:28

thanks for the fast reply

i didnt get you fully though.

as my knowledge of c is novice.

can you elaborate  please.

that would be of much help

adeemkazi92
Associate II
Posted on September 30, 2015 at 14:35

could you please help me with the code sir!!

please!!

ive been stuck here for more than 3 weeks now.

its really frustating.

and can u please suggest any reading material related to stm32 usart usage.

thanx in advance

Posted on September 30, 2015 at 16:14

Try this, a quick blind hack of something that should at least function

#include <
stdio.h
>
#include ''main.h''
static const char String1[] = ''ENTER THE OPERATION

'';
static const char String2[] = ''1 FOR ADDITION 2 FOR SUBTRACTION 

'';
static const char String3[] = ''ENTER 1ST NUMBER

'';
static const char String4[] = ''ENTER 2ND NUMBER

'';
int val;
int x;
int y;
int ANS;
void USART1Configuration(void);
void OutString(const char *s);
char GetChar(void);
void Demo(void);
void START_1(void);
void START_2(void);
void CALCULATE(void);
void GET_NUMBER1(void);
void STORE_NUMBER1(void);
void GET_NUMBER2(void);
void STORE_NUMBER2(void);
void GET_OPERATION(void);
void DISPLAY(void);
int main(void)
{
if (SysTick_Config(SystemCoreClock / 1000))
{
while(1);
}
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
USART1Configuration();
while(1)
{
Demo();
}
}
void USART1Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void OutString(const char *s)
{
while(*s) // Not NUL
{
if (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET) // if it's empty
USART_SendData(USART1, *s++); // Out char, and advance
}
}
char GetChar(void)
{
char ch;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // wait while no data
ch = (char)USART_ReceiveData(USART1);
return(ch);
}
void Demo(void)
{
START_1();
START_2();
GET_OPERATION();
GET_NUMBER1();
STORE_NUMBER1();
GET_NUMBER2();
STORE_NUMBER2();
CALCULATE();
DISPLAY();
}
void START_1(void)
{
OutString(String1);
}
void START_2(void)
{
OutString(String2);
}
void GET_OPERATION(void)
{
while(1)
{
val = GetChar();
if ((val == '1') || (val == '2')) // A valid response?
return;
}
}
void GET_NUMBER1(void)
{
OutString(String3);
}
void STORE_NUMBER1(void)
{
while(1)
{
x = GetChar();
if ((x >= '0') && (x <= '9')) // A valid digit?
{
x = x - '0'; // Convert ASCII digit to decimal
return;
}
}
}
void GET_NUMBER2(void)
{
OutString(String4);
}
void STORE_NUMBER2(void)
{
while(1)
{
y = GetChar();
if ((y >= '0') && (y <= '9')) // A valid digit?
{
y = y - '0'; // Convert ASCII digit to decimal
return;
}
}
}
void CALCULATE(void)
{
switch(val)
{
case '1': ANS = x + y; break;
case '2': ANS = x - y; break;
default: ANS = 0; break;
}
}
void DISPLAY(void)
{
char Buffer[16];
sprintf(Buffer, ''%d

'', ANS); // Convert to ASCII string could be value between -9 and 18
OutString(Buffer);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
adeemkazi92
Associate II
Posted on September 30, 2015 at 17:19

i dont have a kit with me right now.

but looks like this should work.

ill keep you updated.

once again thank you sir.

i owe you one

adeemkazi92
Associate II
Posted on October 01, 2015 at 07:32

thank you very much clive.just one last question. how do i modify to get more than one digit as input.

adeemkazi92
Associate II
Posted on October 01, 2015 at 11:37

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6YG&d=%2Fa%2F0X0000000bqX%2FTCtVWH6eFC8LCaejuIaPO3VLPKufb5drVb2zEk5gPAY&asPdf=false
Posted on October 01, 2015 at 19:14

You still seem to mix the appropriate use of if and while.

Use the get character function to build a string of input, wait for the ENTER key, or however you want to delimit the input. Use sscanf() or atoi() to convert the number, or multiple the current value by 10 as you add digits.

Doing the math a digit at a time is a monumental step backward. The CPU can hold 32-bit numbers and do the math directly on them, you take that path. The multiplication and division is then trivial. Stop making life harder for yourself.

When inputting digits in ASCII

int current = 0;

...

current = (current * 10) + (newchar - '0'); // a simple powers of 10 exercise

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..