CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

(FIXED) need help on SPI interface 16F877A with 25LC640

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
avitron



Joined: 26 Mar 2013
Posts: 4

View user's profile Send private message

(FIXED) need help on SPI interface 16F877A with 25LC640
PostPosted: Tue Apr 16, 2013 5:49 pm     Reply with quote

Could any one help me to find why my Proteus simulation circuit not working ? The data can not write in the EEPROM, no data can be retrieved from EEPROM.

My CCS program compiled without any error.

I'm using PCW ver. 4.140.

the 25LC640 driver is copy from:
http://www.ccsinfo.com/forum/viewtopic.php?t=42712&highlight=25lc640

Here is the EEPROM driver
Code:

//Hardware SPI driver for 25LC640:

#ifndef EEPROM_SELECT
#define EEPROM_SELECT PIN_C2
#define EEPROM_CLK    PIN_C3
#define EEPROM_DI     PIN_C5
#define EEPROM_DO     PIN_C4
#endif

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE    8192

void init_ext_eeprom()
{
output_high(EEPROM_SELECT);   
setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_16);
}

//--------------------------------
int1 ext_eeprom_ready(void)
{
int8 data;

output_low(EEPROM_SELECT);
spi_write(0x05);
data = spi_read(0);
output_high(EEPROM_SELECT);
return(!bit_test(data, 0));
}

//--------------------------------
void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data)
{
while(!ext_eeprom_ready());

output_low(EEPROM_SELECT);
spi_write(0x06);
output_high(EEPROM_SELECT);

output_low(EEPROM_SELECT);
spi_write(0x02);
spi_write(address >> 8);
spi_write(address);
spi_write(data);
output_high(EEPROM_SELECT);
}
//--------------------------------

BYTE read_ext_eeprom(EEPROM_ADDRESS address)
{
int8 data;

while(!ext_eeprom_ready());

output_low(EEPROM_SELECT);
spi_write(0x03);
spi_write(address >> 8);
spi_write(address);

data = spi_read(0);
output_high(EEPROM_SELECT);

return(data);
}



My main program here:
Code:

#include <16F877A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH)
#FUSES PUT                      //Power Up Timer
#FUSES NOLVP                    //No low voltage prgming,

#use delay(clock=20000000)

#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1  (SPI_L_TO_H)
#define SPI_MODE_1_0  (SPI_H_TO_L)
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define EEPROM_SELECT PIN_C2
#define EEPROM_CLK    PIN_C3
#define EEPROM_DI     PIN_C5
#define EEPROM_DO     PIN_C4
#include <SPI_25LC640.c>
// define LCD pins before #include <LCD.c>
#define LCD_ENABLE_PIN  PIN_E0
#define LCD_RS_PIN      PIN_E1
#define LCD_RW_PIN      PIN_E2
#define LCD_DATA4       PIN_D4
#define LCD_DATA5       PIN_D5
#define LCD_DATA6       PIN_D6
#define LCD_DATA7       PIN_D7
#include <LCD.c>

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_comparator(NC_NC_NC_NC);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   
   init_ext_eeprom();
   lcd_init();
   int8 data, addr, read_data;
   output_high(PIN_C0);  // LED  indicate in writing mode
   
   for(data=0xff,addr=0x00; addr<=0x0f; --data, ++addr)   {
      ext_eeprom_ready();
      write_ext_eeprom(addr,data);
      lcd_putc("\fWriting Data...\n");
      printf(lcd_putc,"%x",data);
      delay_ms(5);
   }
   output_low(PIN_C0);

   delay_ms(1500);

   output_high(PIN_C1); // LED indicate in reading mode
   for(addr=0x00; addr<=0x0f; ++addr) {
      read_data= read_ext_eeprom(addr);
      lcd_putc("\fReading Data...\n");
      printf(lcd_putc,"%x",read_data);
      delay_ms(150);
   }
   output_low(PIN_C1);
 
   while(TRUE)
   {
      //TODO: User Code
   }

}



here proteus sim:
[img]
http://imageshack.us/photo/my-images/571/spi25lc640.jpg/
[/img]


Last edited by avitron on Mon Apr 22, 2013 6:02 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 16, 2013 6:05 pm     Reply with quote

simple answer...

Proteus is busted.Contact them to fix their product.

CCS C works fine with real hardware.

hth
jay
avitron



Joined: 26 Mar 2013
Posts: 4

View user's profile Send private message

SOLUTION FOUND
PostPosted: Wed Apr 17, 2013 4:31 pm     Reply with quote

Only for simulation purpose, do it in real HW.

According to Ettore Arena from Labcenter Electronic:

Quote:

... that's a problem with the CCS compiler optimizer not Proteus! Add the directive #OPT 0 just before void main() and it will work fine.
I have analysed the generated assembly code and made hardware tests as well and finally posted the results over there:


see the post under the name "ettore"
http://www.ccsinfo.com/forum/viewtopic.php?t=42712&highlight=25lc640


Last edited by avitron on Wed Apr 17, 2013 5:55 pm; edited 2 times in total
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 5:09 pm     Reply with quote

You missed the point. According to the post at the top of the forum, we
don't discuss Proteus related issues here.

Folks on this forum know those devices have worked with no problems in real
HW many times before (and with default optimization) so arguing for Proteus
here doesn't help your case.
_________________
Google and Forum Search are some of your best tools!!!!
avitron



Joined: 26 Mar 2013
Posts: 4

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 5:41 pm     Reply with quote

Sorry for everyone reading my post.

Here, I'm not intend to rise any argument between Proteus vs CCS.

I understand CCS codes will work on the real HW. And, I appreciated any one who contributed the codes.

I just want to know the solution of my problem to make it works, and contributed what I 'd found.
avitron



Joined: 26 Mar 2013
Posts: 4

View user's profile Send private message

from CCS support (Apr 22)
PostPosted: Mon Apr 22, 2013 6:04 pm     Reply with quote

This message is a reply to CCS e-mail id: 3DM558

The problem you reported has been fixed and will be in the
next compiler release.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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