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

📄 sbq.c

📁 这是一个基于c8051F060的示波器程序
💻 C
字号:

//--------------------------------------------------------------------------------------
// Includes
//--------------------------------------------------------------------------------------
#include <c8051f060.h>                 // SFR declarations
#include <stdio.h>
#include <absacc.h>

#define   DMA_BLOCK_SIZE 1024
#define   data_type    xdata   

typedef unsigned int    u16;
typedef unsigned char   u8;
#define OSC_EXT 1
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F06x
//-----------------------------------------------------------------------------

sfr16 RCAP3    = 0xCA;                 // Timer3 reload value
sfr16 TMR3     = 0xCC;                 // Timer3 counter


sfr16 ADC0     = 0xBE;                 // ADC0 Data
sfr16 ADC0GT   = 0xc4;
sfr16 ADC0LT   = 0xc6;

sfr16 DMA0DS   = 0xDB;                 /* DMA0 DATA ADDRESS POINTER LOW BYTE */ // DMA0 XRAM Address Pointer
sfr16 DMA0CT   = 0xF9;                 /* DMA0 REPEAT COUNTER LIMIT LOW BYTE */ // DMA0 Repeat Counter Limit 256 times
sfr16 DMA0DA   = 0xD9;                 /* DMA0 DATA ADDRESS BEGINNING LOW BYTE */ // DMA0 Address Beginning
sfr16 DMA0CS   = 0xFB;                 /* DMA0 REPEAT COUNTER STATUS LOW BYTE */ // DMA0 Repeat Counter


//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define SYSCLK 22118400                   // SYSCLK frequency in Hz
#define BAUDRATE 115200                   // Baud Rate for UART0
#define FT245PORT  XBYTE[0xB000]
// DMA INSTRUCTIONS
#define DMA0_END_OF_OP     0x00           // End-of-Operation
#define DMA0_END_OF_OP_C   0x80           // End-of-Operation + Continue
#define DMA0_GET_ADC0      0x10           // Retrieve ADC0 Data
#define DMA0_GET_ADC1      0x20           // Retrieve ADC1 Data
#define DMA0_GET_ADC01     0x30           // Retrieve ADC0 and ADC1 Data
#define DMA0_GET_DIFF      0x40           // Retrieve Differential Data
#define DMA0_GET_DIFF1     0x60           // Retrieve Differential and ADC1 Data

#define COMMD_HEADER 	   0xaa
#define COMMD_LEN		   3
#define NUM_SAMPLES        DMA_BLOCK_SIZE // Number of ADC sample to acquire (each sample 2 bytes)
#define XRAM_START_ADD     0x1000         // DMA0 XRAM Start address of ADC data log
#define SAMP_RATE          500000         // ADC sample rate in Hz

volatile u8  data gFulF;

sbit RAM_CS = P5^7;                       // chip select bit is P5^7
sbit nTXE 	= P0^0;
sbit nRXF 	= P0^1;
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void ADC0_Init (void);
void DMA0_Init (void);
void Timer3_Init (int counts);
void EMIF_Init (void);
void SendData(void);

//-------------------------
// Global Variables
//-------------------------
unsigned int xdata *read_ptr;
unsigned int xdata gIDataBuffer[NUM_SAMPLES];
unsigned char data gComLine[COMMD_LEN];
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void cpy_OffXram_to_OnXram(unsigned int* off_addr,unsigned int*on_addr,unsigned int len );
void GetCommd(unsigned char *commdline,unsigned char len);
char GetChar(void);
void SendChar(unsigned char ch);
void SendChar(unsigned char ch)
{
	 while(nTXE == 1);
	 FT245PORT = ch;		

}
char GetChar(void)
{
	while(nRXF);
	return FT245PORT;
}
void GetCommd(unsigned char *commdline,unsigned char len)
{
	unsigned char tmp, i;
	for(i=0;i<len;i++)
		gComLine[i]=0;
	tmp = 0;
	while(tmp != COMMD_HEADER)
	{
		tmp = GetChar();
	}
	gComLine[0] = tmp;                                                                                         
	for(i=1;i<len;i++)
		gComLine[i]=GetChar();
}
void SendCommd(unsigned char *commdline, unsigned len)
{
	unsigned char i;
	for(i=0;i<len;i++)
	{
		SendChar(*(commdline+i));
	}
}
char Is_ASK_Send()// host ask target to send
{
	if((gComLine[0] == 0xaa)&&(gComLine[1] ==0x55)&&(gComLine[2] ==0x03))
		return 1;
	else 
		return 0;
}

void main (void)
{
		
		volatile unsigned char data GetBuffer[3]= {0,0,0};
			
		unsigned char data i=0;
		
		char old_SFRPAGE = SFRPAGE;
		
	  
	  	WDTCN = 0xde;                          // disable watchdog timer
   		WDTCN = 0xad;

    	SYSCLK_Init ();                        // initialize SYSCLK

        PORT_Init ();    

		EMIF_Init ();                          // Storing ADC samples in SRAM on the target board.  	 
 
		SFRPAGE = CONFIG_PAGE;
                                          
        RAM_CS = 0;                           // assert SRAM chip select

        Timer3_Init (SYSCLK/SAMP_RATE);       // Init Timer3 for 100ksps sample rate

        ADC0_Init();                          // configure ADC0    
    	ADC0CN |= 0x80;
    
        DMA0_Init ();
		
		gFulF = 0;
		i=0;
        while(1)
		{
 
		   SFRPAGE = LEGACY_PAGE;			  //LEGACY_PAGE;	
		   
		   GetCommd(gComLine,COMMD_LEN);
		   if(Is_ASK_Send())
		   {
	 	
				if(gFulF)
	   			{
					SendData();  // data sending
					gFulF =0;
					GetBuffer[0] = 0;
			 	    GetBuffer[1] = 0;
					GetBuffer[2] = 0;
					DMA0_Init();
					gFulF=0;
		   		}
			
		   }
		 

  		}    
} 

//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
	   char old_SFRPAGE = SFRPAGE;
	
	   SFRPAGE = CONFIG_PAGE;              // Switch to configuration page
	  
	   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
} 


//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
#if  OSC_EXT
void SYSCLK_Init (void)
{
	   char old_SFRPAGE = SFRPAGE;
	   int i;                                 
	
	   SFRPAGE = CONFIG_PAGE;              // Switch to Configuration Page
	                                   
	   OSCXCN = 0x67;                      // start external oscillator with
	                                       // 22.1184MHz crystal on TB
	
	   for (i=0; i <5000; i++) ;           // XTLVLD blanking interval (>1ms)
	
	   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
	
	   RSTSRC = 0x04;                      // enable missing clock detector reset
	
	   CLKSEL = 0x01;                      // change to external crystal
	
	   OSCICN = 0x00;                      // disable internal oscillator
	
	   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE



}
#else
void SYSCLK_Init(void)
{
	 OSCICN    = 0x07;

}
#endif
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
	   char old_SFRPAGE = SFRPAGE;
	   int i;
	   SFRPAGE = ADC0_PAGE;                // Switch to ADC0 Page
	   
	  
	   ADC0CN = 0x04;                      // ADC Disabled, Timer3 start-of-conversion
	                                      // track 16 SAR clocks before data conversion
	                                       // upon Timer3 OV.  DMA will enable ADC as needed
	                                       //
	   REF0CN = 0x03;                      // turn on bias generator and internal reference.
	   
	   for(i=0;i<10000;i++);               // Wait for Vref to settle (large cap used on target board)
	   AMX0SL = 0x00;                      // Single-ended mode
	
	
	   ADC0CF = 0x10;    					// Select SAR clock frequency =~ 25MHz
	
	   //ADC0CF = (SYSCLK/25000000) << 4;
	   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
	   

}



//-----------------------------------------------------------------------------
// DMA0_Init
//-----------------------------------------------------------------------------
void DMA0_Init (void)
{
       
	   char old_SFRPAGE = SFRPAGE;
	   
	   SFRPAGE = DMA0_PAGE;                // Switch to DMA0 Page  
	   
	   DMA0CN = 0x00;                      // Disable DMA interface
	   
	   // enable DMA0 couter overflow interrupt
	   DMA0CF |=(1 << 3);						   // DMA0CIE: Repeat Counter OverFlag Interrupt Enable
	  

	   DMA0DA = XRAM_START_ADD;            // Starting Point for XRAM addressing
	
	   DMA0CT = NUM_SAMPLES;               // Get NUM_SAMPLES samples
	
	   DMA0IPT = 0x00;                     // Start writing at location 0 instructions address
	
	   // Push instructions onto stack in order they will be executed
	   DMA0IDT = DMA0_GET_ADC0;            // DMA to move ADC0 data.
	   DMA0IDT = DMA0_END_OF_OP;
	
	   DMA0BND = 0x00;                     // Begin instruction executions at address 0
	   DMA0CN  = 0xA0; 
	   
	   EIP2 |= (1 << 7);   				   // enable DMA0 interrupt 
	   EIE2 |= (1 << 7);
	   EA = 1;	 						   // enable all interrupts	
                  
	   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE 
       // added by wl
  
	   						   	
}

//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate ADC sample rate
// specified by <counts> using SYSCLK as its time base.
//
void Timer3_Init (int counts)
{
	   char old_SFRPAGE = SFRPAGE;
	
	   SFRPAGE = TMR3_PAGE;                // Switch to Timer 3 page
	  
	   TMR3CN  = 0x00;                     // Stop Timer3; Clear TF3;
	   TMR3CF  = 0x08;                     // use SYSCLK as timebase
	   RCAP3   = -counts;                  // Init reload values
	   TMR3    = 0xffff;                   // set to reload immediately
	   //EIE2    |=(1<<0);

	   TR3     = 1;                        // start Timer3   
       // EA = 1;
	   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE

}

//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the external memory interface to use upper port pins in
// non-multiplexed mode to a mixed on-chip/off-chip configuration without
// Bank Select.
//
void EMIF_Init (void)
{
	   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
	
	   SFRPAGE = EMI0_PAGE;                // Save SFR_PAGE  
	   EMI0CF = 0x3C;                      // upper ports; non-muxed mode;
	                                       // split mode w/o bank select
	   EMI0TC = 0x45;                      // timing (7-cycle MOVX)
	
	   SFRPAGE = CONFIG_PAGE;
	
	   P4MDOUT |= 0xFF;                    // all EMIF pins configured as
	   P5MDOUT |= 0xFF;                    // push-pull
	   P6MDOUT |= 0xFF;
	   P7MDOUT |= 0xFF;
	
	   SFRPAGE = SFRPAGE_SAVE;             // restore SFR_PAGE
	  
}
 
//-----------------------------------------------------------------------------
// SendData
//-----------------------------------------------------------------------------
//
//Send data out UART0 
void SendData(void)
{
	   unsigned int i;
	   unsigned int temp;
	   char old_SFRPAGE = SFRPAGE;    
      
	   read_ptr = XRAM_START_ADD;//if(!ReadBuffer(tBuffer,DMA_BLOCK_SIZE)) return;  

	   SendChar(0xaa);
	   SendChar(0x55);

	   for (i=0; i<DMA_BLOCK_SIZE; i++)
	   { 
	      temp = *(read_ptr + i);	      
		  SendChar((temp>>8)&0xff);//sendbuffer[i+2] = (temp>>8)&0xff;//High Data Byte
		  SendChar(temp&0xff);     //sendbuffer[i+3] =  temp&0xff;//Low Data Byte  		 
	   }	  	 
	   SFRPAGE = old_SFRPAGE;

}

void timer3ISR() interrupt 14 
{
	TMR3CN &= ~(1<<7);
}
void MDA0Interrupt() interrupt 21 using 1
{
		//u16 i;
		char old_SFRPAGE = SFRPAGE;
	   
	    SFRPAGE = DMA0_PAGE;                // Switch to DMA0 Page  
		DMA0EN = 0;							//disable DMA0
		DMA0CN &= ~(1<<6);
		EIE2 &= ~(1 << 7);					//disable DMA0 Interrupt
		if( DMA0CI == 1 )                   //
		{		
					
				while(DMA0XBY);
				DMA0HLT = 1;
				DMA0HLT = 0;
				gFulF = 1;

		}		
		DMA0CI = 0; 						//clear repeat counter overflow flag
		SFRPAGE = old_SFRPAGE; 
	
}

⌨️ 快捷键说明

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