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 CCS Technical Support

Receive string of characters from rs232 then save to ext.

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



Joined: 03 Apr 2005
Posts: 22
Location: Laguna Philippines

View user's profile Send private message Yahoo Messenger

Receive string of characters from rs232 then save to ext.
PostPosted: Sun Aug 17, 2008 6:34 am     Reply with quote

Hello to all,

Pls give me an example program about receiving string of characters from pc to PIC via rs232 then save to ext. eeprom (24LC256) and then retrieve and display it on LCD. I'll be using PIC16F877 MCU. Thank you:)
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Aug 17, 2008 6:57 am     Reply with quote

Receive data from the PC and show on the LCD: http://www.ccsinfo.com/forum/viewtopic.php?p=42588
Store a string in EEPROM, read it and display on the LCD: http://www.ccsinfo.com/forum/viewtopic.php?p=102468

Oohhh, surprise!!! Both threads have been started by you. Confused

Instead of asking for spoon fed example code try to understand what is happening in both programs. All the info you need is there. If there is something you don't understand than ask here in the forum.
jelodavid



Joined: 03 Apr 2005
Posts: 22
Location: Laguna Philippines

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Aug 17, 2008 7:08 am     Reply with quote

Thank you:)
jelodavid



Joined: 03 Apr 2005
Posts: 22
Location: Laguna Philippines

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Aug 17, 2008 9:10 pm     Reply with quote

Here's my simple code. But i can't see any saved result on LCD. I think i'm stuck somewere. Pls help...

Code:
#include <16F877.H>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <LCD_Driver.h>

#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#include <24256.c>

//define button=================================
#define OK         PIN_B0
#define VIEW      PIN_A4

//define LED output indicators==================
#define MCU_STAT   PIN_B3
#define CHAR_RCV   PIN_B2

int8 i=0; INDEX=0;
int8 CHAR_BUFFER[];
int1 DATA_SAVED,DATA_READY = false;

//Switch debounce==============================
switch_debounce(){
   delay_ms(20);
}

#int_TIMER0
TIMER0_isr() {
   int a;
   if (++a>=20){
      output_toggle(MCU_STAT);
      a=0;
   }
}

//Receive Data Available interrupt=============
#int_RDA
void RDA_isr(void){
   CHAR_BUFFER[INDEX]=getc();
   ++INDEX;
   output_high(CHAR_RCV);
   DATA_READY=true;      
}

//Transmitt Buffer Empty interrupt=============
#int_TBE
void TBE_isr(void){
   
}

#zero_ram
void main(){ 
int8 buffer[17];

lcd_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

 while(1){
   
    //Save to eeprom once the data is available
    if ((DATA_READY)&&(!input(OK))){
       switch_debounce();
       lcd_putc("\fSaving to EEPROM\n");
       for(i=0; i<=INDEX; i++)
          write_ext_eeprom(i, CHAR_BUFFER);
      
       INDEX=0;
       DATA_SAVED=true;
       DATA_READY=false;   
    }
   
      
   //Read eeprom=========================
   if ((DATA_SAVED)&&(!input(VIEW))){
      switch_debounce();
      for (i=0; i<10; i++)
         buffer[i]=read_ext_eeprom(i);
      
      //Display it on LCD==============
      lcd_putc("\fMemory Content\n= ");   
      printf(lcd_putc, buffer);
      DATA_SAVED=false;
      output_low(CHAR_RCV);
      
   }      
   
   }
}
Ttelmah
Guest







PostPosted: Mon Aug 18, 2008 9:24 am     Reply with quote

Unfortunately, a lot of problems. Start with just a few:
You declare a zero sized array (no size), then start writing to it. This will corrupt the RAM memory. You need either to allocate a size to the array, or use the malloc function, to generate a suitable memory block, and point the array at this, _before_ you put data into the buffer.
You use a counter in the timer interrupt. You have not declared this as 'static', so it can be destroyed whenever the code leaves the interrupt. For a running counter, the variable _must_ be static.
You write each byte to EEPROM. EEPROM writes take typically up to 5mSec to complete. At 9600bps, a character can arrive every 1.04mSec. What is going to happen if more data arrives while you are looping and writing?.
There are probably more, but these are the first few 'glaring' ones...

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 18, 2008 2:03 pm     Reply with quote

I was writing down a list of other errors than the ones already mentioned by Ttelmah but with at least six more errors I realised the problem is not in the program but in your knowledge of C.

You have copied parts of other programs but you don't understand the code. Sorry I don't know how to help you best. Maybe you should start all over again by doing a course in C, many can be found on the internet. Start with the simple programs, make sure you understand them and then slowly move on to the more complicated programs. You are now making too big steps.
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