View previous topic :: View next topic |
Author |
Message |
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
Receive string of characters from rs232 then save to ext. |
Posted: Sun Aug 17, 2008 6:34 am |
|
|
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
|
|
|
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
|
Posted: Sun Aug 17, 2008 7:08 am |
|
|
Thank you:) |
|
|
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
|
Posted: Sun Aug 17, 2008 9:10 pm |
|
|
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
|
|
Posted: Mon Aug 18, 2008 9:24 am |
|
|
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
|
|
Posted: Mon Aug 18, 2008 2:03 pm |
|
|
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. |
|
|
|