AVR單片機(jī)Atmega128外擴(kuò)RAM
//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) // 發(fā)送
{
UDR1 = c;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
}
void sendint1(unsigned int c) // 發(fā)送
{
UDR1 = (c0xff00)>>8;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
UDR1 = c0xff;
while(!(UCSR1A 0x40));
UCSR1A |=0x40;
}
void sendstring1(unsigned char * txbuf) // 發(fā)送
{
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;
}
評(píng)論