📄 main.c
字号:
/*****************************************************************************
*
* Atmel Corporation
*
* File : main.c
* Compiler : IAR EWAAVR 2.28a/3.10c
* Revision : $Revision: 1.13 $
* Date : $Date: 24. mai 2004 11:31:20 $
* Updated by : $Author: ltwa $
*
* Support mail : avr@atmel.com
*
* Supported devices : All devices with a TWI module can be used.
* The example is written for the ATmega16
*
* AppNote : AVR315 - TWI Master Implementation
*
* Description : Example of how to use the driver for TWI master
* communication.
* This code reads PORTD and sends the status to the TWI slave.
* Then it reads data from the slave and puts the data on PORTB.
* To run the example use STK500 and connect PORTB to the LEDS,
* and PORTD to the switches. .
*
****************************************************************************/
#define MAIN_C
#include "includes.h"
#define TWI_GEN_CALL 0x00 // The General Call address is 0
// Sample TWI transmission commands
#define SLA_W 0xA0
#define SLA_R 0xA1
// Sample TWI transmission states, used in the main application.
#define SEND_DATA 0x01
#define REQUEST_DATA 0x02
#define READ_DATA_FROM_BUFFER 0x03
unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg )
{
// 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 ( (TWIerrorMsg == TWI_MTX_ADR_NACK) | (TWIerrorMsg == TWI_MRX_ADR_NACK) )
TWI_Start_Transceiver();
return TWIerrorMsg;
}
void main( void )
{
unsigned char messageBuf[TWI_BUFFER_SIZE];
int i;
UsartInit(); //USART初始化
TWI_Master_Initialise(); //TWI初始化
PutStr("TWI initialise success!\r");
SREG |= (1<<7); //Enable_interrupt
messageBuf[0] = SLA_W;
messageBuf[1] = 0x00;
messageBuf[2] = 0x10;
for(i=3;i<TWI_BUFFER_SIZE;i++)
{
messageBuf[i]=i-3;
}
PutStr("Start to write data to 24c64!\r");
delay_nms(1000);
TWI_Start_Transceiver_With_Data( messageBuf, TWI_BUFFER_SIZE );
delay_nms(100);
messageBuf[0] = SLA_W;
messageBuf[1] = 0x00;
messageBuf[2] = 0x15;
PutStr("Start to read data from 24c64!\r");
delay_nms(1000);
TWI_Start_Transceiver_With_Data( messageBuf, 3 );
delay_nms(100);
messageBuf[0] = SLA_R;
TWI_Start_Transceiver_With_Data( messageBuf, TWI_BUFFER_SIZE-2 );
// while(!TWI_statusReg);
for(i=1;i<9;i++)
{
;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -