标题:
松翰SN8F5703单片机IIc例程
[打印本页]
作者:
hexinquan
时间:
2020-12-8 11:18
标题:
松翰SN8F5703单片机IIc例程
松翰单片机5703例程——IIC
//-----------------------------------------------------------------------------
// FILE: Master_I2C.c
//-----------------------------------------------------------------------------
// Copyright 2014 SONiX Technology Corp. All rights reserved.
//
// AUTHOR: SONiX
// DATE: 2014/11/11
//
// The program is an example of Master I2C to implement it.
//
// I2C decription:
// User need to external pull-high at SDA and SCL pin
//
//
// The system clock frequency is IHRC 32MHz
//
// Device: SN8F5708
// Tool chain: KEIL C51 V9.50a
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <string.h>
#include <SN8F5703.h>
#include "GenericTypeDefs.h"
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define MAXDATALEN 5
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
UINT8 u8I2CFlag = 0;
UINT8 u8I2CData[MAXDATALEN];
UINT8 u8I2CAddr = 0;
UINT8 u8I2CLen = 0;
UINT8 u8I2CCnt = 0;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void I2C_Init(void);
UINT8 I2C_Master_Write_One_Byte(UINT8 u8Addr, UINT8 u8data);
UINT8 I2C_Master_Write_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size);
UINT8 I2C_Master_Read_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size);
//-----------------------------------------------------------------------------
// Main loop
//-----------------------------------------------------------------------------
void main(void)
{
UINT8 u8TxData[MAXDATALEN] = {0xAA,0xBB,0xCC,0xDD,0xEE}; // for test
UINT8 u8RxData[] = {0};
UINT8 u8SlaveAddr = 0x2C; // set addr
CLKSEL = 0x06; // Fcpu = 32M/2 = 16M
CLKCMD = 0X69;
CKCON = 0X10;
WDTR = 0x5A; // clear watchdog if watchdog enable
P0 = 0;
P0M = 0;
P0UR = 0xFF;
P1 = 0;
P1M = 0;
P1UR = 0xFF;
P2 = 0;
P2M = 0;
P2UR = 0xFF;
I2C_Init();
// Write one data to slave
I2C_Master_Write_One_Byte(u8SlaveAddr, u8TxData[0]);
// Write multi data to slave
I2C_Master_Write_N_Byte(u8SlaveAddr, u8TxData, sizeof(u8TxData));
// read 1 data form slave
I2C_Master_Read_N_Byte(u8SlaveAddr, u8RxData, 1);
// read multi data form slave
I2C_Master_Read_N_Byte(u8SlaveAddr, u8RxData, sizeof(u8RxData));
while (1)
{
WDTR = 0x5A; // clear watchdog if watchdog enable
}
}
//-----------------------------------------------------------------------------
// Function: void I2C_Init(void)
// Description:
// SCL and SDA need to external pull high
//-----------------------------------------------------------------------------
void I2C_Init(void)
{
I2CCON = 0x43; // I2C enable (ENS1),Fclk = Fcpu/160 = 100K
EI2C = 1; // I2C interrupt enable
EAL = 1; // Interrupt enable
}
//-----------------------------------------------------------------------------
// Function: UINT8 I2C_Master_Write_One_Byte(UINT8 u8Addr, UINT8 u8Data)
// Description:
//
//-----------------------------------------------------------------------------
UINT8 I2C_Master_Write_One_Byte(UINT8 u8Addr, UINT8 u8Data)
{
if (!u8I2CFlag) {
u8I2CFlag = 1; // get Use Right
u8I2CLen = 1; // set length
u8I2CCnt = 0; // clear tx count
u8I2CData[0] = u8Data;
u8I2CAddr = u8Addr & 0xFE; // write mode
I2CCON |= 0x20; // START (STA) = 1
while(u8I2CFlag); // wait Trasmit end
return TRUE;
}
else
return FALSE;
}
//-----------------------------------------------------------------------------
// Function: UINT8 I2C_Master_Write_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size)
// Description:
//
//-----------------------------------------------------------------------------
UINT8 I2C_Master_Write_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size)
{
if (u8size > MAXDATALEN)
return FALSE; // over buf size
if (u8size == 0)
return FALSE; // error size
if (!u8I2CFlag) {
u8I2CFlag = 1; // get Use Right
u8I2CLen = u8size; // set length
u8I2CCnt = 0; // clear tx count
u8I2CAddr = u8Addr & 0xFE; // write mode
memcpy(u8I2CData, u8data, u8size);
I2CCON |= 0x20; // START (STA) = 1
while(u8I2CFlag); // wait Trasmit end
return TRUE;
}
else
return FALSE;
}
//-----------------------------------------------------------------------------
// Function: UINT8 I2C_Master_Read_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size)
// Description:
//
//-----------------------------------------------------------------------------
UINT8 I2C_Master_Read_N_Byte(UINT8 u8Addr, UINT8 *u8data, UINT8 u8size)
{
if (u8size > MAXDATALEN)
return FALSE; // over buf size
if (u8size == 0)
return FALSE; // error size
if (!u8I2CFlag) {
u8I2CFlag = 1; // get Use Right
u8I2CLen = u8size; // set length
u8I2CCnt = 0;
u8I2CAddr = u8Addr | 0x01; // read mode
I2CCON |= 0x20; // START (STA) = 1
while(u8I2CFlag); // wait recevier end
memcpy(u8data, u8I2CData, u8size);
return TRUE;
}
else
return FALSE;
}
//-----------------------------------------------------------------------------
// Function: I2C interrupt
// Description:
//
//-----------------------------------------------------------------------------
void I2C_ISR(void) interrupt ISRI2c // Vector @ 0x43
{
switch (I2CSTA)
{
// tx mode
case 0x08:
I2CCON &= 0xDF; // START (STA) = 0
I2CDAT = u8I2CAddr; // Tx/Rx addr
break;
case 0x18: // write first byte
I2CDAT = u8I2CData[u8I2CCnt++];
u8I2CLen--;
break;
case 0x28: // write n byte
if (u8I2CLen == 0) { // chk length if empty
I2CCON |= 0x10; // STOP (STO)
u8I2CFlag = 0;
u8I2CCnt = 0;
}
else {
I2CDAT = u8I2CData[u8I2CCnt++];
u8I2CLen--;
}
break;
// rx mode
case 0x40: // get slave addr
if(u8I2CLen > 1) I2CCON |= 0x04; // AA = 1
else I2CCON &= 0xFB;
break;
case 0x50: // read n byte
u8I2CData[u8I2CCnt++] = I2CDAT;
if (u8I2CCnt == (u8I2CLen-1)) {
I2CCON &= 0xFB; // AA = 0
}
else {
I2CCON |= 0x04; // AA = 1
}
break;
case 0x58: // read last byte & stop
u8I2CData[u8I2CCnt] = I2CDAT;
I2CCON |= 0x10; // STOP (STO)
u8I2CFlag = 0;
break;
default:
I2CCON |= 0x10; // STOP (STO)
u8I2CFlag = 0;
}
I2CCON &= 0xF7; // Clear I2C flag (SI)
}
复制代码
5703_I2C_20161104.rar
2020-12-8 11:35 上传
点击文件名下载附件
下载积分: 黑币 -5
115.79 KB, 下载次数: 10, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1