At the moment I'm working on an assignment in which I need to be able to read out a TC77 temperature sensor using SPI. I'm using a Arduino uno (Atmega 328P) and the TC77 is placed on the PICKit Serial SPI Demo Board. However I'm not able to get any date from the TC77 (It does work when using a simple arduino program), can anyone tell me where I am going wrong and why?
Just for clarification the code
rx_byte = spi_tranceiver(0); Return 0, if I replace spi_tranceiver(0) to spi_tranceiver(0xFF) it always returns 255.
Thank you in advance.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <string.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define SS 2 // pin 10 arduino uno
#define MOSI 3 // pin 11 arduino uno
#define MISO 4 // pin 12 arduino uno
#define SCK 5 // pin 13 arduino uno
#define TXD 1
#define ENABLE_SS PORTB &= ~(1 << SS)
#define DISABLE_SS PORTB |= (1 << SS)
#define ENABLE_SS2 PORTB &= ~(1 << PORTB1) //pin 9 arduino = pin portb1 for other spi chips
#define DISABLE_SS2 PORTB |= (1 << PORTB1)
#define FOSC 16000000
#define BAUD 9600
#define MYUBBR FOSC/16/BAUD -1
#define UART_BUFF_SIZE 10
#define A 0
#define B 1
#define READ 1
#define WRITE 0
void USART_Init(void);
void init(void);
uint8_t spi_tranceiver (uint8_t data);
void USART_Transmit(uint8_t data);
uint16_t ReadADC();
int16_t Temp_read(void);
uint8_t RX_buf[UART_BUFF_SIZE];
uint8_t TX_buf[UART_BUFF_SIZE];
uint8_t RX_index = 0;
uint8_t TX_index = 0;
char buffer[10]; //Output of the dtostrf function
ISR(USART_RX_vect)
{
RX_buf[RX_index++] = UDR0;
USART_Transmit(RX_buf[RX_index-1]);
}
void USART_Transmit( uint8_t data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
int main(void)
{
int x=0;
init();
/* Clearing buffers */
memset(RX_buf,0,sizeof(RX_buf));
memset(TX_buf,0,sizeof(TX_buf));
/* Enable interrupts */
sei();
char buffer[10]; //Output of the dtostrf function
while (1)
{
uint16_t data = ReadADC();
float temp = (float) data * 0.0625;
dtostrf(temp, 5, 2, buffer);
USART_putstring(buffer);
USART_putstring("-:-");
USART_send('\r');
USART_send('\n');
_delay_ms(2000);
}
}
void init()
{
USART_Init();
/* Set SS, MOSI and SCK output, all others input */
DDRB = (1<<SS)|(1<<MOSI)|(1<<SCK)|(1 << PORTB1);
/* Set TXD as an output */
DDRD = (1 << TXD);
/* Set the slave select pin (Active low) */
DISABLE_SS;
DISABLE_SS2;
/* Enable SPI, Master, set clock rate fosc/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void USART_send( unsigned char data){
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}
// function to initialize UART
void USART_Init(void)
{
// Set baud rate:
UBRR0=103; //UBRR= Fosc /(16*9600) -1 =103.166= 103
// enable receiver and transmitter
UCSR0B |=(1<<RXEN0 |(1 <<TXEN0));
// Set frame format : 8 data 2 stop bit
UCSR0C = (1<<USBS0 )|(3<<UCSZ00);
}
/* spi data buffer load and send */
uint8_t spi_tranceiver (uint8_t data)
{
// Load data into the buffer
SPDR = data;
//Wait until transmission complete
while(!(SPSR & (1<<SPIF) ));
// Return received data
return(SPDR);
}
uint16_t ReadADC()
{
uint8_t rx_byte;
uint8_t rx_byte2;
char buffer[10]; //Output of the dtostrf function
ENABLE_SS;
rx_byte = spi_tranceiver(0);//& 0b01111111; // read the 1st byte; ignore the top 2 bits
rx_byte2 = spi_tranceiver(0) & 0xF8;// >> 1; // read the 2nd byte and shift it down 1 bit - to get rid of the repeated B1
DISABLE_SS;
USART_putstring("First Byte");
sprintf(buffer,"%d",rx_byte);
USART_putstring(buffer);
USART_putstring("Second Byte");
sprintf(buffer,"%d",rx_byte);
USART_putstring(buffer);
uint16_t temp = rx_byte << 8;
temp |= rx_byte2;
float temp2 = (float)temp / 128; // EDIT equals >>3 and * 0.0625;
temp = temp >> 3; // Edit
dtostrf(temp, 5, 2, buffer);
USART_putstring(buffer);
USART_send('\r');
USART_send('\n'); //This two lines are to tell to the terminal to change line
return (rx_byte << 8 | rx_byte2 >> 3);
}