Hello all,
I'm using LPC4088 experiment bundle, in which a temperature sensor (LM75) is connected to the LPC4088 controller using IC2 connection. I'm using this code to test the temperature sensor. However, the program is getting stuck at temperature_enable(); The code changes an RGB LED color based on the temperature value.
This is the main code #include <stdint.h> #include <stdio.h> #include "leds.h" #include "temperature.h"
const int threshold[] = {29, 28, 27, 26, 25, 24}; const int colors[][3] = { { 1, 1, 1}, // White { 1, 0, 1}, // Magenta { 1, 0, 0}, // Red { 1, 1, 0}, // Yellow { 0, 0, 1}, // Blue { 0, 1, 0} // Green };
void pick_leds(float measured) { int i; for (i = 0; i < sizeof(threshold) / sizeof(int); i++) { if (measured >= threshold[i]) { break; } } leds_set(colors[i][0], colors[i][1], colors[i][2]); }
int main(void) { leds_init(); temperature_init(); temperature_enable(); __enable_irq();
while (1) { pick_leds(temperature_read()); //pick_leds(29); } }
// *******************************ARM University Program Copyright © ARM Ltd 2014*************************************
This is the temperature.c #include "temperature.h" #include "i2c.h"
#define SLAVE_ADDRESS 0x90 #define READ 0x1 #define WRITE 0x0 #define START_CONVERT_T 0x51 #define READ_TEMPERATURE 0xAA
void temperature_init(void) { i2c_init(); }
void temperature_enable(void) { uint8_t buff[1]; buff[0] = START_CONVERT_T; i2c_write(SLAVE_ADDRESS, buff, 1); }
float temperature_read(void) { short temp; uint8_t buff[2]; buff[0] = READ_TEMPERATURE; i2c_write(SLAVE_ADDRESS, buff, 1); i2c_read(SLAVE_ADDRESS, buff, 2); temp = (buff[0] << 8) | buff[1];
// Sign extend from 16-bit to 12-bit. temp >>= 4;
// Convert from fixed point to floating point. return temp / (float)16; }
Hello Lilas,
What part of temperature_enable() does the code stop executing?
When it gets stuck, does it stay on a particular line? If you enter debug mode, and go to Peripherals => Core Peripherals => Fault Reports, does any checkmark appear in this dialog when the function executes?
In the future, try placing source code between:
< pre > and < /pre >
tags. See: http://www.keil.com/forum/tips.asp
Thanks, Zack
Thank you Zack for the tip! It gets stuck at
i2c_write(SLAVE_ADDRESS, buff, 1);
Inside that function (in the i2c library) we have:
void i2c_write(uint8_t address, uint8_t *buffer, int buff_len) { uint32_t i = 0; LPC_I2C0->CONSET |= 0x20; start: switch(LPC_I2C0->STAT) { case I2C_I2STAT_M_TX_START: case I2C_I2STAT_M_TX_RESTART: LPC_I2C0->DAT = address | WRITE; I2C_Mode_Clr(Y,0,0,Y); goto start; case I2C_I2STAT_M_TX_SLAW_ACK: LPC_I2C0->DAT = buffer[i]; I2C_Mode_Clr(0,0,0,Y); i++; goto start; case I2C_I2STAT_M_TX_SLAW_NACK:; I2C_Mode_Set(1,1,X,X); I2C_Mode_Clr(Y,Y,0,Y); goto start; case I2C_I2STAT_M_TX_DAT_ACK: if(i <= buff_len -1){ LPC_I2C0->DAT = buffer[i]; I2C_Mode_Clr(0,0,0,Y); i++; goto start; } else{ I2C_Mode_Set(1,X,X,X); I2C_Mode_Clr(Y,0,0,Y); break; } case I2C_I2STAT_M_TX_DAT_NACK: I2C_Mode_Set(1,X,X,X); I2C_Mode_Clr(Y,Y,0,Y); break; case I2C_I2STAT_M_TX_ARB_LOST: I2C_Mode_Set(1,X,X,X); I2C_Mode_Clr(Y,0,0,Y); goto start; case I2C_I2STAT_M_BUS_ERROR: I2C_Mode_Set(X,1,X,X); I2C_Mode_Clr(0,Y,0,Y); LPC_I2C0->CONSET |= 0x20; goto start; default: goto start;; } // Send the following sequence: // - Start bit // - Contents of buffer, from 0..buff_len // - Stop bit }
It is getting stuck at the second case (restart) Im new to the controller and the Keil environment. Could the slave address be wrong?