SAMD21 Xplained Pro with i01 expansion board

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

* Moved to a more appropriate forum. *

 

Hey guys. I am programming Embedded Systems using SAMD21 kit and programming a SAMD21J18A. I am programming in C. The i01 extension board uses an At30TSE758A temperature sensor. I wrote the code to get the temperature and it does work when I debug it. I want to create an array of [10] and store the temperatures in the array. I then want to display the 10 temperatures on the output screen. I have been trying and not having any luck. Can someone help me on this or if you know of a site that has this info, please share.  Thanks in advance.

 

/*
 * Temperature_Sensor_NEW.c
 *
 * Created: 07/01/2020 9:39:21 PM
 * Author : Mike
 */ 

#include "samd21.h"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*) &REG_PORT_PINCFG0;
unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*) &REG_PORT_PMUX0;

#define SLAVE_ADDR 0x4F	/* 1001 111. AT30TSE758*/
//#define  T_LOW_Limit_Register_Address 0x12		/* Pointer Register Value*/
//#define  T_HIGH_Limit_Register_Address 0x13	/* Pointer Register Value*/
#define  T_HIGH_Limit_Register_Address 0x00	/* Pointer Register Value*/
#define  T_LOW_Limit_Register_Address 0x00
#define  Temperature_Register_Address 0x00

void I2C2_init (void);
int I2C2_Byte_Read (unsigned char saddr, unsigned char Pointer_Register_Value, unsigned char* data);
int I2C2_Byte_Write (unsigned char saddr, unsigned char Pointer_Register_Value, unsigned char* data);

int main (void)
{
	unsigned char data;
	unsigned char current_temp;
	I2C2_init();

	I2C2_Byte_Read (SLAVE_ADDR, T_HIGH_Limit_Register_Address, &data);
	current_temp = REG_SERCOM0_I2CM_DATA;//high limit
	int temperature= data;
}

void I2C2_init (void)
{
	REG_PM_APBCMASK |= 0x00000004;	/* SERCOM0 bus clock */
	GCLK->CLKCTRL.reg = 0x4014;		/* SERCOM0 core clock */

	ARRAY_PORT_PINCFG0[8] |= 1;		/* Allow pmux to set PA08 pin configuration */
	ARRAY_PORT_PINCFG0[9] |= 1;		/* Allow pmux to set PA08 pin configuration */
	ARRAY_PORT_PMUX0[4] = 0x22;		/* PA08 = TWI_SDA , PA09 = TWI_SCL */

	REG_SERCOM0_I2CM_CTRLA = 1;		/* Reset SERCOM0 */

	while (REG_SERCOM0_I2CM_CTRLA & 1) {}	/* Wait for reset to complete */

	REG_SERCOM0_I2CM_CTRLA = 0x14;		/* Master mode */
	REG_SERCOM0_I2CM_BAUD = 0;			/* 1 MHz main clock -> ~100KHz */
	REG_SERCOM0_I2CM_CTRLA |= 2;		/* Enable SERCOM0 */
	REG_SERCOM0_I2CM_STATUS = 0x10;		/* Force idle */
}

/*
*
* Read the Nonvolatile T_LOW Limit Register and the Nonvolatile T_HIGH Limit
* Register of the AT30TSE758.
*
*/
int	I2C2_Byte_Read (unsigned char saddr, unsigned char Pointer_Register_Value, unsigned char* data)
{
	while ( (REG_SERCOM0_I2CM_STATUS & 0x30) != 0x10); /* Wait until idle */

	REG_SERCOM0_I2CM_ADDR = SLAVE_ADDR << 1;			/* Send slave Address */

	while ( (REG_SERCOM0_I2CM_INTFLAG & 1) == 0);			/* Wait until SLAVE_ADDR sent */

	REG_SERCOM0_I2CM_DATA = Pointer_Register_Value;		/* Send Pointer Register Value */

	while ( (REG_SERCOM0_I2CM_INTFLAG & 1) == 0);			/* Wait until Pointer Register Value sent */

	REG_SERCOM0_I2CM_ADDR = (saddr << 1) | 1;

	while ( (REG_SERCOM0_I2CM_INTFLAG & 2) == 0);				/* Wait until data received */

	*data = REG_SERCOM0_I2CM_DATA;							/* Read data */

	REG_SERCOM0_I2CM_CTRLB |= 0x40000;					/* Generate NACK */

	REG_SERCOM0_I2CM_CTRLB |= 0x30000;					/* issue a stop */

	return 0;
}

/* millisecond delay based on 1 MHz system clock */
void delayMs (int n)
{
	int i;

	for (; n > 0; n--)
	for (i = 0; i < 199; i++)
	{ __asm ("nop"); }
	}

 

Attachment(s): 

Last Edited: Wed. Jan 8, 2020 - 10:59 PM