Additions from 21/01/2022: Changed clock speed to 16MHz, fixed issue with ADCSRA register, transmitting actual adc data through i2c, using volatile data accross project.
This commit is contained in:
parent
4504b6121b
commit
dff2d872cd
8 changed files with 107 additions and 55 deletions
BIN
16mega.PNG
Executable file
BIN
16mega.PNG
Executable file
Binary file not shown.
After Width: | Height: | Size: 53 KiB |
|
@ -9,57 +9,57 @@
|
|||
<CSub></CSub>
|
||||
<CVariant></CVariant>
|
||||
<CVendor>Atmel</CVendor>
|
||||
<CVersion>1.8.0</CVersion>
|
||||
<DefaultRepoPath>D:/Programs\Atmelstudio\7.0\Packs</DefaultRepoPath>
|
||||
<CVersion>1.2.0</CVersion>
|
||||
<DefaultRepoPath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs</DefaultRepoPath>
|
||||
<DependentComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
|
||||
<Description></Description>
|
||||
<Files xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATtiny_DFP\1.8.332\include\</AbsolutePath>
|
||||
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATtiny_DFP\1.2.118\include</AbsolutePath>
|
||||
<Attribute></Attribute>
|
||||
<Category>include</Category>
|
||||
<Condition>C</Condition>
|
||||
<FileContentHash i:nil="true" />
|
||||
<FileVersion></FileVersion>
|
||||
<Name>include/</Name>
|
||||
<Name>include</Name>
|
||||
<SelectString></SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATtiny_DFP\1.8.332\include\avr\iotn85.h</AbsolutePath>
|
||||
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATtiny_DFP\1.2.118\include\avr\iotn85.h</AbsolutePath>
|
||||
<Attribute></Attribute>
|
||||
<Category>header</Category>
|
||||
<Condition>C</Condition>
|
||||
<FileContentHash>T0lnJZ6iliUJCzU7ZHCMPQ==</FileContentHash>
|
||||
<FileContentHash>RcYmivGpgsCGGCzeWAIjcA==</FileContentHash>
|
||||
<FileVersion></FileVersion>
|
||||
<Name>include/avr/iotn85.h</Name>
|
||||
<SelectString></SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATtiny_DFP\1.8.332\templates\main.c</AbsolutePath>
|
||||
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATtiny_DFP\1.2.118\templates\main.c</AbsolutePath>
|
||||
<Attribute>template</Attribute>
|
||||
<Category>source</Category>
|
||||
<Condition>C Exe</Condition>
|
||||
<FileContentHash>KjvOcFWd++tbnsEMfVPd/w==</FileContentHash>
|
||||
<FileContentHash>GD1k8YYhulqRs6FD1B2Hog==</FileContentHash>
|
||||
<FileVersion></FileVersion>
|
||||
<Name>templates/main.c</Name>
|
||||
<SelectString>Main file (.c)</SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATtiny_DFP\1.8.332\templates\main.cpp</AbsolutePath>
|
||||
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATtiny_DFP\1.2.118\templates\main.cpp</AbsolutePath>
|
||||
<Attribute>template</Attribute>
|
||||
<Category>source</Category>
|
||||
<Condition>C Exe</Condition>
|
||||
<FileContentHash>mkKaE95TOoATsuBGv6jmxg==</FileContentHash>
|
||||
<FileContentHash>YXFphlh0CtZJU+ebktABgQ==</FileContentHash>
|
||||
<FileVersion></FileVersion>
|
||||
<Name>templates/main.cpp</Name>
|
||||
<SelectString>Main file (.cpp)</SelectString>
|
||||
<SourcePath></SourcePath>
|
||||
</d4p1:anyType>
|
||||
<d4p1:anyType i:type="FileInfo">
|
||||
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATtiny_DFP\1.8.332\gcc\dev\attiny85</AbsolutePath>
|
||||
<AbsolutePath>C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATtiny_DFP\1.2.118\gcc\dev\attiny85</AbsolutePath>
|
||||
<Attribute></Attribute>
|
||||
<Category>libraryPrefix</Category>
|
||||
<Condition>GCC</Condition>
|
||||
|
@ -71,8 +71,8 @@
|
|||
</d4p1:anyType>
|
||||
</Files>
|
||||
<PackName>ATtiny_DFP</PackName>
|
||||
<PackPath>D:/Programs/Atmelstudio/7.0/Packs/atmel/ATtiny_DFP/1.8.332/Atmel.ATtiny_DFP.pdsc</PackPath>
|
||||
<PackVersion>1.8.332</PackVersion>
|
||||
<PackPath>C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/ATtiny_DFP/1.2.118/Atmel.ATtiny_DFP.pdsc</PackPath>
|
||||
<PackVersion>1.2.118</PackVersion>
|
||||
<PresentInProject>true</PresentInProject>
|
||||
<ReferenceConditionId>ATtiny85</ReferenceConditionId>
|
||||
<RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
|
||||
|
|
|
@ -7,32 +7,48 @@
|
|||
|
||||
#include "led_gen.h"
|
||||
|
||||
volatile unsigned char led1_state=0;
|
||||
volatile unsigned char led2_state=0;
|
||||
|
||||
void led1_init(void)
|
||||
{
|
||||
DDRB |= (1<<PB1);
|
||||
TCCR1|=(1<<CTC1)|(1<<COM1A0); //on active le mode CTC en comparant OCR1C, COM1A0 : mode toggle
|
||||
OCR1C=34; //d'apres retroingenieurie OCR1C=16
|
||||
OCR1C=68; //d'apres retroingenieurie OCR1C=16
|
||||
TCCR1|=(1<<CS10)|(1<<CS11); //on active la clock, prediv de 4
|
||||
DDRB |= (1<<PB1);
|
||||
led1_state=1;
|
||||
}
|
||||
|
||||
void led2_init(void)
|
||||
{
|
||||
DDRB |= (1<<PB4);
|
||||
GTCCR|=(1<<COM1B0); //COM1B0 : mode toggle
|
||||
OCR1C=34; //d'apres retroingenieurie OCR1C=16
|
||||
OCR1C=68; //d'apres retroingenieurie OCR1C=16
|
||||
TCCR1|=(1<<CS10)|(1<<CS11); //on active la clock, prediv de 4
|
||||
DDRB |= (1<<PB4);
|
||||
led2_state=1;
|
||||
}
|
||||
|
||||
void led1_stop(void)
|
||||
{
|
||||
DDRB &= ~(1<<PB1);
|
||||
TCCR1 &= ~(1<<COM1A0);
|
||||
led1_state=0;
|
||||
}
|
||||
|
||||
void led2_stop(void)
|
||||
{
|
||||
DDRB &= ~(1<<PB4);
|
||||
GTCCR &= ~(1<<COM1B0);
|
||||
led2_state=0;
|
||||
}
|
||||
|
||||
void timer0_init(void)
|
||||
{
|
||||
TCCR0A=0x00; //Normal mode
|
||||
TCCR0B=0x00;
|
||||
TCCR0B |= (1<<CS01); //prescaling with 8
|
||||
TCNT0=0;
|
||||
TIMSK|=(1<<TOIE0);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,5 +15,9 @@ void led1_init(void);
|
|||
void led2_init(void);
|
||||
void led1_stop(void);
|
||||
void led2_stop(void);
|
||||
void timer0_init(void);
|
||||
|
||||
volatile unsigned char led1_state;
|
||||
volatile unsigned char led2_state;
|
||||
|
||||
#endif /* LED_GEN_H_ */
|
|
@ -7,14 +7,17 @@
|
|||
|
||||
#include "led_receptor.h"
|
||||
|
||||
volatile unsigned char intr_count;
|
||||
volatile unsigned char timer_flag=0;
|
||||
volatile unsigned char seconde=0;
|
||||
|
||||
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)|(1<<ADIE)|(1<<ADATE); //ADEN conversion autorisee
|
||||
//On divise 1Mhz par 16 (62.5 KHz) < 200 KHz
|
||||
ADCSRA|= (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE); //ADEN conversion autorisee
|
||||
//On divise 16Mhz par 128 (125 KHz) < 200 KHz
|
||||
//Interruptions autorisees
|
||||
//Auto-Trigger autorise
|
||||
ADCSRB &= ~(0b111<<ADTS0); //free-running mode
|
||||
sei();
|
||||
};
|
||||
|
@ -42,25 +45,42 @@ float ADC_averaging(unsigned char val_ana)
|
|||
return valeur_finale;
|
||||
};
|
||||
|
||||
float measureLed1(void)
|
||||
char measureLed1(void)
|
||||
{
|
||||
float adcLed=69;
|
||||
led1_init();
|
||||
_delay_ms(TIMING);
|
||||
//adcLed=ADC_averaging(ADCH);
|
||||
_delay_ms(TIMING);
|
||||
led1_stop();
|
||||
char adcLed=0;
|
||||
|
||||
if(timer_flag == 1)
|
||||
{
|
||||
if(led1_state==0)
|
||||
{
|
||||
led1_init();
|
||||
adcLed=ADCH;
|
||||
}
|
||||
else
|
||||
{
|
||||
led1_stop();
|
||||
}
|
||||
}
|
||||
return adcLed;
|
||||
};
|
||||
|
||||
float measureLed2(void)
|
||||
char measureLed2(void)
|
||||
{
|
||||
float adcLed=69;
|
||||
led2_init();
|
||||
_delay_ms(TIMING);
|
||||
//adcLed=ADC_averaging(ADCH);
|
||||
_delay_ms(TIMING);
|
||||
led2_stop();
|
||||
char adcLed=0;
|
||||
|
||||
if(timer_flag == 1)
|
||||
{
|
||||
if(led2_state==0)
|
||||
{
|
||||
led2_init();
|
||||
adcLed=ADCH;
|
||||
}
|
||||
else
|
||||
{
|
||||
led2_stop();
|
||||
}
|
||||
timer_flag=0;
|
||||
}
|
||||
return adcLed;
|
||||
};
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
#include "led_gen.h"
|
||||
#include "usiTwiSlave.h"
|
||||
#define F_CPU 8000000
|
||||
#define F_CPU 16000000
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
|
@ -21,14 +21,18 @@
|
|||
#define NOTHING 0x54
|
||||
#define ERROR 0xF0
|
||||
#define VOLTMIN 1
|
||||
#define TIMING 5
|
||||
#define TIMING 10
|
||||
|
||||
void ADC_init(void);
|
||||
void ADC_start_conversion(void);
|
||||
char ADC_read_value(void);
|
||||
float ADC_averaging(unsigned char val_ana);
|
||||
float measureLed1(void);
|
||||
float measureLed2(void);
|
||||
char measureLed1(void);
|
||||
char measureLed2(void);
|
||||
unsigned char compareLed(float led1,float led2);
|
||||
|
||||
volatile unsigned char intr_count;
|
||||
volatile unsigned char timer_flag;
|
||||
volatile unsigned char seconde;
|
||||
|
||||
#endif /* LED_RECEPTOR_H_ */
|
|
@ -16,46 +16,52 @@
|
|||
#include "led_gen.h"
|
||||
#include "led_receptor.h"
|
||||
#include "usiTwiSlave.h"
|
||||
//#define F_CPU 1000000
|
||||
|
||||
#define NOM_CONSTRUC 0x08
|
||||
#define NOM_CAPTEUR 0x10
|
||||
#define VALEUR_DEMAN 0x49
|
||||
#define BASIC_MODE 0x42
|
||||
|
||||
volatile unsigned char rawVal;
|
||||
volatile float adcled1;
|
||||
volatile float adcled2;
|
||||
volatile unsigned char adcled1;
|
||||
volatile unsigned char adcled2;
|
||||
volatile unsigned char recVal;
|
||||
|
||||
/*ISR (ADC_vect){
|
||||
ISR (ADC_vect){
|
||||
adcled1=measureLed1();
|
||||
adcled2=measureLed2();
|
||||
recVal=compareLed(adcled1,adcled2);
|
||||
}*/
|
||||
//recVal=compareLed(adcled1,adcled2);
|
||||
ADCSRA |= (1<<ADSC);
|
||||
}
|
||||
|
||||
ISR (TIMER0_OVF_vect) //Interrupt vector for Timer0
|
||||
{
|
||||
if (intr_count==5) //waiting for 63 because to get 1 sec delay
|
||||
{
|
||||
timer_flag=1-timer_flag;
|
||||
intr_count=0; //making intr_count=0 to repeat the count
|
||||
}
|
||||
else intr_count++; //incrementing c upto 63
|
||||
}
|
||||
|
||||
int main( void )
|
||||
{
|
||||
float test1,test2;
|
||||
unsigned char cmd,i=0;
|
||||
unsigned char nom_capteur[8]= {'T','u','r','k','i','s','h','Z'};
|
||||
unsigned char sensor_type[8]= {'C','a','t','c','h','E','y','e'};
|
||||
uint8_t slaveAdress;//data = 0x45;
|
||||
slaveAdress = 0x01;
|
||||
MCUCR |= (1<<PUD);
|
||||
//led1_init();
|
||||
//led2_init();
|
||||
|
||||
//USI_TWI_Slave_Initialise(slaveAdress);
|
||||
usiTwiSlaveInit(slaveAdress);
|
||||
|
||||
sei();
|
||||
//ADC_init();
|
||||
//ADC_start_conversion();
|
||||
ADC_init();
|
||||
ADC_start_conversion();
|
||||
timer0_init();
|
||||
|
||||
while(1)
|
||||
{
|
||||
test1=measureLed1();
|
||||
test2=measureLed2();
|
||||
if( usiTwiDataInTransmitBuffer() )
|
||||
{
|
||||
cmd = usiTwiReceiveByte();
|
||||
|
@ -76,11 +82,11 @@ int main( void )
|
|||
break;
|
||||
|
||||
case VALEUR_DEMAN :
|
||||
usiTwiTransmitByte(0x45/*recVal*/);
|
||||
usiTwiTransmitByte(adcled1/*recVal*/);
|
||||
break;
|
||||
|
||||
case BASIC_MODE :
|
||||
usiTwiTransmitByte(0x45);
|
||||
usiTwiTransmitByte(adcled1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -228,6 +228,7 @@ Change Activity:
|
|||
/* clear all interrupt flags, except Start Cond */ \
|
||||
( 0 << USI_START_COND_INT ) | ( 1 << USIOIF ) | ( 1 << USIPF ) | \
|
||||
( 1 << USIDC ) | ( 0x0 << USICNT0 ); \
|
||||
ADCSRA=ADCSRA|(1<<ADIE)|(1<< ADEN)|(1<< ADSC); \
|
||||
}
|
||||
|
||||
#define SET_USI_TO_SEND_DATA() \
|
||||
|
@ -447,6 +448,7 @@ uint8_t usiTwiAmountDataInReceiveBuffer(void)
|
|||
|
||||
ISR( USI_START_VECTOR )
|
||||
{
|
||||
ADCSRA = ADCSRA & ~( (1<<ADIE)|(1<<ADEN) );
|
||||
uint8_t usi_pins;
|
||||
// http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__interrupts.html
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue