Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. /*-----------------------------------------------
  3.  * Inclusions
  4.  * ---------------------------------------------*/
  5. #include <avr/io.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #include <config.h>
  10. #include "ltc2450.h"
  11.  
  12.  
  13. /*-----------------------------------------------
  14.  * Function Implementations
  15.  * ---------------------------------------------*/
  16.  
  17. void ltc2450_init() {
  18.     UBRR0 = 0;
  19.     // CS is output
  20.     SPI_DDR |= (1<<SPI_CS);
  21.     LTC2450_UNSELECT();
  22.     // SCK is output
  23.     SPI_DDR |= (1<<SPI_SCK);
  24.     // MISO is input
  25.     SPI_DDR &= ~(1<<SPI_MISO);
  26.     //select MSPIM, SPI-mode 0
  27.     UCSR0C = (1<<UMSEL01)|(1<<UMSEL00)|(0<<UCPHA0)|(0<<UCPOL0);
  28.     //enable receiver
  29.     UCSR0B = (1<<RXEN0) | (1<<TXEN0);
  30.     //set "baudrate" to maximum ( BAUD=Fosc/2(UBRR0+1) )
  31.     UBRR0 = 0;
  32.     // clear old sample
  33.     ltc2450_read();
  34. }
  35.  
  36. uint16_t ltc2450_read() {
  37.     uint8_t msb, lsb;
  38.     LTC2450_SELECT();
  39.     // read MSB
  40.     UDR0 = 0xFF;
  41.     while (!(UCSR0A&(1<<RXC0)));
  42.     msb = UDR0;
  43.     // read LSB
  44.     UDR0 = 0xFF;
  45.     while (!(UCSR0A&(1<<RXC0)));
  46.     lsb = UDR0;
  47.     LTC2450_UNSELECT();
  48.     uint16_t result = (((uint16_t)msb) << 8) | ((uint16_t)lsb << 0);
  49.     return result;
  50. }
  51.  
  52.