//RFID Application for Biological Telemetry
//By Diana M. Rodriguez Tobon June, 2005
//The code detects the field clock and controls the MOD input in the //transponder 
// It also reads the temperature of the animal and codes it into //manchester code 
//protocol: byte(00000000) byte(11111111) byte(11111111) 100 byte(data)   
/* This code is for the AtmelTiny13 Microcontroller */


/************************************
//Port Description   

Connect XT1 on PORTE to PB3(XTAL1 on 2323) on PORTB on the STK500

Inputs:
B.2 = Field Clock Input (T0)
B.4 = ADC input ADC2

Outputs:
B.0 = Manchester data output
B.1 = 2 KHz Clock


/************************************
//Includes     
#include <Tiny13.h>


/************************************
//Function Prototypes 
void initialize(void);   //Initialization          


/************************************
//Global Variables  

unsigned char voltageD;  //digital voltage from ADC
unsigned char index;	 // byte number from 0 to 103
unsigned char byte;	 //byte to transmit
unsigned char halfcycle; //,number of half-cycles field clock has gone
       //through transmission 


//**********************************************************
//timer 0 compare Interrupt Sub Routine
//Enter here every field clock rising edge
interrupt [TIM0_COMPA] void timer0_com(void)
{
   // Three sync bytes (00-ff-ff), then 100 data bytes.
  	
	PORTB.1 = ~PORTB.1; //field clock for debugging 
	   
    // Byte boundary.
    if (halfcycle == 16)   //if byte has been sent  
    {   			   //set next byte to transmit	

   	halfcycle = 0;	    	
        voltageD=ADCH;
	if (voltageD == 255) voltageD = 0;  //to make sure 255 is 
    							//is not valid data

      //get new analog to digital value
	ADCSRA.6=1;
        
    	if (index == 0)     byte = 0x00;	// set next byte according to 
    	else if (index < 3) byte = 0xff;	// protocol
   		else byte = voltageD;
 
    	bytenum++;
    	if (index == 103) index = 0;
    	 
    }
    
    // On the falling edge of the clock, the output matches the data
    if (halfcycle & 1)
    	PORTB.0 = (byte >> (halfcycle >> 1)) & 0x01;
    // On the rising edge of the clock, the output is the data inverted
    else
    	PORTB.0 = (~(byte >> (halfcycle >> 1))) & 0x01;

    halfcycle++; 
}                                                           


//**********************************************************       
//Entry point and infinite loop
void main(void)
{  
   initialize();
  
   while(1)
   {
   }//end while(1)
}//end main  



//********************************************************** 
//Initialize ports and variables
void initialize(void)
{
  //set up the ports 
  DDRB.0=1;	 // PORT.0 and PORT.1 are outputs 
  DDRB.1=1;
  PORTB.0=0;
  PORTB.1=0;
  
  //Variables
  index=0; 
  voltageD=0x00; 
  byte=o;
  halfcycle=0;

  	
  


  //set up timer 0   
  OCR0A = 32;  			//set the compare to 32 periods
  TCCR0A=0b00000010;		//CTC mode 
  TCCR0B=0b00000111;          //rising edge of external clock 
  TIMSK0=0b00000100;		//enable compare match
   
  //set ADC registers in ADC noise canceler(Idle mode)
  ADMUX=0b01100010;           // Vref=1.1v(internalV), select ADCH  
					// select input channel PB4,
  ADCSRA=0b10000110;		// enable the ADC, single conversion mode
          				// clk prescaler to 64=150kHZ resolution
// no adc conversion complete interrup 
// enable
  
   //turn on the interrupts
   #asm ("sei");   
  
}//end initialize  
