Senin, 29 September 2014

/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 9/29/2014
Author  :
Company :
Comments:


Chip type               : ATmega32
Program type            : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega8.h>
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>
#include <string.h>

float sensor1,sensor2;
char *suhu,*suhu2;
unsigned char data[14];
unsigned char crc,mon;
int i;
//unsigned char cek[4];

   int cnt=0;
   unsigned char dataq;  
   unsigned char tempCommandType=0;
   unsigned char commandType=0;

// Standard Input/Output functions
#include <stdio.h>

#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x40




// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the AD conversion result
adc_data[input_index]=ADCW;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
   input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
}

// Declare your global variables here
unsigned char kbhit(void){
//return nonzero if char waiting  polled version
unsigned char b;

  b=0;
  if(UCSRA & (1<<RXC))
  { b=1;
    PORTB ^=(1<<PORTB0);
  }
  return b;
}

void adc1(void)
    {
    sensor1=((float) adc_data[0])/10.24f;
    suhu=(char*)&sensor1; 
    //printf("%.2f",sensor1);
    crc=0;
    data[0]=0xAA;
    data[1]=0x01;
    data[2]=0xDA;
    data[3]=suhu[0];
    data[4]=suhu[1];
    data[5]=suhu[2];
    data[6]=suhu[3];
    data[7]=0x00;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    data[11]=0x00;
    data[12]=0x00;
    data[13]=0x00; 
   
    for(i=0;i<14;i++)
    {
    crc=crc+data[i];
    mon=256-crc;      
    };
    
    putchar(data[0]);
    putchar(data[1]);
    putchar(data[2]);
    putchar(data[3]);
    putchar(data[4]);
    putchar(data[5]);
    putchar(data[6]);
    putchar(data[7]);
    putchar(data[8]);
    putchar(data[9]);
    putchar(data[10]);
    putchar(data[11]);
    putchar(data[12]);
    putchar(data[13]);
    putchar(mon); 
    
   //printf("%.2f",sensor1);
   }  
  
void adc2(void)
    {
    sensor2=((float) adc_data[1])/2.8444f;
    suhu2=(char*)&sensor2; 
    //printf("%.2f",sensor2);
    crc=0;
    data[0]=0xAA;
    data[1]=0x01;
    data[2]=0xDB;
    data[3]=suhu2[0];
    data[4]=suhu2[1];
    data[5]=suhu2[2];
    data[6]=suhu2[3];
    data[7]=0x00;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    data[11]=0x00;
    data[12]=0x00;
    data[13]=0x00; 
   
    for(i=0;i<14;i++)
    {
    crc=crc+data[i];
    mon=256-crc;      
    };
   
    putchar(data[0]);
    putchar(data[1]);
    putchar(data[2]);
    putchar(data[3]);
    putchar(data[4]);
    putchar(data[5]);
    putchar(data[6]);
    putchar(data[7]);
    putchar(data[8]);
    putchar(data[9]);
    putchar(data[10]);
    putchar(data[11]);
    putchar(data[12]);
    putchar(data[13]);
    putchar(mon);  }

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTB=0x00;
DDRB=0x01;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;


// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 38400
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Global enable interrupts
#asm("sei")




while (1)
      {    
     
      while(kbhit()==0);
   dataq=getchar();
  
   switch(cnt)
   { case 0:  if(dataq==0x55)
                 cnt++;
              break;
     case 1: 
              if(dataq==0x01)
                  cnt++;
              else if(dataq==0x55)
                  cnt=1;
              else
                  cnt=0;   
              break;
     case 2:  if(dataq==0xDa)
              { cnt++;
                tempCommandType=1;
              }   
              else if(dataq==0xdb)
              { cnt++;
                tempCommandType=2;
              }              
              else if(dataq==0x55)
              {  cnt=1;                
              }
              else
              {   cnt=0;                 
              }
              break;
     case 3:  if(dataq==0xD0 && tempCommandType==1)
                 commandType=1;
              else if(dataq==0xCf && tempCommandType==2)
                 commandType=2;  
              else if(dataq==0x55)
                 cnt=1;
              else
                 cnt=0;
              break;     
                             
                                          
   }
  
  
   if(commandType==1)
   { //baca winspeed
      adc1();
      commandType=0;
      cnt=0;
   }               
   else if(commandType==2)
   { //baca windir
      adc2();
      commandType=0;
      cnt=0;
   }

     
      /*
      if(kbhit()){cek[0]=getchar();if(cek[0]==0x55){delay_ms(10);
      if(kbhit()){cek[1]=getchar();if(cek[1]==0x01){
      if(kbhit()){cek[2]=getchar();if(cek[2]==0xda){
      if(kbhit()){cek[3]=getchar();if(cek[3]==0xd0){adc1();
      }}
      }
      if(cek[2]==0xdb){if(kbhit()){cek[3]=getchar();if(cek[3]==0xcf){adc2();
      }}
      }
      }
     
      }}
     

      }}*/

      }       
}
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 9/29/2014
Author  :
Company :
Comments:


Chip type               : ATmega32
Program type            : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega32.h>
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>
#include <string.h>

float sensor1,sensor2;
char *suhu,*suhu2;
unsigned char data[14];
unsigned char crc,mon;
int i;
unsigned char cek[4];

// Standard Input/Output functions
#include <stdio.h>

#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned int adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x40

// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the AD conversion result
adc_data[input_index]=ADCW;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
   input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
}

// Declare your global variables here
unsigned char kbhit(void){
//return nonzero if char waiting  polled version
unsigned char b;

  b=0;
  if(UCSRA & (1<<RXC)) b=1;
  return b;
}

void adc1(void)
    {
    sensor1=adc_data[0]/10;
    suhu=(char*)&sensor1; 
    //printf("%.2f",sensor1);
    crc=0;
    data[0]=0xAA;
    data[1]=0x01;
    data[2]=0xDA;
    data[3]=suhu[0];
    data[4]=suhu[1];
    data[5]=suhu[2];
    data[6]=suhu[3];
    data[7]=0x00;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    data[11]=0x00;
    data[12]=0x00;
    data[13]=0x00; 
   
    for(i=0;i<14;i++)
    {
    crc=crc+data[i];
    mon=256-crc;      
    };
    
    putchar(data[0]);
    putchar(data[1]);
    putchar(data[2]);
    putchar(data[3]);
    putchar(data[4]);
    putchar(data[5]);
    putchar(data[6]);
    putchar(data[7]);
    putchar(data[8]);
    putchar(data[9]);
    putchar(data[10]);
    putchar(data[11]);
    putchar(data[12]);
    putchar(data[13]);
    putchar(mon); 
    
   //printf("%.2f",sensor1);
   }  
  
void adc2(void)
    {
    sensor2=adc_data[1]/3;
    suhu2=(char*)&sensor2; 
    //printf("%.2f",sensor2);
    crc=0;
    data[0]=0xAA;
    data[1]=0x01;
    data[2]=0xDB;
    data[3]=suhu2[0];
    data[4]=suhu2[1];
    data[5]=suhu2[2];
    data[6]=suhu2[3];
    data[7]=0x00;
    data[8]=0x00;
    data[9]=0x00;
    data[10]=0x00;
    data[11]=0x00;
    data[12]=0x00;
    data[13]=0x00; 
   
    for(i=0;i<14;i++)
    {
    crc=crc+data[i];
    mon=256-crc;      
    };
   
    putchar(data[0]);
    putchar(data[1]);
    putchar(data[2]);
    putchar(data[3]);
    putchar(data[4]);
    putchar(data[5]);
    putchar(data[6]);
    putchar(data[7]);
    putchar(data[8]);
    putchar(data[9]);
    putchar(data[10]);
    putchar(data[11]);
    putchar(data[12]);
    putchar(data[13]);
    putchar(mon);  }

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 19200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x26;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 93.750 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCF;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      if(kbhit()){cek[0]=getchar();if(cek[0]==0x55){delay_ms(10);
      if(kbhit()){cek[1]=getchar();if(cek[1]==0x01){
      if(kbhit()){cek[2]=getchar();if(cek[2]==0xda){
      if(kbhit()){cek[3]=getchar();if(cek[3]==0xd0){adc1();
      }}
      }
      if(cek[2]==0xdb){if(kbhit()){cek[3]=getchar();if(cek[3]!=0xcf){printf("sa(d0)lah");}if(cek[3]==0xcf){adc2();
      }}
      }
      }
     
      }}
     

      }}

      }  
}