📄 main.c
字号:
#include <iom16v.h>
#include <macros.h>
#include "muti_mcu_function.h"
// Sample TWI transmission commands
#define TWI_CMD_MASTER_WRITE 0x10
#define TWI_CMD_MASTER_READ 0x20
// Sample TWI transmission states, used in the main application.
#define SEND_DATA 0x01
#define REQUEST_DATA 0x02
#define READ_DATA_FROM_BUFFER 0x03
#define REQUEST_DATA_two 0x04
#define TRUE 1
#define FALSE 0
#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;
Uchar GPS_status_frame;
Uchar GPS_receive_buffer[72];
Uchar GPS_send_buffer[72];
Uchar GPS_data_conut;
Uchar GPS_data_start;
Uchar GPS_data_end;
Uchar *com_send_ptr;
Uchar com_datasen_len;
void GPS_data_send();
void delay_1ms(void) //8ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N*8 ms延时函数
{
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 )
{
// A failure has occurred, use TWIerrorMsg to determine the nature of the failure
// and take appropriate actions.
// Se header file for a list of possible failures messages.
// Here is a simple sample, where if received a NACK on the slave address,
// then a retransmission will be initiated.
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)
{
//PORTB = TWIerrorMsg;
TWI_Start_Transceiver(status);
}
return TWIerrorMsg;
}
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;
}
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 system_init()
{
GPS_data_conut=1;
GPS_status_frame=0x00;
GPS_validframe_flag=FALSE;
receive_data_flag=FALSE;
}
#pragma interrupt_handler uart0_rx_isr:12
#pragma interrupt_handler uart0_tx_isr:14
void uart0_rx_isr(void)
{
UCSRA|=(1<<RXC);//清接收中断标志
if(GPS_validframe_flag)
{return;}
if(GPS_status_frame==0x00)//////////////接收数据起始字符68H
{
GPS_data_start=UDR;
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]=UDR;
GPS_data_conut++;
if(GPS_data_conut==GPS_datalen+1)
{
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]=GPS_data_end;
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 main( void )
{
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;
/*DDRB = 0xFF; // Set to ouput
PORTB = 0x00; // Startup pattern
DDRA = 0xFF; // Set to ouput
PORTA = 0x00;
PORTD = 0x00;
DDRD = 0x80;*/
port_init();
uart0_init();
system_init();
SFIOR&=~BIT(2);
OSCCAL=0xb9;
/************************/
//always mcu keep slave status, only master in the sending datas status
// Own TWI slave address
TWI_slaveAddress = 0x10;
TWI_targetSlaveAddress = 0x30;
TWI_targetSlaveAddress_two = 0x20;
TWI_Slave_Initialise( (TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) );
SEI();
TWI_Start_Transceiver(TWI_slave_MCU);
while(1)
{
// pressedButton = PIND;
// Check if the TWI Transceiver has completed an operation.
if(receive_data_flag)
{
send_com_data();
receive_data_flag=FALSE;
GPS_validframe_flag=FALSE;
GPS_data_conut=1;
}
pressedButton = 0x01;
if (pressedButton) // Check if any button is pressed
{
if((pressedButton==0x01)) //master check
{
button_check =1 ;
TWI_Master_Initialise();
delay_nms(100);
messageBuf[0] = (TWI_targetSlaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address.
messageBuf[1] = TWI_CMD_MASTER_WRITE; // The first byte is used for commands.
messageBuf[2] = temp_data[key];
TWI_Start_Transceiver_With_Data( messageBuf, 3 , TWI_master_MCU );
//key++;
//if(key==190)key=0;
if ( ! TWI_Transceiver_Busy() )
{
delay_nms(2);
}
else
{
TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info( ), TWI_master_MCU);
}
//added for the second mcu
/*TWI_Master_Initialise();
messageBuf[0] = (TWI_targetSlaveAddress_two<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address.
messageBuf[1] = TWI_CMD_MASTER_WRITE; // The first byte is used for commands.
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);
} */
key++;
if(key==190)key=0;
}
}
else
{
if(button_check == 1)
{
TWI_Slave_Initialise( (TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) );
TWI_Start_Transceiver(TWI_slave_MCU);
button_check = 0;
}
if ( ! TWI_Transceiver_Busy() )
{
// Check if the last operation was successful
if ( TWI_statusReg.lastTransOK )
{
// Check if the last operation was a reception
if ( TWI_statusReg.RxDataInBuf )
{
TWI_Get_Data_From_Transceiver(messageBuf, 3, TWI_slave_MCU);
if ( TWI_statusReg.genAddressCall )
{
delay_nms(1);
}
// Ends up here if the last operation was a reception as Slave Address Match
else
{
// Example of how to interpret a command and respond.
// TWI_CMD_MASTER_WRITE stores the data to PORTB
if (messageBuf[0] == TWI_CMD_MASTER_WRITE)
PORTB = messageBuf[1];
// TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch.
if (messageBuf[0] == TWI_CMD_MASTER_READ)
{
key = messageBuf[1];
PORTA=0xff;
messageBuf[0]= temp_data[key];
TWI_Start_Transceiver_With_Data( messageBuf, 1, TWI_slave_MCU );
}
}
}
// Ends up here if the last operation was a transmission
else
{
// __no_operation(); // Put own code here.
}
// Check if the TWI Transceiver has already been started.
// If not then restart it to prepare it for new receptions.
if ( ! TWI_Transceiver_Busy() )
{
TWI_Start_Transceiver(TWI_slave_MCU);
}
}
// Ends up here if the last operation completed unsuccessfully
else
{
TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info(), TWI_slave_MCU );
}
}
}
}
}
void GPS_data_send()
{
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++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -