首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

DZ16 IIC問題求助

DZ16 IIC問題求助

求助各位大大.....我的IIC始終不能作動下面是我的程序設定
我的MC9S08DZ16蕊片時脈是20MHZ  我用的EEPROM是AT24C02B請各位大大幫幫忙!!
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define W_EEPROM_ADDR 0xA0;
#define R_EEPROM_ADDR 0xA1;
void IICdelay(void);
void outputdelay(void);
void IICInit(void);
void writeEEPROM(unsigned char caddr_data,unsigned char data);
unsigned char ReadEEPROM(unsigned char caddr);
unsigned char i=0;
unsigned char Databuff[8];

void MCG_Init(void);
void main(void) {
   
    MCG_Init();
        SOPT1_IICPS=1;
    IICInit();
    PTADD=0xFF;
   
  EnableInterrupts; /* enable interrupts */
  /* include your code here */
   
    writeEEPROM(0x01,0x01);
    IICdelay();
    writeEEPROM(0x02,0x02);
    IICdelay();
    writeEEPROM(0x03,0x04);
    IICdelay();
    writeEEPROM(0x04,0x08);
    IICdelay();
    writeEEPROM(0x05,0x10);
    IICdelay();
    writeEEPROM(0x06,0x20);
    IICdelay();
    writeEEPROM(0x07,0x40);
    IICdelay();
    writeEEPROM(0x08,0x80);
    IICdelay();
    Databuff[0]=ReadEEPROM(0x01);
    Databuff[1]=ReadEEPROM(0x02);
    Databuff[2]=ReadEEPROM(0x03);
    Databuff[3]=ReadEEPROM(0x04);
   
    Databuff[4]=ReadEEPROM(0x05);
    Databuff[5]=ReadEEPROM(0x06);
    Databuff[6]=ReadEEPROM(0x07);
    Databuff[7]=ReadEEPROM(0x08);            
  for(;;) {
    //__RESET_WATCHDOG(); /* feeds the dog */

    for(i=0;i<8;i++) {
      
      PTAD=Databuff[i];
      outputdelay();
      outputdelay();
      
     
   }
  } /* loop forever */
  /* please make sure that you never leave main */
}
void IICdelay(void) {
  unsigned int i=200;
  while(i--) {
  }
}
void outputdelay(void) {
  unsigned int j=0;
  for(j=0;j<0x6000;j++){
  }
   
}
void IICInit(void) {
IICC1_IICEN=1;
IICC1_TXAK=1;
IICC1_MST=0;


IICF=0x4B;
IICS_SRW=0;

}
void writeEEPROM(unsigned char caddr_data,unsigned char data) {
   IICC1_TXAK=0;
  IICC1|=0x30;
  IICD=0xA0;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICD=caddr_data;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICD=data;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICS_IICIF=1;
  IICC1_MST=0;
}
unsigned char ReadEEPROM(unsigned char caddr) {
unsigned char cdata;
   IICC1_TXAK=0;
  IICC1|=0x30;
  IICD=0xA0;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICD=caddr;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICC1_RSTA=0;
  IICD=0xA1;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  while(IICS_RXAK);
  IICC1_TX=0;
  IICC1_TXAK=1;
  cdata=IICD;
  while(!IICS_IICIF);
  IICS_IICIF=1;
  IICC1_MST=0;
  cdata=IICD;
  return cdata;
}
void MCG_Init(void) {
// if (ExtClkFlg == 1) { /* If using a 4 MHz crystal */   
       if (SRS_LOC == 0)  {   /* If no loss of clock has occurred, enter PEE to generate 16 MHz bus */   
           /* FEI -> FBE Mode */   
           MCGC2 = 0x36;      /* BDIV by 1, xtal freq RANGE high, High GAIN, EREFS set */   
           MCGC1 = 0xB8;      /* RDIV by 128 down to 31.25 kHz for FLL, Switch to ext. ref  */   
           while (MCGSC_IREFST);       /* Check internal ref. no longer on */   
           MCGC3 = 0xA0;      /* LOL interrupt, LOC reset (CME) enabled (ONLY after IREFS = 0)  */     
           while (!MCGSC_OSCINIT);     /* Wait until crystal initializes */   
           while (MCGSC_CLKST != 0b10);/* Check external ref. switched */   
         
           /* FBE -> PBE */   
           MCGC3 = 0xE2;      /* Set PLLS bit with VDIV mult by 32 */     
           MCGC1 = 0x90;      /* RDIV by 4 (4 MHz/4 = 1 MHz for PLL) */   
           while (!MCGSC_PLLST);   
           while (!MCGSC_LOCK);     
   
           /* PBE -> PEE */   
           MCGC1 =  0x10;     /* CLKS changed to [0:0] - PLL output = 32 MHz clock, 16 MHz bus */   
           while (MCGSC_CLKST != 0b11);   
       }
}
我也碰到过类似的问题!!在读写开始在使能一下IIC模块,我的当时就是这样解决的!!
返回列表