86 lines
No EOL
2.1 KiB
C
Executable file
86 lines
No EOL
2.1 KiB
C
Executable file
/*
|
|
* tp8.c
|
|
*
|
|
* Created: 30/03/2021 14:10:10
|
|
* Author : yboujon1
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <avr/io.h>
|
|
#define F_CPU 3686400
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
void ADC_init(void);
|
|
float read_temp(void);
|
|
int ADC_read_value(void);
|
|
int compute_pwm(float temp);
|
|
void pwm0_init(void);
|
|
void pwm0_setalpha(float percent);
|
|
|
|
int main(void)
|
|
{
|
|
float temperature;
|
|
unsigned int alpha;
|
|
DDRC = 0xFF; //port C en sortie
|
|
ADC_init(); //initialisation ADC
|
|
pwm0_init(); //initialisation pwm0
|
|
while (1)
|
|
{
|
|
ADC_read_value(); //lit la valeur d'ADC (poten
|
|
temperature = read_temp(); //lit la temperature
|
|
alpha=compute_pwm(temperature); //calcule le rapport cyclique en fonction de la température
|
|
PORTC = ~(char)temperature; //affiche temp sur les leds
|
|
pwm0_setalpha(alpha); //defini le rapport cyclique, vitesse du ventilateur
|
|
_delay_ms(100); //on attend 100 ms
|
|
};
|
|
}
|
|
void ADC_init(void)
|
|
{
|
|
DDRA &=~(1<<PA0); //on met la pin0 du port A en entrée pour le ventilateur
|
|
ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADEN); //on divise 3,6MHz par 64 pour >200KHz
|
|
//on autorise la conversion
|
|
};
|
|
|
|
float read_temp(void)
|
|
{
|
|
float x = (0.48828125*ADC_read_value()); //on converti la tension en température
|
|
return x;
|
|
};
|
|
|
|
int compute_pwm(float temp)
|
|
{
|
|
if(temp<20) //si temperature inferieur à 20
|
|
{
|
|
return 0; //alors alpha=0
|
|
}
|
|
if(temp>=20 && temp<30) //si entre 20 et 30
|
|
{
|
|
int a=(temp-20)*5+49; //fonction affine calculée
|
|
return a;
|
|
}
|
|
else
|
|
{
|
|
return 99; //Car comme c'est un int, l'approximation
|
|
//le mettra au-dessus de 100
|
|
};
|
|
}
|
|
|
|
int ADC_read_value(void)
|
|
{
|
|
ADCSRA|=(1<<ADSC);
|
|
do{
|
|
//rien
|
|
}while((ADCSRA&(1<<ADIF))==0); //on attends que la conversion soit fini avec ADIF, qui sera
|
|
//a 1 quand nous aurons fini notre conversion
|
|
ADCSRA|=(1<<ADIF);
|
|
return ADC;
|
|
}
|
|
|
|
void pwm0_init(void){
|
|
TCCR0|=(1<<WGM00)|(1<<CS00)|(1<<COM01);//mode PWM, divisé par 1, non-inverting
|
|
DDRB|=(1<<PB3);//PB3 donc OC0 en sortie
|
|
}
|
|
|
|
void pwm0_setalpha(float percent){
|
|
OCR0=percent*2.56; //on fait apha/100*256 comme expliqué precedemment
|
|
} |