新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > AVR單片機Atmega128外擴RAM

        AVR單片機Atmega128外擴RAM

        作者: 時間:2013-12-12 來源:網絡 收藏


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


        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 靖远县| 双峰县| 中卫市| 高阳县| 新邵县| 新民市| 芷江| 建昌县| 西乌珠穆沁旗| 肇州县| 平陆县| 石棉县| 龙陵县| 琼中| 旌德县| 黄大仙区| 锦屏县| 正蓝旗| 砚山县| 永宁县| 徐州市| 襄城县| 独山县| 永昌县| 天气| 忻州市| 富裕县| 全南县| 纳雍县| 丹巴县| 剑河县| 安阳市| 武隆县| 苍山县| 弋阳县| 通化县| 乌兰县| 平果县| 左云县| 类乌齐县| 云和县|