没有合适的资源?快使用搜索试试~ 我知道了~
C8051F02X的功能模块应用的C语言代码举例
需积分: 9 18 下载量 99 浏览量
2009-08-30
12:56:01
上传
评论
收藏 849KB PDF 举报
温馨提示
试读
102页
C8051F020,021,022,023的功能模块应用的C语言代码举例 C8051F02X开发的宝典资料
资源推荐
资源详情
资源评论
Rev. 1.2 12/03 Copyright © 2003 by Silicon Laboratories AN122-DS12
AN122
ANNOTATED ‘C’ EXAMPLES FOR THE ‘F02X FAMILY
Relevant Devices
This application note applies to the following devices:
C8051F020, C8051F021, C8051F022, and
C8051F023.
Introduction
This note contains example code written in ‘C’ that
can be used as a starting point for the development
of applications based on the C8051F02x family of
devices.
Index of Programs by Peripheral
The following short descriptions provide an index
to the attached programs, organized by peripheral.
ADC0 Examples
The following are example programs which use
ADC0.
“ADC0_Buf1.c”
This program shows an example of using ADC0 in
interrupt mode using Timer3 overflows as a start-
of-conversion source to sample AIN0
<NUM_SAMPLES> times, storing the results in
XDATA space. Once <NUM_SAMPLES> have
been collected, the samples are transmitted out
UART0. Once the transmission has completed,
another <NUM_SAMPLES> of data are collected
and the process repeats.
“ADC0_Int1.c”
This program shows an example of using ADC0 in
interrupt mode using Timer3 overflows as a start-
of-conversion to measure the output of the on-chip
temperature sensor. The temperature is calculated
from the ADC0 result and is transmitted out
UART0.
“ADC0_Int2m.c”
This program shows an example of using ADC0 in
interrupt mode using Timer3 overflows as a start-
of-conversion to measure the the voltages on AIN0
through AIN7 and the temperature sensor. The
voltages are calculated from the resulting codes
and are transmitted out UART0.
“ADC0_OSA1.c”
This program shows an example of using ADC0 in
interrupt mode using Timer3 overflows as a start-
of-conversion to measure the output of the on-chip
temperature sensor. The ADC0 results are filtered
by a simple integrate-and-dump process whose
integrate/decimate ratio is given by the constant
<INT_DEC>. The temperature is calculated from
the ADC0 result and is transmitted out UART0.
“ADC0_Poll1.c”
This program demonstrates operation of ADC0 in
polled mode. The ADC0 is configured to use writes
to AD0BUSY as its start of conversion source and
to measure the output of the on-chip temperature
sensor. The temperature sensor output is converted
to degrees Celsius and is transmitted out UART0.
DAC0 Examples
The following are example programs which use
DAC0. These can easily be converted to use DAC1
if desired.
AN122
2 Rev. 1.2
“DAC0_DTMF1.c”
Example source code which outputs DTMF tones
on DAC0. DAC0's output is scheduled to update at
a rate determined by the constant <SAMPLER-
ATED>, managed and timed by Timer4.
Oscillator Examples
The following are example programs which config-
ure the internal and external oscillators. They also
show how to measure the internal and external
oscillator frequency and implement a real time
clock.
“OSC_Cry1.c”
This program shows an example of how to config-
ure the External Oscillator to drive a 22.1184 MHz
crystal and to select this external oscillator as the
system clock source. Also enables the missing
clock detector reset function. Assumes an
22.1184 MHz crystal is attached between XTAL1
and XTAL2.
“OSC_Int1.c”
This program shows an example of how to config-
ure the internal oscillator to its maximum fre-
quency (~16 MHz). Also enables the Missing
Clock Detector reset function.
“INT_OSC_Measure1”
This program shows an example of how the exter-
nal oscillator can be used to measure the internal
oscillator frequency. In this example, the internal
oscillator is set to its highest setting. The external
oscillator is configured for a 22.1184 MHz crys-
tal. The PCA counter is used as a generic 16-bit
counter that uses EXTCLK / 8 as its time base. We
configure Timer0 to count SYSCLKs, and count
the number of INTCLKs in 1 second's worth of
EXTCLK / 8
“EXT_OSC_Measure1”
This program shows an example of how the inter-
nal oscillator can be used to measure the external
oscillator frequency. In this example, the internal
oscillator is set to its highest setting. The external
oscillator is configured for a high frequency crys-
tal. The PCA counter is used as a generic 16-bit
counter that uses EXTCLK / 8 as its time base. We
configured Timer0 to count SYSCLKs, and count
the number of EXTCLK/8 ticks in 16 million
SYSCLKs (or the number of SYSCLKs in
1 second) to obtain the external oscillator fre-
quency.
“OSC_RTC_Cal1”
This program shows an example of how an external
crystal oscillator can be used to measure the inter-
nal oscillator frequency to a sufficient degree to
enable UART operation (better than +/- 2.5%). In
this example, a 32.768kHz watch crystal (with
associated loading capacitors) is connected
between XTAL1 and XTAL2. The PCA counter is
used as a generic 16-bit counter that uses
EXTCLK / 8 as its time base. We preload it to gen-
erate an overflow in 8 counts. Timer0 is config-
ured as a 16-bit counter that is set to count
SYSCLKs. The internal oscillator is configured to
its highest setting, and provides the system clock
source.
A set of real time clock (RTC) values for seconds,
minutes, hours, and days are maintained by the
interrupt handler for Timer 3, which is configured
to use EXTCLK / 8 as its time base and to reload
every 4096 counts. This generates an interrupt once
every second.
Timer Examples
“Timer0_Poll1.c”
This program shows an example of using Timer0 in
polled mode to implement a delay counter with a
resolution of 1 ms.
AN122
Rev. 1.2 3
External Memory Interface (EMIF)
Examples
“EMIF_1.c”
This program configures the external memory
interface to read and write to an external SRAM
mapped to the upper port pins (P4-7).
UART Examples
The following examples show how to use UART0
and UART1 in polled mode and in interrupt mode.
“UART0_Stdio1”
This program configures UART0 to operate in
polled mode, suitable for use with the <stdio>
functions printf() and scanf(), to which examples
areprovided. Assumes an 22.1184 MHz crystal is
attached between XTAL1 and XTAL2. The system
clock frequency is stored in a global constant
SYSCLK. The target UART baud rate is stored in
a global constant BAUDRATE.
“UART0 Autobaud1”
This program shows an example of how the PCA
can be used to enable accurate UART auto-baud
detection when running from the on-chip internal
oscillator. This algorithm assumes a 0x55 character
( ASCII "U") is sent from the remote transmitter.
Baud rates between 4800 to 19.2kbps can be reli-
ably synchronized. UART0 is then configured to
operate in polled mode, suitable for use with the
<stdio> functions printf() and scanf().
“UART0_Int1”
This program configures UART0 to operate in
interrupt mode, showing an example of a string
transmitter and a string receiver. These strings are
assumed to be NULL-terminated. Assumes an
22.1184 MHz crystal is attached between XTAL1
and XTAL2. The system clock frequency is stored
in a global constant SYSCLK. The target UART
baud rate is stored in a global constant
BAUDRATE.
“UART1_Int1”
This program is the same as “UART0_Int1” except
it uses UART1.
FLASH Examples
“FLASH_Scratch”
This program illustrates how to erase, write, and
read FLASH memory from application code writ-
ten in 'C' and exercises the upper 128-byte FLASH
sector.
PCA Examples
“Freq_Gen1”
This program uses the PCA in Frequency Output
mode to generate a square wave on P0.0.
SPI Examples
“SPI_EE_Pol1”
This program shows an example of how to inter-
face to a SPI EEPROM using the SPI0 interface in
polled-mode. The SPI EEPROM used here is a
Microchip 25LC320 (4k bytes). Assumes a
22.1184MHz crystal is attached between XTAL1
and XTAL2.
“SPI_EE_Int1”
This program is the same as SPI_EE_Pol1 execpt it
uses the SPI0 interface in interrupt mode.
AN122
4 Rev. 1.2
Example Code
“ADC0_Buf1.c”
//-----------------------------------------------------------------------------
// ADC0_Buf1.c
//-----------------------------------------------------------------------------
// Copyright 2001 Cygnal Integrated Products, Inc.
//
// AUTH: BW
// DATE: 27 AUG 01
//
// This program shows an example of using ADC0 in interrupt mode using Timer3
// overflows as a start-of-conversion to sample AIN0 <NUM_SAMPLES> times,
// storing the results in XDATA space. Once <NUM_SAMPLES> have been
// collected, the samples are transmitted out UART0. Once the transmission
// has completed, another <NUM_SAMPLES> of data are collected and the process
// repeats.
//
// Assumes an 22.1184MHz crystal is attached between XTAL1 and XTAL2.
//
// The system clock frequency is stored in a global constant SYSCLK. The
// target UART baud rate is stored in a global constant BAUDRATE. The
// ADC0 sampling rate is stored in a global constant SAMPLERATE0. The number
// of samples collected during each batch is stored in <NUM_SAMPLES>. The
// maximum value of <NUM_SAMPLES> is 2048 on a C8051F02x device with 4096
// bytes of XRAM (assuming no external RAM is connected to the External
// Memory Interface).
//
// Target: C8051F02x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <stdio.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 = 0xbe; // 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
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
AN122
Rev. 1.2 5
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SAMPLERATE0 50000 // ADC0 Sample frequency in Hz
#define NUM_SAMPLES 2048 // number of ADC0 samples to take in
// sequence
#define TRUE 1
#define FALSE 0
sbit LED = P1^6; // LED=’1’ means ON
sbit SW1 = P3^7; // SW1=’0’ means switch pressed
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
xdata unsigned samples[NUM_SAMPLES]; // array to store ADC0 results
bit ADC0_DONE; // TRUE when NUM_SAMPLES have been
// collected
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
int i; // loop counter
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0
Timer3_Init (SYSCLK/SAMPLERATE0); // initialize Timer3 to overflow at
// desired ADC0 sample rate
ADC0_Init (); // init ADC
EA = 1; // Enable global interrupts
while (1) {
// collect samples...
ADC0_DONE = FALSE;
LED = 1; // turn LED on during sample process
EIE2 |= 0x02; // enable ADC0 interrupts
剩余101页未读,继续阅读
资源评论
大兔子周
- 粉丝: 1
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功