微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR单片机Atmega128外扩RAM

AVR单片机Atmega128外扩RAM

时间:12-12 来源:互联网 点击:

硬件连接和普通的51单片机外扩是一样的。

内存分布。

以下是测试程序:

// Target : ATMEGA128
// Crystal : 7.3728Mhz
// Modifed By : Moxudong
// Date : 2010.03

#includeavr/io.h>
#includeutil/delay.h>
#include avr/interrupt.h> //中断函数头文件

#includestdio.h>
#includestring.h>

//PG0 0x65
#define WRL (PORTG=PORTG0XFE)
#define WRH (PORTG=PORTG|0X01)

//PG1
#define RDL (PORTG=PORTG0XFD)
#define RDH (PORTG=PORTG|0X02)

//PG2
#define ALEL (PORTG=PORTG0XFB)
#define ALEH (PORTG=PORTG|0X04)

//MCU时钟频率
#undef F_CPU
//#define F_CPU 7372800UL
//默认的系统BAUD
#define baud 115200
//#define fosc 7372800UL //晶振7.3728MHZ
#define fosc 16000000UL

//#define baud 9600//波特率

#define MCUBAUD9600 1

//declare memory mapped variables
//#define txbuf1_head 0x1100

//extern unsigned char txbuf1[256];
#define ext_PORT1 ((volatile unsigned char *)0x1100)
unsigned char *p=(unsigned char *)ext_PORT1;
unsigned char testramtable[4]={0x00,0x55,0xaa,0x00};

void Delay1ms(void)
{
unsigned int i;
for(i=0;i=(unsigned int)(16*143-2);i++);
}

void Delayxms(unsigned char ms)
{
unsigned char i;
for(i=0;i=ms;i++)
Delay1ms();
}

//unsigned char readram(unsigned int iaddr);
void port_init(void);
void uart1_init(void);
//void writeram(unsigned int iaddr, unsigned char ctemp);
void test_ram(void);
void test_net(void);
//void sendstring1(unsigned int * txbuf);

char char2hex(char t1)
{
if((t1>=00) (t1=0x09))
{
t1=t1+0x30;//'0'--'9'
}
else
{
if((t1>=0x0a)(t1=0x0f))//'A'--'F'
t1=t1-0x0a+0x61;
}
return t1;
}

void sendinthex1(unsigned int c)
{
char temph=0,templ=0;
char t1=0,t2=0;
temph=c/256;
templ=c%256;
t1=(c/256)/16;
//t1=t1>>8;
UDR1 = char2hex(t1);
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
t1=(c/256)%16;
UDR1 = char2hex(t1);
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
t2=(c%256)/16;//templ0xf0;
//t2=t2>>8;
UDR1 = char2hex(t2);
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
t2=(c%256)%16;//templ0x0f;
UDR1 = char2hex(t2);
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
}

void sendchar1(char c) // 发送
{
UDR1 = c;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
}

void sendint1(unsigned int c) // 发送
{
UDR1 = (c0xff00)>>8;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
UDR1 = c0xff;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
}

void sendstring1(unsigned char * txbuf) // 发送
{
unsigned int j;
for (j = 0; *txbuf; j++, txbuf++)
sendchar1(*txbuf);
}

void port_init(void)
{
//PA AD0-AD7 地址
//PC AD8-AD15
PORTA = 0xFF;
DDRA = 0xFF;

PORTC = 0xFF; //m103 output only
DDRC = 0x00;

//PB4 NETRST O

PORTB = 0xFF;
DDRB = 0x10;

PORTD = 0xFF;
DDRD = 0x00;

PORTE = 0xFF;
DDRE = 0x00;

PORTF = 0xFF;
DDRF = 0x00;

PORTG = 0x1F;
DDRG = 0x00;
}

//UART0 initialisation
// desired baud rate:115200
// actual baud rate:111111 (3.7%)
// char size: 8 bit
// parity: Disabled
/*
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;

// UBRRL = (fosc / 16 / (baud + 1)) % 256;
// UBRRH = (fosc / 16 / (baud + 1)) / 256;

UBRR0L = (F_CPU / 16 / (baud + 1)) % 256;//0x03;//0x08; //set baud rate lo
UBRR0H = (F_CPU / 16 / (baud + 1)) / 256;//0x00; //set baud rate hi
UCSR0B = 0x18;//0x98;
}
*/

//UART1 initialisation
// desired baud rate:115200
// actual baud rate:111111 (3.7%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = (1UCSZ11)|(1UCSZ10);//8bit+1bit stop
UBRR1L=(fosc/16/(baud+1))%256;
UBRR1H=(fosc/16/(baud+1))/256;
UCSR1B =(1RXEN1)|(1TXEN1);//RXCEN TXCEN
}

/*
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
//external interupt on INT0
}
*/

//call this routine to initialise all peripherals
void init_devices(void)
{
cli(); //disable all interrupts
port_init();
uart1_init();
}

#if 0

unsigned char readram(unsigned int iaddr)
{
//unsigned char caddl,caddh;
unsigned char cdatatemp=0;
RDH;
WRH;
ALEH;
DDRA = 0xFF;
DDRC = 0xFF;
PORTC=iaddr>>8;
PORTA=(unsigned char)iaddr;
asm("nop;");
ALEL; //LATCH IT
DDRA = 0x00;//PORT INPUT
RDL;
// send wr
asm("nop;");
cdatatemp=PINA;
RDH;
ALEH;
return cdatatemp;
}
void writeram(unsigned int iaddr, unsigned char ctemp)
{
//unsigned char caddl,caddh;
unsigned char cdatatemp=0;
RDH;
WRH;
ALEH;
DDRA = 0xFF;
DDRC = 0xFF;
PORTC=iaddr>>8;
PORTA=(unsigned char)iaddr;
ALEL;//锁存A0-A7
WRL;
PORTA=ctemp;
asm("nop;");
WRH;
ALEH;
return;
}

#endif

#define RAMSTARTADDR 0X1100
#define RAMENDADDR 0X90FF
#define RAMLEN 32768
// 测试32KRAM 用的是UT62256和74HC573

int main(void)
{
unsigned int k=0;
unsigned int i=0,j=0;
unsigned char DATA,u;

init_devices();

MCUCR = 0x80; // 允许外部并行扩展接口
XMCRA = 0x00; //0x00 external memory

XMCRB = 0x01; // 释放PC7,作为通用I/O引脚使用
DDRC = 0xff; // PC7,PC6用于输出,(不影响PC0-PC6地址线)
PORTC = 0x00; // PC7,PC6输出0,(不影响PC0-PC6地址线)
//由于是32KB所以PC7用作62256片选使能,可以释放P7为普通IO使用,输出低电平使能62256
sendstring1("init system ok!");
sendstring1("now test system-ram all is 32k !");

sendstring1("----now write ram");
_delay_ms(1300);
#if 1
for(i=0;i32768;i++)
{
if(i%2)
*(p+i)=(unsigned char)(p+i);
else
*(p+i)=0xff;
_delay_us(10);
}
// #else
sendstring1("----write ok");
sendstring1("----now check write");
sendstring1("----now read ram");
for(i=0;i32768;i++)
{
DATA = *(p+i);
sendstring1("addr=");
sendinthex1((unsigned int)(p+i));
sendstring1("=");
sendinthex1(DATA);
sendstring1("");
}
#endif
sendstring1("---- test system-ram end!");
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top