搜索
bottom↓
回复: 5
打印 上一主题 下一主题

谁能帮我把mega128读写外部ram的gcc程序改成icc的?

[复制链接]

出0入0汤圆

跳转到指定楼层
1
发表于 2012-11-4 20:22:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题,程序如下:
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;
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入36汤圆

2
发表于 2012-11-5 07:57:54 来自手机 | 只看该作者
光看这个程序块,还真不知道咋改。起码你得把引脚操作宏定义一起给出吧?

出0入0汤圆

3
 楼主| 发表于 2012-11-5 09:04:36 | 只看该作者
GZZXB 发表于 2012-11-5 07:57
光看这个程序块,还真不知道咋改。起码你得把引脚操作宏定义一起给出吧?

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

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

#include<stdio.h>
#include<string.h>

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

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

//PG2
#define ALEL (PORTG=PORTG&0XFB)
#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;//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(unsigned 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);
}

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 = (1<<UCSZ11)|(1<<UCSZ10);//8bit+1bit stop
    UBRR1L=(fosc/16/(baud+1))%256;
    UBRR1H=(fosc/16/(baud+1))/256;
    UCSR1B =(1<<RXEN1)|(1<<TXEN1);//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!\r\n");
    sendstring1("now test system-ram all is 32k !\r\n");

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

出0入4汤圆

4
发表于 2012-11-5 09:26:26 | 只看该作者
asm("nop;") p前的;去掉
将以下3行去掉:
#include<avr/io.h>
#include<util/delay.h>
#include <avr/interrupt.h> //中断函数头文件
添加如下n行:
#include <iom128.h>
#include <macros.h>
#include <eeprom.h> //酌情添加
#pragma interrupt_handler timer0_ovf_isr:x //酌情添加
#pragma interrupt_handler timer1_capt_isr:y //酌情添加

出0入0汤圆

5
 楼主| 发表于 2012-11-5 10:50:32 | 只看该作者
本帖最后由 hscaihong 于 2012-11-5 10:53 编辑
sunliezhi 发表于 2012-11-5 09:26
asm("nop;") p前的;去掉
将以下3行去掉:
#include


这是我修改后的程序,其中读写还是有问题,关键的地方我标了出来,请大家帮忙!

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

#include <iom128v.h>
#include<macros.h>


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

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

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

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

//#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;
    UDR0 = char2hex(t1);
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
    t1=(c/256)%16;
    UDR0 = char2hex(t1);
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
    t2=(c%256)/16;//templ&0xf0;
    //t2=t2>>8;
    UDR0 = char2hex(t2);
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
    t2=(c%256)%16;//templ&0x0f;
    UDR0= char2hex(t2);
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
}

void sendchar1(char c) // 发送
{
    UDR0 = c;
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
}

void sendint1(unsigned int c) // 发送
{
    UDR0= (c&0xff00)>>8;
    while(!(UCSR0A & 0x40));
    UCSR0A |=0x40;
    UDR0 = c&0xff;
    while(!(UCSR0A & 0x40));
    UCSR0A |=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 = 0xF3;   //设置RXD0和TXD0
/**DDRE  = 0x02;*/
DDRE=(1<<PE1)|(1<<PE2)|(1<<PE3);
/*DDRE=0x00*/;

    PORTF = 0xFF;
    DDRF = 0x00;

    PORTG = 0x1F;
    DDRG = 0x00;
}
void uart0_init(void)
{
UCSR0B = 0x00;                     //禁止发送和接收
UCSR0A = 0x02;                     //倍速异步模式USX0=1
UCSR0C = 0x06;                     //0000 0110,UCSZ01=1,UCSZ00=1;8位字符,1位停止位
UBRR0L=(fosc/8/(baud+1))%256;   //若为正常异步模式USX0=0则位(Crystal/16/(Baud+1))%256
UBRR0H=(fosc/8/(baud+1))/256;   //参见ATMAGE128使用手册
UCSR0B = 0x18;                     //允许发送和接收
}
void init_devices(void)
{
    /*CLI(); //disable all interrupts*/
    port_init();
    uart0_init();
}


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;
}


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

void 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!\r\n");
    sendstring1("now test system-ram all is 32k !\r\n");

    sendstring1("----now write ram\r\n");
    Delayxms(300);
       
    for(i=0;i<32768;i++)
    {
        if(i%2)
        *(p+i)=(unsigned char)(p+i);     
/*这块icc编译时候有错误:【 conversion from `pointer to unsigned char' to `unsigned char' is undefined
    iccavr -o mega128_readram -LC:\icc\lib\ -g -ucrtatmega.o -bfunc_lit:0x8c.0x20000 -dram_end:0x10ff -bdata:0x100.0x10ff -dhwstk_size:16 -beeprom:1.4096 -fihx_coff -S2 @mega128_readram.lk   -lcatmega】
请问:改成*(p+i)=(unsigned char)*(p+i);可以吗?*/

else
        *(p+i)=0xff;
        Delayxms(10);
    }
// #else
    sendstring1("----write ok\r\n");
    sendstring1("----now check write\r\n");
    sendstring1("----now read ram\r\n");
    for(i=0;i<32768;i++)
    {
        DATA = *(p+i);
        sendstring1("addr=");
        sendinthex1((unsigned int)(p+i));
        sendstring1("=");
        sendinthex1(DATA);
        sendstring1("\r\n");
    }
    sendstring1("---- test system-ram end!\r\n");
}

出0入0汤圆

6
 楼主| 发表于 2012-11-6 09:20:03 | 只看该作者
请问我用74hc373p做地址锁存器的时候,为什么串口不能发送数据了,我用的是cy62148外部ram,其中有几个高位用普通i/o口代替了,我看了连接是没有问题,请问问题出在哪里?
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 07:05

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表