You are on page 1of 5

ADC

Objective: To write a simple C program for ADC and test the same on the development board

Components Needed:
STM8AF Development Board, Connecting wires, LEDs

Software Needed:
IAR embedded workbench for STM8

Peripheral c drivers: gpio, it, adc2

Output: Output is seen using on board potentiometer and external LEDs which is connected to Port B.

C-Program
/* Includes ------------------------------------------------------------------*/

#include "stm8s.h"

/**

* @addtogroup ADC2_ContinuousConversion

* @{

*/

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

uint16_t Conversion_Value = 0;

/* Private function prototypes -----------------------------------------------*/

static void ADC_Config(void);

static void GPIO_Config(void);

void Delay(uint32_t nCount);

/* Private functions ---------------------------------------------------------*/

/**

* @brief ADC2_ContinuousConversion main entry point.

* @param None

* @retval None
Sy NO 5/3, 4TH Floor, Behind YES Bank ATM, Opp SKR Kalyana Mantapa, KundalahalliGate,
Marathahalli main road, Bangalore- 560037
PH: 9900 30 6000 / 080 2854 6010
*/

void main(void)

/* GPIO configuration ------------------------------------------*/

GPIO_Config();

/* ADC configuration -------------------------------------------*/

ADC_Config();

/* Enable EOC interrupt */

ADC2_ITConfig(ENABLE);

/* Enable general interrupts */

enableInterrupts();

/* The LEDs state is changed in the interrupt routine */

while (1)

/*Start Conversion */

ADC2_StartConversion();

GPIO_Write(GPIOB, Conversion_Value);

Delay((uint32_t)0xFFFF);

/**

* @brief Configure ADC2 Continuous Conversion with End Of Conversion interrupt


Sy NO 5/3, 4TH Floor, Behind YES Bank ATM, Opp SKR Kalyana Mantapa, KundalahalliGate,
Marathahalli main road, Bangalore- 560037
PH: 9900 30 6000 / 080 2854 6010
* enabled .

* @param None

* @retval None

*/

static void ADC_Config()

/* Init GPIO for ADC2 */

GPIO_Init(GPIOE, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);

/* De-Init ADC peripheral*/

ADC2_DeInit();

/* Init ADC2 peripheral */

ADC2_Init(ADC2_CONVERSIONMODE_SINGLE, ADC2_CHANNEL_8, ADC2_PRESSEL_FCPU_D2, \

ADC2_EXTTRIG_TIM, DISABLE, ADC2_ALIGN_RIGHT, ADC2_SCHMITTTRIG_CHANNEL8,\

DISABLE);

/**

* @brief Configure GPIO for LEDs and buttons available on the evaluation board

* @param None

* @retval None

*/

static void GPIO_Config(void)

/* Initialize LEDs mounted on STM8-128 EVAL board */

GPIO_Init( GPIOB, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_LOW_FAST);

}
Sy NO 5/3, 4TH Floor, Behind YES Bank ATM, Opp SKR Kalyana Mantapa, KundalahalliGate,
Marathahalli main road, Bangalore- 560037
PH: 9900 30 6000 / 080 2854 6010
/**

* @brief Delay.

* @param nCount

* @retval None

*/

void Delay(uint32_t nCount)

/* Decrement nCount value */

while (nCount != 0)

nCount--;

#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 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) */


Sy NO 5/3, 4TH Floor, Behind YES Bank ATM, Opp SKR Kalyana Mantapa, KundalahalliGate,
Marathahalli main road, Bangalore- 560037
PH: 9900 30 6000 / 080 2854 6010
/* Infinite loop */

while (1)

#endif

/**

* @}

*/

Sy NO 5/3, 4TH Floor, Behind YES Bank ATM, Opp SKR Kalyana Mantapa, KundalahalliGate,
Marathahalli main road, Bangalore- 560037
PH: 9900 30 6000 / 080 2854 6010

You might also like