⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ma16.i

📁 本代码是AVR单片机的完整IIC通信程序
💻 I
字号:
#line 1 "D:\avr_prm\ATmege16\ma16.c"




#line 1 "C:/icc/include/iom16v.h"




#line 7 "C:/icc/include/iom16v.h"


#line 10 "C:/icc/include/iom16v.h"


#line 13 "C:/icc/include/iom16v.h"


#line 16 "C:/icc/include/iom16v.h"
























































































































































































































































































































































































































































































































#line 6 "D:\avr_prm\ATmege16\ma16.c"
#line 1 "C:/icc/include/macros.h"






























#line 35 "C:/icc/include/macros.h"














void _StackCheck(void);
void _StackOverflowed(char);




#line 7 "D:\avr_prm\ATmege16\ma16.c"














struct data
{
    unsigned bit0:1;
    unsigned bit1:1;
    unsigned bit2:1;
    unsigned bit3:1;
    unsigned bit4:1;
    unsigned bit5:1;
    unsigned bit6:1;
    unsigned bit7:1;
}bit_flag;





 unsigned char GPS_status_frame;
 unsigned char GPS_receive_buffer[72];
 unsigned char GPS_send_buffer[72];
 unsigned char GPS_data_conut;
 unsigned char GPS_data_start;
 unsigned char GPS_data_end;

 unsigned char *com_send_ptr;
 unsigned char com_datasen_len;

void delay_1ms(void)
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }

void delay_nms(unsigned int n)
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg,unsigned status )
{






 if(status == TWI_master_MCU)
 {
  if ( (TWIerrorMsg == TWI_MTX_ADR_NACK) | (TWIerrorMsg == TWI_MRX_ADR_NACK) )
    TWI_Start_Transceiver(status);
 }
 if(status == TWI_slave_MCU)
 {

  TWI_Start_Transceiver(status);
 }

  return TWIerrorMsg;
}
void port_init(void)
{
(*(volatile unsigned char *)0x3A)  = 0xFF;
(*(volatile unsigned char *)0x3B) = 0x00;
(*(volatile unsigned char *)0x37)  = 0xFF;
(*(volatile unsigned char *)0x38) = 0x00;
(*(volatile unsigned char *)0x35) = 0x00;
(*(volatile unsigned char *)0x34)  = 0x00;
(*(volatile unsigned char *)0x32) = 0x00;
(*(volatile unsigned char *)0x31) = 0x80;
}
void uart0_init(void)
{
(*(volatile unsigned char *)0x2B)=0x00;

(*(volatile unsigned char *)0x2A)=0xd8;
(*(volatile unsigned char *)0x40)=0x00;
(*(volatile unsigned char *)0x29)=0x33;

(*(volatile unsigned char *)0x40)=(1 << (7))|0x06;
}
#pragma interrupt_handler uart0_rx_isr:12
#pragma interrupt_handler uart0_tx_isr:14
void uart0_rx_isr(void)
{
(*(volatile unsigned char *)0x2B)|=(1<< 7);
	if(bit_flag.bit1)
		{return;}
	if(GPS_status_frame==0x00)
		{
		GPS_data_start=(*(volatile unsigned char *)0x2C);
		if(GPS_data_start!=0x24)
			{
			GPS_status_frame=0x00;
			}
		else
			{
			GPS_status_frame=0x01;
			GPS_receive_buffer[0]=GPS_data_start;
			}
		}
	else if(GPS_status_frame==0x01)
		{
		GPS_receive_buffer[GPS_data_conut]=(*(volatile unsigned char *)0x2C);
		GPS_data_conut++;
		if(GPS_data_conut== 70 +1)
			{
			GPS_status_frame=0x02;
			}
		}
	else if(GPS_status_frame==0x02)
		{
		GPS_data_end=(*(volatile unsigned char *)0x2C);
		if(GPS_data_end!=0x0a)
			{
 bit_flag.bit1 = 0;
			}
		else
			{
			GPS_receive_buffer[71]=GPS_data_end;
 bit_flag.bit1 = 1;
			}
		GPS_status_frame=0x00;
 bit_flag.bit2 = 1;
		}
}
void uart0_tx_isr(void)
    {
(*(volatile unsigned char *)0x2B)|=(1<< 6);
       if(com_datasen_len>0)
		{
(*(volatile unsigned char *)0x2C)=*com_send_ptr;
		com_send_ptr++;
		com_datasen_len--;
		}
	else
		{
 bit_flag.bit0 = 0;
		}
   }
void init_devices(void)
{
 port_init();
 uart0_init();
(*(volatile unsigned char *)0x51)=0xb9;
 bit_flag.bit0 = 0;
(*(volatile unsigned char *)0x55) = 0x00;
(*(volatile unsigned char *)0x5B)  = 0x00;
(*(volatile unsigned char *)0x59) = 0x00;
(*(volatile unsigned char *)0x50)&=~(1 << (2));
 asm("sei");

}
void system_init()
{
 GPS_data_conut=1;
GPS_status_frame=0x00;
 bit_flag.bit1 = 0;
 bit_flag.bit2 = 0;
}
void send_com_data()
{
	if(! bit_flag.bit0)
    		{
    		com_datasen_len=72;
		com_datasen_len--;
		com_send_ptr=GPS_receive_buffer;
 bit_flag.bit0 = 1;
(*(volatile unsigned char *)0x2C)=*com_send_ptr++;
    		}
}
void main(void)
{
 asm("cli");
  unsigned char messageBuf[4],TWI_slaveAddress,TWI_targetSlaveAddress,TWI_targetSlaveAddress_two;
  unsigned	char temp_data[250],pressedButton,key=0,button_check=0;
  int i;
  for(i=0;i<250;i++)
  temp_data[i]=i;
  TWI_statusReg.lastTransOK=1;
  init_devices();
  system_init();
  TWI_slaveAddress = 0x10;
  TWI_targetSlaveAddress = 0x30;
  TWI_targetSlaveAddress_two = 0x20;
  TWI_Slave_Initialise( (TWI_slaveAddress<<TWI_ADR_BITS) | (1 <<TWI_GEN_BIT) );
 asm("sei");
  TWI_Start_Transceiver(TWI_slave_MCU);
  while(1)
  {
	if(bit_flag.bit2)
		{
		send_com_data();
 bit_flag.bit2 = 0;
 bit_flag.bit1 = 0;
		GPS_data_conut=1;
		}


	   pressedButton = 0x01;
	   if (pressedButton)
       	  {
	  	   	 if((pressedButton==0x01))
	   		 	 {
	   	             button_check =1 ;
	   	 			 TWI_Master_Initialise();
		 			 delay_nms(100);
         			 messageBuf[0] = (TWI_targetSlaveAddress<<TWI_ADR_BITS) | (0 <<TWI_READ_BIT);
         			 messageBuf[1] = 0x10;
	     			 messageBuf[2] = temp_data[key];
		 			 TWI_Start_Transceiver_With_Data( messageBuf, 3 , TWI_master_MCU );


		   		     if ( ! TWI_Transceiver_Busy() )
   		   			 	 {
	 	 	  			  	delay_nms(2);
						 }
					 else
					 	 {
	  		  			   	TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info( ), TWI_master_MCU);
					     }


#line 255 "D:\avr_prm\ATmege16\ma16.c"
					 key++;
		 			 if(key==190)key=0;
	              }
	       }
	   else
	       {
	       	   if(button_check == 1)
		  	   	 {
		    	  	 TWI_Slave_Initialise( (TWI_slaveAddress<<TWI_ADR_BITS) | (1 <<TWI_GEN_BIT) );
			         TWI_Start_Transceiver(TWI_slave_MCU);
			         button_check = 0;
		         }
			   if ( ! TWI_Transceiver_Busy() )
    	   	     {

       		        if ( TWI_statusReg.lastTransOK )
      				    {


       		 	 		    if ( TWI_statusReg.RxDataInBuf )
        		 	   		   	 {
         			   			  	  TWI_Get_Data_From_Transceiver(messageBuf, 3, TWI_slave_MCU);
			 		   				  if ( TWI_statusReg.genAddressCall )
          			   	  			  {
									     delay_nms(1);
						  			  }

          	   		   	   			  else
         			   	  			  {


             						 	 if (messageBuf[0] == 0x10)
(*(volatile unsigned char *)0x38) = messageBuf[1];

             							if (messageBuf[0] == 0x20)
            				   			    {
   	 						   	 			 	 key = messageBuf[1];
(*(volatile unsigned char *)0x3B)=0xff;
			  									 messageBuf[0]= temp_data[key];
              					 				 TWI_Start_Transceiver_With_Data( messageBuf, 1, TWI_slave_MCU );

            		   		   			    }
          				  			  }
                      			 }

        	   		   	     else
        			  		 	 {

        	  		  			 }


             		if ( ! TWI_Transceiver_Busy() )
             		   {
          	  		   	    TWI_Start_Transceiver(TWI_slave_MCU);
       		 		   }
      	  		}

       		   else
       		    {
           		   TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info(), TWI_slave_MCU );
           		}
    	 }
  }
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -