STM32单片机C语言
1、stdint.h简介
stdint.h 是从 C99 中引进的一个标准 C 库的文件
路径:D:\MDK5.34\ARM\ARMCC\include
大家都统一使用一样的标准,这样方便移植
配置MDK支持C99
位操作
如何给寄存器某个值赋值
举个例子:uint32_t temp = 0;
宏定义
带参数的宏定义
https://blog.csdn.net/xiaoyilong2007101095/article/details/77067686
去看一下宏定义
#define LED1(x) do{ x ? \HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_SET) : \HAL_GPIO_WritePin(LED1_GPIO_PORT, LED1_GPIO_PIN, GPIO_PIN_RESET); \}while(0)
建议大家使用 do{ ... }while(0)
来 构造宏定义,这样不会受到大括号、分号、运算符优先级等的影响,总是会按你期望的方式调用运行!
假设LED1(1)
参数为真 ,那么就执行第一句,如果是LED1(0)
,那么就执行第二句
条件编译
让编译器只对满足条件的代码进行编译,不满足条件的不参与编译!
extern声明
类型别名(typedef)
为现有数据类型创建一个新的名字,或称为类型别名,用来简化变量的定义
/*typedef 现有类型 新名字*/
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
类型别名应用
下面是结构体应用typedef
struct GPIO_TypeDef
{__IO uint32_t CRL;__IO uint32_t CRH;//…
};
struct GPIO_TypeDef gpiox; //声明typedef struct
{__IO uint32_t CRL;__IO uint32_t CRH;//…
} GPIO_TypeDef;
GPIO_TypeDef gpiox; //声明
在C中定义一个结构体类型要用typedef:
typedef struct Student
{int a;
}Stu;
于是在声明变量的时候就可:Stu stu1;
如果没有typedef
就必须用struct Student stu1;
来声明
这里的Stu
实际上就是struct Student
的别名。
另外这里也可以不写Student
(于是也不能struct Student stu1;
了)
typedef struct
{int a;
}Stu;
但在c++里很简单,直接
struct Student
{int a;
};
于是就定义了结构体类型Student
,声明变量时直接Student stu2;
2其次:
在c++中如果用typedef的话,又会造成区别:
struct Student
{int a;
}stu1;//stu1是一个变量
typedef struct Student2
{int a;
}stu2;//stu2是一个结构体类型
使用时可以直接访问stu1.a
但是stu2则必须先 stu2 s2;
然后 s2.a=10;
3 掌握上面两条就可以了,不过最后我们探讨个没多大关系的问题
如果在c程序中我们写:
typedef struct
{int num;int age;
}aaa,bbb,ccc;
结构体
由若干基本数据类型集合组成的一种自定义数据类型,也叫聚合类型
struct 结构体名
{成员列表;
} 变量名列表(可选);
struct student
{char *name; /* 姓名 */int num; /* 学号 */int age; /* 年龄 */char group; /* 所在学习小组 */float score; /* 成绩 */
}stu1, stu2;
应用举例(定义&使用)
struct student
{char *name; /* 姓名 */int num; /* 学号 */int age; /* 年龄 */char group; /* 所在学习小组 */float score; /* 成绩 */
};struct student stu3,stu4;
stu3.name = "张三";
stu3.num = 1;
stu3.age = 18;
stu3.group = 'A';
stu3.score = 80.9;
用上typedef
typedef struct
{char *name; /* 姓名 */int num; /* 学号 */int age; /* 年龄 */char group; /* 所在学习小组 */float score; /* 成绩 */
}student;student stu3,stu4;
stu3.name = "张三";
stu3.num = 1;
stu3.age = 18;
stu3.group = 'A';
stu3.score = 80.9;
应用举例(ST源码,使用类型别名)
typedef struct
{uint32_t Pin ; /* 引脚号 */uint32_t Mode ; /* 工作模式 */uint32_t Pull ; /* 上下拉 */uint32_t Speed ; /* IO速度 */
} GPIO_InitTypeDef;
指针
指针就是内存的地址
指针变量是保存了指针的变量
类型名 * 指针变量名
char * p_str = “This is a test!”;
//*p_str:取p_str 变量的值
//p_str是地址,p_str[0] = 'T'
//&p_str:取p_str变量的地址