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

📄 f32x_adc0_externalinput.c

📁 利用c8051 的adc 做按键控制led
💻 C
字号:
//-----------------------------------------------------------------------------
// F32x_ADC0_ExternalInput.c
//-----------------------------------------------------------------------------
//
// 
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include "c8051F320.h"                 // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------

sfr16 TMR2RL   = 0xca;                 // Timer2 reload value 
sfr16 TMR2     = 0xcc;                 // Timer2 counter
sfr16 ADC0     = 0xbd;                 // ADC0 result

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       12000000          // SYSCLK frequency in Hz
#define BAUDRATE     115200            // Baud rate of UART in bps
// io_key
sbit SW2 = P2^0;                          // SW2='0' means switch presse
sbit Led2 = P1^0;
#define TRUE  1
#define FALSE 0
unsigned char SW2_FLAG;
unsigned char KeyFlag;
//motor
sbit m_p   =P1^3;
sbit m_n   =P1^2;

// adc_key

sbit Led3 = P0^7;  
sbit Led1 = P1^1;                    // LED='1' means ON
unsigned int adc0_temp;
unsigned char num_flag;

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void PORT_Init (void);
void Timer2_Init(void);
void ADC0_Init(void);
void adc_key(void);
void kdelay(unsigned int kdelay);
void IO_key (void);
void Led_flicker ( unsigned int X);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) 
{
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer 
                                       // enable)

   SYSCLK_Init ();                     // Initialize system clock to 
                                       // 12MHz
   PORT_Init ();                       // Initialize crossbar and GPIO
   Timer2_Init();                      // Init Timer2 to generate 
                                       // overflows to trigger ADC
   ADC0_Init();                        // Initialize ADC0

   EA = 1;							         // enable global interrupts
   while (1) {                         // spin forever
 // Led3=0;
  //  Led2=0;
  IO_key();
   }
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
// 
// Return Value:  None
// Parameters:    None
//
// This routine initializes the system clock to use the internal 12MHz 
// oscillator as its clock source.  Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
   OSCICN = 0x83;                      // configure internal oscillator for
                                       // 12MHz / 1
   RSTSRC = 0x04;                      // enable missing clock detector
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value:  None
// Parameters:    None
//
// Configure the Crossbar and GPIO ports.
// P0.4 - UART TX (push-pull)
// P0.5 - UART RX 
// P2.4 - ADC0 analog input
// P2.2 - LED (push-pull)
// 
//-----------------------------------------------------------------------------

void PORT_Init (void)
{
  // XBR0     = 0x01;                    // Enable UART0
 //  XBR1     = 0x40;                    // Enable crossbar and weak pull-ups
 //  P0MDOUT |= 0x10;                    // Set TX pin to push-pull
//   P2MDOUT |= 0x04;                    // enable LED as a push-pull output
//   P2MDIN &= ~0x10;                    // set P2.2 as an analog input

    P2MDIN    = 0xC1;
  //  P2MDIN    = 0xFD;
    P0MDOUT   = 0x80;
    P1MDOUT   = 0x1F;                      // P1MDOUT   = 0x03;
    XBR1      = 0x40;
    Led1 =1;
    Led2 =1;
    Led3 =1; 

}

//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value:  None
// Parameters:    None
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at 100uS 
// intervals.  Timer 2 overflow automatically triggers ADC0 conversion.
// 
//-----------------------------------------------------------------------------

void Timer2_Init (void)
{
   TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
                                       // use SYSCLK as timebase, 16-bit 
                                       // auto-reload
   CKCON  |= 0x10;                     // select SYSCLK for timer 2 source
   TMR2RL  = 65535 - (SYSCLK / 10000); // init reload value for 10uS
   TMR2    = 0xffff;                   // set to reload immediately
   TR2     = 1;                        // start Timer2
}

//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Return Value:  None
// Parameters:    None
//
// Configures ADC0 to make single-ended analog measurements on pin P2.4
//  
//-----------------------------------------------------------------------------

void ADC0_Init (void)
{
   ADC0CN = 0x02;                      // ADC0 disabled, normal tracking, 
                                       // conversion triggered on TMR2 overflow

 //  REF0CN = 0x03;                      // Enable on-chip VREF and buffer
   REF0CN = 0x08; 
  // AMX0P = 0x0C;                       // ADC0 positive input = P2.4
  // AMX0N = 0x1F;                       // ADC0 negative input = GND
                                       // i.e., single ended mode
    AMX0P     = 0x09;
    AMX0N     = 0x1F;


   ADC0CF = ((SYSCLK/3000000)-1)<<3;   // set SAR clock to 3MHz

   ADC0CF |= 0x00;                     // right-justify results 

   EIE1 |= 0x08;                       // enable ADC0 conversion complete int.

   AD0EN = 1;                          // enable ADC0
    
}



//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
// 
//
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
   if (AMX0P==0x09)
    {
      adc0_temp =ADC0; // read ADC value and add to running
      adc_key();
      AMX0P     = 0x0D;
	  }
	  else if(AMX0P==0x0D)
	  {
      adc0_temp =ADC0; // read ADC value and add to running
      adc_key();
      AMX0P     = 0x0C;
      }
	  else if (AMX0P==0x0C)
	  {
	  adc0_temp =ADC0; // read ADC value and add to running
      adc_key();
      AMX0P     = 0x0B;
	  }
	  else if (AMX0P==0x0B)
	  {
	  adc0_temp =ADC0; // read ADC value and add to running
      adc_key();
      AMX0P     = 0x0A;
	  }
	  else if (AMX0P==0x0A)
	   {
	   adc0_temp =ADC0; // read ADC value and add to running
      adc_key();
      AMX0P     = 0x09;
	  } 

   AD0INT = 0;         // clear ADC0 conv. complete flag
     

   

}

void adc_key(void)
{
 
 	if(adc0_temp > 0x3FF)
	{ num_flag = 0;}						//no
	else if((adc0_temp<0x183) &&(adc0_temp>0xE8 ))//S3
	{num_flag = 1;}						//s5
	else if((adc0_temp <0x21E )&&(adc0_temp>0x183)) // S4
	{num_flag = 2;}						//s4
	else if((adc0_temp <0x2B9)&&(adc0_temp> 0x21E))   //S5
	{num_flag = 3;}						//s3
	else if((adc0_temp <0x354 )&&(adc0_temp >0x2B9)) // S6
	{num_flag = 4;}						//s2
	else if((adc0_temp <0x3EF )&&(adc0_temp >0x354))  //  S7
	{num_flag = 5;}	
	else if((adc0_temp <0x9B )&&(adc0_temp>0x8B))   
	{num_flag =	6;}	
    else if((adc0_temp <0x8B)&&(adc0_temp>0x7B))	//cymbl 7 
	{num_flag =	7;}
	else if((adc0_temp <0x75)&&(adc0_temp>0x6B))	//cymbl 6
	{num_flag =	8;}	  
	else if((adc0_temp <0x6B)&&(adc0_temp>0x5B))
	{num_flag =	9;}	 
    else if((adc0_temp <0x5B)&&(adc0_temp>0x4B))
	{num_flag =10;}
	else if((adc0_temp <0x4B)&&(adc0_temp>0x3B))
	{num_flag =11;}  
	else if((adc0_temp <0x3B)&&(adc0_temp>0x2B))
	{num_flag =12;}  
	else if((adc0_temp <0x2B)&&(adc0_temp>0x1B))
	{num_flag =13;} 	
	else
	{num_flag = 0;}		 				//no
    switch (num_flag)
	       {
		    
		    case 1: { Led_flicker(1);}//{m_p = 0; m_n=1; kdelay(10000); m_n =0;}  //{Led3 = 0;kdelay(10000); Led3=1;}
		   			break;
		    case 2: {Led_flicker(2);}//{m_p = 1; m_n=0; kdelay(10000); m_p =0;}   //{Led3 = 0;kdelay(30000); Led3=1;}
					break;
			case 3: {Led_flicker(3);}// {Led1 = 0;kdelay(60000); Led1=1;}      // {m_p = 1; m_n=0; kdelay(90000); m_p =0;}   //
					break;
			case 4:  {Led2 = 0;kdelay(90000); Led2=1;}      //{m_p = 0; m_n=1; kdelay(90000); m_n =0;}    
					break;
			case 5:  {Led3 = 0;kdelay(120000); Led3=1;}
					break;
            case 6:  //{Led3= 0;kdelay(240000);Led3=1;}
		            break;
            case 7:  //{Led1 =0;kdelay(60000); Led1=1;}
			       break;
            case 8: //{Led2 =0;kdelay(60000); Led2=1;}
			       break;
			case 9: //{Led3 =0;kdelay(60000); Led3=1;}
			       break;
            case 10://{Led_flicker(1);}
			       break;
            case 11://{Led_flicker(2);}
                   break;
            case 12://{Led_flicker(3);}
			       break;
            case 13: //{Led_flicker(1);Led_flicker(2);Led_flicker(3);}
			     break;
			}
	
}

void Led_flicker (unsigned int x)
{
 unsigned int j;
 unsigned int led_flag;
 
  for (j=1;j<20; j++)
  { 
    led_flag=x;
  switch (led_flag)
     {
	 case 1:{Led1=~Led1;kdelay(1000);}//Led1 =1;}
            break;
     case 2:{Led2=~Led2; kdelay(1000);}//Led2 =1;}
            break;
     case 3:{Led3=~Led3; kdelay(1000);}//Led3 =1;}
            break;
	 }
 }
   Led1 =1;
   Led2 =1;
   Led3 =1;  
}



void kdelay(unsigned int kdelay) 
{ 
	unsigned int i; 
    
	for(kdelay +=10;kdelay>0;kdelay--) 
	{ 
		for(i=0;i<124;i++);
	} 
}

// IO KEY  

//-----------------------------------------------------------------------------

void IO_key()

  {
  

     if(SW2==TRUE)
	 {
	 	SW2_FLAG=TRUE;
	 }
	 else if(SW2_FLAG == TRUE)
	 {
	    SW2_FLAG = FALSE;
	 	KeyFlag = TRUE;
	 }

   if(KeyFlag==TRUE)
   {
      KeyFlag = FALSE;
  	  Led2 = ~Led2;                            // change state of LED
   } 
 //  if(SW2==0)
  //   { Led2 = ~Led2; }  
    } 



//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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