本帖最后由 kyzb001 于 2010-11-11 23:08 编辑
我应该是读到数据了。 单是貌似读的地址有问题。 不知道是发送的时候有问题还是读取的时候有问题。 明天弄。
代码我改了。小菜鸟一个,代码不好,高手见谅。
发送代码:::
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "Driver\DrvGPIO.h"
#include "Driver\DrvI2C.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
uint8_t Device_Addr0;
uint8_t Tx_Data0;
uint8_t DataLen0;
volatile uint8_t EndFlag0 = 0;
void I2C0_Callback_Tx(uint32_t status)
{
if (status == 0x08) /* 发送启动信号 */
{
DrvI2C_WriteData(I2C_PORT0, Device_Addr0);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
printf("start \n");
}
else if (status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
DrvI2C_Ctrl(I2C_PORT0, 1, 1, 1, 0);
printf("stop \n");
}
else if (status == 0x28) /* DATA has been transmitted and ACK has been received */
{
DrvI2C_WriteData(I2C_PORT0, Tx_Data0);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
EndFlag0 = 1;
//printf("send data %d \n",Tx_Data0);
}
else
{
printf("Status 0x%x is NOT processed\n", status);
}
}
int32_t main (void)
{
uint32_t i;
STR_UART_T sParam;
UNLOCKREG();
SYSCLK->;PWRCON.XTL12M_EN = 1;
DrvSYS_Delay(5000);
DrvGPIO_InitFunction(E_FUNC_UART0);
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
sParam.u32BaudRate = 115200;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
DrvUART_Open(UART_PORT0, &sParam);
printf("\nuart open \n");
DrvGPIO_InitFunction(E_FUNC_I2C0);
DrvI2C_Open(I2C_PORT0, 10000);
DrvI2C_EnableInt(I2C_PORT0);
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 0, 0);
Device_Addr0 = 0xa0;
for (i = 0; i < 32; i++)
{
Tx_Data0= 0x10 + i;
EndFlag0 = 0;
DrvI2C_InstallCallback(I2C_PORT0, I2CFUNC, I2C0_Callback_Tx);
//DrvI2C_Ctrl(I2C_PORT0, 1, 0, 0, 0);
while (EndFlag0 == 0);
printf("send data %d \n",Tx_Data0);
EndFlag0 = 0;
}
printf("I2C0(Master) to I2C1(Slave) Test OK\n");
DrvI2C_DisableInt(I2C_PORT0);
DrvI2C_Close(I2C_PORT0);
while(1);
}
接收代码:::
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "Driver\DrvGPIO.h"
#include "Driver\DrvI2C.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t Device_Addr0;
volatile uint8_t EndFlag0 = 0,cou = 0;
uint8_t Slave_Buff1[32] = {0};
/*---------------------------------------------------------------------------------------------------------*/
/* I2C0 (Master) Rx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_Callback_Rx(uint32_t status)
{
if (status == 0x08) /* START已经发送并且准备发送SLA+W */
{
DrvI2C_WriteData(I2C_PORT0, Device_Addr0);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
printf("\nStar is success\n");
}
else if (status == 0x10) /* 已经发送重新启动并且准备发送 SLA+R */
{
DrvI2C_WriteData(I2C_PORT0, Device_Addr0);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x40) /* SLA+R已经发送并且 ACK已经接收 */
{
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
}
else if (status == 0x50) /* 数据已经发送并且ACK已经返回 */
{
Slave_Buff1[cou++] = DrvI2C_ReadData(I2C_PORT0);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
EndFlag0 = 1;
}
else if (status == 0x58) /* 数据已经发送并且NACK已经返回 */
{
Slave_Buff1[cou++] = DrvI2C_ReadData(I2C_PORT0);
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
EndFlag0 = 1;
printf("\n Over \n");
}
else
{
printf("Status 0x%x is NOT processed\n", status);
}
}
int32_t main (void)
{
uint32_t i;
STR_UART_T sParam;
UNLOCKREG();
SYSCLK->;PWRCON.XTL12M_EN = 1;
DrvSYS_Delay(5000);
DrvGPIO_InitFunction(E_FUNC_UART0);
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
sParam.u32BaudRate = 115200;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
DrvUART_Open(UART_PORT0, &sParam);
printf("\nuart open \n");
DrvGPIO_InitFunction(E_FUNC_I2C0);
DrvI2C_Open(I2C_PORT0, 10000);
DrvI2C_EnableInt(I2C_PORT0);
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 0, 0);
Device_Addr0 = 0xa1;
for (i = 0; i < 32; i++)
{
EndFlag0 = 0;
DrvI2C_InstallCallback(I2C_PORT0, I2CFUNC, I2C0_Callback_Rx);
while (EndFlag0 == 0);
}
DrvI2C_DisableInt(I2C_PORT0);
printf("\nLoad data finish\n");
for(i = 0; i < 32; i++)
{
printf("\n %d \n",Slave_Buff1);
}
printf("I2C0(Master) to I2C1(Slave) Test OK\n");
DrvI2C_Close(I2C_PORT0);
while(1);
} |