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

📄 ma16._c

📁 本代码是AVR单片机的完整IIC通信程序
💻 _C
字号:
//ICC-AVR application builder : 2006-10-21 上午 03:16:49
// Target : M16
// Crystal: 12.000Mhz

#include <iom16v.h>
#include <macros.h>

#define TRUE 1
#define FALSE 0
//#define baud  0x4d
//#define fosc 12000000
//#define baud 9600
#define Uchar unsigned char
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; 
#define send_busy_flag bit_flag.bit0
#define GPS_validframe_flag bit_flag.bit1
#define receive_data_flag bit_flag.bit2
#define GPS_datalen 70 ///70 个有效数据

Uchar GPS_status_frame;
Uchar GPS_receive_buffer[72];
Uchar GPS_send_buffer[72];
Uchar GPS_data_conut;
Uchar GPS_data_end;

Uchar *com_send_ptr;
Uchar com_datasen_len;

void Delay(Uchar time)
{
	int i,uidata=10000;
for(i=0;i<time;i++)
while(--uidata)
{
  asm("nop");
}
 asm("nop");
 asm("nop");
 asm("nop");
}

void port_init(void)
{
 DDRA  = 0xFF; // Set to ouput
 PORTA = 0x00;
 DDRB  = 0xFF; // Set to ouput
 PORTB = 0x00; // Startup pattern
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD = 0x80;
}
void uart0_init(void)
{
UCSRA=0x00;
//UCSRB=(1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN);
UCSRB=0xd8;
UBRRH=0x00;
UBRRL=0x33;
//UCSRC| = BIT(URSEL);
UCSRC=BIT(URSEL)|0x06;
}
#pragma interrupt_handler uart0_rx_isr:12
#pragma interrupt_handler uart0_tx_isr:14
void uart0_rx_isr(void)
{
 	//GPS_receive_buffer[0]=UDR;
 	//UCSRA|=(1<<RXC);//清接收中断标志
	if(GPS_validframe_flag)
		{return;}
	if(GPS_status_frame==0x00)//////////////接收数据起始字符68H
		{
		if(UDR!=0x24)
			{
			GPS_status_frame=0x00;
			}
		else
			{
			GPS_status_frame=0x01;
			GPS_receive_buffer[0]=UDR;
			}
		}
	else if(GPS_status_frame==0x01)//////////开始接收数据
		{
		GPS_receive_buffer[GPS_data_conut]=UDR;
		GPS_data_conut++;
		if(GPS_data_conut==GPS_datalen)
			{
			GPS_status_frame=0x02;
			}
		}
	else if(GPS_status_frame==0x02)
		{
		GPS_data_end=UDR;
		if(GPS_data_end!=0x0a)
			{
			GPS_validframe_flag=FALSE;
			}
		else
			{
			GPS_receive_buffer[71]=UDR;
			GPS_validframe_flag=TRUE;
			}
		GPS_status_frame=0x00;
		receive_data_flag=TRUE;
		}*/
}
void uart0_tx_isr(void) 
    { 
       UCSRA|=(1<<TXC);//清发送中断标志 
       if(com_datasen_len>0)/////////////数据缓冲区数据没有发送完毕
		{
		UDR=*com_send_ptr;
		com_send_ptr++;
		com_datasen_len--;
		}
	else
		{
		send_busy_flag=FALSE;
		}
   }
void init_devices(void)
{
 CLI(); //disable all interrupts
 port_init();
 uart0_init();
 OSCCAL=0xb9;
send_busy_flag=FALSE;
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
void system_init()
{
 GPS_data_conut=1;
GPS_status_frame=0x00;
GPS_validframe_flag=FALSE;
receive_data_flag=FALSE;
}
void send_com_data()
{
	if(!send_busy_flag)
    		{
    		com_datasen_len=72;
		com_datasen_len--;
		com_send_ptr=GPS_receive_buffer;
		send_busy_flag=TRUE;
	 	UDR=*com_send_ptr++;
    		}
}
void main(void)
{
  Uchar i;
  init_devices();
  system_init();
  while(1)
  {
	if(receive_data_flag)
		{
		send_com_data();
		receive_data_flag=FALSE;
		}
  }
}

⌨️ 快捷键说明

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