#include <avr/io.h>
#include "AD9832.h"

// Inicicializace SPI a odvysilani dat dle materialu k ATMEGA16

void SPI_init (void){
  DDRB 	= 0xFF;
  PORTB	= 0xFF;
  //DDRB  = _BV(PB7) | _BV(PB5) | _BV(PB5);	// SCK,MOSI,PB0 as Fsync 
  PORTB = _BV(PB7) | _BV(PB0);			//  Vychozi stav	SCK a PB0 high 
  SPCR  = _BV(SPE)| _BV(MSTR)| _BV(CPOL); // 	Povolit SPI v Master mode, hodiny na sestup. hranu.
  // SPCR |= _BV(SPR0)					//	Pokud je SPR0 a SPR1 = 0, pak frekvence SCK = fosc/4 
}

void SPI_write16 (unsigned short data){ //zapis 16bit dat pomoci SPI
  unsigned char MSdata = ((data>>8) & 0xff);  	//Horni byte dat
  unsigned char LSdata = (data & 0xff);			//Dolni byte dat
  PORTB &= ~_BV(PB0);						// 	Fsync 0 --> zacatek prenosu
	SPDR = MSdata;							// 	Odeslat horni byte
  while (!(SPSR & (1<<SPIF)));			//	pockat do konce bytu
  SPDR = LSdata;							// 	Odeslat dolni byte
  while (!(SPSR & (1<<SPIF)));			//	cekani do odeslani
  PORTB |= _BV(PB0);						// 	Fsync 1 --> konec prenosu
}
