ADC Application of STM8S

Publisher:鑫森淼焱Latest update time:2015-10-19 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Software environment: IAR FOR STM8 V1.0

//Author: Nicole

//Function: AD single conversion program, suitable for temperature collection and voltage collection
//Date: 2010.11.05

#include "iostm8s105c6.h"
unsigned int DATA;
unsigned int DATAH;
unsigned int DATAL;
// Function: delay function
// Input parameter: ms -- the number of milliseconds to delay, assuming the CPU frequency is 2MHZ
void DelayMS(unsigned int ms)
{
  unsigned char i;
  while(ms != 0)
  {
    for(i=0;i<250;i++)
    {
    }
    for(i=0;i<75;i++)
    {
    }
    ms--; 
  }     
}
// Clock configuration default
void CLK_Init(void)
{
}

// Set PD0 to push-pull output to push the LED
void GPIO_Init(void)
{
  PD_DDR = 0x01;
  PD_CR1 = 0x01;              
  PD_CR2 = 0x00;
}

// Initialize A/D module
void ADC_Init(void)
{
  ADC_CR2 = 0x00;              // A/D result data is left-aligned
  ADC_CR1 = 0x00;              // ADC clock = main clock/2=1MHZ                       
                              // ADC conversion mode = single
                              // Disable ADC conversion
  ADC_CSR = 0x00;              // Select channel 0
  ADC_TDRL = 0x01;             // Disable Schmitt trigger 
}
main()
{
  int i;
  GPIO_Init();
  ADC_Init();
  while(1)                     // Enter infinite loop                             

  {

    ADC_CR1 = 0x01;            // Set the lowest bit of the CR1 register to 1, enabling ADC conversion
    for(i=0;i<100;i++);        // Delay for a period of time, at least 7uS, to ensure that the ADC module is powered on
    ADC_CR1 = ADC_CR1 | 0x01; // Set the lowest bit of the CR1 register to 1 again
                              // Enable ADC conversion
    while((ADC_CSR & 0x80) == 0); // Wait for ADC to end      

    DATAH = ADC_DRH;               // Read the high 8 bits of the ADC result
   
    DATAL = ADC_DRL;
   
    DATA = (DATAH<<2) + DATAL; // Get data with ten-bit precision
   
    //PD_ODR = 0x01;    // PD0 turns off
   
    DelayMS(DATA);    // Delay for a while
   
    PD_ODR = PD_ODR ^ 0x01;
   
   // DelayMS(DATA); 
   
  }

}

Keywords:STM8S Reference address:ADC Application of STM8S

Previous article:Power-on/power-off reset of STM8S
Next article:STM8S-Application of independent watchdog

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号