day8ARM作业

#include "si7006.h"
#include "iic.h"
extern void delay_ms(int ms);
// si7006初始化
void si7006_init()
{
    // //主机发送起始信号
    // i2c_start();
    // //主机发送7bit为的从机地址和1bit位的写标志  
    // //传感器从机地址为0x40,写标志位为0
    // i2c_write_byte(0x80);
    // //等待从机应答
    // i2c_wait_ack();
    // //发送8bit位的寄存器地址
    // i2c_write_byte(0XE6);
    // //等待从机应答
    // i2c_wait_ack();
    // //主机发送配置数据的协议
    // i2c_write_byte(0x3A);
    // //等待从机应答
    // i2c_wait_ack();
    // //主机发送终止信号
    // i2c_stop();

    //主机发起起始信号
    i2c_start();
    //主机发送7bit从机地址+1bit写标志  0X40<<1|0=0x80
    i2c_write_byte(0X80);
    //等待从机应答
    i2c_wait_ack();
    //主机发送8bit寄存器地址0XE6
    i2c_write_byte(0XE6);
    //等待从机应答
    i2c_wait_ack();
    //主机发送配置的数据  0X3A
    i2c_write_byte(0X3A);
    //等待从机应答
    i2c_wait_ack();
    //主机发起终止信号
    i2c_stop();
}

// 读取温度数据
short si7006_read_tem()
{
    char tem_h;//接收2字节的温度数据的高8位
    char tem_l;//接收2字节的温度数据的低8位
    short tem;
    //主机发送起始信号
    i2c_start();
    //主机发送7bit位从机地址与1位写标志位
    i2c_write_byte(0X80);
    //等待从机应答
    i2c_wait_ack();
    //主机发送8bit寄存器地址0xE3
    i2c_write_byte(0XE3);
    //等待从机应答
    i2c_wait_ack();
    //主机重复发送初始信号
    i2c_start();
    //主机发送7bit位从机地址与1位读标志位
    i2c_read_byte(0x81);
    //等待从机应答
    i2c_wait_ack();
    //给从机一个延时测量的时间
    delay_ms(10);
    //读取温度的高8位赋值给tem_h,返回应答信号0
    tem_h = i2c_read_byte(0);
    //读取温度的低8位赋值给tem_l,返回应答信号1
    tem_l = i2c_read_byte(0);
    //主机发送终止信号
    tem = tem_h << 8 | tem_l;
    return tem;


    // char tem_h, tem_l;
    // short tem;
    // // 主机发起起始信号
    // i2c_start();
    // // 主机发送7bit从机地址+1bit写标志  0X40<<1|0=0x80
    // i2c_write_byte(0X80);
    // // 等待从机应答
    // i2c_wait_ack();
    // // 主机发送8bit寄存器地址0XE3
    // i2c_write_byte(0XE3);
    // // 等待从机应答
    // i2c_wait_ack();
    // // 主机发送重复起始信号
    // i2c_start();
    // // 主机发送7bit从机地址+1bit读标志  0X40<<1|1=0x81
    // i2c_write_byte(0X81);
    // // 等待从机应答
    // i2c_wait_ack();
    // // 加一个延时给从机一个测量时间
    // delay_ms(10);
    // // 读取温度的高8bit,返回应答信号
    // tem_h = i2c_read_byte(0);
    // // 读取低8bit,返回非应答信号
    // tem_l = i2c_read_byte(1);
    // // 主机发起终止信号
    // tem = tem_h << 8 | tem_l;
    // return tem;
}
// 读取湿度数据
unsigned short si7006_read_hum()
{
   
   
    // unsigned char hum_h, hum_l;
    // unsigned short hum;
    // 主机发起起始信号
    // i2c_start();
    // // 主机发送7bit从机地址+1bit写标志  0X40<<1|0=0x80
    // i2c_write_byte(0X80);
    // // 等待从机应答
    // i2c_wait_ack();
    // // 主机发送8bit寄存器地址0XE5
    // i2c_write_byte(0XE5);
    // // 等待从机应答
    // i2c_wait_ack();
    // 主机发送重复起始信号
    // i2c_start();
    // // 主机发送7bit从机地址+1bit读标志  0X40<<1|1=0x81
    // i2c_write_byte(0X81);
    // // 等待从机应答
    // i2c_wait_ack();
    // // 加一个延时给从机一个测量时间
    // delay_ms(10);
    // // 读取温度的高8bit,返回应答信号
    // hum_h = i2c_read_byte(0);
    // // 读取低8bit,返回非应答信号
    // hum_l = i2c_read_byte(1);
    // // 主机发起终止信号
    // hum = hum_h << 8 | hum_l;
    // return hum;


    unsigned char hum_h;
    unsigned char hum_l;
    unsigned short hum;
    i2c_start();
    i2c_write_byte(0x80);
    i2c_wait_ack();
    i2c_write_byte(0XE5);
    i2c_wait_ack();
    i2c_start();
    i2c_write_byte(0x81);
    i2c_wait_ack();
    delay_ms(10);
    hum_h = i2c_read_byte(0);
    hum_l = i2c_read_byte(1);
    hum = hum_h << 8 | hum_l;
    return hum;
}
void SP1_init()
{
    //使能GPIOB寄存器
    RCC->AHB4RSTCLRR |= (0x1<<1);
    //设置PB6为输出模式
    GPIOB->MODER &= (~(0x3<<12));
    GPIOB->MODER |= (0x1<<12);
    //设置PB6位推挽输出
    GPIOB->OTYPER &= (0x1<<6);
    //设置PB6为低速输出
    GPIOB->OSPEEDR &= (~(0x3<<12));
    //设置PB6无上拉下拉电阻;
    GPIOB->PUPDR &= (~(0x3<<12));
}
void SP1_ctl(int flag)
{
    if(flag == 1)
    {
        GPIOB->ODR |= (0x1<<6);
    }
    else if(flag == 0)
    {
        GPIOB->ODR &= (~(0x1<<6));
    }
}
void fan_ctl(int flag)
{
    if (flag == 1)
    {
        GPIOE->ODR |= (0x1 << 9);
    }
    else if (flag == 0)
    {
        GPIOE->ODR &= (~(0x1 << 9));
    }
}
void fan_init()
{

    GPIOE->MODER &= (~(0x3 << 18));
    GPIOE->MODER |= (0x1 << 18);

    GPIOE->OTYPER &= (~(0x1 << 9));

    GPIOE->OSPEEDR &= (~(0x3 << 18));
    GPIOE->PUPDR &= (~(0x3 << 18));
}
#include"si7006.h"
#include"iic.h"
#include "ap3216c.h"
extern void printf(const char *fmt, ...);
//手动封装一个延时函数
void delay_ms(int ms)
{
    int i,j;
    for(i=0;i<ms;i++)
    {
        for(j=0;j<2000;j++)
        {

        }
    }
}
int main()
{
    SP1_init();
    fan_init();
    i2c_init();
    si7006_init();
    unsigned short hum;
    short tem;
    while(1)
    {
        //读取温度数据
        tem=si7006_read_tem();
        //进行温度数据的计算
        tem=tem*175.72/65536-46.85;
        //读取湿度数据
        hum=si7006_read_hum();
        //计算湿度数据
        hum=hum*125/65536-6;
        if(tem == -46 && hum == 118)
        {
            // SP1_ctl(1);
            fan_ctl(1);
        }
        else 
        {
            fan_ctl(0);
        }
        printf("tem:%d hum:%d\n",tem,hum);
        delay_ms(1000);
    }
    return 0;
}

// int main()

// {
// 	unsigned short ir, ps, als;
// 	if (ap3216c_init() == 0)
// 		printf("init success!\n");
// 	else {
// 		printf("init failed!\n");
// 		return 1;
// 	}
// 	while(1)
// 	{
// 		ap3216c_read_data(&ir, &ps,&als);
// 		printf("ir = %d, ps = %d, als = %d\n",
// 				ir, ps, als);
// 		delay_ms(1000);
// 	}
// 	return 0;
// }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值