|
|
View previous topic :: View next topic |
Author |
Message |
balamagesh Guest
|
Help me |
Posted: Sun Dec 29, 2002 10:19 pm |
|
|
i am using ex_extee.c with external EEPROM (93LC46B which is a 16 bit) how to convert 9346.c which is the 8 bit version of the abvoe mentioned External EEPROM. help me
___________________________
This message was ported from CCS's old forum
Original Post ID: 10319 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Help me |
Posted: Mon Dec 30, 2002 5:05 pm |
|
|
Quote: |
I am using ex_extee.c with external EEPROM (93LC46B which is a 16 bit)
how to convert 9346.c which is the 8 bit version of the above mentioned
External EEPROM. |
I modified the CCS example file 9346.c to make it work
with the 93LC46B.
These routines expect a 16-bit data word. So you have to
send and receive a "long" variable to the read and write
routines.
This file is not tested, since I don't have the EEPROM chip.
Hopefully it will work, but if it doesn't, you will have to
debug it. Also, make sure your hardware is wired correctly.
Code: |
//////////////////////////////////////////////////////////////
// Library for a MicroChip 93LC46B (x16 org)
//
// init_ext_eeprom(); Call before the other functions are used.
//
// write_ext_eeprom(a, d); Write the byte d to the address a.
//
// d = read_ext_eeprom(a); Read the byte d from the address a.
//
// The main program may define eeprom_select, eeprom_di,
// eeprom_do and eeprom_clk to override the defaults below.
//
//////////////////////////////////////////////////////////////
#ifndef EEPROM_SELECT
#define EEPROM_SELECT PIN_B7
#define EEPROM_CLK PIN_B6
#define EEPROM_DI PIN_B5
#define EEPROM_DO PIN_B4
#endif
#define EEPROM_ADDRESS byte
#define EEPROM_SIZE 128
#define hi(x) (*((int8 *)&x+1))
#define lo(x) (char)(x)
//-----------------------------------------------
void init_ext_eeprom()
{
byte cmd[2];
byte i;
output_low(EEPROM_DI);
output_low(EEPROM_CLK);
output_low(EEPROM_SELECT);
// Load the opcode and address for an EWEN instruction
// into a 2-byte array.
cmd[0]=0x80; // Address bit A4 = 1.
// Start bit = 1, Opcode = 0,0, and Address bit A5 = 1.
cmd[1]=0x9;
// Shift the 2 byte array left by 4 bits, so it becomes 0x9800.
// This is the bit pattern shown in Figure 3-5 (EWEN Timing)
// of the 93LC46A/B data sheet. It consists of the Start bit,
// EWEN opcode, and address. The address is only 6 bits.
for(i=1;i<=4;++i)
shift_left(cmd,2,0);
// We use 9 clock pulses to send the Start bit, Opcode and Address.
output_high(EEPROM_SELECT);
for(i=1;i<=9;++i) {
output_bit(EEPROM_DI, shift_left(cmd,2,0));
output_high(EEPROM_CLK);
output_low(EEPROM_CLK);
}
output_low(EEPROM_DI);
output_low(EEPROM_SELECT);
}
//----------------------------------------------------
void write_ext_eeprom(EEPROM_ADDRESS address, long data)
{
byte cmd[4];
byte i;
// Fill a 4-byte array with the Start bit, WRITE opcode,
// address, and 16 bits of data. This is 25 bits, total.
cmd[0] = lo(data) << 2;
cmd[1] = (hi(data) << 2) | (lo(data) >> 6);
cmd[2] = (address << 2) | (hi(data) >> 6);
cmd[3] = 0x5; // Start bit = 1, and Opcode = 0,1
// Shift the 4-byte array left by 5 bits.
// This will left-justify the bits in the array.
for(i=1;i<=5;++i)
shift_left(cmd,4,0);
// Send the 9 bits of header information, and the 16 bits of
// data to the EEPROM. This requires 25 clocks.
output_high(EEPROM_SELECT);
for(i=1;i<=25;++i) {
output_bit(EEPROM_DI, shift_left(cmd,4,0));
output_high(EEPROM_CLK);
output_low(EEPROM_CLK);
}
output_low(EEPROM_DI);
output_low(EEPROM_SELECT);
delay_ms(11);
}
//----------------------------------------------------
long read_ext_eeprom(EEPROM_ADDRESS address)
{
byte cmd[4];
byte i;
long data;
// Setup an array with the READ opcode and the address.
// Fill in the unused bits with zeros. These will be
// shifted out while we are shifting in the data from
// the EEPROM.
cmd[0]=0;
cmd[1]=0;
cmd[2]=address << 2;
cmd[3]=0x6; // Start bit and READ opcode
// Shift the 4-byte array left by 5 bits,
// so that it's left justified in the array.
for(i=1;i<=5;++i)
shift_left(cmd,4,0);
// Send out 25 clocks to the EEPROM. The first 9 clocks
// will be the Start bit, Opcode, and address.
// The next 16 clocks will bring in the data from the EEPROM.
output_high(EEPROM_SELECT);
for(i=1;i<=25;++i) {
output_bit(EEPROM_DI, shift_left(cmd,4,0));
output_high(EEPROM_CLK);
output_low(EEPROM_CLK);
if(i > 9) // Clocks 10 through 25 bring in the data.
shift_left(&data,2,input(EEPROM_DO));
}
output_low(EEPROM_SELECT);
return(data);
} |
-------------
Edit: Put it in a code block and updated the hi(x) function.
___________________________
This message was ported from CCS's old forum
Original Post ID: 10340
Last edited by PCM programmer on Thu Mar 05, 2009 2:04 pm; edited 1 time in total |
|
|
balamagesh Guest
|
Re: Help me |
Posted: Tue Dec 31, 2002 11:45 pm |
|
|
Many thanks and i am using below main.c function to use 9346.c(16 bit version) which u gave.
main()
{
long add=0x0001;
long value=0x0011;
long data;
init_ext_eeprom();
write_ext_eeprom(add,value); // write_ext_eeprom
delay_ms(20);
data = read_ext_eeprom(add);
}
if there is any changes in the above code pls advice me to do the modifications
___________________________
This message was ported from CCS's old forum
Original Post ID: 10376 |
|
|
balamagesh Guest
|
Re: Help me |
Posted: Fri Jan 10, 2003 12:31 am |
|
|
Thanks a lot PCM Programmer , serial EEPROm is working fine thanks for the 16 bit codeby the by ur name pls
:=:=
:=:=i am using ex_extee.c with external EEPROM (93LC46B which is a 16 bit) how to convert 9346.c which is the 8 bit version of the above mentioned External EEPROM.
:=-------------------------------------------------------
:=
:=I modified the CCS example file, 9346.C, to make it work
:=with the 93LC46B.
:=
:=These routines expect a 16-bit data word. So you have to
:=send and receive a "long" variable to the read and write
:=routines.
:=
:=This file is not tested, since I don't have the EEPROM chip.
:=Hopefully it will work, but if it doesn't, you will have to
:=debug it. Also, make sure your hardware is wired correctly.
:=
:=
:=<PRE>
:=//////////////////////////////////////////////////////////////
:=// Library for a MicroChip 93LC46B (x16 org)
:=//
:=// init_ext_eeprom(); Call before the other functions are used.
:=//
:=// write_ext_eeprom(a, d); Write the byte d to the address a.
:=//
:=// d = read_ext_eeprom(a); Read the byte d from the address a.
:=//
:=// The main program may define eeprom_select, eeprom_di,
:=// eeprom_do and eeprom_clk to override the defaults below.
:=//
:=//////////////////////////////////////////////////////////////
:=
:=#ifndef EEPROM_SELECT
:=
:=#define EEPROM_SELECT PIN_B7
:=#define EEPROM_CLK PIN_B6
:=#define EEPROM_DI PIN_B5
:=#define EEPROM_DO PIN_B4
:=
:=#endif
:=
:=#define EEPROM_ADDRESS byte
:=#define EEPROM_SIZE 128
:=
:=#define hi(x) (*(&x+1))
:=#define lo(x) (char)(x)
:=
:=//-----------------------------------------------
:=void init_ext_eeprom()
:={
:= byte cmd[2];
:= byte i;
:=
:= output_low(EEPROM_DI);
:= output_low(EEPROM_CLK);
:= output_low(EEPROM_SELECT);
:=
:=// Load the opcode and address for an EWEN instruction
:=// into a 2-byte array.
:=
:= cmd[0]=0x80; // Address bit A4 = 1.
:=
:=// Start bit = 1, Opcode = 0,0, and Address bit A5 = 1.
:= cmd[1]=0x9;
:=
:=// Shift the 2 byte array left by 4 bits, so it becomes 0x9800.
:=// This is the bit pattern shown in Figure 3-5 (EWEN Timing)
:=// of the 93LC46A/B data sheet. It consists of the Start bit,
:=// EWEN opcode, and address. The address is only 6 bits.
:= for(i=1;i<=4;++i)
:= shift_left(cmd,2,0);
:=
:=// We use 9 clock pulses to send the Start bit, Opcode and Address.
:= output_high(EEPROM_SELECT);
:= for(i=1;i<=9;++i) {
:= output_bit(EEPROM_DI, shift_left(cmd,2,0));
:= output_high(EEPROM_CLK);
:= output_low(EEPROM_CLK);
:= }
:= output_low(EEPROM_DI);
:= output_low(EEPROM_SELECT);
:=}
:=
:=//----------------------------------------------------
:=void write_ext_eeprom(EEPROM_ADDRESS address, long data)
:={
:= byte cmd[4];
:= byte i;
:=
:= // Fill a 4-byte array with the Start bit, WRITE opcode,
:= // address, and 16 bits of data. This is 25 bits, total.
:= cmd[0] = lo(data) << 2;
:= cmd[1] = (hi(data) << 2) | (lo(data) >> 6);
:= cmd[2] = (address << 2) | (hi(data) >> 6);
:= cmd[3] = 0x5; // Start bit = 1, and Opcode = 0,1
:=
:=// Shift the 4-byte array left by 5 bits.
:=// This will left-justify the bits in the array.
:= for(i=1;i<=5;++i)
:= shift_left(cmd,4,0);
:=
:=// Send the 9 bits of header information, and the 16 bits of
:=// data to the EEPROM. This requires 25 clocks.
:= output_high(EEPROM_SELECT);
:= for(i=1;i<=25;++i) {
:= output_bit(EEPROM_DI, shift_left(cmd,4,0));
:= output_high(EEPROM_CLK);
:= output_low(EEPROM_CLK);
:= }
:= output_low(EEPROM_DI);
:= output_low(EEPROM_SELECT);
:= delay_ms(11);
:=}
:=
:=//----------------------------------------------------
:=
:=long read_ext_eeprom(EEPROM_ADDRESS address)
:={
:= byte cmd[4];
:= byte i;
:= long data;
:=
:=// Setup an array with the READ opcode and the address.
:=// Fill in the unused bits with zeros. These will be
:=// shifted out while we are shifting in the data from
:=// the EEPROM.
:= cmd[0]=0;
:= cmd[1]=0;
:= cmd[2]=address << 2;
:= cmd[3]=0x6; // Start bit and READ opcode
:=
:=// Shift the 4-byte array left by 5 bits,
:=// so that it's left justified in the array.
:= for(i=1;i<=5;++i)
:= shift_left(cmd,4,0);
:=
:=// Send out 25 clocks to the EEPROM. The first 9 clocks
:=// will be the Start bit, Opcode, and address.
:=// The next 16 clocks will bring in the data from the EEPROM.
:= output_high(EEPROM_SELECT);
:= for(i=1;i<=25;++i) {
:= output_bit(EEPROM_DI, shift_left(cmd,4,0));
:= output_high(EEPROM_CLK);
:= output_low(EEPROM_CLK);
:= if(i > 9) // Clocks 10 through 25 bring in the data.
:= shift_left(&data,2,input(EEPROM_DO));
:= }
:= output_low(EEPROM_SELECT);
:= return(data);
:=}
:=
:=</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10584 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|