?? iic.c
字號:
/************************************************
* 文件名:iic.c *
* 文件功能:TWI,兩線串行通信,發送及接收程序 *
* 32KHz頻率,一次發送/接收4個字節 *
* 使能PWI接口要在主程序中, *
* 空閑時為從機接收模式 *
* 使能: TWAR = TWI_ADDR; *
* TWCR = (BIT(TWEA)|BIT(TWEN)); *
* 比特率: TWBR = 30; //波特率 *
* TWSR = BIT(TWPS0); //4分頻,32KHz *
* 作者:xxlxws *
* 日期:2007-05-29 *
************************************************/
#include <ioavr.h>
#include <intrinsics.h>
#include "mylib.h"
#include "common.h"
#include "main.h"
#include "init.h"
#include "iic.h"
#include "func.h"
#include "inter.h"
extern struct _Data Data;
/************************************************
* 函數名:I2C_Write *
* 入口參數:無 *
* 出口參數:無 *
* 作者:xxlxws *
* 日期:2007-05-29 *
* 函數功能:I2C寫4個字節到從機地址(TWI_ADDR) *
************************************************/
void I2C_Write(void)
{
uint8 i;
TWBR = 30; /*波特率*/
TWSR = BIT(TWPS0); /*4分頻,32KHz*/
I2C_Start(); /*發送啟始信號*/
I2C_Wait(); /*等待應答*/
I2C_SendByte(TWI_ADDR); /*發送從機地址*/
I2C_Wait();
for(i=0; i<4; i++)
{
I2C_SendByte(Data.Write[i]); /*發送4個字節數據*/
I2C_Wait();
}
I2C_Stop(); /*發送停止位*/
TWBR = 0; /*清波特率,轉為從機接收模式*/
TWSR = 0;
return;
}
/************************************************
* 函數名:I2C_Read *
* 入口參數:無 *
* 出口參數:無 *
* 作者:xxlxws *
* 日期:2007-05-29 *
* 函數功能:I2C讀4個字節 *
************************************************/
void I2C_Read(void)
{
uint8 i;
I2C_RcvAckByte(); /*從機被尋址應答*/
for(i=0; i<4; i++)
{
I2C_Wait();
Data.Read[i] = TWDR; /*接收信號并給應答位*/
I2C_RcvAckByte();
}
I2C_Wait(); /*等待停止位*/
I2C_RcvAckByte();
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -