原创
ATEMGA128-16读写外部32KRAM-70NS的完整测试程序
地址分配
0X0000-0X10FF 内部4KRAM
0X1100-0X7FFF 外部32KRAM
系统频率16M ,BAUD=9600,下面对程序对0/1/2/2+1等待状态下,RAM进行测试,全部通过!!
//ICC-AVR application builder : 2006-8-14 16:03:08
// Target : M128
// Crystal: 16.000Mhz
//1.debug rs232
//
#include<iom128v.h>
#include<macros.h>
#include<stdio.h>
#include<string.h>
//MCU时钟频率
#define F_CPU 16000000
//默认的系统BAUD
#define baud 115200
#define MCUBAUD9600 1
//declare memory mapped variables
#define txbuf1_head 0x1100
//外部RAM大小0X7FFF-0X1100=0X6EFF=28415byte
extern volatile unsigned char txbuf1[28415];
//define mappings
void mapping_init(void)
{
asm(
".area memory(abs)\n"
".org 0x1100\n"
" _txbuf1:: .blkb 28415\n"
".text\n"
);
}
//定义外部RAM地址
#define ext_PORT1 ((volatile unsigned char *)0x1100)
//定义一个指针指向外部RAM首地址
unsigned char *p=(unsigned char *)ext_PORT1;
//RAM测试的读写数据
unsigned char testramtable[4]={0x00,0x55,0xaa,0x00};
void Delay(void)
{
}
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 watchdog_init(void);
void uart1_init(void);
//void writeram(unsigned int iaddr, unsigned char ctemp);
void RAM_TEST(unsigned char );
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(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;//templ&0xf0;
//t2=t2>>8;
UDR1 = char2hex(t2);
while(!(UCSR1A & 0x40));
UCSR1A |=0x40;
t2=(c%256)%16;//templ&0x0f;
UDR1 = char2hex(t2);
while(!(UCSR1A & 0x40));
UCSR1A |=0x40;
}
void sendchar1(char c) // 发送
{
UDR1 = c;
while(!(UCSR1A & 0x40));
UCSR1A |=0x40;
}
void sendint1( int c) // 发送
{
UDR1 = (c&0xff00)>>8;
while(!(UCSR1A & 0x40));
UCSR1A |=0x40;
UDR1 = c&0xff;
while(!(UCSR1A & 0x40));
UCSR1A |=0x40;
}
void sendstring1(unsigned char * txbuf) // 发送
{
unsigned int j;
for (j = 0; *txbuf; j++, txbuf++)sendchar1(*txbuf);
//for(;*txbuf!='/0';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;
// PE0 RXD0
// PE1 TXD0
// PE4 NET_IRQ i
// PE5 INFRA_IRQ i
//
PORTE = 0xFF;
DDRE = 0x00;
PORTF = 0xFF;
DDRF = 0x00;
PORTG = 0x1F;
DDRG = 0x00;
}
//Watchdog initialisation
// prescale: 2048K cycles
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
//WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
/* reset WDT */
/* Write logical one to WDTOE and WDE */
//WDTCR |= (1<<WDTOE) | (1<<WDE);
WDTCR=0X18; //现在把WDTCH给关掉了
/* Turn off WDT */
WDTCR = 0x00;
//WDTCR=0X17;
}
//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)
{
#ifdef MCUBAUD9600
// UBRRL = (fosc / 16 / (baud + 1)) % 256;
// UBRRH = (fosc / 16 / (baud + 1)) / 256;
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x67; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x18;
#else
//baud115200
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = (F_CPU / 16 / (baud + 1)) % 256;//0x03;//0x08; //set baud rate lo
UBRR1H = (F_CPU / 16 / (baud + 1)) / 256;//0x00; //set baud rate hi
UCSR1B = 0x18;//0x98;
#endif
}
/*
#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)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
port_init();
mapping_init();
watchdog_init();
uart1_init();
//External RAM will reside between 8000h - FFFFh.
//There will be 2 wait states for both read and write.
// MCUCR = 0x80;
//EICRA = 0x03; //extended ext ints
//EICRB = 0x00; //extended ext ints
//EIMSK = 0x01;
//TIMSK = 0x00; //timer interrupt sources
//ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
//测试RTL8019AS
void test_net(void)
{
}
//
void RAM_TEST(unsigned char umode)
{
unsigned int k=0;
unsigned int i=0,j=0;
unsigned char DATA,u;
// unsigned char *p=(unsigned char *)ext_PORT1;
// unsigned char *pointer=p;
sendstring1("init system ok!\n");
sendstring1("now test system-ram all is 32k !\n");
for(u=0;u<4;u++)
{
sendstring1("----now write ram ");
sendinthex1(testramtable);
sendstring1("\n");
i=0;
do
{ if(!umode)*(p+i)=testramtable;//testok
else txbuf1=testramtable;
i++;
}while(i<0x6f00);//while(i<0x6eff);//while(i>0x6eff);
sendstring1("----write ok\n");
sendstring1("----now check write\n");
k=0x1100;
i=0;
do
{
if(!umode)DATA = *(p+i);//test ok
else DATA=txbuf1;
if(DATA!=testramtable)
{sendstring1("addr = ");
sendinthex1(k);
//sendstring1(" =");
//sendinthex1(DATA);
sendstring1("\n");
}
k++;i++;
}while(k<0x8000);//while(k<0x1110);
//0x7fff);
sendstring1("---- test system-ram end!\n");
}
}
void main(void)
{
init_devices();
sendstring1("RAMTEST START !\n");
sendstring1("-----POINTER READ AND WRITE EXTERNAL 32K RAM----------\n");
sendstring1("0 READ AND WRITE NO WAIT PC7 RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x40; //0x00 external memory
XMCRB = 0x01; // 释放PC7,作为通用I/O引脚使用
DDRC = 0xff; // PC7用于输出,(不影响PC0-PC6地址线)
PORTC = 0x00; // PC7输出0,(不影响PC0-PC6地址线)
RAM_TEST(0);
sendstring1("1 READ AND WRITE 1 WAIT PC7 RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x44; //0x00 external memory
XMCRB = 0x01; // 释放PC7,作为通用I/O引脚使用
DDRC = 0xff; // PC7,PC6用于输出,(不影响PC0-PC5地址线)
PORTC = 0x00; // PC7,PC6输出0,(不影响PC0-PC5地址线)
RAM_TEST(0);
sendstring1("2 READ AND WRITE 2 WAIT PC7 RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x48; //0x00 external memory
XMCRB = 0x01; // 释放PC7,作为通用I/O引脚使用
DDRC = 0xff; // PC7,PC6用于输出,(不影响PC0-PC5地址线)
PORTC = 0x00; // PC7,PC6输出0,(不影响PC0-PC5地址线)
RAM_TEST(0);
sendstring1("3 READ AND WRITE 2+1 WAIT PC7 RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x4C; //0x00 external memory
XMCRB = 0x01; // 释放PC7,作为通用I/O引脚使用
DDRC = 0xff; // PC7,PC6用于输出,(不影响PC0-PC5地址线)
PORTC = 0x00; // PC7,PC6输出0,(不影响PC0-PC5地址线)
RAM_TEST(0);
sendstring1("******POINTER READ AND WRITE EXTERNAL 32K RAM*******\n");
sendstring1("\n-----BUFFER READ AND WRITE EXTERNAL 32K RAM----------\n");
sendstring1("4 READ AND WRITE NOWAIT PC7 NO RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x40; //external memory
RAM_TEST(1);
sendstring1("5 READ AND WRITE 1 WAIT PC7 NO RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x44; //external memory
RAM_TEST(1);
sendstring1("6 READ AND WRITE 2WAIT PC7 NO RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x48; //external memory
RAM_TEST(1);
sendstring1("7 READ AND WRITE 2+1WAIT PC7 NO RELEASED !\n");
MCUCR = 0x80; // 允许外部并行扩展接口,忽略高位0X8000的等待时间
XMCRA = 0x4A; //external memory
RAM_TEST(1);
sendstring1("\n*******BUFFER READ AND WRITE EXTERNAL 32K RAM***********\n");
sendstring1("---- RAM TEST OK!-----\n");
//while(1){}
// sendstring1("now test rtl8019as!\n");
// test_net();
//sendstring1("----test rtl8019as end!\n");
}
文章评论(0条评论)
登录后参与讨论