ARM 作业5

main.c

#include "AP3216C.h"
#include "iic.h"
#include "led.h"
#include "sp1.h"
#include "fan1.h"
#include "motor.h"
extern void printf(const char *fmt, ...);
void delay_ms(unsigned int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}
int main()

{
	led1_init();
	sp1_init();
	fan1_init();
	motor_init();
	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);
		if(als == 0)
		{
			sp1_ctl(1);
		}
		else if(als != 0)
		{
			sp1_ctl(0);
		}
		printf("ir = %d, ps = %d, als = %d\n",
				ir, ps, als);
		delay_ms(1000);
	}
	return 0;
}

sp1.c

#include "sp1.h"

void sp1_init()
{
    GPIOB->MODER &= (~(0x3<<12));
	GPIOB->MODER |= (0x1<<12);
    GPIOB->OTYPER &= (~(0x1<<6));
    GPIOB->OSPEEDR &= (~(0x3<<12));
    GPIOB->PUPDR &= (~(0x3<<12));
}

void sp1_ctl(int flag)
{
    if(flag==1)
    {
       GPIOB->ODR |= (0x1<<6);
    }
    else if(flag==0)
    {
       GPIOB->ODR &= (~(0x1<<6));
    }
}

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__

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值