a Guest
|
Problem about Software Serial receive error character??? |
Posted: Thu Feb 03, 2005 5:23 am |
|
|
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;
}
}
} |
|