Skip to content

Instantly share code, notes, and snippets.

@Aunsiels
Created January 31, 2015 19:09
Show Gist options
  • Save Aunsiels/9e1f002d243521624891 to your computer and use it in GitHub Desktop.
Save Aunsiels/9e1f002d243521624891 to your computer and use it in GitHub Desktop.
with Stm32.USB; use Stm32.USB;
with Stm32.Defines; use Stm32.Defines;
with Stm32.ADC; use Stm32.ADC;
with Interfaces; use Interfaces;
procedure TestADC is
--Specific parameters.
Params : constant ADC_Params :=
(Resolution => Resolution_12b,
Scan_Conv_Mode => Disable,
Continuous_Conv_Mode => Enable,
External_Trig_Conv_Edge => None,
External_Trig_Conv => T1_CC1,
Data_Align => Right,
Nbr_Of_Conversion => 1);
--Common parameters.
Common_Params : constant ADC_Common_Params :=
(Mode => Mode_Independent,
Prescaler => Div_8,
DMA_Access_Mode => Disabled,
Two_Sampling_Delay => Delay_5Cycles);
--The value where the read datas are written
Temp_Value : Unsigned_16 := 0;
type Datas is array(0..1) of Unsigned_8;
--I use this variable to separate Temp_Value in two parts in order to be able to
--send them thanks to character (8bits).
Temp_Value1 : Datas;
for Temp_Value1'Address use Temp_Value'Address;
--The channel of the TempSensor for my card. It may also be 18. Read the doc.
ADC_Channel_TempSensor : constant ADC_Channel_Number := 16;
begin
--Common parameters initialization
ADC_Init_Common(Common_Params);
--ADC initialization
ADC_Init(1,Params);
--Channel config
Regular_Channel_Config(1,ADC_Channel_TempSensor,1,Sample_Time_144Cycles);
--Temperature sensor
ADC_TempSensorVrefintCmd(Enable);
--Activation ADC
Configure_ADC(1,Enable);
--Start conversion
Start_Conv(1);
loop
--I check if the conversion has completed.
if ADC_GetFlagStatus(1,EOC) then
--Get the value of the conversion
Temp_Value := ADC_GetConversionValue(1);
--and send it by USB.
Send("" & Character'Val(Temp_Value1(1)) & Character'Val(Temp_Value1(0)) & " ");
--Clear the flags to be sure we do not read the same value.
ADC_ClearFlag(1,EOC);
end if;
end loop;
end TestADC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment