163 lines
3.3 KiB
C
Executable file
163 lines
3.3 KiB
C
Executable file
/*
|
||
* i2c_test_attiny85.c
|
||
*
|
||
* Created: 12/11/2021 15:26:38
|
||
* Author : 40008304
|
||
*/
|
||
|
||
#include <avr/io.h>
|
||
#define F_CPU 1000000
|
||
#include <util/delay.h>
|
||
|
||
#define FRONT 0x51
|
||
#define RIGHT 0x52
|
||
#define LEFT 0x53
|
||
#define NOTHING 0x54
|
||
#define ERROR 0xF0
|
||
#define VOLTMIN 1
|
||
|
||
void led1_init(void);
|
||
void led2_init(void);
|
||
void led1_stop(void);
|
||
void led2_stop(void);
|
||
void ADC_init(void);
|
||
char ADC_read_value(void);
|
||
float ADC_averaging(void);
|
||
float measureLed1(void);
|
||
float measureLed2(void);
|
||
char compareLed(void);
|
||
|
||
int main(void)
|
||
{
|
||
DDRB |= (1<<PB0);
|
||
DDRB |= (1<<PB2);
|
||
led2_init();
|
||
led1_init();
|
||
ADC_init();
|
||
char sendingData;
|
||
while (1)
|
||
{
|
||
sendingData = compareLed();
|
||
switch(sendingData)
|
||
{
|
||
case NOTHING:
|
||
PORTB |= (1<<PINB0); //les deux sont eteintes
|
||
PORTB |= (1<<PINB2);
|
||
break;
|
||
case FRONT:
|
||
PORTB &= ~(1<<PINB0); //les deux sont allumees
|
||
PORTB &= ~(1<<PINB2);
|
||
break;
|
||
case RIGHT:
|
||
PORTB &= ~(1<<PINB0); //a droite allumee (led0)
|
||
PORTB |= (1<<PINB2);
|
||
break;
|
||
case LEFT:
|
||
PORTB |= (1<<PINB0); //a gauche allumee (led1)
|
||
PORTB &= ~(1<<PINB2);
|
||
break;
|
||
default:
|
||
break;
|
||
};
|
||
};
|
||
}
|
||
|
||
void led1_init(void)
|
||
{
|
||
DDRB |= (1<<PB1);
|
||
TCCR1|=(1<<CTC1)|(1<<COM1A0); //on active le mode CTC en comparant OCR1C, COM1A1 <20> 1 pour desactiver /OC1A
|
||
OCR1C=16; //d'apres retroingenieurie OCR1C=16
|
||
TCCR1|=(1<<CS10); //on active la clock avec CS10 = 1, pas de prediv (plus precis pour haute freq)
|
||
}
|
||
|
||
void led2_init(void)
|
||
{
|
||
DDRB |= (1<<PB4);
|
||
GTCCR|=(1<<COM1B0); //COM1B0 a 1 pour desactiver /OC1A
|
||
OCR1B=16; //d'apres retroingenieurie OCR1B=16
|
||
TCCR1|=(1<<CS10); //on active la clock avec CS10 = 1, pas de prediv (plus precis pour haute freq)
|
||
}
|
||
|
||
void led1_stop(void)
|
||
{
|
||
TCCR1 &= ~(1<<COM1A0);
|
||
}
|
||
|
||
void led2_stop(void)
|
||
{
|
||
GTCCR &= ~(1<<COM1B0);
|
||
}
|
||
|
||
void ADC_init(void)
|
||
{
|
||
DDRB &=~(1<<PB3); //Pin B3 en entree
|
||
ADMUX |= (1<<MUX1)|(1<<MUX0)|(1<<ADLAR); //MUX = 0010 donc sortie PB4 et ADLAR a 1 pour avoir les bits de poids fort en premier
|
||
ADCSRA |= (1<<ADEN)|(1<<ADPS2); //On divise 1Mhz par 16 (62.5 KHz) < 200 KHz, ADEN conversion autorisee
|
||
};
|
||
|
||
char ADC_read_value(void)
|
||
{
|
||
ADCSRA|=(1<<ADSC);
|
||
while(ADCSRA & (1<<ADSC));
|
||
return ADCH;
|
||
};
|
||
|
||
float ADC_averaging(void)
|
||
{
|
||
float valeur_finale=0;
|
||
char precision;
|
||
for(precision = 75; precision > 0 ; precision --)
|
||
{
|
||
valeur_finale = valeur_finale +(((ADC_read_value() * 0.01961) - valeur_finale) / 15);
|
||
}
|
||
return valeur_finale;
|
||
};
|
||
|
||
float measureLed1(void)
|
||
{
|
||
float adcLed;
|
||
led2_stop();
|
||
led1_init();
|
||
_delay_ms(250);
|
||
adcLed=ADC_averaging();
|
||
_delay_ms(250);
|
||
return adcLed;
|
||
};
|
||
|
||
float measureLed2(void)
|
||
{
|
||
float adcLed;
|
||
led1_stop();
|
||
led2_init();
|
||
_delay_ms(250);
|
||
adcLed=ADC_averaging();
|
||
_delay_ms(250);
|
||
return adcLed;
|
||
};
|
||
|
||
char compareLed(void)
|
||
{
|
||
float adcLed1 = measureLed1();
|
||
float adcLed2 = measureLed2();
|
||
float adcCompare = adcLed1 - adcLed2;
|
||
if (adcCompare < -VOLTMIN) // adcLed2 > adcLed1
|
||
{
|
||
return LEFT;
|
||
}
|
||
else if (adcCompare > VOLTMIN)
|
||
{
|
||
return RIGHT;
|
||
}
|
||
else
|
||
{
|
||
if((adcLed1 > VOLTMIN) && (adcLed2 > VOLTMIN))
|
||
{
|
||
return FRONT;
|
||
}
|
||
else
|
||
{
|
||
return NOTHING;
|
||
};
|
||
};
|
||
};
|
||
|