Skip to content

Instantly share code, notes, and snippets.

@rat
Created September 11, 2016 02:38
Show Gist options
  • Save rat/15e17fb30430e7aa9a73fc50f1dc9406 to your computer and use it in GitHub Desktop.
Save rat/15e17fb30430e7aa9a73fc50f1dc9406 to your computer and use it in GitHub Desktop.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "usart/usart.h"
#include <stdlib.h>
uint8_t SPI(uint8_t data);
int main(void)
{
USART_init();
DDRB = (1<<PB5) | (1<<PB3) | (1<<PB2); // SCK, MOSI, SS = OUTPUT -- MISO = INPUT
SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<SPR1); // Enable SPI, Master, Mode 1, fosc/64
PORTB = (1<<PB2); // SS HIGH
while(1)
{
uint8_t MSB = 0, LSB = 0, DECI,temperature = 0;
float temp;
PORTB &= ~(1<<PB2); // SS LOW
MSB = SPI(0x00);
LSB = SPI(0x00);
temperature = (MSB<<1) | (LSB>>7);
DECI = ((LSB>>3) & (~(1<<5)));
temp = temperature + (DECI*0.0625);
PORTB |= (1<<PB2); // SS HIGH
char str[20];
dtostrf(temp, 0, 3, str);
USART_putstring(str);
USART_send('\n');
_delay_ms(1000);
}
}
uint8_t SPI(uint8_t data)
{
SPDR = data;
while(!(SPSR &(1<<SPIF)));
return SPDR;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment