嵌入式C程序使用技巧_第1页
嵌入式C程序使用技巧_第2页
嵌入式C程序使用技巧_第3页
嵌入式C程序使用技巧_第4页
嵌入式C程序使用技巧_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第第页嵌入式C程序使用技巧在学习和工作开发的时候,经常需要使用到各种各样不太常用的操作,这种情况一般是自己手动写一些小程序来处理。因为它们不太常用,所以经常用了又没保存,等到下一次在使用的时候又需要重写,这样的非常浪费时间和精力。

所以想在这里统一记录一下,以备下次重新使用。代码以实用为主,如果缺陷,欢迎指出。

1、十六进制字符转整型数字

功能:将16进制的字符串转换为10进制的数字。我是没有找到相应的库函数,所以参考网上的代码自己手动写了个函数来实现。

常用的函数有atoi,atol,他们都是将10进制的数字字符串转换为int或是long类型,所以在有些情况下不适用。

/*=============================================================================#

FileName:

hex2dec.cpp#

Desc:

Convert

a

hex

string

to

a

int

number#

Auth(or):

Caibiao

Lee#

Version:

#

LastChange:

2023-11-26

#

History:=============================================================================*/

#include

#include

#include

#include

int

c2i(char

ch)

{

//

如果是数字,则用数字的ASCII码减去48,

如果ch

=

'2'

,则

'2'

-

48

=

2

if(i(sdi)git(ch))

return

ch

-

48;

//

如果是字母,但不是A~F,a~f则返回

if(

ch

'F'

//

如果是大写字母,则用数字的ASCII码减去55,

如果ch

=

'A'

,则

'A'

-

55

=

10

//

如果是小写字母,则用数字的ASCII码减去87,

如果ch

=

'a'

,则

'a'

-

87

=

10

if(isalpha(ch))

return

isupper(ch)

?

ch

-

55

:

ch

-

87;

return

-1;

}

int

hex2dec(char

*hex)

{

int

len;

int

num

=

0;

int

(te)mp;

int

bits;

int

i;

char

str[64]

=

{0};

if(NULL==hex)

{

printf("input

para

error

");

return

0;

}

if(('0'==hex[0])

}else

{

strcpy(str,hex);

}

printf("input

num

=

%s

",str);

//

此例中

str

=

"1de"

长度为3,

hex是main函数传递的

len

=

strlen(str);

for

(i=0,

temp=0;

i

运行结果:

biao@ubuntu:~/test/flash$

./a.out

0x400input

num

=

400

value

hex

=

0x400

value

dec

=

1024

biao@ubuntu:~/test/flash$

2、字符串转整型

功能:将正常输入的16进制或是10进制的字符串转换为int数据类型。

/*=============================================================================#

FileName:

hex2dec.cpp#

Desc:

Convert

a

hex/dec

string

to

a

int

number#

Author:

Caibiao

Lee#

Version:

#

LastChange:

2023-12-03

#

History:=============================================================================*/#include

#include

#include

#include

int

String2int(char

*strChar){

int

len=0;

const

char

*pstrCmp1="0123456789ABCDEF";

const

char

*pstrCmp2="0123456789abcdef";

char

*pstr=NULL;

int

uiValue=0;

int

j=0;

unsigned

int

t=0;

int

i=0;

if(NULL==strChar)

return

-1;

if(0>=(len=strlen((const

char

*)strChar)))

return

-1;

if(NULL!=(pstr=st(rs)tr(strChar,"0x"))||NULL!=(pstr=strstr(strChar,"0X")))

{

pstr=(char

*)strChar+2;

if(0>=(len=strlen((const

char

*)pstr)))

return

-1;

for(i=(len-1);i>=0;i--)

{

if(pstr[i]>'F')

{

for(t=0;t

3、创建文件并填充固定数据

功能:创建固定大小的一个文件,并且把这个文件填充为固定的数据。

/*=============================================================================#

FileName:

CreateFile.cpp#

Desc:

创建固定大小的文件,然后填充固定的数据#

Author:

Caibiao

Lee#

Version:

#

LastChange:

2023-11-26

#

History:=============================================================================*/#include

#include

#include

#include

//#define

FILL_DATA_VALUE

0xff#define

FILL_DATA_VALUE

0x30

//char

0

int

c2i(char

ch)

{

if(i(sd)igit(ch))

return

ch

-

48;

if(

ch

'F'

if(isalpha(ch))

return

isupper(ch)

?

ch

-

55

:

ch

-

87;

return

-1;

}

int

hex2dec(char

*hex)

{

int

len;

int

num

=

0;

int

temp;

int

bits;

int

i;

char

str[64]

=

{0};

if(NULL==hex)

{

printf("input

para

error

");

return

0;

}

if(('0'==hex[0])

}else

{

strcpy(str,hex);

}

printf("input

num

=

%s

",str);

len

=

strlen(str);

for

(i=0,

temp=0;

i

运行结果:

biao@ubuntu:~/test/flash$

gcc

CreateFile.cpp

biao@ubuntu:~/test/flash$

lsa.out

CreateFile.cpp

hex2dec.cpp

main.cpp

out.binbiao@ubuntu:~/test/flash$

./a.out

./out.bin

0x10input

num

=

10

Need

To

Write

Data

Len

16

Fill

Data

Vale

=

0x30

biao@ubuntu:~/test/flash$

lsa.out

CreateFile.cpp

hex2dec.cpp

main.cpp

out.binbiao@ubuntu:~/test/flash$

vim

out.bin

1

0000000000000000

4、批量处理图片

功能:批处理将图片前面固定的字节数删除。

/*=============================================================================#

FileName:

Cu(tF)ile.cpp#

Desc:

批量处理,将图片的前面固定字节删除#

Author:

Caibiao

Lee#

Version:

#

LastChange:

2023-11-26

#

History:=============================================================================*/#include

#include

#include

#include

#include

#define

START_RE(AD)_POSI(TI)ON

128#define

PHOTO_START_TIME

83641//l_s32PhotoTime

=

92809;

int

Cut_file(char

*

InputFile){

FILE

*l_pFileInput

=

NULL;

FILE

*l_pFileOutput

=

NULL;

char

l_ars8OutputName[128]

=

{0};

unsigned

char

l_arru8TempData[1024]

=

{0};

int

l_s32Ret

=

0;

stat(ic)

unsigned

int

ls_u32Num

=

0;

if(NULL==

InputFile)

{

goto

ERROR;

}

//sprintf(l_ars8OutputName,"./outfile/_%s",

sprintf(l_ars8OutputName,"./outfile/00%d.jpg",ls_u32Num++);

//printf("out

file

name

%s

",l_ars8OutputName);

l_pFileInput

=

fopen(InputFile,"rb+");

if(NULL==l_pFileInput)

{

printf("input

file

open

error");

goto

ERROR;

}

l_pFileOutput

=

fopen(l_ars8OutputName,"w+");

if(NULL==l_pFileOutput)

{

printf("out

file

open

error");

goto

ERROR;

}

fseek(l_pFileInput,START_READ_POSITION,SEEK_SET);

while(!feof(l_pFileInput))

{

l_s32Ret

=

fread(l_arru8TempData,1,1024,l_pFileInput);

if(l_s32Ret

运行结果:

biao@ubuntu:~/test/photo$

gcc

CutFile.cpp

biao@ubuntu:~/test/photo$

lsa.out

CutFile.cpp

image

outfilebiao@ubuntu:~/test/photo$

./a.out

./image/1Y083642.jpg./image/1Y083714.jpg./image/1Y083747.jpg./image/1Y083820.jpg./image/1Y083853.jpg./image/1Y083925.jpg./image/1Y084157.jpg./image/1Y084228.jpg./image/1Y084301.jpg./image/1Y084334.jpg./image/1Y084406.jpg./image/1Y084439.jpg./image/1Y084711.jpg./image/1Y084742.jpg./image/1Y173524.jpg./image/1Y173556.jpg./image/1Y173629.jpg./image/1Y173702.jpg./image/1Y173933.jpg./image/1Y174004.jpg./image/1Y174244.jpg./image/1Y174315.jpg./image/1Y174348.jpg./image/1Y174420.jpg./image/1Y174454.jpg./image/1Y174733.jpgbiao@ubuntu:~/test/photo$

tree.├──

a.out├──

CutFile.cpp├──

image│

├──

1Y083642.jpg│

├──

1Y083714.jpg│

├──

1Y083747.jpg│

├──

1Y083820.jpg│

├──

1Y083853.jpg│

├──

1Y083925.jpg│

├──

1Y084157.jpg│

├──

1Y084228.jpg│

├──

1Y084301.jpg│

├──

1Y084334.jpg│

├──

1Y084406.jpg│

├──

1Y084439.jpg│

├──

1Y084711.jpg│

├──

1Y084742.jpg│

├──

1Y173524.jpg│

├──

1Y173556.jpg│

├──

1Y173629.jpg│

├──

1Y173702.jpg│

├──

1Y173933.jpg│

├──

1Y174004.jpg│

├──

1Y174244.jpg│

├──

1Y174315.jpg│

├──

1Y174348.jpg│

├──

1Y174420.jpg│

├──

1Y174454.jpg│

└──

1Y174733.jpg└──

outfile

├──

000.jpg

├──

0010.jpg

├──

0011.jpg

├──

0012.jpg

├──

0013.jpg

├──

0014.jpg

├──

0015.jpg

├──

0016.jpg

├──

0017.jpg

├──

0018.jpg

├──

0019.jpg

├──

001.jpg

├──

0020.jpg

├──

0021.jpg

├──

0022.jpg

├──

0023.jpg

├──

0024.jpg

├──

0025.jpg

├──

002.jpg

├──

003.jpg

├──

004.jpg

├──

005.jpg

├──

006.jpg

├──

007.jpg

├──

008.jpg

└──

009.jpg

2

directories,

54

filesbiao@ubuntu:~/test/photo$

运行前需要创建两个目录,image用来存放需要处理的图片,outfile用来存放处理过后的文件。这种处理文件批处理方式很暴力,偶尔用用还是可以的。

5、IO控制小程序

(嵌入式)设备系统一般为了节省空间,一般都会对系统进行裁剪,所以很多有用的命令都会被删除。在嵌入式设备中要调试代码也是比较麻烦的,一般只能看串口打印。现在写了个小程序,专门用来查看和控制(海思)Hi3520DV300(芯片)的IO电平状态。

/*=============================================================================#

FileName:

Hi3520_IO_CTRL.cpp#

Desc:

Hi3520DV300

IO

Write

and

Read#

Author:

Caibiao

Lee#

Version:

#

LastChange:

2023-11-30#

History:=============================================================================*/#include

#include

#include

"hstGpioAL.h"

int

PrintfInputTips(char

*ps8Name){

printf("===========

error!!!

========");

printf("usage

Write:

%s

GPIO

bit

value

",

ps8Name);

printf("usage

Read

:

%s

GPIO

bit

",

ps8Name);

printf("eg

Write

1

to

GPIO1_bit02

:

%s

1

2

1",

ps8Name);

printf("eg

Read

GPIO1_bit02

Value

:

%s

1

2

",

ps8Name);

printf("=============BT20==================")

printf("(USB)

HUB

GPIO_0_2

1_UP;

0_Down

");

printf("RESET_HD

GPIO_13_0

0_EN;

1_disEN");

printf("Power_HD

GPIO_13_3

1_UP;

0_Down

");

return

0;}

int

main(int

argc,

char

**argv){

if((3!=argc)

return

-1;

}

unsigned

char

l_u8GPIONum

=

0;

unsigned

char

l_u8GPIOBit

=

0;

unsigned

char

l_u8SetValue

=

0;

GPIO_GROUP_E

l_eGpioGroup;

GPIO_BIT_E

l_eBit;

GPIO_DATA_E

l_(eDa)ta;

l_u8GPIONum

=

atoi(argv[1]);

l_u8GPIOBit

=

atoi(argv[2]);

if(l_u8GPIONum

6、文件固定位置插入数据

在文件的固定位置插入固定的数据。

#include

#include

#include

#define

B(ASIC)_FILE_NAME

"./nandflash.bin"#define

UBOOT_FILE_NAME

"./u-boot.bin"#define

KERNEL_FILE_NAME

"./kernel.bin"#define

ROOTFS_FILE_NAME

"./rootfs.bin"#define

APP_FILE_NAME

"./app.bin"

#define

UBOOT_POSITION

0x00#define

KERNEL_POSITION

0x100000#define

ROOTFS_POSITION

0x500000#define

APP_POSITION

0x2700000

int

InsertData(FILE

*pfBasic,FILE

*psInsert,int

s32Position){

int

l_S32Ret

=

0;

unsigned

char

l_arru8Temp[1024]

=

{0xff};

fseek(pfBasic,s32Position,SEEK_SET);

fseek(psInsert,0,SEEK_SET);

while(1)

{

l_S32Ret

=

fread(l_arru8Temp,1,1024,psInsert);

if(l_S32Ret

>

0)

{

l_S32Ret

=

fwrite(l_arru8Temp,1,l_S32Ret,pfBasic);

if(l_S32Ret

InsertData(l_pfBasec,l_pfUboot,UBOOT_POSITION))

{

printf("line

%d

error

",__LINE__);

goto

ERROR;

}

if(0>

InsertData(l_pfBasec,l_pfKernel,KERNEL_POSITION))

{

printf("line

%d

error

",__LINE__);

goto

ERROR;

}

if(0>

InsertData(l_pfBasec,l_pfRootfs,ROOTFS_POSITION))

{

printf("line

%d

error

",__LINE__);

goto

ERROR;

}

if(0>

InsertData(l_pfBasec,l_pfApp,APP_POSITION))

{

printf("line

%d

error

",__LINE__);

goto

ERROR;

}

ERROR:

if(NULL!=l_pfBasec)

{

fclose(l_pfBasec);

l_pfBasec

=

NULL;

}

if(NULL!=l_pfUboot)

{

fclose(l_pfUboot);

l_pfUboot

=

NULL;

}

if(NULL!=l_pfKernel)

{

fclose(l_pfKernel);

l_pfKernel

=

NULL;

}

if(NULL!=l_pfRootfs)

{

fclose(l_pfRootfs);

l_pfRootfs

=

NULL;

}

if(NULL!=l_pfApp)

{

fclose(l_pfApp);

l_pfApp

=

NULL;

}

return

0;}

7、获取本地IP地址

在(linux)设备中获取本地IP地址可以使用下面的程序,支持最大主机有三个网口的设备,当然这个网卡数可以修改。

#include

#include

#include

#include

#include

int

get_local_ip(char

*ps8IpList){

struct

ifa(ddr)s

*ifAddrStruct;

char

l_s8IpAddr[INET_ADDRSTRLEN];

void

*tmpAdd

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论