std::function转函数指针
时间: 2023-12-15 16:03:26 浏览: 39
```cpp
#include <iostream>
#include <functional>
void foo() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
std::function<void()> func = foo; // 将函数foo赋值给std::function对象func
func(); // 调用std::function对象func,输出:Hello, World!
void (*funcPtr)() = func.target<void(*)()>(); // 将std::function对象func转换为函数指针
funcPtr(); // 通过函数指针调用函数foo,输出:Hello, World!
return 0;
}
```
在上面的示例中,我们首先定义了一个名为`foo`的函数。然后,我们使用`std::function`将函数`foo`赋值给了一个`std::function<void()>`类型的对象`func`。接下来,我们通过调用`func()`来调用`foo`函数。
要将`std::function`转换为函数指针,我们可以使用`target`成员函数。在示例中,我们使用`func.target<void(*)()>()`将`func`转换为函数指针,并将其赋值给`funcPtr`。然后,我们可以通过调用`funcPtr()`来调用`foo`函数。
相关问题
51单片机中用keil编写具体程序实现:1602液晶显示器显示DS18B20与HD-SR04数据,距离小于10cm时唤醒总系统,温度大于37度小于40度时蜂鸣器发响,温度大于34度小于等于37度时带动步进电机
以下是基于51单片机的keil编写的程序实现:
```c
#include <reg51.h>
#include <intrins.h>
#define LCD_data P0 //1602液晶数据线
sbit LCD_RS = P2^0; //1602液晶RS引脚
sbit LCD_RW = P2^1; //1602液晶RW引脚
sbit LCD_EN = P2^2; //1602液晶EN引脚
sbit DQ = P3^4; //DS18B20数据线
sbit TRIG = P3^5; //HC-SR04触发引脚
sbit ECHO = P3^6; //HC-SR04回响引脚
sbit BEEP = P3^7; //蜂鸣器引脚
sbit StepP = P1^0; //步进电机正转引脚
sbit StepN = P1^1; //步进电机反转引脚
sbit StepC = P1^2; //步进电机控制引脚
unsigned char dis[16]; //存储显示的字符
//延时函数
void Delay(unsigned int i)
{
while(i--);
}
//1602液晶写命令
void Write_Cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_data = cmd;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
Delay(10);
}
//1602液晶写数据
void Write_Data(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_data = dat;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
Delay(10);
}
//1602液晶初始化
void LCD_Init()
{
Write_Cmd(0x38); //设置16*2,5*7点阵,8位数据接口
Write_Cmd(0x0c); //开显示,光标不显示
Write_Cmd(0x06); //文字向右走,光标不动
Write_Cmd(0x01); //清屏
}
//DS18B20读取温度函数
float Read_Temp(void)
{
unsigned char tempdata[2];
unsigned int temp;
float t;
unsigned char i;
DQ = 1; //开始通讯
Delay(5);
DQ = 0;
Delay(80);
DQ = 1;
Delay(10);
if(DQ)
{
return 0;
}
Delay(50);
for(i = 0; i < 2; i++)
{
tempdata[i] = 0;
}
for(i = 0; i < 8; i++)
{
DQ = 0; //开始通讯
tempdata[0] >>= 1;
tempdata[1] >>= 1;
DQ = 1;
if(DQ)
{
tempdata[0] |= 0x80;
}
Delay(5);
}
for(i = 0; i < 8; i++)
{
DQ = 0; //开始通讯
tempdata[0] >>= 1;
tempdata[1] >>= 1;
DQ = 1;
if(DQ)
{
tempdata[1] |= 0x80;
}
Delay(5);
}
for(i = 0; i < 16; i++)
{
DQ = 0; //开始通讯
temp >>= 1;
DQ = 1;
if(DQ)
{
temp |= 0x8000;
}
Delay(5);
}
t = (float)temp / 16.0;
return t;
}
//HC-SR04读取距离函数
unsigned int Read_Distance(void)
{
unsigned int time;
TRIG = 0;
Delay(5);
TRIG = 1;
Delay(10);
TRIG = 0;
while(!ECHO);
time = 0;
while(ECHO)
{
time++;
Delay(5);
}
return time / 58;
}
//步进电机正转函数
void Step_Forward()
{
StepC = 1;
StepP = 0;
StepN = 1;
Delay(100);
StepC = 0;
Delay(100);
}
//步进电机反转函数
void Step_Backward()
{
StepC = 1;
StepP = 1;
StepN = 0;
Delay(100);
StepC = 0;
Delay(100);
}
//主函数
void main()
{
float temp;
unsigned int dist;
LCD_Init(); //1602液晶初始化
while(1)
{
temp = Read_Temp(); //读取温度
dist = Read_Distance(); //读取距离
//显示温度和距离
sprintf(dis, "T:%.2fC D:%dcm", temp, dist);
for(i = 0; i < 16; i++)
{
Write_Data(dis[i]);
}
if(dist < 10) //距离小于10cm时唤醒总系统
{
//唤醒总系统
}
if(temp > 37.0 && temp < 40.0) //温度大于37度小于40度时蜂鸣器响
{
BEEP = 1;
}
else
{
BEEP = 0;
}
if(temp > 34.0 && temp <= 37.0) //温度大于34度小于等于37度时带动步进电机
{
Step_Forward();
}
else
{
Step_Backward();
}
}
}
```
单片机控制电机正反转的c语言程序
单片机控制电机正反转的C语言程序通常会使用PWM控制技术来实现。首先,需要定义输入输出引脚和电机控制相关的参数,并初始化相关端口。然后编写一个函数来控制正转和反转的动作,可以根据需要设定不同的占空比来控制电机的转速。接着在主函数中,通过按键或其他输入方式来触发函数,从而实现电机的正反转控制。下面是一个简单的示例:
```c
#include <reg51.h>
sbit motorPinA = P1^0; // 电机A控制引脚
sbit motorPinB = P1^1; // 电机B控制引脚
sbit button = P2^0; // 按键引脚
void motorCW() // 电机正转函数
{
motorPinA = 1;
motorPinB = 0;
// 可以根据需要设置PWM占空比来控制转速
}
void motorCCW() // 电机反转函数
{
motorPinA = 0;
motorPinB = 1;
// 可以根据需要设置PWM占空比来控制转速
}
void main()
{
while(1)
{
if (button == 1) // 如果按键被按下
{
motorCW(); // 调用电机正转函数
// 延时一段时间或者通过其他方式控制持续时间
motorPinA = 0; // 关闭电机A控制引脚
motorPinB = 0; // 关闭电机B控制引脚
}
else if (button == 0) // 如果按键被释放
{
motorCCW(); // 调用电机反转函数
// 延时一段时间或者通过其他方式控制持续时间
motorPinA = 0; // 关闭电机A控制引脚
motorPinB = 0; // 关闭电机B控制引脚
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)