2024/5/23 ARMday8

do_irq.c

#include "key_it.h"
//#include "led.h"
#include"si7006.h"//导入温度传感器头文件
#include"iic.h"//导入iic头文件
#include "ap3216c.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
    i2c_init();//iic初始化
    si7006_init();//温湿度传感器初始化
    unsigned short hum;//定义湿度
    short tem;//定义温度
    unsigned short ir, ps, als;
    ap3216c_init();
    //获取中断号
    unsigned int irqno=(GICC->IAR & (0x3FF));
    switch (irqno)
    {
    case 99://KEY1
        //处理中断
        GPIOE->ODR ^= (0x1<<8);//点亮LED1
        //读取温度数据
        tem=si7006_read_tem();
        //进行温度数据的计算
        tem=tem*175.72/65536-46.85;
        //读取湿度数据
        hum=si7006_read_hum();
        //计算湿度数据
        hum=hum*125/65536-6;
        printf("tem:%d hum:%d\n",tem,hum);//打印温度和湿度
        delay_us();
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<3);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<9);
        break;
    case 98://KEY3
        //处理中断
        GPIOE->ODR ^= (0x1<<10);
        ap3216c_read_data(&ir, &ps,&als);
		printf("ir = %d, ps = %d, als = %d\n",
				ir, ps, als);
        delay_us();
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<2);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<8);
        break;
    case 97:
        //处理中断
        GPIOF->ODR ^= (0x1<<10);
        //清除中断队列
        GICD->ICPENDR[3] |= (0x1<<1);
        //清除中断挂起标志
        EXTI->FPR1 |= (0x1<<7);
        break;
    default:
        break;
    }
    //清除中断号
    GICC->EOIR = irqno;
}

main.c

#include"si7006.h"//导入温度传感器头文件
#include"iic.h"//导入iic头文件
#include "gpio.h"
#include "led.h"
#include "uart4.h"
#include "key_it.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()
{

	//使能PE、PF时钟
	*((unsigned int *)0x50000A28) |= (0x3<<4);	
	led1_init();
	led2_init();
	led3_init();
	key1_init();
	key2_init();
	key3_init();
    while(1)
    {
		printf("do_main\n");
        
        delay_ms(1000);
    }
    return 0;
}

ap3216c.c

#include "iic.h"
#include "ap3216c.h"
extern void delay_ms(unsigned int ms);
extern void printf(const char *fmt, ...);
unsigned char ap3216c_init(void)
{
	i2c_init();
	// 对ap3216进行软件复位
	ap3216c_write_byte(AP3216C_ADDR, 0x00, 0x4); 
	delay_ms(100);  // 至少进行10ms的延时
	// 激活ALS PS IR功能
	ap3216c_write_byte(AP3216C_ADDR, 0x00, 0x3);
	delay_ms(100);
	if (ap3216c_read_byte(AP3216C_ADDR,0x00) == 0x3)
	{
		//printf("ap3216 init success!\n");
		return 0;
	} else {
		printf("ap3216 init failed!\n");
		return 1;	
	}

	return 0;
}
unsigned char ap3216c_read_byte(unsigned char slave_addr,
					unsigned char reg_addr)
{
	unsigned char dat;
	i2c_start();
	i2c_write_byte(slave_addr << 1);
	i2c_wait_ack();
	i2c_write_byte(reg_addr);
	i2c_wait_ack();
	i2c_start();
	i2c_write_byte((slave_addr << 1) | 1);
	i2c_wait_ack();
	dat = i2c_read_byte(1);
	i2c_stop();
	return dat;
}


void  ap3216c_write_byte(unsigned char slave_addr,
					unsigned char reg_addr, 
					unsigned char dat)
{
	i2c_start();
	i2c_write_byte(slave_addr << 1);
	i2c_wait_ack();
	i2c_write_byte(reg_addr);
	i2c_wait_ack();
	i2c_write_byte(dat);
	i2c_wait_ack();
	i2c_stop();
}

void ap3216c_read_data(unsigned short* ir, 
					unsigned short* ps, 
					unsigned short* als)
{
	unsigned char buf[6] = {0};
	unsigned char i;
	for(i = 0; i < 6; i++)
	{
		buf[i] = ap3216c_read_byte(AP3216C_ADDR, 0x0A+i);
	}
	
	// 读取ir的转换结果
	if(buf[0] & 0x80)
		*ir = 0;
	else 
		*ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x3);

	// 读取ALS的转换结果  
	*als = ((unsigned short)buf[3] << 8) | buf[2];

	if(buf[4] & 0x40)
		*ps = 0;
	else 
		*ps = ((unsigned short)buf[5] << 4) | (buf[4] & 0xF);

}

ap3216c.h

#ifndef __AP3216C_H__
#define __AP3216C_H__

#include "iic.h"

/* 通过程序模拟实现I2C总线的时序和协议
 * GPIOF ---> AHB4
 * I2C1_SCL ---> PF14
 * I2C1_SDA ---> PF15
 *
 * */
#define     AP3216C_ADDR    0x1E

unsigned char ap3216c_init(void);
unsigned char ap3216c_read_byte(unsigned char slave_addr,
					unsigned char reg_addr);
void  ap3216c_write_byte(unsigned char slave_addr,
					unsigned char reg_addr, 
					unsigned char dat);

void ap3216c_read_data(unsigned short* ir, 
					unsigned short* ps, 
					unsigned short* als);

#endif //__SI7006_H__

si7006.c

#include"si7006.h"//导入函数声明头文件
#include"iic.h"//导入要使用的函数头文件
extern void delay_ms(int ms);
//si7006初始化
void si7006_init()
{
    //主机发起起始信号
    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,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;
}

si7006.h

#ifndef __SI7006_H__
#define __SI7006_H__

void si7006_init();//温湿度传感器初始化函数声明
short si7006_read_tem();//读取温度函数声明
unsigned short si7006_read_hum();//读取湿度函数声明
#endif

led.c

#include "led.h"
#include"stm32mp1xx_gpio.h"
//led灯1初始化
void led1_init(){
    //配置为输出模式
    GPIOE->MODER &= (~(0x3<<20));	
	GPIOE->MODER |= (0x1<<20);	
 
    //配置为推挽输出
 
    GPIOE->OTYPER &= (~(0x1<<10));
 
    //配置低速输出
    GPIOE->OSPEEDR &= (~(0x3<<20));	
 
    //配置为无上拉下拉电阻
	GPIOE->PUPDR &= (~(0x3<<20));	
}
 
//led灯2初始化
void led2_init(){
    //配置为输出模式
    GPIOF->MODER &= (~(0x3<<20));	
	GPIOF->MODER |= (0x1<<20);	
 
    //配置为推挽输出
    GPIOF->OTYPER &= (~(0x1<<10));
    //配置低速输出
    GPIOF->OSPEEDR &= (~(0x3<<20));
 
    //配置为无上拉下拉电阻
	GPIOF->PUPDR &= (~(0x3<<20));	
}
 
//led灯3初始化
void led3_init(){
    //配置为输出模式
	GPIOE->MODER &= (~(0x3<<16));	
	GPIOE->MODER |= (0x1<<16);
 
	//配置为推挽输出	
	GPIOE->OTYPER &= (~(0x1<<8));
 
	//配置低速输出	
	GPIOE->OSPEEDR &= (~(0x3<<16));
 
	//配置为无上拉下拉电阻
	GPIOE->PUPDR &= (~(0x3<<16));	
}
 
//led灯1控制
void led1_ctl(int flag){
    if(flag==1){
        GPIOE->ODR |= (0x1<<10);
    }else if(flag==0){
        GPIOE->ODR &= (~(0x1<<10));	
    }
}
 
//led灯2控制 
void led2_ctl(int flag){
    if(flag==1){
       GPIOF->ODR |= (0x1<<10);	
    }else if(flag==0){
        GPIOF->ODR &= (~(0x1<<10));	
    }
}
 
//led灯3控制
void led3_ctl(int flag){
    if(flag==1){
        GPIOE->ODR |= (0x1<<8);
    }else if(flag==0){
        GPIOE->ODR &= (~(0x1<<8));	
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值