1.数据类型
stdint关键字是32中专门用的。
2.一些关键字
关键字:#define
用途:用一个字符串代替一个数字,便于理解,防止出错;提取程序中经常出现的参数,便于快速修改
定义宏定义:
#define ABC 12345 //将12345定义为ABC
引用宏定义:
int a=ABC;//等效于int a=12345;
关键字:typedef
用途:将一个比较长的变量类型名换个名字,便于使用
定义typedef
typedef unsigned char uint8_t; //将“unsigned char”定义为“uint8_t"
引用typedef
uint8_t a; //等效于unsigned char a;
3.结构体
(1)一般形式
struct <结构体名>
{
<成员类型> <成员名>;
<成员类型> <成员名>;
......
<成员类型> <成员名>;
};
struct student 结构体名字是student
{
int num; //学号
char name[10]; //年龄
char sex; //性别
};
struct student stu1,stu2; //此时struct student这个整体类似于int的作用
//定义了两个类型为student结构类型的两个结构体变量
// stu1 stu2
(2)变量引用:
StructName
是结构体类型名,不是指针,不能用->
操作符。
pStructName
是一个指向结构体的指针变量,->
操作符就是专门设计用来让指针访问其所指向结构体的成员,所以可以写成pStructName->x = 'A';
。
struct{char x; int y; float z;} c; //表示c这个结构体里面有char类型的x;
// int类型的y ; float类型的z;
//.........结构体的引用...........//
c.x='a'; //c下面的x等于a
c.y = 66; //c下面的y等于66
c.z=1.23; //c下面的z等于1.23
// 特殊用法 //
typedef struct{char x; int y; float z;} StructName_t; //这样原来的可以变成
StructName_t c;
*实例:GPIO
GPIO_InitTypeDef是“人”,一个总体的定义
GPIO_InitStruct是“张三”,特定的某个人*
GPIO_InitTypeDef GPIO_InitStruct; //现在有一个人,叫张三
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP ; //张三的鼻子长什么样
GPIO_InitStruct.GPIO_Pin= GPIO_Pin_0 | GPIO_Pin_1 ; // 张三的嘴巴长什么样
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz; //张三的脖子长什么样
GPIO_Init(GPIOA, &GPIO_InitStruct); //把张三数据提交到GPIOA
注:上述类型是一个结构体指针
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct) //调用GPIO_StructInit函数时,你需要传入一个指向GPIO_InitTypeDef结构体的指针。
{ //借助这个指针,函数能够访问并修改该结构体的成员变量。
/* Reset GPIO init structure parameters values */
GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
}
调用GPIO_StructInit
函数时,你需要传入一个指向GPIO_InitTypeDef
结构体的指针。借助这个指针,函数能够访问并修改该结构体的成员变量。
4.枚举
enum{MONDAY = 1,TUESDAY = 2,WEDNESDAY = 3} week;
//week取值变量的范围只能是括号里的值
//TUESDAY和WEDNESDAY的2,3可以不用写,顺序累加的数值程序会自动填入
enum{MONDAY = 1,TUESDAY ,WEDNESDAY } week;
//同样的一般用typedef改一下名字
typedef enum{MONDAY = 1,TUESDAY = 2,WEDNESDAY = 3} week_t;
week_t week;
week = MONDAY; //week=1;
week = TUESDAY; //week=2;
week = WENDNESDAY; //week = 3;