函数pwr的功能c语言,想请教C语言编程。

根据题主的描述编写了下面的小程序。首先是程序的部分截图:

f4268acd93a8ec06b10ab915fb06b52b.png

在代码led.c中各函数功能描述如下:

① void pwr_all_on(void)是开启16盏LED。

② void pwr_all_off(void)是关闭16盏LED。

③ void print_led_state(void)的依次描述16盏LED灯的状态。

④ int get_led(int led_no)是获取指定次序(1~16号)LED灯的状态。返回值为1,表示灯开启;返回值为0,表示灯关闭。

⑤ void set_led(int led_no, int state)用于设置指定次序(1~16号)LED灯的状态。

int led_no表示LED次序

int state表示LED灯状态。1表示LED开启,0表示LED关闭。

最后是led.c中的源代码:

-------------------------------led.c-------------------------------------------

#include

#include

#include

// 表示LED灯的状态

#define POWER_ON    (1)

#define POWER_OFF   (0)

// LED各位依次有电的状态

static uint16_t pwr_on[16] = {

0x1, 0x2, 0x4, 0x8,

0x10,0x20, 0x40, 0x80,

0x100, 0x200, 0x400, 0x800,

0x1000, 0x2000, 0x4000, 0x8000

};

// LED各位依次无电的状态

static uint16_t pwr_off[16] = {

0xfffe, 0xfffd, 0xfffb, 0xfff7,

0xffef, 0xffdf, 0xffbf, 0xff7f,

0xfeff, 0xfdff, 0xfbff, 0xf7ff,

0xefff, 0xdfff, 0xbfff, 0x7fff

};

// 在不同的软硬件平台上,char、int、long等的存储空间并不一致,

// 因而为了确定使用的是16位存储空间,我使用了stdint.h中的uint16_t

// 表示16位无符号整形。

// led_state表示16盏LED灯,其中第0位表示LED1,

// 第1位表示LED2,以此类推

static uint16_t led_state = 0xffff;

// 用于设置led_state状态

// int led_no表示LED灯的序号,有效范围1~16

// int state表示灯的状态。取值POWER_ON或POWER_OFF

void set_led(int led_no, int state);

// 获取特定LED灯的状态

// 返回值为POWER_ON或者POWER_OFF

// int led_no为指定的LED灯序号。有效值为1~16

int get_led(int led_no);

// 依次描述16盏LED的状态

void print_led_state(void);

// 开启所有的LED灯

void pwr_all_on(void);

// 关闭所有的LED灯

void pwr_all_off(void);

int main(void) {

printf("power all on\n");

pwr_all_on();

print_led_state();

printf("\npower off LED 16\n");

set_led(16, POWER_OFF);

printf("power off LED 1\n");

set_led( 1, POWER_OFF);

print_led_state();

printf("\npower all off\n");

pwr_all_off();

print_led_state();

return 0;

}

void set_led(int led_no, int state) {

if (led_no < 1 || led_no > 16) {

fputs("The specificed LED number is unavailable and "

"the effective range is from 1 to 16.\n", stderr);

exit(0);

}

uint16_t set_token;

if(POWER_ON == state) {

led_state |= pwr_on[led_no-1];

} else if (POWER_OFF == state) {

led_state &= pwr_off[led_no-1];

} else {

fputs("The state of LED is unavailable and "

"the effective range is  0 or 1.\n", stderr);

exit(0);

}

}

int get_led(int led_no) {

if (led_no < 1 || led_no > 16) {

fputs("The specificed LED number is unavailable and "

"the effective range is from 1 to 16.\n", stderr);

exit(0);

}

int state = led_state & pwr_on[led_no-1];

if (state) {

state = POWER_ON;

} else {

state = POWER_OFF;

}

return state;

}

void print_led_state(void) {

int i;

printf("LED state:\n");

for (i=1; i<=16; i++) {

if (POWER_ON == get_led(i)) {

printf("\tLED %2d is power on.\n", i);

} else {

printf("\tLED %2d is power off.\n", i);

}

}

}

void pwr_all_on(void) {

led_state = ~(led_state ^ led_state);

}

void pwr_all_off(void) {

led_state ^= led_state;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MPU6050是一种集成了3轴加速度计和3轴陀螺仪的微电子机械(Gyroscopes and Accelerometers)系统。姿态解算是使用MPU6050传感器数据进行定位和方向估计的过程。 在C语言中,可以使用MPU6050的库文件或驱动程序来读取传感器数据,并进行姿态解算。以下是一个简单示例代码: ```c #include <stdio.h> #include <wiringPi.h> #include <wiringPiI2C.h> // 定义MPU6050相关的寄存器地址 #define MPU6050_ADDR 0x68 #define PWR_MGMT_1 0x6B #define ACCEL_XOUT_H 0x3B #define GYRO_XOUT_H 0x43 int main(void) { int fd; wiringPiSetup(); fd = wiringPiI2CSetup(MPU6050_ADDR); // 初始化MPU6050 wiringPiI2CWriteReg8(fd, PWR_MGMT_1, 0); while (1) { // 读取加速度计数据 int accel_x = wiringPiI2CReadReg16(fd, ACCEL_XOUT_H); int accel_y = wiringPiI2CReadReg16(fd, ACCEL_YOUT_H); int accel_z = wiringPiI2CReadReg16(fd, ACCEL_ZOUT_H); // 读取陀螺仪数据 int gyro_x = wiringPiI2CReadReg16(fd, GYRO_XOUT_H); int gyro_y = wiringPiI2CReadReg16(fd, GYRO_YOUT_H); int gyro_z = wiringPiI2CReadReg16(fd, GYRO_ZOUT_H); // 进行姿态解算和处理数据 // 输出结果 delay(100); // 等待100毫秒 } return 0; } ``` 以上示例代码使用了wiringPi库来读取MPU6050传感器数据。在初始化MPU6050后,可以使用`wiringPiI2CReadReg16`函数从相应的寄存器地址读取加速度计和陀螺仪数据。读取到的数据可以用于姿态解算和其他相关应用。 请注意,以上代码只是一个简单的示例,并没有包含完整的姿态解算和数据处理部分。具体的姿态解算算法和数据处理方法可能因应用需求有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值