No exact or substring matches. trying for part
PICList
Thread
'Trying to access EEPROM on PIC16CE625 with HiTech '
2000\03\27@060652
by
Darren Leigh Gardner
|
I am trying to write to and read from EEPROM on a PIC16CE625 using the Hi
Tech C compiler.
I am having all kinds of hastles and can not get it working. I have pretty
much just translated
the asm source code, supplied from Mircochip. Anyway to cut a long story
short it is not working.
If anyone has a moment or too and has a PIC16CE625 can they try it out for
me, and or give me
some tips on what i am doing wrong. It would be much appreciated.
All the code is pased below.
The code and pic and everything works as it flashes the led 4 times on
startup
as it is suppose to, it just does not seem to be reading or writing from
Memory.
This is just a small project I have been working on and it is starting to
annoy me.
Thanks.
Darren
/*
Code.c eeprom
*/
#include
#define u_char unsigned char
#define u_int unsigned int
/*port setup constants*/
#define PORTAIO 0x18 //IO direction - xxx11000
#define PORTBIO 0x63 //IO direction - 01100011 (1 input)
#define LED RB7 //Output
#define FALSE 0
#define TRUE 1
void MedDelay(u_char);
void MSDelay(u_char);
void FlashLED(u_char NoOfFlashes,u_char MSDelayTime); //Flashes
the LED
/*EEPROM STUFF*/
#define NOP() asm("nop")
#define testbit(data,bitno) ((data>>bitno)&0x01)
#define i2c_port GPIO
#define Start_bit() EESDA=0
char e2_err;
char PC_offset;
char Read_Random(char adres);
void Write_Byte(char adres,char data);
void Delay_5()
{
}
void Delay_6m()
{
char licz=0;
do
{
Delay_5();
Delay_5();
Delay_5();
} while(--licz);
}
void Stop_bit()
{
EESDA=0;
EESCL=1;
Delay_5();
EESDA=1;
}
void Transfer_Byte(char eebyte)
{
char count=8;
e2_err=0;
do
{
NOP();
EESCL=0;
EESDA=0;
if (testbit(eebyte,7)) EESDA=1;
EESCL=1;
NOP();
eebyte<<=1;
} while(--count);
NOP();
EESCL=0;
NOP();
EESDA=1;
NOP();
NOP();
EESCL=1;
NOP();
if(EESDA) e2_err=1;
EESCL=0;
if(e2_err) Stop_bit();
else NOP(); // because compiler bug
}
char Read_Byte()
{
char count;
char eedata;
EESDA=1;
NOP();
EESCL=1;
count=8;
do
{
EESCL=1;
eedata=(eedata<<1)|EESDA;
EESCL=0;
EESDA=1;
} while(--count);
NOP();
NOP();
EESDA=1;
EESCL=1;
NOP();
NOP();
EESCL=0;
Stop_bit();
return eedata;
}
char Read_Current()
{
EESCL=1;
EESDA=1;
Delay_5();
Start_bit();
Transfer_Byte(0xA1);
if(!e2_err) return Read_Byte();
else
{
Delay_6m();
return 0;
}
}
char Read_Random(char adres)
{
EESCL=1;
EESDA=1;
Delay_5();
Start_bit();
Transfer_Byte(0xA0);
if(!e2_err) Transfer_Byte(adres);
if(!e2_err) return Read_Current();
else
{
Delay_6m();
return 0;
}
}
void Write_Byte(char adres,char data)
{
EESCL=1;
EESDA=1;
Delay_5();
Start_bit();
Transfer_Byte(0xA0);
if(!e2_err) Transfer_Byte(adres);
if(!e2_err) Transfer_Byte(data);
if(!e2_err) Stop_bit();
else Delay_6m();
}
/*-----------------------------------------------------------------
-----------*/
/*-----------------------------------------------------------------
-----------*/
void main()
{
u_char Result = 0;
TRISA = PORTAIO; /*port io configuration*/
PORTA = 0;
TRISB = PORTBIO;
PORTB = 0;
//Set up the OPTION register (Timer1 on CLKOUT with no prescaler)
OPTION = 0b10000000;
//Enable Timer 0 interrupt
INTCON = 0b00000000; /*disable interrupts (global, peripheral,
timer
0)*/
CMCON = 0b00000111; //Turn off the comparator
EEVDD = 1; //Turn on EEPROM
//Flash the LED 4 times to show feed back of starting up.
FlashLED(4, 125);
Write_Byte(0, 0x13);
MSDelay(50);
Result = Read_Random(0);
if (Result ==0x13) LED = 1; else LED = 0;
MSDelay(250);
MSDelay(250);
MSDelay(250);
MSDelay(250);
MSDelay(250);
MSDelay(250);
MSDelay(250);
MSDelay(250);
} //end main
/*-----------------------------------------------------------------
-----------*/
/*void FlashLED(u_char NoOfFlashes,u_char MSDelayTime) - This
function flashes the LED.
- NoOfFlashes - The number of times to flash the LED.
- MSDelayTime - The rate at which the LED is flashed.
/*-----------------------------------------------------------------
-----------*/
/*-----------------------------------------------------------------
-----------*/
void FlashLED(u_char NoOfFlashes,u_char MSDelayTime)
{
u_char LEDLoopCounter = 0;
for (LEDLoopCounter = 0; LEDLoopCounter < NoOfFlashes;
LEDLoopCounter++)
{
LED = 1;
MSDelay(MSDelayTime);
LED = 0;
MSDelay(MSDelayTime);
}
}
/*-----------------------------------------------------------------
-----------*/
/*-----------------------------------------------------------------
-----------*/
/*-----------------------------------------------------------------
-----------*/
void MSDelay(unsigned char mSec)
{
u_char LoopCnt = 0;
while(mSec > 0)
{
for(LoopCnt = 10; LoopCnt > 0; LoopCnt--)
MedDelay(10);
mSec--;
}
}
/*-----------------------------------------------------------------
-----------*/
/*-----------------------------------------------------------------
-----------*/
void MedDelay(unsigned char uSec)
{
while(uSec > 0)
uSec--;
}
/*-----------------------------------------------------------------
-----------*/
2000\03\27@142436
by
Robert Boardman
|
ref ce/eeprom routines these were used on a ce674 change the org and
pclath if required
----- Original Message -----
From: Darren Leigh Gardner <spam_OUTgardnerdTakeThisOuT
OCEAN.COM.AU>
To: <.....PICLISTKILLspam
@spam@MITVMA.MIT.EDU>
Sent: Monday, March 27, 2000 11:54
Subject: Trying to access EEPROM on PIC16CE625 with HiTech C compiler.
> I am trying to write to and read from EEPROM on a PIC16CE625 using the Hi
> Tech C compiler.
> I am having all kinds of hastles and can not get it working. I have
pretty
{Quote hidden}> much just translated
> the asm source code, supplied from Mircochip. Anyway to cut a long story
> short it is not working.
>
> If anyone has a moment or too and has a PIC16CE625 can they try it out for
> me, and or give me
> some tips on what i am doing wrong. It would be much appreciated.
>
> All the code is pased below.
>
> The code and pic and everything works as it flashes the led 4 times on
> startup
> as it is suppose to, it just does not seem to be reading or writing from
> Memory.
>
> This is just a small project I have been working on and it is starting to
> annoy me.
>
> Thanks.
>
> Darren
>
>
> /*
> Code.c eeprom
> */
>
> #include
>
> #define u_char unsigned char
> #define u_int unsigned int
>
> /*port setup constants*/
> #define PORTAIO 0x18 file://IO direction - xxx11000
> #define PORTBIO 0x63 file://IO direction - 01100011 (1 input)
>
> #define LED RB7 file://Output
>
> #define FALSE 0
> #define TRUE 1
>
> void MedDelay(u_char);
> void MSDelay(u_char);
> void FlashLED(u_char NoOfFlashes,u_char MSDelayTime);
file://Flashes
{Quote hidden}> the LED
>
> /*EEPROM STUFF*/
> #define NOP() asm("nop")
> #define testbit(data,bitno) ((data>>bitno)&0x01)
>
> #define i2c_port GPIO
> #define Start_bit() EESDA=0
>
> char e2_err;
> char PC_offset;
>
> char Read_Random(char adres);
> void Write_Byte(char adres,char data);
>
> void Delay_5()
> {
> }
>
> void Delay_6m()
> {
> char licz=0;
> do
> {
> Delay_5();
> Delay_5();
> Delay_5();
> } while(--licz);
> }
>
> void Stop_bit()
> {
> EESDA=0;
> EESCL=1;
> Delay_5();
> EESDA=1;
> }
>
> void Transfer_Byte(char eebyte)
> {
> char count=8;
> e2_err=0;
> do
> {
> NOP();
> EESCL=0;
> EESDA=0;
> if (testbit(eebyte,7)) EESDA=1;
> EESCL=1;
> NOP();
> eebyte<<=1;
> } while(--count);
> NOP();
> EESCL=0;
> NOP();
> EESDA=1;
> NOP();
> NOP();
> EESCL=1;
> NOP();
> if(EESDA) e2_err=1;
> EESCL=0;
> if(e2_err) Stop_bit();
> else NOP(); // because compiler bug
> }
>
> char Read_Byte()
> {
> char count;
> char eedata;
> EESDA=1;
> NOP();
> EESCL=1;
> count=8;
> do
> {
> EESCL=1;
> eedata=(eedata<<1)|EESDA;
> EESCL=0;
> EESDA=1;
> } while(--count);
> NOP();
> NOP();
> EESDA=1;
> EESCL=1;
> NOP();
> NOP();
> EESCL=0;
> Stop_bit();
> return eedata;
> }
>
> char Read_Current()
> {
> EESCL=1;
> EESDA=1;
> Delay_5();
> Start_bit();
> Transfer_Byte(0xA1);
> if(!e2_err) return Read_Byte();
> else
> {
> Delay_6m();
> return 0;
> }
> }
>
> char Read_Random(char adres)
> {
> EESCL=1;
> EESDA=1;
> Delay_5();
> Start_bit();
> Transfer_Byte(0xA0);
> if(!e2_err) Transfer_Byte(adres);
> if(!e2_err) return Read_Current();
> else
> {
> Delay_6m();
> return 0;
> }
> }
>
> void Write_Byte(char adres,char data)
> {
> EESCL=1;
> EESDA=1;
> Delay_5();
> Start_bit();
> Transfer_Byte(0xA0);
> if(!e2_err) Transfer_Byte(adres);
> if(!e2_err) Transfer_Byte(data);
> if(!e2_err) Stop_bit();
> else Delay_6m();
> }
>
>
>
/*-----------------------------------------------------------------
> -----------*/
>
/*-----------------------------------------------------------------
{Quote hidden}> -----------*/
> void main()
> {
> u_char Result = 0;
>
> TRISA = PORTAIO; /*port io configuration*/
> PORTA = 0;
> TRISB = PORTBIO;
> PORTB = 0;
>
>
> file://Set up the OPTION register (Timer1 on CLKOUT with no
prescaler)
{Quote hidden}> OPTION = 0b10000000;
>
> file://Enable Timer 0 interrupt
> INTCON = 0b00000000; /*disable interrupts (global, peripheral,
> timer
> 0)*/
>
> CMCON = 0b00000111; file://Turn off the comparator
>
> EEVDD = 1; file://Turn on EEPROM
>
> file://Flash the LED 4 times to show feed back of starting up.
> FlashLED(4, 125);
>
> Write_Byte(0, 0x13);
> MSDelay(50);
> Result = Read_Random(0);
> if (Result ==0x13) LED = 1; else LED = 0;
>
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> MSDelay(250);
> } file://end main
>
>
>
>
/*-----------------------------------------------------------------
> -----------*/
> /*void FlashLED(u_char NoOfFlashes,u_char MSDelayTime) - This
> function flashes the LED.
> - NoOfFlashes - The number of times to flash the LED.
> - MSDelayTime - The rate at which the LED is flashed.
>
/*-----------------------------------------------------------------
> -----------*/
>
/*-----------------------------------------------------------------
{Quote hidden}> -----------*/
> void FlashLED(u_char NoOfFlashes,u_char MSDelayTime)
> {
> u_char LEDLoopCounter = 0;
> for (LEDLoopCounter = 0; LEDLoopCounter < NoOfFlashes;
> LEDLoopCounter++)
> {
> LED = 1;
> MSDelay(MSDelayTime);
> LED = 0;
> MSDelay(MSDelayTime);
> }
> }
>
>
/*-----------------------------------------------------------------
> -----------*/
>
>
/*-----------------------------------------------------------------
> -----------*/
>
/*-----------------------------------------------------------------
{Quote hidden}> -----------*/
> void MSDelay(unsigned char mSec)
> {
> u_char LoopCnt = 0;
> while(mSec > 0)
> {
> for(LoopCnt = 10; LoopCnt > 0; LoopCnt--)
> MedDelay(10);
> mSec--;
> }
> }
>
/*-----------------------------------------------------------------
> -----------*/
>
>
>
/*-----------------------------------------------------------------
> -----------*/
> void MedDelay(unsigned char uSec)
> {
> while(uSec > 0)
> uSec--;
> }
>
/*-----------------------------------------------------------------
> -----------*/
>
2000\03\27@142440
by
Robert Boardman
|
routines for ce/eeprom used on a 16ce674 change the org & pclath as required
running on 4mhz internal osc
;read write routines for ce674
;load EEADDR WITH ADDRESS
;load EEDATA WITH DATA
;CALL WRITEM TO WRITE TO EEPROM
;CALL READM TO READ TO EEPROM
WRITEM CALL WRITE_BYTE
BTFSS PC_OFFSET,7 ;IF ERROR DO WRITE AGAIN
GOTO WRITEM
RETURN
READM CALL READ_RANDOM
BTFSS PC_OFFSET,7 ;IF ERROR DO READ AGAIN
GOTO READM
MOVF EEDATA,W
RETURN
org 0X400 ;0x940
READ_CURRENT
MOVLW B'10000100' ; PC offset for read current addr. EE_OK bit7='1'
MOVWF PC_OFFSET ; Load PC offset
GOTO INIT_READ_CONTROL
WRITE_BYTE
MOVLW B'10000000' ; PC offset for write byte. EE_OK: bit7 = '1'
GOTO INIT_WRITE_CONTROL
READ_RANDOM
MOVLW B'10000011' ; PC offset for read random. EE_OK: bit7 = '1'
INIT_WRITE_CONTROL
MOVWF PC_OFFSET ; Load PC offset register, value preset in W
MOVLW B'10100000' ; Control byte with write bit, bit 0 = '0'
START_BIT
BCF I2C_PORT,SDA ; Start bit, SDA and SCL preset to '1'
;******* Set up output data (control, address, or data) and counter ********
;***************************************************************************
PREP_TRANSFER_BYTE
MOVWF EEBYTE ; Byte to transfer to EEPROM already in W
MOVLW .8 ; Counter to transfer 8 bits
MOVWF COUNTER
;************ Clock out data (control, address, or data) byte ************
;***************************************************************************
OUTPUT_BYTE
NOP
BCF I2C_PORT,SCL ; Set clock low during data set-up
RLF EEBYTE, F ; Rotate left, high order bit into carry bit
BCF I2C_PORT,SDA ; Set data low, if rotated carry bit is
BTFSC STATUS,C ; a '1', then:
BSF I2C_PORT,SDA ; reset data pin to a one, otherwise leave low
NOP
BSF I2C_PORT,SCL ; clock data into EEPROM
DECFSZ COUNTER, F ; Repeat until entire byte is sent
GOTO OUTPUT_BYTE
NOP ; Needed to meet Timing (Thigh=4000nS)
;************************** Acknowledge Check *****************************
;***************************************************************************
BCF I2C_PORT,SCL ; Set SCL low, 0.5us < ack valid < 3us
NOP ; Needed to meet Timing (Tlow= 4700nS)
BSF I2C_PORT,SDA ; set data line high to check for
acknowledge
GOTO $+1
BSF I2C_PORT,SCL ; Raise SCL, EEPROM acknowledge still valid
NOP ; Tsu:dat (allow time for ack setup)
BTFSC I2C_PORT,SDA ; Check SDA for acknowledge (low)
BCF PC_OFFSET,EE_OK ; If SDA not low (no ack), set error flag
BCF I2C_PORT,SCL ; Lower SCL, EEPROM release bus
BTFSS PC_OFFSET,EE_OK ; If no error continue, else stop bit
GOTO STOP_BIT
;***** Set up program counter offset, based on EEPROM operating mode *****
;***************************************************************************
MOVLW PAGE1
MOVWF PCLATH
MOVF PC_OFFSET,W
ANDLW B'00000111'
ADDWF PCL, F
GOTO INIT_ADDRESS ;PC offset=0, write control done, send
address
GOTO INIT_WRITE_DATA ;PC offset=1, write address done, send
data
GOTO STOP_BIT ;PC offset=2, write done, send stop bit
GOTO INIT_ADDRESS ;PC offset=3, write control done, send
address
GOTO INIT_READ_CONTROL ;PC offset=4, send read control
GOTO READ_BIT_COUNTER ;PC offset=5, set counter and read byte
GOTO STOP_BIT ;PC offset=6, random read done, send stop
;********** Initalize EEPROM data (address, data, or control) bytes ******
;***************************************************************************
INIT_ADDRESS
INCF PC_OFFSET, F ; Increment PC offset to 2 (write) or to 4
(read)
MOVF EEADDR,W ; Put EEPROM address in W, ready to send to
EEPROM
GOTO PREP_TRANSFER_BYTE
INIT_WRITE_DATA
INCF PC_OFFSET, F ; Increment PC offset to go to STOP_BIT next
MOVF EEDATA,W ; Put EEPROM data in W, ready to send to EEPROM
GOTO PREP_TRANSFER_BYTE
INIT_READ_CONTROL
BSF I2C_PORT,SCL ; Raise SCL
BSF I2C_PORT,SDA ; raise SDA
INCF PC_OFFSET, F ; Increment PC offset to go to READ_BIT_COUNTER
next
MOVLW B'10100001' ; Set up read control byte, ready to send to
EEPROM
GOTO START_BIT ; bit 0 = '1' for read operation
;************************** Read EEPROM data *****************************
;***************************************************************************
READ_BIT_COUNTER
BSF I2C_PORT,SDA ; set data bit to 1 so we're not pulling bus
down.
NOP
BSF I2C_PORT,SCL
MOVLW .8 ; Set counter so 8 bits will be read into
EEDATA
MOVWF COUNTER
READ_BYTE
BSF I2C_PORT,SCL ; Raise SCL, SDA valid. SDA still input from
ack
BSF STATUS,C ; Assume bit to be read = 1
BTFSS I2C_PORT,SDA ; Check if SDA = 1
BCF STATUS,C ; if SDA not = 1 then clear carry bit
RLF EEDATA, F ; rotate carry bit (=SDA) into EEDATA;
BCF I2C_PORT,SCL ; Lower SCL
BSF I2C_PORT,SDA ; reset SDA
DECFSZ COUNTER, F ; Decrement counter
GOTO READ_BYTE ; Read next bit if not finished reading byte
BSF I2C_PORT,SCL
NOP
BCF I2C_PORT,SCL
;****************** Generate a STOP bit and RETURN ***********************
;***************************************************************************
STOP_BIT
BCF I2C_PORT,SDA ; SDA=0, on TRIS, to prepare for transition to
'1'
BSF I2C_PORT,SCL ; SCL = 1 to prepare for STOP bit
CALL DELAY4 ; wait 4 cycles Tsu:sto (4.7 us)
MOVLW PAGE0
MOVWF PCLATH
BSF I2C_PORT,SDA ; Stop bit, SDA transition to '1' while SCL
high
BTFSS PC_OFFSET,EE_OK ; Check for error
RETLW NO ; if error, send back NO
RETLW OK ; if no error, send back OK
DELAY4 RETLW 0
{Original Message removed}
2000\03\27@194118
by
William Chops Westfield
> #define testbit(data,bitno) ((data>>bitno)&0x01)
If you change this to:
#define testbit(data,bitno) (data&(1<<bitno))
then the compiler will have a much better chance of being able to
optimize it...
It's not wrong, just potentially slow...
BillW
More... (looser matching)
- Last day of these posts
- In 2000
, 2001 only
- Today
- New search...