hscaihong 发表于 2012-11-4 20:22:48

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

如题,程序如下:
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;
}

GZZXB 发表于 2012-11-5 07:57:54

光看这个程序块,还真不知道咋改。起码你得把引脚操作宏定义一起给出吧?

hscaihong 发表于 2012-11-5 09:04:36

GZZXB 发表于 2012-11-5 07:57 static/image/common/back.gif
光看这个程序块,还真不知道咋改。起码你得把引脚操作宏定义一起给出吧?

// 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;
#define ext_PORT1 ((volatile unsigned char *)0x1100)
unsigned char *p=(unsigned char *)ext_PORT1;
unsigned char testramtable={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");
}

sunliezhi 发表于 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 //酌情添加

hscaihong 发表于 2012-11-5 10:50:32

本帖最后由 hscaihong 于 2012-11-5 10:53 编辑

sunliezhi 发表于 2012-11-5 09:26 static/image/common/back.gif
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;
#define ext_PORT1 ((volatile unsigned char *)0x1100)
unsigned char *p=(unsigned char *)ext_PORT1;
unsigned char testramtable={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");
}

hscaihong 发表于 2012-11-6 09:20:03

请问我用74hc373p做地址锁存器的时候,为什么串口不能发送数据了,我用的是cy62148外部ram,其中有几个高位用普通i/o口代替了,我看了连接是没有问题,请问问题出在哪里?
页: [1]
查看完整版本: 谁能帮我把mega128读写外部ram的gcc程序改成icc的?