MIKROKONTROLER
I. TUJUAN
Mahasiswa mampu memahami prinsip dasar pengaturan constant voltage dan
menerapkannya menggunakan pemrograman pada mikrokontroller ARM STM32F407.
Dalam sumber tegangan ideal, resistansi harus nol dan semua voltase dijatuhkan di
tahanan beban. Namun, sumber tegangan ideal biasanya tidak mungkin dilakukan, dan sumber
tegangan tipikal masih memiliki beberapa bentuk tahan internal.
ADC Simultan
Bila Vref diset pada nilai 5 Volt, maka dari gambar 3 dapat didapatkan :
V(-) untuk C7 = Vref * (13/14) = 4,64
V(-) untuk C6 = Vref * (11/14) = 3,93
V(-) untuk C5 = Vref * (9/14) = 3,21
V(-) untuk C4 = Vref * (7/14) = 2,5
V(-) untuk C3 = Vref * (5/14) = 1,78
V(-) untuk C2 = Vref * (3/14) = 1,07
V(-) untuk C1 = Vref * (1/14) = 0,36
Misal :
Vin diberi sinyal analog 3 Volt, maka output dari C7=0, C6=0, C5=0, C4=1, C3=1, C2=1, C1=1,
sehingga didapatkan output ADC yaitu 100 biner
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "lcd_character.h"
/* USER CODE BEGIN Includes */
int main(void)
{
int a;
float SP, PV, Vo, Error, POT1;
char pvoltage[20],spoint[20],error[20],vout[20];
/* USER CODE BEGIN 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
for(i=0;i<=500;i++)
{
Error=SP-PV;
if(Error>0)
{
PV=PV+0.1;
}
if(Error<0)
{
PV=PV-0.1;
}
if(Error==0)
{
PV=PV;
}
}
Vo=PV;
sprintf(vout,"Vo:%.2f",Vo);
lcd_gotoxy(8,1);
lcd_puts(vout);
sprintf(spoint,"SP:%.2f",SP);
lcd_gotoxy(8,0);
lcd_puts(spoint);
sprintf(error,"E :%.2f",E);
lcd_gotoxy(0,0);
lcd_puts(error);
}
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
ADC_ChannelConfTypeDef sConfig;
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number
of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION12b;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 8;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc1);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = 3;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = 4;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_12;
sConfig.Rank = 5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_13;
sConfig.Rank = 6;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_14;
sConfig.Rank = 7;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer
and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = 8;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**
* Enable DMA controller clock
*/
void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__DMA2_CLK_ENABLE();
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE***
VI. ANALISA
Pada percobaan constant voltage kali ini bertujuan untuk memperoleh tegangan output
yang konstan sesuai dengan set point yang telah ditentukan, meskipun tegangan input yang
diberikan berubah-ubah sesuai dengan putaran potensio meter.
Pada percobaan kali ini, set point disetting 90V, dengan rumus error = SP – PV, dimana
PV merupakan present value atau nilai tegangan yang diset pada potensiometer. Pada program,
apabila error>0 maka nilai dari PV=PV+0.1, apabila error<0 maka nilai PV=PV-0.1, dan apabila
error=0 maka nilai PV=PV. Proses increment dan decrement tersebut pada program dilakukan
sebanyak 500 kali sesuai dengan program for.
POT1=Nilai_ADC[5];
POT1=(POT1/4095)*100;
Setting PV pada program
sprintf(pvoltage,"PV:%.2f",POT1);
melalui pembacaan ADC
lcd_gotoxy(0,1);
pada potensiometer
lcd_puts(pvoltage);
PV = POT1;
SP=90; Setting SP pada program
for(i=0;i<=500;i++)
{
Error=SP-PV; Perumusan error pada program
if(Error>0)
{ Program pada if untuk error>0,
PV=PV+0.1; maka nilai PV akan increment
}
Program for untuk increment
if(Error<0)
atau decrement nilai PV, pada
{ Program pada if untuk error<0, program for ini disetting
PV=PV-0.1; maka nilai PV akan decrement sebanyak 500 iterasi
}
if(Error==0)
{
PV=PV; Program pada if untuk error=0
}
} Nilai Vo merupakan nilai akhir
Vo=PV; dari PV setelah melewati banyak
iterasi
Pada percobaan pertama, nilai PV diset sekitar 2V, dan diperoleh tegangan output
Vo=52V dengan error=38V. Pada percobaan kedua, nilai PV diset 25.4V, dan diperoleh
Vo=75.5V dengan error=14.6V. Dari kedua percobaan tersebut, nilai Vo tidak mencapai set
point. Hal ini karena dengan nilai increment decrement 0.1 setiap iterasi, apabila terdapat 500
iterasi maka penambahan PV yang dimungkinkan hanya sebesar 50V, sehingga dengan set
PV=2V maka diperoleh Vo=2+50=52 V.
Pada percobaan ketiga dengan set PV=43.1V, diperoleh Vo=89.9V yang mendekati SP.
Hal ini berarti nilai iterasi sebanyak 500 dengan increment decrement sebesar 0.1 dapat
menjangkau nilai set point SP=90V. Sehingga pada percobaan constant voltage ini terdapat batas
minimum tegangan sehingga diperoleh tegangan output Vo yang mendekati SP. Secara
perhitungan, tegangan minimum PV untuk mencapai setpoint yaitu PV=SP-50V. 50V
merupakan tegangan maksimum yang dapat diincrementkan atau didecrementkan pada tegangan
PV dengan iterasi sebanyak 500 dan nilai increment atau decrement sebesar 0.1.
Dengan mengatur PV=100V atau lebih besar dari set point, juga didapatkan tegangan
output Vo yang mendekati SP. Artinya program diatas bekerja untuk tegangan present value PV
dibawah maupun diatas set point SP.
Untuk lebih jelasnya, akan dipaparkan pada video dengan link dibawah ini:
https://youtu.be/-P5-Sdc2Pyg
VII. KESIMPULAN
Berdasarkan praktikum yang telah dilakukan dapat ditarik kesimpulan bahwa :
1. Pemrograman constant voltage bertujuan untuk memperoleh tegangan output yang
mendekati set point yang telah ditentukan, meskipun dengan tegangan input yang
berubah-ubah sesuai dengan pengaturan tegangan input awal.
2. Untuk memperoleh tegangan output yang mendekati set point, maka dilakukan increment
atau decrement terhadap tegangan input dengan banyak iterasi, bergantung pada nilai
awal tegangan input tersebut.
3. Apabila present value kurang dari set point maka akan diincrementkan, sedangkan
apabila pesent value lebih dari set point maka akan didecrementkan.