Added tp8

This commit is contained in:
Yohan Boujon 2021-02-03 18:37:18 +01:00
parent a82c8f4bed
commit c31db35b5b
5 changed files with 483 additions and 0 deletions

32
tp8/exo1.c Executable file
View file

@ -0,0 +1,32 @@
/*
* tp8.c
*
* Created: 30/03/2021 14:10:10
* Author : yboujon1
*/
#include <avr/io.h>
#define F_CPU 3686400
#include <util/delay.h>
void pwm0_init(void);
void pwm0_setalpha(float percent);
int main(void)
{
pwm0_init();
pwm0_setalpha(25);
int i;
while (1)
{
//rien
};
}
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
}

122
tp8/exo2.c Executable file
View file

@ -0,0 +1,122 @@
/*
* tp8.c
*
* Created: 30/03/2021 14:10:10
* Author : yboujon1
*/
#include <avr/io.h>
#define F_CPU 3686400
#include <util/delay.h>
void timer1_init(int top);
void timer1_setalpha(float percent);
int main(void)
{
timer1_init(369);
timer1_setalpha(33); //soit 33% pour qu'ils soient complémentaires
while (1)
{
//rien
};
}
void timer1_init(int top){
ICR1=top;
TCCR1A|=(1<<WGM11)|(1<<COM1A1)|(3<<COM1B0);//OC1A en non-inverting et OC1B en inverting
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS10);//on prend le mode 14, on defini div = 1
DDRD|=(1<<PD4)|(1<<PD5);//PD4 et PD5 donc OC1A et OC1B en sortie
}
void timer1_setalpha(float percent){
OCR1A=percent*3.69; //soit 33% en mode non invert
OCR1B=percent*3.69; //soit 66% en mode non invert, mais en phase inverse
}
/*
* 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);
void ADC_start_conversion(void);
void uart_init(unsigned int ubrr);
void read_temp(int* e, int* d);
void uart_putchar(char c, FILE *stream);
static FILE out_rs232 = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE); //on defini le flux de sortie avec uart putchar
volatile unsigned char etat=0;
volatile unsigned int valeur;
int main(void)
{
ADC_init();
sei();
uart_init(23);
stdout =&out_rs232;
int entier, decimal;
while (1)
{
ADC_start_conversion();
if (etat == 1){
read_temp(&entier,&decimal);
printf("La temperature est : %d.%d.C\n\t",entier,decimal); //on affiche la température
etat = 0;
};
_delay_ms(500);
};
}
void ADC_init(void)
{
DDRA &=~(1<<PA2); //on met la pin2 du port A en entrée
ADMUX|=(2<<MUX0); //on règle l'input sur la PIN2 sans différentiel ni gain
ADCSRA|=(1<<ADPS2)|(1<<ADPS1)|(1<<ADEN)|(1<<ADIE); //on divise 3,6MHz par 64 pour >200KHz
//on autorise la conversion
//on autorise l'interruption
};
void ADC_start_conversion(void)
{
ADCSRA|=(1<<ADSC);
};
void read_temp(int* e, int* d)
{
float x = (0.48828125*valeur); //on converti la tension en température
*e = (int)x; //on transforme le float en int pour l'entier
if (*e<0){ // on verifie si il est négatif
*d = -(int)((x-*e)*10); //si oui on prend l'inverse pour que la décimale soit positive
}
else{
*d = (int)((x-*e)*10); //si non on fait l'entier moins la décimale et on multiplie par 10 simplement
};
};
void uart_init(unsigned int ubrr)
{
DDRD = (1<<PD1); //on met la pin1 donc TxD
UBRRL = ubrr; //le bit low reçois la valeur UBRR
UCSRB = (1<<TXEN); //on autorise le transfert
UCSRC = (1<<URSEL)|(1<<UPM1)|(3<<UCSZ0); //on active UCSRC, avec parité et sur 8 bits
};
void uart_putchar(char c, FILE *stream)
{
do{
//rien
}while(!(UCSRA & (1<<UDRE))); //on attends que le bit soit envoyé pour en renvoyé
UDR = c; //on lui affecte la valeur
};
ISR (ADC_vect){
etat = 1;
valeur = ADC;
};

86
tp8/main.c Executable file
View file

@ -0,0 +1,86 @@
/*
* 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
}

86
tp8/tp8.componentinfo.xml Executable file
View file

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Store xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="AtmelPackComponentManagement">
<ProjectComponents>
<ProjectComponent z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<CApiVersion></CApiVersion>
<CBundle></CBundle>
<CClass>Device</CClass>
<CGroup>Startup</CGroup>
<CSub></CSub>
<CVariant></CVariant>
<CVendor>Atmel</CVendor>
<CVersion>1.6.0</CVersion>
<DefaultRepoPath>D:/Programs\Atmelstudio\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\ATmega_DFP\1.6.364\include\</AbsolutePath>
<Attribute></Attribute>
<Category>include</Category>
<Condition>C</Condition>
<FileContentHash i:nil="true" />
<FileVersion></FileVersion>
<Name>include/</Name>
<SelectString></SelectString>
<SourcePath></SourcePath>
</d4p1:anyType>
<d4p1:anyType i:type="FileInfo">
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATmega_DFP\1.6.364\include\avr\iom16.h</AbsolutePath>
<Attribute></Attribute>
<Category>header</Category>
<Condition>C</Condition>
<FileContentHash>BDa+/Y5e630de26bwSjZpg==</FileContentHash>
<FileVersion></FileVersion>
<Name>include/avr/iom16.h</Name>
<SelectString></SelectString>
<SourcePath></SourcePath>
</d4p1:anyType>
<d4p1:anyType i:type="FileInfo">
<AbsolutePath>D:/Programs\Atmelstudio\7.0\Packs\atmel\ATmega_DFP\1.6.364\templates\main.c</AbsolutePath>
<Attribute>template</Attribute>
<Category>source</Category>
<Condition>C Exe</Condition>
<FileContentHash>KjvOcFWd++tbnsEMfVPd/w==</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\ATmega_DFP\1.6.364\templates\main.cpp</AbsolutePath>
<Attribute>template</Attribute>
<Category>source</Category>
<Condition>C Exe</Condition>
<FileContentHash>mkKaE95TOoATsuBGv6jmxg==</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\ATmega_DFP\1.6.364\gcc\dev\atmega16</AbsolutePath>
<Attribute></Attribute>
<Category>libraryPrefix</Category>
<Condition>GCC</Condition>
<FileContentHash i:nil="true" />
<FileVersion></FileVersion>
<Name>gcc/dev/atmega16</Name>
<SelectString></SelectString>
<SourcePath></SourcePath>
</d4p1:anyType>
</Files>
<PackName>ATmega_DFP</PackName>
<PackPath>D:/Programs/Atmelstudio/7.0/Packs/atmel/ATmega_DFP/1.6.364/Atmel.ATmega_DFP.pdsc</PackPath>
<PackVersion>1.6.364</PackVersion>
<PresentInProject>true</PresentInProject>
<ReferenceConditionId>ATmega16</ReferenceConditionId>
<RteComponents xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d4p1:string></d4p1:string>
</RteComponents>
<Status>Resolved</Status>
<VersionMode>Fixed</VersionMode>
<IsComponentInAtProject>true</IsComponentInAtProject>
</ProjectComponent>
</ProjectComponents>
</Store>

157
tp8/tp8.cproj Executable file
View file

@ -0,0 +1,157 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>7.0</ProjectVersion>
<ToolchainName>com.Atmel.AVRGCC8.C</ToolchainName>
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
<avrdevice>ATmega16</avrdevice>
<avrdeviceseries>none</avrdeviceseries>
<OutputType>Executable</OutputType>
<Language>C</Language>
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
<OutputFileExtension>.elf</OutputFileExtension>
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
<AssemblyName>tp8</AssemblyName>
<Name>tp8</Name>
<RootNamespace>tp8</RootNamespace>
<ToolchainFlavour>Native</ToolchainFlavour>
<KeepTimersRunning>true</KeepTimersRunning>
<OverrideVtor>false</OverrideVtor>
<CacheFlash>true</CacheFlash>
<ProgFlashFromRam>true</ProgFlashFromRam>
<RamSnippetAddress>0x20000000</RamSnippetAddress>
<UncachedRange />
<preserveEEPROM>true</preserveEEPROM>
<OverrideVtorValue>exception_table</OverrideVtorValue>
<BootSegment>2</BootSegment>
<eraseonlaunchrule>0</eraseonlaunchrule>
<AsfFrameworkConfig>
<framework-data>
<options />
<configurations />
<files />
<documentation help="" />
<offline-documentation help="" />
<dependencies>
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.42.0" />
</dependencies>
</framework-data>
</AsfFrameworkConfig>
<avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
<avrtoolserialnumber />
<avrdeviceexpectedsignature>0x1E9403</avrdeviceexpectedsignature>
<com_atmel_avrdbg_tool_simulator>
<ToolOptions>
<InterfaceProperties>
</InterfaceProperties>
<InterfaceName>
</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.simulator</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>Simulator</ToolName>
</com_atmel_avrdbg_tool_simulator>
<avrtoolinterface>ISP</avrtoolinterface>
<com_atmel_avrdbg_tool_stk500>
<ToolOptions>
<InterfaceProperties>
<IspClock>460800</IspClock>
</InterfaceProperties>
<InterfaceName>ISP</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>STK500</ToolName>
</com_atmel_avrdbg_tool_stk500>
<avrtoolinterfaceclock>460800</avrtoolinterfaceclock>
<ResetRule>0</ResetRule>
<EraseKey />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<ToolchainSettings>
<AvrGcc>
<avrgcc.common.Device>-mmcu=atmega16 -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\gcc\dev\atmega16"</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.symbols.DefSymbols>
<ListValues>
<Value>NDEBUG</Value>
</ListValues>
</avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\</Value>
</ListValues>
</avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcc.linker.libraries.Libraries>
<ListValues>
<Value>libm</Value>
</ListValues>
</avrgcc.linker.libraries.Libraries>
<avrgcc.assembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\</Value>
</ListValues>
</avrgcc.assembler.general.IncludePaths>
</AvrGcc>
</ToolchainSettings>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<ToolchainSettings>
<AvrGcc>
<avrgcc.common.Device>-mmcu=atmega16 -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\gcc\dev\atmega16"</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.symbols.DefSymbols>
<ListValues>
<Value>DEBUG</Value>
</ListValues>
</avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\</Value>
</ListValues>
</avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcc.linker.libraries.Libraries>
<ListValues>
<Value>libm</Value>
</ListValues>
</avrgcc.linker.libraries.Libraries>
<avrgcc.assembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\</Value>
</ListValues>
</avrgcc.assembler.general.IncludePaths>
<avrgcc.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcc.assembler.debugging.DebugLevel>
</AvrGcc>
</ToolchainSettings>
</PropertyGroup>
<ItemGroup>
<Compile Include="main.c">
<SubType>compile</SubType>
</Compile>
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>