/*********************************************
This program was produced by the
CodeWizardAVR V1.23.8c Evaluation
Automatic Program Generator
© Copyright 1998-2003 HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro

Project : 
Version : 
Date    : 06.09.2005
Author  : Freeware, for non-commercial use only
Company : 
Comments: 


Chip type           : ATmega32
Program type        : Application
Clock frequency     : 16,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 512
*********************************************/

#include <mega32.h>  
#include <spi.h> 
#include <delay.h>
       

#define DREQ PINB.0 
#define XDCS PORTB.1
#define XCS PORTB.2
#define XRESET PORTB.3
#define SS PORTB.4
#define MOSI PORTB.5
#define MISO PINB.6
#define SCK PORTB.7

  
#define test1 PORTA.1
#define test2 PORTA.2
//#define SPIWait() {while(!(SPSR.7));};


unsigned char TiClk;  

// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
	unsigned char data;
	data=SPDR;
	// Place your code here
}
// Timer 0 interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
	TiClk++;
	
} 


// Declare your global variables here 
   
void SPIWait()
	{
	while (!(SPSR.7));
 }  
   
void InitSinusTest() {
      	do{                   
		XRESET=0;		//VS 1011 Reset Lo     
        	delay_ms(100);
      		spi(0xFF);    
      		XCS = 1;            	//xCS auf Hi
      		XDCS = 1;            	//xDCS auf Hi
      		XRESET=1;      		//VS 1011 Reset Hi   
      		delay_ms(100);                 
   
       		XCS=0;                	//XCS low (am eingang)
 
		//schreiben ins Register
            }    
  	   	while(!DREQ); 
  	
      		test1=SPSR.7;
    		spi(0x02);		//Schreiben
       		SPIWait();
      		spi(0x00); 	        //Adresse  
       		SPIWait();
       		spi(0x08);              //Daten
       		SPIWait();
       		spi(0x20);              //Daten   
       		SPIWait();
          	
		XCS=1;                //XCS  high (am eingang)

	
} 
       


void main(void)  { 


// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out 
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func0=In Func1=Out Func2=Out Func3=Out Func4=In Func5=Out Func6=In Func7=Out 
PORTB=0x00;
DDRB=0xbe;
// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
GICR|=0x00;
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 1000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0xD1;
SPSR=0x00;

// Clear the SPI interrupt flag
#asm
    in   r30,spsr
    in   r30,spdr
#endasm

// Global enable interrupts
#asm("sei")
 	
     
InitSinusTest();	//Resetten, Control-Daten senden       
 
 
while (1)
      {
        PORTA.0=!PORTA.0;
    	XDCS=0;                // XDCS auf low (am eingang)
       
                                             
       
     	//Sinus Test
   	spi(0x53);   
   	SPIWait();  
	spi(0xEF);
	SPIWait();
	spi(0x6E);
	SPIWait();
 	spi(0x7D);
 	SPIWait();                 
	spi(0x00);
	SPIWait();
	spi(0x00);
	SPIWait();  
	spi(0x00);
	SPIWait();  
	spi(0x00);
	SPIWait();      
  	 
	      
        delay_ms(500);          // 500 ms warten! 
        
        
        //Sinus Test Ende!   
 	spi(0x45);
 	SPIWait();
	spi(0x78);
	SPIWait();
 	spi(0x69);
 	SPIWait();                
	spi(0x74);
	SPIWait();
	spi(0x00);
	SPIWait();  
	spi(0x00);
	SPIWait();  
	spi(0x00);
	SPIWait();    
 	spi(0x00); 
 	SPIWait(); 
     
      	delay_ms(500);          // 500 ms warten! 
     
        XDCS=1;  
      // SPIWait()
	
      };
}
