• 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






Re: 關於中斷的問題
#7
版主
版主


查看用戶資訊
24Cxx 系列的 I2C EEPROM 算是最基本的 Slave Device ,建議先用 24Cxx 系列的元件來練習 I2C 的 Master 的傳送,等初步熟悉了 I2C 的 protocol 後再連接你的 Slave Device 這樣比較不會搞不清楚是 Master & Slave 的錯。

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


Re: 關於中斷的問題
#6
新會員
新會員


查看用戶資訊
跟意麵大說的一樣
小弟我真的是土法煉鋼型的
小弟知道降的學習 底子會很不穩
但..被趕鴨子上架了..ORZ
而且小弟的slave device還不是EEPROM
而是其他種類的device
小弟搞懂了I2C的通訊協定之後
就用PICC裡面的範例程式所改過來的
感謝意麵大大的提醒
我會去買書回來好好學習的~~!!!
謝謝大家..^^


發表於: 2007/8/17 14:02
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於中斷的問題
#5
資深會員
資深會員


查看用戶資訊
大致上看了一遍,發現你是用土法煉鋼的方式在寫程式,用自己的直覺和手頭上一些零散的資料在寫程式做實驗,這種學習方式會遇到很多問題的,建議你先去買一本曾百由老師出的C語言與PIC18微控制器這本書,先很快的看過一遍,再依書上的說明自己把程式打一次,這樣的學習效率會比較好.

回歸正題,在這本書上的360頁有寫到,I2C讀eeprom的程序:
1.開始訊號.(S)
2.控制信號(7bit)
3.確認訊號(ACK)
4.寫入資的記憶體位址(address 8bit)
5.確認訊號(ACK)
6.重新開始訊號.(S)
7.Address 7bit
8.讀取狀態(RD(1) 1bit)
9.確認訊號(ACK)
10.讀取資料(DATA 8bit)
11.未確認信號(NACK)
12.結束訊號(p)

感覺上還蠻複的,所以我都用SPI的EEPROM不用I2C,其實以上這些複雜的東東,microchip有提供範例程式,
C:/mcc18/src/extended/pmc/i2c/i2c1eebw.c

unsigned char EEByteWrite1unsigned char controlunsigned char addressunsigned char data )
{
  
IdleI2C1();                      // ensure module is idle
  
StartI2C1();                     // initiate START condition
  
while ( SSP1CON2bits.SEN );      // wait until start condition is over 
  
if ( PIR2bits.BCL1IF )           // test for bus collision
  
{
    return ( -
);                 // return with Bus Collision error 
  
}
  else                             
// start condition successful
  
{
    if ( 
WriteI2C1control ) )    // write byte - R/W bit should be 0
    
{
      return ( -
);               // set error for write collision
    
}

    
IdleI2C1();                    // ensure module is idle
    
if ( !SSP1CON2bits.ACKSTAT )   // test for ACK condition received
    

      if ( 
WriteI2C1address ) )  // write word address for EEPROM
      
{
        return ( -
);             // set error for write collision
      
}

      
IdleI2C1();                  // ensure module is idle
      
if ( !SSP1CON2bits.ACKSTAT // test for ACK condition received
      

        if ( 
WriteI2C1data ) )   // data byte for EEPROM
        
{
          return ( -
);           // set error for write collision
        
}
      }
      else
      {
        return ( -
);             // return with Not Ack error condition   
      
}
    }
    else
    {
      return ( -
);               // return with Not Ack error condition   
    
}
  }

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


Re: 關於中斷的問題
#4
資深會員
資深會員


查看用戶資訊
小弟寫了個簡單的中斷程式
主程式內只有
while(1)
然後timer1 25ms中斷一次
但每次程式跑一跑
一開始還正常
之後就會出現 只有time1正常計時並執行中斷程式
主迴圈內的printf就再也沒被執行過了
不知道版上各位高手是否有遇過降的問題
可以指點一下小弟我
感謝大家..^^
p.s. 小弟是使用picc編譯
========================
小弟使用的是18F452
不好意思 以上這個問題已經解決了
是小弟的程式結構不好
調整之後已經解決了
但現在出現另一個問題
就是在char read_ext_eeprom(int address) {}
這段程式內的
data = i2c_read(0); // read byte
這邊常常都會停很久
導致MCU重啟
而我在I2C的設定中
已經設定在read的部份 會自動restart wdt
(#use I2C(master, sda=I2C_SDA, scl=I2C_SCL,RESTART_WDT,FORCE_HW))
不知道為什麼還是會讓MCU重啟
麻煩大家幫我看看了 謝謝
意麵大哥,謝謝你提醒我把code貼上來..^^
p.s 每次都po些笨問題,謝謝大家不吝指教..^^
//================================
主程式
void main
(void){
    
InitI2C();
    
InitPIC();
    
I2C_Init();
    
InitInterrupts();

    
write_ext_eeprom(0x00,0x0A);
    
write_ext_eeprom(0x01,0x0E);
    
write_ext_eeprom(0x02,0x03);
    
write_ext_eeprom(0x03,0x00);
    
write_ext_eeprom(0x05,0x00);
    
write_ext_eeprom(0x04,0x00);
    
write_ext_eeprom(0x06,0x00);
    
write_ext_eeprom(0x07,0x00);
    
printf("Startrn");

    while(
1){
        
restart_wdt();
        if(
printf_flag == 1){
           
printf("now data = %xrn",j8);
           
j8 0;
           
printf_flag 0;
        }
    }
}

//==============================
//中斷部份
#int_timer1 //0.025sec
timer1_interrupt(){
    
disable_interrupts(GLOBAL);
    
Timer1Count++;
    
restart_wdt();
    
j8 read_ext_eeprom(0x08);
    if (
Timer1Count>=40){
        
Timer1Count 0;
        
printf_flag 1;
    }
    
enable_interrupts(GLOBAL);
}

//==================================
//I2C(write)
void write_ext_eeprom(int addressint data) {
    
int i =9;
    
restart_wdt();
    
i2c_start();        // start bit
    
i2c_write(0x1C);    // control byte
    
i2c_write(address); // Address low byte
    
i2c_write(data);    // data byte
    
i2c_stop();         // stop bit
    
delay_ms(11);       // delay to allow write
}

//==================================
//I2C(read)
char read_ext_eeprom(int address) {
    
int i =9;
    
restart_wdt();
    
i2c_start();        // start bit
    
i2c_write(0x1C);    // control byte
    
i2c_write(address); // address low byte
    
i2c_start();        // restart
    
i2c_write(0x1D);    // control byte
    
data i2c_read(0); // read byte
    
i2c_stop();         // stop bit
    
return(data);
}


發表於: 2007/8/17 10:40
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於中斷的問題
#3
新會員
新會員


查看用戶資訊
不好意思
晚點將程式補上
(程式不在身邊..)
感謝大大...^^

發表於: 2007/8/16 14:34
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於中斷的問題
#2
資深會員
資深會員


查看用戶資訊
可以列出所有的程式嗎??至少列出中斷那部分...還有是用那一顆PIC??

發表於: 2007/8/16 0:43
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


關於中斷的問題
#1
新會員
新會員


查看用戶資訊
小弟寫了個簡單的中斷程式
主程式內只有
while(1)
然後timer1 25ms中斷一次
但每次程式跑一跑
一開始還正常
之後就會出現 只有time1正常計時並執行中斷程式
主迴圈內的printf就再也沒被執行過了
不知道版上各位高手是否有遇過降的問題
可以指點一下小弟我
感謝大家..^^
p.s. 小弟是使用picc編譯
========================
小弟使用的是18F452
不好意思 以上這個問題已經解決了
是小弟的程式結構不好
調整之後已經解決了
但現在出現另一個問題
就是在char read_ext_eeprom(int address) {}
這段程式內的
data = i2c_read(0); // read byte
這邊常常都會停很久
導致MCU重啟
而我在I2C的設定中
已經設定在read的部份 會自動restart wdt
(#use I2C(master, sda=I2C_SDA, scl=I2C_SCL,RESTART_WDT,FORCE_HW))
不知道為什麼還是會讓MCU重啟
麻煩大家幫我看看了 謝謝
意麵大哥,謝謝你提醒我把code貼上來..^^
p.s 每次都po些笨問題,謝謝大家不吝指教..^^
================================
主程式
void main(void)
{
InitI2C();
InitPIC();
I2C_Init();
InitInterrupts();

write_ext_eeprom(0x00,0x0A);
write_ext_eeprom(0x01,0x0E);
write_ext_eeprom(0x02,0x03);
write_ext_eeprom(0x03,0x00);
write_ext_eeprom(0x05,0x00);
write_ext_eeprom(0x04,0x00);
write_ext_eeprom(0x06,0x00);
write_ext_eeprom(0x07,0x00);

printf("Start\r\n");

while(1)
{
restart_wdt();
if(printf_flag == 1)
{
printf("now data = %x\r\n",j8);
j8 = 0;
printf_flag = 0;
}
}
}

==============================
中斷部份
#int_timer1 //0.025sec
timer1_interrupt()
{
disable_interrupts(GLOBAL);
Timer1Count++;
restart_wdt();
j8 = read_ext_eeprom(0x08);
if (Timer1Count>=40)
{
Timer1Count = 0;
printf_flag = 1;
}
enable_interrupts(GLOBAL);
}

==================================
I2C(write)
void write_ext_eeprom(int address, int data) {
int i =9;
restart_wdt();
i2c_start(); // start bit
i2c_write(0x1C); // control byte
i2c_write(address); // Address low byte
i2c_write(data); // data byte
i2c_stop(); // stop bit
delay_ms(11); // delay to allow write
}

==================================
I2C(read)
char read_ext_eeprom(int address) {
int i =9;
restart_wdt();
i2c_start(); // start bit
i2c_write(0x1C); // control byte
i2c_write(address); // address low byte
i2c_start(); // restart
i2c_write(0x1D); // control byte
data = i2c_read(0); // read byte
i2c_stop(); // stop bit
return(data);
}

發表於: 2007/8/15 16:03
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... ]

教育訓練中心

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