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

Problem about Software Serial receive error character???

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







Problem about Software Serial receive error character???
PostPosted: Thu Feb 03, 2005 5:23 am     Reply with quote

i use software rs232 communicate between pic16f876 and pic16f877. but when i send data from 16f876 to pic16f877 and data send to PC. pic16f877 receive data error.
What wrong in my source code?
my 16f876_PinRb6 connect to clock 3.579545 MHz for smartcard

i send 'A','B','C','D','E','F','G' but 16f877 receive "กจฅิ"<---error data



below is my sourcecode for pic16f876

#include <16F876.h>
#device *=16
#device adc=8
#use delay(clock=3579545,RESTART_WDT)
#fuses NOWDT,XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#define EEPROM_SDA PIN_B4
#define EEPROM_SCL PIN_B5
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B7,bits=8,stream=mcu_data)
#use i2c(Master,Fast,sda=PIN_B4,scl=PIN_B5)

#include <2465.C>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ZERO_RAM

#define RW PIN_A0
#define RS PIN_A1
#define EN PIN_A2

#define LED0 PIN_A3
#define LED1 PIN_A4

#int_RB
RB_isr()
{

}

void sendatr(void)
{
int8 atrdata[7] = {'A','B','C','D','E','F','G'};
int8 coatr;
int8 dataatr;

disable_interrupts(Global);

for(coatr=0;coatr<=6;coatr++)
{
fputc(atrdata[coatr],mcu_data);
}

enable_interrupts(Global);
}

void main()
{
char card1[10]="Card_OK";
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
init_ext_eeprom();

delay_ms(10);
init_lcd();
string_to_lcd(1,1,card1);

disable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

sendatr();

while(1)
{
delay_ms(1);
}

}


and


sourcecode for pic16f877______Receiver


#include <16F877.h>
#device *=16
#device adc=8
#use delay(clock=4000000,RESTART_WDT)
#fuses NOWDT,XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PC_DATA)
#use rs232(baud=9600,parity=N,xmit=PIN_B0,rcv=PIN_B0,bits=8,stream=MCU_DATA)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <C:\Micropro\SourceCode_Picc\CRC_CCITT_FFFF.C>
#ZERO_RAM

#define CARD_IN !input(PIN_B1)

int CIN=0;

#define BUFFER_SIZE 20
int ext_buffer_next_in;
int ext_buffer_next_out;
#define DATA_IN (ext_buffer_next_in != ext_buffer_next_out)
char Send_buffer[BUFFER_SIZE];
char Receive_buffer[BUFFER_SIZE];

int16 generate_16bit_crc(char* datach,int16 lendata);

#int_RB
RB_isr()
{

}

#int_EXT
EXT_isr()
{
char datamcu;

if(kbhit(MCU_DATA))
{
datamcu=fgetc(MCU_DATA);
fputc(datamcu,PC_DATA);
}
}

void Setup_MCU(void)
{
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

ext_int_edge(H_TO_L); // init interrupts
disable_interrupts(INT_RB);
disable_interrupts(INT_EXT);
disable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
}

void main()
{
Setup_MCU();
Set_tris_a(0b000000);
Set_tris_b(0b00000001);
Set_tris_d(0b00000000);

ext_buffer_next_in = 0;
ext_buffer_next_out = 0;

init_lcd();

fprintf(PC_DATA,"Smart Card V.1\r\n");

while(1)
{
if(CARD_IN)
{
IF(CIN==0)
{
out_led(0x01);
fprintf(PC_DATA,"CARD IN.\r\n");

OUTPUT_LOW(PIN_A5); //reset
DELAY_MS(10); //reset
OUTPUT_HIGH(PIN_A5); //reset

CIN=1;
enable_interrupts(global);
enable_interrupts(int_ext);
}
}
else
{
cardout:
disable_interrupts(global);
disable_interrupts(int_ext);
clear_lcd();
out_led(0x02);
CIN=0;
}
}
}
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Feb 03, 2005 7:09 am     Reply with quote

You are communicating asynchronously PIC to PIC.
This means your design on the receive end is dictated by the fact that you ALWAYS! have to be able to receive any in bound char.
Now you have a putc in the middle of the receive..that means when your
receive PIC is sending out the char it is definitedly! not available to receive a char...it takes as long to print are char as it does to receive a char.
There are ways to fix this
1) run the pics synchronously by making the sender wait for a ready to receive semaphore from the receiver.
2) the better way is to write a receive circular buffer and stuff it char by char with a rs232 receive isr. to replace the function of kbhit with a routine to look at the depth of received chars in the buff and dump them to your print display.

I notice you are already thinking about using a buffer so you are about to find your own solution.
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