• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
  • slider image 488
  • slider image 491
:::


Browsing this Thread:   1 Anonymous Users






請問關I2C的問題
#1
新會員
新會員


查看用戶資訊
請問i2c和icd2是不是沒有辦法同時使用
我是使用pic18f452來做的
一開始可以傳送和接收
但傳送兩個以上slave時會有錯誤
所以改用icd2抓bug
但換上icd2後卻完全沒辦法使用了
以下是課本的程式
==========================
master
==========================
#include <p18cxxx.h>
#include <delays.h>

#define I2C_Write 0x00
#define I2C_Read 0x01
#define I2C_Device1 0x20

void F_Initial(void);
void F_I2C_START(void);
unsigned char F_I2C_WAIT(void);
unsigned char F_I2C_Send_Address(unsigned char,unsigned char);
void F_I2C_Send_Data(unsigned char);
void F_I2C_STOP(void);
unsigned char F_I2C_Read_Data(void);

void main(void)
{
unsigned char UC_I2C_CMD=I2C_Write,UC_I2C_Data=0;
F_Initial();
while(1)
{
switch (UC_I2C_CMD)
{
case I2C_Write:
F_I2C_START();
if (F_I2C_Send_Address(I2C_Device1,UC_I2C_CMD))
F_I2C_Send_Data(UC_I2C_Data++);
F_I2C_STOP();
Delay10KTCYx(20);
UC_I2C_CMD=I2C_Read;
break;
case I2C_Read:
F_I2C_START();
if (F_I2C_Send_Address(I2C_Device1,UC_I2C_CMD))
LATD = F_I2C_Read_Data();
F_I2C_STOP();
Delay10KTCYx(20);
UC_I2C_CMD=I2C_Write;
break;
default:
break;
}
}
}

void F_Initial(void)
{
TRISD = 0;
LATD = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
SSPSTATbits.SMP = 1;
SSPSTATbits.CKE = 0;
SSPCON1 = 0x38;
SSPADD = 0x30;
}

void F_I2C_START(void)
{
SSPCON2bits.SEN = 1;
while(!F_I2C_WAIT());
}

unsigned char F_I2C_WAIT(void)
{
if ( PIR1bits.SSPIF )
{
PIR1bits.SSPIF = 0;
return (1);
}
else
return (0);
}

unsigned char F_I2C_Send_Address(unsigned char UC_Address,unsigned char UC_RW)
{
SSPBUF = ((UC_Address & 0xFE) | UC_RW);
while(!F_I2C_WAIT());
if ( SSPCON2bits.ACKSTAT )
return (0);
else
return (1);
}

void F_I2C_Send_Data(unsigned char UC_Data)
{
SSPBUF = UC_Data;
while(!F_I2C_WAIT());
}

void F_I2C_STOP(void)
{
SSPCON2bits.PEN = 1;
while(!F_I2C_WAIT());
}

unsigned char F_I2C_Read_Data(void)
{
unsigned char UC_Temp=0;
SSPCON2bits.RCEN = 1;
while(!F_I2C_WAIT());
UC_Temp = SSPBUF;
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(!F_I2C_WAIT());
return ( UC_Temp );
}
==========================
slave
==========================
#include <p18cxxx.h>
#include <portb.h>
#include <i2c.h>

#define I2C_Address 0x20

unsigned char I2C_Data=0x01;

void F_Initial ( void );
void F_Enable_High_Interrupts ( void );
void F_Check_I2C_CMD(void);

void High_INT_SER ( void );

#pragma code High_Interrupt_Vector=0x08
void High_INT_VEC (void)
{
_asm
goto High_INT_SER
_endasm
}
#pragma code

#pragma interrupt High_INT_SER
void High_INT_SER (void)
{
if ( PIR1bits.SSPIF == 1 )
{
PIR1bits.SSPIF = 0;
F_Check_I2C_CMD();
}
}

void main (void)
{
F_Initial();
while(1);
}

void F_Initial ()
{
TRISD = 0;
LATD = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
SSPADD = I2C_Address;
SSPSTATbits.SMP = 1;
SSPSTATbits.CKE = 0;
SSPCON1 = 0x36;
PIE1bits.SSPIE = 1;
IPR1bits.SSPIP = 1;
F_Enable_High_Interrupts();
}

void F_Enable_High_Interrupts (void)
{
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
}

void F_Check_I2C_CMD(void)
{
unsigned char UC_I2C_CMD,UC_Temp;
UC_I2C_CMD = (0x2D & SSPSTAT);
switch (UC_I2C_CMD)
{
case 0x09:
UC_Temp = ReadI2C();
break;
case 0x29:
LATD = ReadI2C();
break;
case 0x0C:
SSPCON1bits.CKP = 0;
SSPBUF = I2C_Data;
SSPCON1bits.CKP = 1;
I2C_Data <<= 1;
if ( I2C_Data == 0)
I2C_Data=0x01;
break;
default:
break;
}
}

謝謝大家的回答

發表於: 2006/5/23 11:28
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------