1287 views|0 replies

1663

Posts

0

Resources
The OP
 

Microcontroller programming using C library functions [Copy link]

1.printf function

This is very simple. Just redirect the printf function.

This is the STM8L microcontroller code. Note: If it is an IAR compiler, you need to enable library-FULL

It can be used on msp430, stm32, and stm8l. Replace 1 and 2 with the functions of the corresponding microcontroller.

int fputc(int ch, FILE *f)//printf
{
USART_SendData8(USART1, (uint8_t) ch);//1

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}//2

return ch;
}
2. memset() function

memset(void *, int, size_t);//IAR environment
*memset (void *s, char val, int n);//keil environment
Here we only talk about the use in programming in MCU. The above are the memset functions of two compilation environments, and the functions they implement are the same.

The first parameter: points to a starting address

The second parameter: any value (0~255) value

The third parameter: length n (n consecutive bytes from the starting address)

Set all n consecutive bytes from the starting address to value

We usually use it to clear the array to 0, which is very convenient, for example:

memset(RevBuffer,0,sizeof(RevBuffer)); //Clear array
RevBuffer: character array
3.memcpy function

void *memcpy(void*dest, const void *src, size_t n);

Starting from the starting position of the memory address pointed to by the source src, copy n bytes of data to the starting position of the memory address pointed to by the destination dest.

char buf[]="123456";
char buf2[5];
memcpy(buf2,buf,3);
printf("%s\r\n",buf2);

Output: 123
4.strcpy function

strcpy is a standard library function of C language. strcpy copies the string starting from the src address and containing the '\0' terminator to the address space starting at dest. The return value is of type char*.

Prototype declaration: char *strcpy(char* dest, const char *src);

Header files: #include <string.h> and #include <stdio.h>

Function: Copy the string starting from the address src and containing the NULL terminator to the address space starting from dest

Note: The memory areas pointed to by src and dest cannot overlap and dest must have enough space to accommodate the string in src.

Returns a pointer to dest.

char buf[]="123";
char buf1[5];
strcpy(buf1,buf);
printf("%s\r\n",buf1);
5. atoi function (note the reference #include <stdlib.h>)

(stands for ascii to integer) is a function that converts a string into an integer.

int atoi(const char *nptr);

*nptr: Convert the string pointed to by the parameter nptr to an integer (type is int).

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n;
char *str = "1234567";
n = atoi(str);
printf("n=%d\n",n) ;
return 0;

}

This post is from Microcontroller MCU
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list