📄 emif_1.c
字号:
//-----------------------------------------------------------------------------
// EMIF_1.c
//-----------------------------------------------------------------------------
// Copyright 2001 Cygnal Integrated Products, Inc.
//
// AUTH: BW
// DATE: 11 DEC 01
//
// This program configures the external memory interface to read and write
// to an external SRAM mapped to the upper port pins. Assumes an external
// 22.1184MHz crystal is attached between XTAL1 and XTAL2.
//
// 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
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define RAM_BANK 0x20; // bank select bit is P4^5
#define RAM_CS 0x10; // chip select bit is P4^4
sbit LED = P1^6; // LED = 1 means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void EMIF_Init (void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
unsigned char xdata *pchar; // memory access pointer
unsigned long i;
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0
EMIF_Init (); // initialize memory interface
P4 &= ~RAM_BANK; // select lower bank
P4 &= ~RAM_CS; // assert RAM chip select
// clear xdata space
pchar = 0;
for (i = 0; i < 65536; i++) {
*pchar++ = 0;
// print status to UART0
if ((i % 16) == 0) {
printf ("\nwriting 0x%04x: %02x ", (unsigned) i, (unsigned) 0);
} else {
printf ("%02x ", (unsigned) 0);
}
}
// verify all are zero
pchar = 0;
for (i = 0; i < 65536; i++) {
if (*pchar != 0) {
printf ("Erase error!\n");
while (1);
}
// print status to UART0
if ((i % 16) == 0) {
printf ("\nverifying 0x%04x: %02x ", (unsigned) i, (unsigned) *pchar);
} else {
printf ("%02x ", (unsigned) *pchar);
}
pchar++;
}
// write xdata space
pchar = 0;
for (i = 0; i < 65536; i++) {
*pchar = ~i;
// print status to UART0
if ((i % 16) == 0) {
printf ("\nwriting 0x%04x: %02x ", (unsigned) i, (unsigned) ((~i) & 0xff));
} else {
printf ("%02x ", (unsigned) ((~i) & 0xff));
}
pchar++;
}
// verify
pchar = 0;
for (i = 0; i < 65536; i++) {
if (*pchar != ((~i) & 0xff)) {
printf ("Verify error!\n");
while (1);
}
// print status to UART0
if ((i % 16) == 0) {
printf ("\nverifying 0x%04x: %02x ", (unsigned) i, (unsigned) *pchar);
} else {
printf ("%02x ", (unsigned) *pchar);
}
pchar++;
}
while (1);
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an 22.1184MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // wait for oscillator to start
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
XBR0 |= 0x04; // Enable UART0
XBR2 |= 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P1MDOUT |= 0x40; // enable LED as push-pull output
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
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; // SMOD00 = 1
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// 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)
{
EMI0CF = 0x3c; // upper ports; non-muxed mode;
// split mode w/o bank select
EMI0TC = 0x00; // fastest timing (4-cycle MOVX)
P74OUT |= 0xfe; // all EMIF pins configured as
// push-pull
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -