www.pudn.com > 24cxx.rar > 24CxxMain.c


#include 
#include 
#include"xd.h" 
#define XDmain 
#include"xdprj.h" 
 
 
uchar r_buf[64]; 
 
uchar w_buf[64]; 
 
void main() 
{ 
	uint i; 
	 
     uchar *r; 
     uint b; 
     uint c; 
 
     uchar *w; 
     uint x; 
     uint y; 
 
	tms(100); 
 
	//-----初始化cpu------ 
    setreg_m16();	//初始化cpu 
 
 
	for(i=0;i<64;i++)		//清主机读和写缓存区 
		r_buf[i]=w_buf[i]=0; 
 
	for(i=0;i<64;i++)		//写缓存区赋初值 
		w_buf[i]=i; 
 
     r=r_buf;	//读参数 
     b=0x00; 
     c=64; 
 
     w=w_buf;	//写参数 
     x=0x00; 
     y=64; 
 
     wt24c(w,x,y);	//写 
 
	txd_buf[68]=syserr; 
 
     rd24c(r,b,c);	//读 
      
     //-----读出的数据发送到PC----- 
     for(i=0;i<64;i++) 
     	txd_buf[i]=r_buf[i]; 
      
     txd_buf[69]=syserr; 
      
     txdpo(txd_buf, 70); 
      
     while(1); 
     //---------------------------- 
 
	c=0; 
 
 
} 
 
 
 
//------------------------------------------------------------------------------------------------------------------------- 
//ICC-AVR application builder : 2005-11-9 22:09:34 
// Target : M16 
// Crystal: 8.0000Mhz 
 
 
void port_init(void) 
{ 
 PORTA = 0xff; 
 DDRA  = 0x00; 
 PORTB = 0xff; 
 DDRB  = 0x00; 
 PORTC = 0xff; //m103 output only 
 DDRC  = 0b00000011;	//设PC0和PC1为输出 
 PORTD = 0xff; 
 DDRD  = 0x00; 
} 
 
//TWI initialize 
// bit rate:100 
void twi_init(void) 
{ 
 TWCR= 0X00; //disable twi 
 TWBR= 0x64; //set bit rate 
 TWSR= 0x00; //set prescale 
 TWAR= 0x00; //set slave address 
 TWCR= 0x04; //enable twi 
} 
 
//UART0 initialize 
// desired baud rate: 9600 
// actual: baud rate:9615 (0.2%) 
// char size: 8 bit 
// parity: Disabled 
void uart0_init(void) 
{ 
 UCSRB = 0x00; //disable while setting baud rate 
 UCSRA = 0x00; 
 UCSRC = BIT(URSEL) | 0x06; 
 UBRRL = 0x33; //set baud rate lo 
 UBRRH = 0x00; //set baud rate hi 
 UCSRB = 0x08; 
} 
 
//call this routine to initialize all peripherals 
void setreg_m16(void) 
{ 
 //stop errant interrupts until set up 
 CLI(); //disable all interrupts 
 port_init(); 
 twi_init(); 
 uart0_init(); 
 
 MCUCR = 0x00; 
 GICR  = 0x00; 
 TIMSK = 0x00; //timer interrupt sources 
 SEI(); //re-enable interrupts 
 //all peripherals are now initialized 
}