⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upsd33_adc.c

📁 基于uPSD3334单片机pwm波形产生程序
💻 C
字号:
/*------------------------------------------------------------------------------
upsd3300_adc.c


Description:
The uPSD3300 ADC device driver is intended to provide functions to initialize
and read the analog to digital converter for the uPSD family.
See uPSD3300_adc.h for function proto types.

------------------------------------------------------------------------------*/



/*#include "upsd3300.h"
#include "upsd3300_hardware.h"
#include "upsd33_adc.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned  int

#if (FREQ_OSC > 20000)                      // Set up ADC  Divider
      #define ADC_CLOCK_DIVIDER 1
#else 
      #define ADC_CLOCK_DIVIDER 0
#endif
		*/

/*****************************************************************************
*                         ADC_EnableAll()
*功能:ADC使能,设置P1口通道为ADC输入,选择ADC时钟频率
*参数:无
******************************************************************************/
void ADC_EnableAll(void) 
{
    ACON = 0;                           // 关闭ADC

    P1SFS0 = 0xFF;
    P1SFS1 = 0xFF;                      // 选择P1口相应位为ADC输入
    ADCPS =(0x08 + 1);  //使能ADC时钟,设置时钟频率
    _nop_();
    _nop_();
    
    ACON = 0x20;                        // 使能ADC;Enable ADC

    // NOTE: USER CODE MUST WAIT AT LEAST 20 MS before calling ADC_Read (Using the ADC)
}



/******************************************************************************
*                       ADC_Init(channel)
*功能描述:ADC初始化,选择模拟通道输入,初始化时钟,关闭ADC中断
*参    数:channel - uchar 选择ADC通道
*注    意:当ADC通道改变得时候,此子程序一定要被调用
*****************************************************************************/
void ADC_Init (unsigned char channel) 
{
    unsigned char temp;
    ACON = 0;                           

    temp = (0x01) << channel;           // 选择通道
    P1SFS0 |= temp;         
    P1SFS1 |= temp;                     // 设置P1口相应位为ADC输入

    ADCPS =(0x08 + ADC_CLOCK_DIVIDER);  // 使能ADC时钟,设置时钟频率
    _nop_();
    ACON = 0x20;                        // 允许ADC转换

    // NOTE: USER CODE MUST WAIT AT LEAST 20 MS before calling ADC_Read (Using the ADC)
}

/******************************************************************************
*                      uint  ADC_Read(channel)
*功能描述:读A/D转换后的数据
*参    数:channel,初始化程序中选择的通道
*返 回 值:ADC转换结果,12位
*注    意:该子程序调用前,ADC_Init()必须先被调用
*******************************************************************************/
unsigned int ADC_Read( unsigned char channel )
{
    unsigned int  temp_ADC_result;  
    ACON &= 0xE0;                        //清除输入通路  ~(00101110B) = (11010001B)

    ACON |= (channel<<2);                 //选择通路
    _nop_ ();
    _nop_ ();   

    ACON |= 0x02;                           //开始ADC转换
    _nop_ ();                               //延时一个机器周期: ADST: 1->0

    while( (ACON & 0x01) != 1 );            //等待转换结束

      // Note: For increased ADC accuracy, the while loop above should be
      //       replaced with code that puts the MCU into Idle mode via PCON
      //       and makes use of the ADC interrupt to exit the Idle mode.
      //       The user would need to enable the ADC int and define the ADC ISR.

    temp_ADC_result = (ADAT1<<8)+ADAT0;     //Calculate ADC conversion result

    return (temp_ADC_result);
}

/******************************************************************************
*                       uint ADC_Get(channel)
*功能描述:直接得到AD转换值
*参    数:channel,选择读取通道
*返 回 值:uint ADC转换结果
*****************************************************/
uint ADC_Get (uchar channel)
{  uint i,j;
   ADC_EnableAll();
   ADC_Init(channel);
   for(j=250;j>0;j--); 
   i = ADC_Read(channel);
   return(i);
}

void getVA  (uchar channel)
{ 
  uchar str_v[] = {"The Voltage is:"};
  uint temp;
  uint value;
  uchar vv[3];
  uchar templ;
  value = ADC_Get(channel);
  value = value*100;
  temp = (float)(value/1023)*5;
  temp1 = value % 51;
  if(temp1 >= 26)temp++;
  
  vv[2] = temp%10;
  vv[1] = (temp/10%)10;     //小数点后1位 
  vv[0] = temp/100;         // 个位

  printstr(0,0,str_v);
  printchar(5,1,vv[0]);
  printchar(6,1,0x2e);
  printchar(7,1,vv[0]);
  printchar(8,1,vv[0]);
}

   */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -