/***************************************************** This program was produced by the CodeWizardAVR V1.24.2c Professional Automatic Program Generator © Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.ro e-mail:office@hpinfotech.ro Project : Version : Date : 13/12/2010 Author : F4CG Company : F4CG Comments: Chip type : ATmega8535 Program type : Application Clock frequency : 16,000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 128 *****************************************************/ #include #include #include int TempComp=0; int CompRef=0; long NbOT=0; int t; float Vt=0; int NbT=0; int Nbio; int Vf; float Kv=250000; int Flag; int NbTF; #define CPT1 1 // I2C Bus functions #asm .equ __i2c_port=0x18 ;PORTB .equ __sda_bit=0 .equ __scl_bit=1 #endasm #include // LM75 Temperature Sensor functions #include // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm #include // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) { TempComp=TCNT0; if(NbOT>5) { t=NbOT*256+(256-CompRef)+TempComp; Vt=(float)Vt+(0.85/t)*250000; //methode à re-simplifier CompRef=TempComp; NbT=NbT+1; NbOT=0; } } // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { NbOT=NbOT+1; } // Timer 2 overflow interrupt service routine interrupt [TIM2_OVF] void timer2_ovf_isr(void) { Nbio=Nbio+1; if(Nbio==977) { Vf=(float)(Vt/NbT); Flag=1; NbTF=NbT; Vt=0; NbT=0; Nbio=0; } } #define ADC_VREF_TYPE 0x00 // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input|ADC_VREF_TYPE; // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // Declare your global variables here void main(void) { unsigned char Tampon[16]; int TensB; int ADC; int Temperature; char signe; int Charge; int Vte; int Vref; int temp; unsigned char Mode = 0; // 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=0xFF; 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=P State2=T State1=T State0=T PORTD=0x08; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 250,000 kHz // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x03; 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; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: 250,000 kHz // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x04; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Rising Edge // INT1: Off // INT2: Off GICR|=0x40; MCUCR=0x03; MCUCSR=0x00; GIFR=0x40; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x41; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off // Analog Comparator Output: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC Clock frequency: 125,000 kHz // ADC Voltage Reference: AREF pin // ADC High Speed Mode: Off // ADC Auto Trigger Source: None ADMUX=ADC_VREF_TYPE; ADCSRA=0x87; SFIOR&=0xEF; // I2C Bus initialization i2c_init(); // LM75 Temperature Sensor initialization // thyst: 75°C // tos: 80°C // O.S. polarity: 0 lm75_init(CPT1,23,24,0); // LCD module initialization lcd_init(16); // Global enable interrupts #asm("sei") ADC=read_adc(0); Vref=(ADC*(656.88/1024.00)); while (1) { ADC=read_adc(0); TensB=(ADC*(656.88/1024.00)); //methode à revoir Charge=(TensB-400)*(100.00/(Vref-400)); temp=lm75_temperature_10(CPT1); if (temp<0) { signe='-'; temp=-temp; } else {signe='+';} switch (Mode) //idée : numéroter les écrans pour choisir celui qu'on veut voir { case 0 ://affichage général { lcd_gotoxy(0,0);//RAZ sprintf(Tampon,"Temp = %c%i.%u\xdfC",signe,temp/10,temp%10);//mise en forme temperature lcd_puts(Tampon); lcd_gotoxy(0,1); sprintf(Tampon,"Batterie = %3d ",Charge); lcd_puts(Tampon); lcd_gotoxy(0,2); sprintf(Tampon,"Vbat = %d,%d V",TensB/10,TensB%10); l cd_puts(Tampon); if(Flag==1) { lcd_gotoxy(0,3); sprintf(Tampon," "); lcd_puts(Tampon); lcd_gotoxy(0,3); Vte=(float)15.3*NbTF; sprintf(Tampon,"V =%d,%d Km/h",Vte/10,Vte%10); lcd_puts(Tampon); Flag=0; } break; } case 1 ://Jauge batterie { lcd_gotoxy(0,0); sprintf(Tampon," Batterie "); lcd_puts(Tampon); lcd_gotoxy(0,1); sprintf(Tampon,"Restant : %3d%%",Charge); lcd_puts(Tampon); lcd_gotoxy(0,2); sprintf(Tampon," "); lcd_puts(Tampon); lcd_gotoxy(0,3); sprintf(Tampon," curseur "); lcd_puts(Tampon); break; } case 2 ://temperature { lcd_gotoxy(0,0); sprintf(Tampon," Temperature "); lcd_puts(Tampon); lcd_gotoxy(0,1); sprintf(Tampon,"Temp = %c%i.%u\xdfC",signe,temp/10,temp%10);//mise en forme temperature lcd_puts(Tampon); lcd_gotoxy(0,2); sprintf(Tampon," "); lcd_puts(Tampon); lcd_gotoxy(0,3); sprintf(Tampon," curseur "); lcd_puts(Tampon); break; } case 3 ://alerte temperature { lcd_gotoxy(0,0); sprintf(Tampon," Temperature "); lcd_puts(Tampon); lcd_gotoxy(0,1); sprintf(Tampon,"Temp = %c%i.%u\xdfC",signe,temp/10,temp%10);//mise en forme temperature lcd_puts(Tampon); lcd_gotoxy(0,2); sprintf(Tampon," "); lcd_puts(Tampon); lcd_gotoxy(0,3); sprintf(Tampon," curseur "); lcd_puts(Tampon); break; } case 4 ://alerte batterie { lcd_clear(); lcd_gotoxy(0,0); sprintf(Tampon,"!A Batterie A!"); lcd_puts(Tampon); lcd_gotoxy(0,1); sprintf(Tampon," Batterie"); lcd_puts(Tampon); lcd_gotoxy(0,2); sprintf(Tampon," trop faible"); lcd_puts(Tampon); lcd_gotoxy(0,3); sprintf(Tampon," STOP NOW !"); lcd_puts(Tampon); delay_ms(100); break; } default : { lcd_clear(); break; } }//fin switch /****choix du mode à afficher ****/ if(PINB.4 == 0x00)//PB4 en logique inverssée { Mode++; if(Mode > 2 ) Mode = 0; //retour général while(PINB.4 == 0x00)//pour fronts montants {} } if(PIND.3 == 0x00)//PD3 en logique inverssée { if(Mode == 0 ) Mode = 2; //retour général else Mode--; while(PIND.3 == 0x00)//pour fronts montants {} } if(Charge<50) { Mode = 4; } } }