連載
» 2016年07月26日 07時00分 公開

オンボードされた温湿度センサーからFPGAで値を得るMAX 10 FPGAで学ぶFPGA開発入門(12)(6/7 ページ)

[大原 雄介,MONOist]
/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */
 
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "terasic_includes.h"
#include "I2C.h"
 
#define R_TEMPERATURE      0x00
#define R_HUMIDITY         0x01
#define R_CONFIGURATION    0x02
#define R_MANUFACTURER_ID  0xFE
#define R_DEVICE_ID        0xFF
 
#define HDC1000_I2C_ADRESS 0x80 // 1000_000 + Write bit 0
 
long	HEXtable[11] = {0x40,	// 0:1000000
						0x79,	// 1:1111001
						0x24,	// 2:0100100
						0x30,	// 3:0110000
						0x19,	// 4:0011001
						0x12,	// 5:0010010
						0x02,	// 6:0000010
						0x78,	// 7:1111000
						0x00,	// 8:0000000
						0x10,	// 9:0010000
						0x7F	// Blank
};
#define	KEY_START	0x01
#define	KEY_STOP	0x02
 
 
void LEDR_out(int num)
{
	if(num > 9)	return;
	if(num < 0)
	{
		IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 0x0);	// -の値がきたらBlankにする
	}
	else
	{
		IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 1 << num);
	}
}
 
void HEX_out(int digit, int num)
{
	if(num > 9)	return;
	if(num < 0)	num=10;	// -の値がきたらBlankにする
	if((digit < 0)||(digit > 2))	return;
	if(digit)
		IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, HEXtable[num]);
	else
		IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, HEXtable[num]);
}
 
bool Read_Configuration(void)
{
	bool bPass;
	alt_u16 data;
 
	bPass = I2C_Read16(RH_TEMP_I2C_SCL_BASE,RH_TEMP_I2C_SDA_BASE,HDC1000_I2C_ADRESS,R_CONFIGURATION,&data);
	if(bPass){
 
		printf("Read Configuration Data = 0x%x \r\n",data);
 
		if(((data>>12)&0x0001) == 0) printf("Acquisition mode:Temperature or Humidity is acquired.\r\n ");
		else printf("Acquisition mode:Temperature and Humidity are acquired in sequence,Temperature first.\r\n ");
 
		if(((data>>10)&0x0001) == 0) printf("Temperature Measurement Resolution: 14 bit.\r\n ");
		else  printf("Temperature Measurement Resolution: 11 bit.\r\n ");
		if(((data>> 8)&0x0003) == 0) printf("Humidity Measurement Resolution: 14 bit.\r\n ");
		else if(((data>> 8)&0x0003) == 1) printf("Humidity Measurement Resolution: 11 bit.\r\n ");
		else  printf("Humidity Measurement Resolution: 8 bit.\r\n ");
 
	}
 
	return bPass;
}
 
 
 
bool Write_Configuration(alt_u8 acquisition_mode , alt_u8 temp_resolution,alt_u8 humidity_resolution)
{
	bool bPass;
	alt_u16 data;
 
	data = ((acquisition_mode&0x0001) <<12) + ((temp_resolution&0x0001)<<10) + ((humidity_resolution&0x0003)<<8);
 
	//printf("Write Configuration Data = 0x%x \r\n",data);
 
	bPass = I2C_Write16(RH_TEMP_I2C_SCL_BASE,RH_TEMP_I2C_SDA_BASE,HDC1000_I2C_ADRESS,R_CONFIGURATION,data);
 
	return bPass;
}
 
bool RH_Temp_Sensor_init(void)
{
	bool bPass;
    alt_u16 data;
	bPass = I2C_Read16(RH_TEMP_I2C_SCL_BASE,RH_TEMP_I2C_SDA_BASE,HDC1000_I2C_ADRESS,R_MANUFACTURER_ID,&data);
    if(bPass){
		printf("Manufacturer ID is %x\r\n",data);// 0x5449 expected
    }else{
	    printf("I2C MultipleRead Failed!\r\n");
    }
 
    bPass = I2C_Read16(RH_TEMP_I2C_SCL_BASE,RH_TEMP_I2C_SDA_BASE,HDC1000_I2C_ADRESS,R_DEVICE_ID,&data);
    if(bPass){
	    printf("Device ID is %x\r\n",data); // 0x1000 expected
    }else{
	    printf("I2C Read16 Failed!\r\n");
    }
 
    bPass =  Read_Configuration();
    if(!bPass){
		printf("I2C Read_Configuration Failed!\r\n");
	}
 
	bPass =  Write_Configuration(1,0,0);
    if(!bPass){
		printf("[Note] Set Configuration Failed!\r\n");
	}
 
	return bPass;
}
 
 
int main()
{
	alt_u8 KeyStatus, FLAG=0;	// 0:Stop 1:Running
	bool    bPass;
	alt_u8  rh_temp[4];
	alt_u16 data;
	int		temp_int;
 
	// Initial Display
	HEX_out(0, -1);
	HEX_out(1, -1);
	LEDR_out(-1);
 
	// Sensor Initialize
	RH_Temp_Sensor_init();
 
	while(1)
	{
		usleep(500000);
 
		KeyStatus = IORD_ALTERA_AVALON_PIO_DATA(KEY_BASE);
		if(( KeyStatus & KEY_STOP ) == 0 )
		{
			FLAG = 0;
			HEX_out(0, -1);
			HEX_out(1, -1);
			LEDR_out(-1);
		}
		else if (( KeyStatus & KEY_START ) == 0 )
		{
			FLAG = 1;
		}
 
		if( FLAG )
		{
			bPass =  I2C_MultipleRead(RH_TEMP_I2C_SCL_BASE,
									  RH_TEMP_I2C_SDA_BASE,
									  HDC1000_I2C_ADRESS,
									  R_TEMPERATURE,
									  rh_temp,4);
		    if(bPass)
		    {
				data = (rh_temp[0] << 8) | rh_temp[1] ;
				data = (rh_temp[0] << 8) | rh_temp[1] ;
				temp = (double)data/397.188 -40.0;
				temp_int = (int)(temp*10.0);
				HEX_out(1,temp_int/100);
				HEX_out(0,(temp_int%100)/10);
				LEDR_out(temp_int%10);
				printf("Temperature: %.2f \n",temp);
		    }
		}
	}
  return 0;
}
 
List 5

Copyright © ITmedia, Inc. All Rights Reserved.