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


Browsing this Thread:   1 Anonymous Users






PIC16F628A 軟體 I2C
#1
初級會員
初級會員


查看用戶資訊
大家好
我是 Microchip 的新手
最近在寫軟體 I2C
可是卻一直連不起來
想請教一下是哪邊有問題
煩請各位指教
謝謝

MCU:PIC16F628A
EEPROM:24C16
MPLAB IDE v8.00
PICC v8.05

程式如下:

#include <pic.h>
#include "global.h"

void Delay( void )
{
NOP();NOP();NOP();NOP();
}

void I2cStart( void )
{
SDA = 1;
NOP();NOP();
SCL_IO = OUT;
SDA_IO = OUT;
NOP();NOP();
SDA = H;
NOP();NOP();
SCL = H;
NOP();NOP();
SDA = L;
NOP();NOP();
}

void I2cSendAddr( unsigned char SlaveAddr, unsigned char Mode )// Write : Mode = 0, Read : Mode = 1
{
unsigned char Addr;
signed char i;
Addr = SlaveAddr | Mode;
for( i=7;i>=0;i-- )
{
SCL = L;
NOP();NOP();
SDA = ( Addr >> i ) & 0x01;
NOP();NOP();
SCL = H;
NOP();NOP();
}
SDA_IO = IN;
SCL = L;
NOP();NOP();
SCL = H;
NOP();NOP();
}

void I2cSendData( unsigned char Data )
{
signed char i;
SDA_IO = OUT;
NOP();NOP();
for( i =7;i>=0;i-- )
{
SCL = L;
NOP();NOP();
SDA = ( Data >> i) & 0x01;
NOP();NOP();
SCL = H;
NOP();NOP();
}
SDA_IO = IN;
SCL = L;
NOP();NOP();
SCL = H;
NOP();NOP();
}

unsigned char I2cRecData( void )
{
signed char i;
unsigned char temp2;
temp2 = 0x00;
SDA_IO = IN;
NOP();NOP();
for( i=7;i>=0;i-- )
{
SCL = L;
NOP();NOP();
SCL = H;
NOP();NOP();
temp2 << 1;
if( SDA == H ) temp2 |= 0x01;
}
SDA_IO = OUT;
NOP();NOP();
SCL = L;
NOP();NOP();
SDA = H;
NOP();NOP();
SCL = H;
NOP();NOP();
return temp2;
}

void I2cStop( void )
{
SDA_IO = OUT;
NOP();NOP();
SDA = L;
NOP();NOP();
SCL = H;
NOP();NOP();
SDA = H;
NOP();NOP();
SCL_IO = IN;
SDA_IO = IN;
NOP();NOP();
}

void main( void )
{
unsigned char temp1;

EEPROM_WRITE(0x03,0x00);

I2cStart();
I2cSendAddr(0xa0,0);
I2cSendData(0x00);
I2cSendData(0xaa);
I2cStop();

Delay();

I2cStart();
I2cSendAddr(0xa0,0);
I2cSendData(0x00);
I2cStart();
I2cSendAddr(0xa0,1);
temp1 = I2cRecData();
I2cStop();

EEPROM_WRITE(0x03,temp1);
}

發表於: 2007/12/11 16:37
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: PIC16F628A 軟體 I2C
#2
新會員
新會員


查看用戶資訊
同鞋A:
你可以參考Microchip AN997範例程式 , 我用過OK

發表於: 2007/12/11 17:37
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: PIC16F628A 軟體 I2C
#3
初級會員
初級會員


查看用戶資訊
謝謝
最近才有空做測試
AN997範例程式的確可用

不過不知為何我自己寫的I2C都不能用
請教一下各位
在PIC上寫軟體I2C需要注意哪些地方呢

我之前都寫8051架構的
最近才開始使用PIC

發表於: 2007/12/26 17:43
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: PIC16F628A 軟體 I2C
#4
版主
版主


查看用戶資訊
用軟體去模擬I2C的動作,第一就是如何使用 I/O腳功能。因為 I2C 的介面是屬於 Open Drain 的驅動方式,外部 I2C Bus
都會接有 nK ohm 的提升電阻。所以就不能用一般的控制方式來控制 I2C,想一想如果Master & Slave 都同時要送出信號時會有何種情形發生,有可能 Fan Out 較小的 IC 可能就會掛掉。
使用 PIC 做軟體 I2C 的模擬時,須先將 SCL & SDA 設為輸入功能並將 "0" 寫到 SCL & SDA。利用改變 SCL & SDA 兩腳的 TRIS 做為輸入時就當是輸出 High 電位(因為高阻抗且有提升電阻),做為輸出時,因為早就寫零進去了所以輸出就會為 Low。用這種方式就不會有 Bus 打架的現象。

I2C 的程式執行結果為何?基本上最好是有一台示波器可以監看 Bus 上的訊號,單看程式是很不好除錯的。

發表於: 2007/12/27 11:28
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: PIC16F628A 軟體 I2C
#5
初級會員
初級會員


查看用戶資訊
原來如此
受教了

發表於: 2007/12/27 18:12
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... ]

教育訓練中心

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