📄 f06x_ad0average.c
字号:
//------------------------------------------------------------------------------------
// F06x_SAR16Data.c
//------------------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
//
// Data Acquisition example for 'F06x
//
// Target: C8051F060TB Target Board
//
// Description: This example illustrates the use of ADC1 and the DMA to acquire and store
// data. It is intended for use with the C8051F060TB target board in the development kit.
//
// This code measures a signal at the AIN0 input with the 16-bit SAR ADC0.
// The data is sampled at a rate of 100kHz. ADC output data is transferred to
// XDATA memory space using the DMA.
//
// With the EMIF configured for off-chip XRAM access (on the upper ports),
// this code moves the ADC data to the C8051F060TB target boards's SRAM device.
// Once data acquisition is complete, the code then prompts the user to press the P3.7
// button on the target board when ready to receive the data via the UART0 serial port.
// The TB features an RS-232 transceiver and connector, so the data can be transferred
// to a PC via its serial port.
//
// The code is set to acquire up to 32768 samples (for 64kbytes of data). The SRAM device
// can accommodate up to 128kbytes, but this requires banking (A16 signal on the SRAM).
//
// Target Board Configuration: This example uses RS-232 communications, typically with
// a PC. A serial cable should be connected to the J5 RS-232 DB-9 connector on the
// target board and to a serial port on a PC. (A serial port will also be needed for
// PC communications with the EC-2 serial adapter for programming and debug).
// Data can be input from the PC's serial port using commonly available Hyperterminal.
// The following target board jumpers must be configured (see user's guide for schematic):
//
// J2: installed (voltage supply)
// J6 and J9: installed (RS-232 combs)
// J17 and J28: installed (external voltage reference for SIR)
// J11: installed (SRAM chip select)
// J14: Installed to connect to (SRAM) A16 to GND, and NOT P3.7 which will be used
// for push-button
// J1: Installed to connect P3.7 to push-button.
// J20: Installed to enable +/-5V switching power supply. Connect pin 1 (Van, +5V) to
// pin 2 (/SHIN).
//
// See the User's Guide for additional information and jumper connection information.
//
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Includes
//--------------------------------------------------------------------------------------
#include <c8051f060.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F06x
//-----------------------------------------------------------------------------
sfr16 RCAP3 = 0xCA; // Timer3 reload value
sfr16 TMR3 = 0xCC; // Timer3 counter
sfr16 ADC0 = 0xBE; // ADC0 Data
//sfr16 ADC1 = 0xBE; // ADC1 Data
sfr16 DMA0DS = 0xDB; // DMA0 XRAM Address Pointer
sfr16 DMA0CT = 0xF9; // DMA0 Repeat Counter Limit
sfr16 DMA0DA = 0xD9; // DMA0 Address Beginning
sfr16 DMA0CS = 0xFB; // DMA0 Repeat Counter
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define SYSCLK 22118400 // 系统时钟
#define BAUDRATE 115200 // UART0波特率
// 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 NUM_SAMPLES 3*z-1 // Number of ADC sample to acquire (each sample 2 bytes)
#define XRAM_START_ADD 0x0000 // DMA0 XRAM Start address of ADC data log
#define SAMP_RATE 300000 // 采样频率
sbit LED = P0^7; // LED: '1' = ON; '0' = OFF
sbit BUTTON = P3^7; // pushbutton on the target board
sbit RAM_CS = P4^5; // chip select bit is P4^4
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void ADC0_Init (void);
//void ADC1_Init (void);
void DMA0_Init (void);
void Timer3_Init (int counts);
void EMIF_Init (void);
//void SendData(void);
void Sample(void);
//-------------------------
// 全局变量
//-------------------------
unsigned int data n=6;
unsigned int data z=1;
unsigned char Conv_Complete = 0;
unsigned int xdata * data read_ptr;
unsigned long int xdata ad00 _at_ 0x06e0; //ad0采样第一组
//unsigned long int xdata ad10 _at_ 0x06e4; //ad1采样第一组
unsigned long int xdata ad01 _at_ 0x06e4; //ad0采样第二组
//unsigned long int xdata ad11 _at_ 0x06ec; //ad1采样第二组
unsigned long int xdata ad02 _at_ 0x06e8; //ad0采样第三组
//unsigned long int xdata ad12 _at_ 0x06f4; //ad1采样第三组
unsigned int xdata ad00_int _at_ 0x0700;
//unsigned int xdata ad10_int _at_ 0x13e8;
unsigned int xdata ad01_int _at_ 0x0702;
//unsigned int xdata ad11_int _at_ 0x13ec;
unsigned int xdata ad02_int _at_ 0x0704;
//unsigned int xdata ad12_int _at_ 0x13f0; //转换为int型数据
//-------------------------- ----------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void)
{
WDTCN = 0xde; // 禁止看门狗
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
UART0_Init (); // initialize UART0
ADC0_Init (); // configure ADC0 and ADC1 for differential
// measurement.
//ADC1_Init ();
Timer3_Init (SYSCLK/SAMP_RATE+1); // Init Timer3 for 100ksps sample rate
z=z<<n;
DMA0_Init (); // Configure DMA to move NUM_SAMP samples.
SFRPAGE = DMA0_PAGE;
while(!(DMA0EO==1)) ;
DMA0HLT =1;
Sample();
/*SFRPAGE = UART0_PAGE;
printf ("Data Acquisition in progress...\n");
SFRPAGE = DMA0_PAGE; // Switch to DMA0 Page
while (!(DMA0CN & 0x40)); // Wait for DMA to obtain and move ADC samples
// by polling DMA0INT bit.
SFRPAGE = LEGACY_PAGE;
printf ("Data Acquisition complete.\nPress P3.7 button when ready to receive file.\n");
while (BUTTON != 0); // Wait for user to press P3.7 on the TB.
//SendData(); */ // Send data via the UART0.
// Done.
}
//------------------------------------------------
//calcu2n_Init
//------------------------------------------------
//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
char old_SFRPAGE = SFRPAGE;
SFRPAGE = CONFIG_PAGE; // Switch to configuration page
XBR0 = 0x04; // Enable UART0 on crossbar设置i/o数字交叉口
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0xFF; // enable Port0 outputs as push-pull推挽方式
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
char old_SFRPAGE = SFRPAGE; //保存原来的sfr页,只有中断时才自动进栈,函数调用要自己保存
SFRPAGE = UART0_PAGE; // 转到uart0所在页
SCON0 = 0x50; // SCON: mode 1, 8-bit UART, enable RX
SSTA0 = 0x10; // Timer 1 generates UART0 baud rate and
// UART0 baud rate divide by two disabled
SFRPAGE = TIMER01_PAGE; // Switch to Timer 0/1 page
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD = 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -