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

📄 adc1.c

📁 c8051f新华龙单片机开发运用程序
💻 C
字号:
/*****************************************************************************
功能:实现ADC采样芯片外的模拟电压,通过LCD显示,并通过串口(J6)发送到PC机
     注:试验时把LCD电源跳线(J20_1和J20_2)联接好!
版本:V1.0
*****************************************************************************/
#include <c8051f310.h>                 // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbd;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2

#define BAUDRATE     9600              // Baud rate of UART in bps
#define SYSCLK       24500000          // SYSCLK frequency in Hz     
#define SAMPLE_RATE  50000             // Sample frequency in Hz
#define INT_DEC      256               // integrate and decimate ratio

void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void Write_CHAR(unsigned char yjchar);
void Write_COM(unsigned char yjcom );
void YJ_Init(void);
void SPI0_Init (void);

long result;
unsigned char NCDdata[10]={0x30};
//------------------------------------------------------------------------------
//main
//------------------------------------------------------------------------------
void main (void) {
   int temp_int, temp_frac,i;            // integer and fractional portions of
   unsigned int temperature;
   long  x;
                                         // Disable Watchdog timer
   PCA0MD &= ~0x40;                      // WDTE = 0 (clear watchdog timer 
                                         // enable)
   OSCICN |= 0x03;                       // Set internal oscillator to highest
                                         // setting (24500000)
   PORT_Init ();                         // initialize crossbar and GPIO
   UART0_Init ();                        // initialize UART0
   SPI0_Init ();
   Timer3_Init (SYSCLK/SAMPLE_RATE);     // initialize Timer3 to overflow at
                                         // sample rate
   ADC0_Init ();                         // init ADC

   AD0EN = 1;                            // enable ADC
   
   EA = 1;  
   while(result==0);				     //等于0,侧等待                     
   while (1) {
     EA = 0;                             // 关中断
     temperature = result;
	 temperature &=0x000fff;
     EA = 1;                             //开中断
     temperature = temperature*100L*3/1024;
     temp_int  = temperature/100;
	 temp_frac = temperature - (temp_int * 100);
    for(x=0;x<4000;x++);
	
    for(x=0;x<4000;x++);

     printf ("V_input is %02d.%02dV\n", temp_int, temp_frac);
	 
	 YJ_Init();
	 NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;NCDdata[3]=0x2e;
	 NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;NCDdata[6]=0x20;NCDdata[7]=0x20;NCDdata[8]=0x20;
	 NCDdata[9]=0x20;NCDdata[10]=0x20;NCDdata[11]=0x20;NCDdata[12]=0x20;NCDdata[13]=0x20;NCDdata[14]=0x20;
     YJ_Init();
	 for(i=0;i<6;i++)
	  {
         Write_CHAR(NCDdata[i]);}	 
	}

}
//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{   
   P0SKIP    = 0xCE;
   P1SKIP    = 0x7F;
   XBR0    = 0x03;                     // Enable UART on P0.4(TX) and P0.5(RX)                     
   XBR1    = 0x40;                     // Enable crossbar and weak pull-ups
   P2MDIN    = 0xef;                   //P2.4配置为模拟输入
//   P1SKIP    = 0x02;
   P0MDOUT |= 0xff;                    // enable TX0 as a push-pull output  
   P1MDOUT |= 0xff;
   P2MDOUT |= 0xF7;
   P3MDOUT |= 0x04;                    // P3.3 push-pull output
 
}
//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
   SCON0   = 0x10;                     // SCON0: mode 0, 8-bit UART, enable RX
   TMOD    = 0x20;                     // TMOD: timer 1, mode 2, 8-bit reload
   TH1     = 0x96;                     //-(SYSCLK/BAUDRATE/16);    
                                       // set Timer1 reload value for baudrate   
   TR1    = 1;                         // start Timer1
                                       // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1
   TI0    = 1;                         // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   ADC0CN = 0xc5;                      // ADC0 T3定时采样
   REF0CN = 0x0e;                      // 启用内部基准源
   AMX0P = 0X0C;                       // 选择采样输入源P2.4作为正输入
   AMX0N=0X1F;                         //选择采样输入源GND作为负输入ADC工作在单端方式
   ADC0CF = 0x38;			
   EIE1 |= 0x08;                       // 启用 ADC 中断
}
//-----------------------------------------------------------------------------
// Timer3配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
   CKCON=0X80;
   TMR3RL  = -counts;
   TMR3    = 0xffff;
   EIE1   &= ~0x80;//禁止定时器3中断
   TMR3CN |= 0x04;//定时器3允许
}

//-----------------------------------------------------------------------------
// ADC0采样中断
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
   static unsigned int_dec=INT_DEC;    
   static long accumulator=0L;                     
    ADC0CN &= ~0x20;				    // 清 ADC 中断标志位
    accumulator += ADC0;                // 累加ADC采样数据
    int_dec--;                          // 指针减1

    if (int_dec == 0)
	 {                                  // 累加完了吗?
      int_dec = INT_DEC;                // 指针复位
      result = accumulator>>8;
      accumulator = 0L;                  // 累加和变量清0
     }
}
//------------------------------------------------------------------------------
//END OF FILE

⌨️ 快捷键说明

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