So I am trying to get this light sensor to work with the Arduino Nano:
https://www.fasttech.com/products/1012006
I have to program it in pure C, no Arduino IDE or libraries, per stipulation for my EE senior design class. I've been wading through the data sheet and examples for two days, and I still haven't gotten it to work. My code runs all the way through, but it prints out a blank space when I try to print my result. When I did it with the Arduino Libraries, it spat out about 240 Lux. But somewhere, somehow, I'm screwing up the C implementation. Has anyone else had any luck with TWI in C to read data from a sensor?
Thanks for any help, here is my code.
/*
* main.c
*
* Created on: Mar 13, 2014
* Author: hollis
*/
#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <serialLib.h>
#include <analogLib.h>
#include <digitalLib.h>
#include </usr/avr/include/util/twi.h>
#include </usr/avr/include/avr/interrupt.h>
void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void init_aio();
int read_analog();
void output(int pin, int state);
#define SCL_CLOCK 400000L
unsigned char intensity = 0;
int value = 0;
int buffer = 1000;
int main(void){
int i;
USART_init();
PORTC &= (1<<PC4);
PORTC &= (1<<PC5);
unsigned char address = 0x23;
//Initialize TWI
TWSR = 0;
TWBR = ((F_CPU/SCL_CLOCK-16)/2);
uint8_t twst;
//Send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))); //Wait for transmission to complete
//USART_putstring("I got here.\n\r");
twst = TW_STATUS & 0x08; //Check status register
if ( (twst != TW_START) && (twst != TW_REP_START)) //Mask prescaler
return 1;
TWDR = address; //Send address, then interrupt flag and enable bit
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))); //Wait for ACK (Transmission complete)
//USART_putstring("I got here too.\n\r");
if(TWINT) //Determines resolution (I think)
TWDR = 0x10;
TWCR = (1<<TWINT) | (1<<TWEN); //Clears interrupt
/*twst = TW_STATUS & (0x20 + 0x28 + 0x30); //Check TWSR
if ( (twst == TW_MT_SLA_ACK) || (twst == TW_MR_SLA_ACK) ){ //Mask prescaler
return 1;
}
USART_putstring("I got here also.\n\r");
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); //Read data, look for ACK
while(!(TWCR & (1<<TWINT)));
USART_putstring("I got here also too.\n\r");
//intensity = TWDR;*/
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //Send stop signal
while(TWCR & (1<<TWSTO)); //Wait for stop signal to finish
while(1){
//Send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))); //Wait for transmission to complete
//USART_putstring("I got here.\n\r");
twst = TW_STATUS & 0x08; //Check status register
if ( (twst != TW_START) && (twst != TW_REP_START)) //Mask prescaler
return 1;
TWDR = address + 1; //Send address, then interrupt flag and enable bit
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))); //Wait for ACK (Transmission complete)
//USART_putstring("I got here too.\n\r");
twst = TW_STATUS & (0x38 + 0x40 + 0x48); //Check TWSR
if ( (twst == TW_MT_SLA_ACK) || (twst == TW_MR_SLA_ACK) ){ //Mask prescaler
return 1;
}
//USART_putstring("I got here also.\n\r");
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); //Read data, look for ACK
while(!(TWCR & (1<<TWINT)));
//USART_putstring("I got here also too.\n\r");
//i = 0;
//i <<= 8;
i = TWDR;
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //Send stop signal
while(TWCR & (1<<TWSTO)); //Wait for stop signal to finish
intensity = i/1.2;
//intensity = itoa(intensity, value, buffer);
USART_send(intensity);
USART_putstring("\n\r");
//USART_putstring("I got here\n\r");
}
}