• slider image 71
  • slider image 72
  • slider image 73
:::


Browsing this Thread:   1 Anonymous Users






24C02問題
#1
高級會員
高級會員


查看用戶資訊
目前使用PIC16F877A 寫一個每按壓一次SW,藉由24C02寫入並讀出數值(由7-SEG顯示),但目前按壓無任何動作,
單純寫入24C02數值並由7-SEG讀出數值都是oK的,但是寫入後要再讀出,並由7-SEG顯示卻無法動作,繁複看了好幾次一直找不到問題點,是否有前輩可以幫忙指導一下

#include <pic.h> //覃蚚PIC16F87XA等儂腔芛恅璃
//#include "877_I2C.H"
//#include "24C02.H"

void delay(int num);
void I2C_Init(void);
void I2C_Start(void);
void I2C_RptStart(void);
void I2C_Stop(void);
int I2C_Read(unsigned char ack);
unsigned char I2C_Write(unsigned char I2CWriteData);
void write_ext_eeprom(unsigned char address,unsigned char data);
unsigned char read_ext_eeprom(unsigned char address);


__CONFIG(HS & LVPDIS & WDTDIS);



const unsigned char LED[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 //0~9
};

//---------------------------------------
void main() //翋滲杅,等儂羲儂綴憩岆植涴跺滲杅羲宎堍俴
{
unsigned char Reset_Count=0;
unsigned char EEP_Address=0x04;

TRISD=0X00; //7-SEG Output
PORTD=0Xff; //7-SEG low
TRISB1=1; //RB1 input
TRISE=0x00; //control 7-SEG
PORTE=0b11111101; //RE1 low

I2C_Init();
//Reset_Count=read_ext_eeprom(EEP_Address);
//Reset_Count++;
PORTD=LED[Reset_Count];
// write_ext_eeprom(EEP_Address,Reset_Count);
while(1)
{


if(RB1==0)
{
delay(20);
if(RB1==0)
{
Reset_Count++;
if(Reset_Count>=10)
{
Reset_Count=0;
}
write_ext_eeprom(EEP_Address,Reset_Count);

Reset_Count=read_ext_eeprom(EEP_Address);
PORTD=LED[Reset_Count];


}
}

}
}

//----------------I2C----------------------------------
void I2C_Init()
{
TRISC3=1; //SCL set input
TRISC4=1; //SCL set input

SSPCON=0x38; //設定I2C模式
SSPCON2=0x00;
SSPADD=0x09; //主頻4M ,傳輸率:100K

CKE=0;
SMP=1;
SSPIF=0;
BCLIF=0;
}

void I2C_Start()
{
SEN=1;
while(SEN);
}

void I2C_RptStart()
{
RSEN=1;
while(RSEN);
}

void I2C_Stop()
{
PEN=1;
while(PEN);
}

int I2C_Read(unsigned char ack)
{
unsigned char I2CReadData;

RCEN=1;
while(RCEN);
I2CReadData=SSPBUF;
if(ack)
{
ACKDT=0;
}
else
{
ACKDT=1;
}

ACKEN=1;
while(ACKEN);
return(I2CReadData);
}

unsigned char I2C_Write(unsigned char I2CWriteData)
{
SSPBUF=I2CWriteData;
while(RW);
return(!ACKSTAT);
}


//----------------24c02--------------------------------
void write_ext_eeprom(unsigned char address, unsigned char data)
{
I2C_Start();
I2C_Write(0b10100000); //A2~A0:000, R/W=0
I2C_Write(address);
I2C_Write(data);
I2C_Stop();
delay(150); //數值沒寫無法正常跑
}

unsigned char read_ext_eeprom(unsigned char address)
{
unsigned char data;

I2C_Start();
I2C_Write(0b10100000); //A2~A0:000, R/W=0
I2C_Write(address);
I2C_RptStart();
I2C_Write(0b10100001); //A2~A0:000, R/W=1
data=I2C_Read(0);
I2C_Stop();
return(data);
}

//----------------delay--------------------------------
void delay(int num)
{
int aa,bb;
aa=num;
while(aa)
{
bb=num;
while(bb)
{bb--;}
aa--;
}
}

發表於: 2018/4/5 0:52
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 24C02問題
#2
資深會員
資深會員


查看用戶資訊
當RB1==0 20ms之後Reset_Count++,
當RB1持續==0 Reset_Count會一直++下去,
EEPROM也會一直被寫入...,

應該要改偵測到一次按鍵動作,做一次寫入和更新顯示.

發表於: 2018/4/9 9:57
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 24C02問題
#3
高級會員
高級會員


查看用戶資訊
我的按鍵是有壓才是0, delay(20)是防止機械彈跳使用

發表於: 2018/4/9 23:47
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.

[進階搜尋]