|
|
View previous topic :: View next topic |
Author |
Message |
A Guest
|
Please Help me about Software Serial. |
Posted: Wed Mar 23, 2005 9:45 am |
|
|
Please Help me about Software Serial.
I receive error data from other microcontroller. i use pic18f458 and pic16f876
When i receive data from pic16f876 sent to board 18f458. data is ok but when i sent data from 18f458 to 16f876 data is error
because i use one wire communication and have some problem about #INT_RB in pic16f876
On circuit board 18f458. i use RB0 like External interrupt. it receive data ok. but on pic16f876 board i must use rb6 for
crystal 3.579545MHz or 4000000 MHz and i must use rb7 to receive data from pic18f458. Then i use #INT_RB but it have
some big problem about one wire serial. because #int_rb is run all the time and i can't do anything in loop main.
below is my sourcecode for pic18f458
this source is send data{ 'A','B',0x02,'C','D' }; from 16f876 to 18f458 receive data ok because i send data from pic18f458 to pc
and send data { 'E','F',0x02,'G','H' }; back to pic16f876 but data is error. i know because i use lcd check data from pic16f876
Please help me. how i should solve this problem in #INT_RB in pic16f876 to receive data correct
because it very important for me. because i must use this for my thesis. if i can't solve this problem. i must change thesis. :(
Please.... Thank you for all help. Hope someone can help me from this big problem
and i must communicate at baud 9600
//===============================================================
#include <18F458.h>
#device adc=8
#use delay(clock=4000000,RESTART_WDT)
#fuses NOWDT,WDT128,XT, NOPROTECT, NOOSCSEN, BROWNOUT, BORV45, PUT, NOCPD, NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOCPB, NOWRTC, NOEBTR, NOEBTRB
#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)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ZERO_RAM
#define BUFFER_SIZE 32
register byte buffin[BUFFER_SIZE];
register byte buffout[BUFFER_SIZE];
register BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int CIN=0;
#define RS PIN_C0
#define RW PIN_C1
#define EN PIN_C2
#define SE1 PIN_A1
#define SE2 PIN_A2
#define CARD_IN !input(PIN_B1)
.....
....
#int_EXT
EXT_isr()
{
int t;
if(kbhit(MCU_DATA))
{
buffer[next_in]=fgetc(MCU_DATA);
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
{
next_in=t; // Buffer full !!
}
}
}
#define bkbhit (next_in!=next_out)
BYTE bgetc()
{
BYTE c;
while(!bkbhit);
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void get_packet( byte * packet_ptr )
{
int clen=0;
packet_ptr[0]=bgetc(); //nad
packet_ptr[1]=bgetc(); //pcb
packet_ptr[2]=bgetc(); //len
for(clen=0;clen<packet_ptr[2];clen++)
{
packet_ptr[clen+3]=bgetc();
packet_ptr[clen+4]='\0';
}
}
void send_packet( byte * packet_out , int len_out_packet )
{
int copacket=0;
disable_interrupts(GLOBAL);
for(copacket=0;copacket<len_out_packet;copacket++)
{
fputc(packet_out[copacket],mcu_data);
delay_us(1000);
}
enable_interrupts(GLOBAL);
}
void main()
{
byte EFGH[5]={ 'E','F',0x02,'G','H' };
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_OFF);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
ext_int_edge( h_to_l );
disable_interrupts(INT_EXT);
disable_interrupts(GLOBAL);
CIN==0;
next_in=0;
next_out=0;
out_led(0x00);
while(true)
{
if(CARD_IN)
{
if(CIN==0)
{
delay_ms(500);
CIN=1;
fprintf(PC_DATA,"\nCard Inserted.\n");
enable_interrupts(global);
enable_interrupts(int_ext);
output_low(Pin_A5);
delay_ms(10);
output_high(Pin_A5);
}
out_led(0x02);
get_packet(buffin);
fprintf(PC_DATA,"%S",buffin);
delay_ms(500);
send_packet(EFGH,5); // send data to pic16f876 but error ???????????????????
}
else
{
out_led(0x03);
CIN=0; Next_in=0; Next_out=0;
}
}
}
and below is sourcecode for pic16f876
//==================================================================
#include <16F876.h>
#device *=16
#device adc=8
#use delay(clock=4000000,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=4800,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 BUFFER_SIZE 32
#priority rb,rtcc
register byte buffin[BUFFER_SIZE];
register byte buffout[BUFFER_SIZE];
register BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int codata1=0;
int cout=0;
char odata[5]={ 'A','B',0x02,'C','D' };
#define RW PIN_A0
#define RS PIN_A1
#define EN PIN_A2
#define LED0 PIN_A3
#define LED1 PIN_A4
void send_packet( byte * packet_out , int len_out_packet )
{
int copacket=0;
disable_interrupts(INT_RB);
disable_interrupts(GLOBAL);
for(copacket=0;copacket<len_out_packet;copacket++)
{
fputc(packet_out[copacket],mcu_data);
delay_us(100);
}
enable_interrupts(GLOBAL);
}
#int_RTCC
RTCC_isr()
{
char Hello[]="Hello";
int checkc;
disable_interrupts(INT_RTCC);
for(checkc=0;checkc<5;checkc++)
{
Wcharxy(1,(checkc+1),buffin[checkc]); // write to lcd
}
}
#int_RB
RB_isr()
{
if(kbhit(MCU_DATA))
{
disable_interrupts(INT_RB);
buffin[next_in]=fgetc(MCU_DATA);
buffin[next_in+1]='\0';
next_in++;
enable_interrupts(INT_RB);
}
if(next_in==4)
{
buffin[next_in+1]='\0';
disable_interrupts(INT_RB);
enable_interrupts(INT_RTCC);
next_in=0;
}
}
void main()
{
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();
init_lcd();
disable_interrupts(INT_RTCC);
disable_interrupts(INT_RB);
disable_interrupts(GLOBAL);
next_in=0; Next_out=0;
while(1)
{
send_packet(odata,5);
enable_interrupts(INT_RB);
}
} |
|
|
a Guest
|
|
Posted: Wed Mar 23, 2005 11:07 am |
|
|
sorry for my false in my code. but true sourcecode is
//==================================================================
#include <16F876.h>
....
....
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B7,bits=8,stream=MCU_DATA)
baud 9600
but this code is not work data is not correct.
Please Help me???? |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Mar 23, 2005 12:06 pm |
|
|
1) Be aware that RB7 share the interrupt trigger with RB6, RB5 and RB4.
2) Donīt need to manipulate interrupts enables/disables inside the isr.
Let the compiler to do so.
3) Receiving the incomming data in RB7, every time it change (H-L / L-H)
will trigger an RB_INT.
Work around is doing a dirty work of wiring RB0 as RCV pin instead of
RB7 and using EXT_INT to trigger the software UART handler.
Best wishes,
Humberto |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Mar 23, 2005 12:32 pm |
|
|
Humberto wrote: | 1) Be aware that RB7 share the interrupt trigger with RB6, RB5 and RB4.
2) Donīt need to manipulate interrupts enables/disables inside the isr.
Let the compiler to do so.
3) Receiving the incomming data in RB7, every time it change (H-L / L-H)
will trigger an RB_INT.
Work around is doing a dirty work of wiring RB0 as RCV pin instead of
RB7 and using EXT_INT to trigger the software UART handler.
Best wishes,
Humberto |
You can also keep track of the PortB values and check them to make sure which pin is changing and whether it is going high or low. This will give you a software version of the external interrupt on RB7 |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Mar 23, 2005 1:40 pm |
|
|
Mark wrote: Quote: |
You can also keep track of the PortB values and check them to make sure which pin is changing and whether it is going high or low. This will give you a software version of the external interrupt on RB7
|
Of course Mark, that is true whenever the code is doing what you say,
but the poster code is doing this:
Code: |
disable_interrupts(INT_RTCC);
disable_interrupts(INT_RB);
disable_interrupts(GLOBAL);
next_in=0; Next_out=0;
while(1)
{
send_packet(odata,5);
enable_interrupts(INT_RB);
}
|
Humberto |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Mar 23, 2005 1:55 pm |
|
|
Humberto wrote: | Mark wrote: Quote: |
You can also keep track of the PortB values and check them to make sure which pin is changing and whether it is going high or low. This will give you a software version of the external interrupt on RB7
|
Of course Mark, that is true whenever the code is doing what you say,
but the poster code is doing this:
Code: |
disable_interrupts(INT_RTCC);
disable_interrupts(INT_RB);
disable_interrupts(GLOBAL);
next_in=0; Next_out=0;
while(1)
{
send_packet(odata,5);
enable_interrupts(INT_RB);
}
|
Humberto |
He could update the PortB values before he reenables the ints
Code: |
while(1)
{
send_packet(odata,5);
// Update the PortB values here
enable_interrupts(INT_RB);
}
|
|
|
|
A Guest
|
|
Posted: Thu Mar 24, 2005 3:00 am |
|
|
To humberot
i must use RB7 is receiver and transmitter because i must use it as smartcard
pic16f876+24lc64 and pin rb6 is clock and rb7 is data. i must use rb7 for receive and transmit.
when i use rb7 as receive data in software serial. i will disable_rtcc and when receive data
complete i will disable_rb and enable_rtcc. because i think i will use rtcc for print data to lcd
and check data on lcd. And i disable_rtcc in #INT_RTCC. because i thing when i print data to lcd
it should use a long time and rtcc may be overflow before i show data on lcd complete. |
|
|
A Guest
|
|
Posted: Thu Mar 24, 2005 3:15 am |
|
|
To Mark
i don't know. what is the Port B values. please example this value. because i am a new
programmer for this compiler and i must use rb7 for receive and transmit data between
pic16f876 and pic18f458 and this is very simple code in #int_rb.
but it receive data error
and when i send data from pic16f876 to pic18f458. Pic18f458 is receive data OK. but when
i send data from pic18f458 to 16f876. Pic16f876 receive data ERROR. i dont' know what happen
in my code. because it is a one part of my code.
below is your code
Please explain the PortB values i should update. i'm very confuse about this. because this is a test sourcecode for my work.
Thank you very much.
//====================================================
He could update the PortB values before he reenables the ints
Code:
while(1)
{
send_packet(odata,5);
// Update the PortB values here
enable_interrupts(INT_RB);
}
//==================================================== |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Mar 24, 2005 8:41 am |
|
|
Quote: |
To humberto
i must use RB7 is receiver and transmitter because i must use it as smartcard
pic16f876+24lc64 and pin rb6 is clock and rb7 is data. i must use rb7 for receive and transmit.
|
I guess you are a little confused.
RB6: Interrupt-on-change-pin or In-Circuit-Debugger pin. Serial Programming Clock.
RB7: Interrupt-on-change-pin or In-Circuit-Debugger pin. Serial Programming Data.
Serial Programming Clock and Data refers to the In-
Circuit Programming use only, not to any other peripheral.
In PORT_C there are dedicated pins to interface I2C, SPI
devices. Also there are the pins of the USART for
synchronous/asynchronous comunications.
Quote: |
#use i2c(Master,Fast,sda=PIN_B4,scl=PIN_B5)
|
You can use any pin to interface I2C devices, but using RC3 (SCL)
and RC4 (SDA) you have all the advantages of a dedicated
hardware plus the compiler built-in functions functionalities.
Hope this help you,
Humberto |
|
|
A Guest
|
|
Posted: Thu Mar 24, 2005 9:42 am |
|
|
i'm not confuse about rb6 and rb7. i know rb6 is a clock for programming
and rb7 is data for programmin but i must use rb7 like data pin and rb6 like clock for my MCU. because i use pic16f876+24lc64 like smartcard(it'is called Silvercard) and circuit in Smartcard like this picture on below link
i make my board like smartcard and i connect it to lcd and 2 led for debug data when i programm and receive data from terminal (i make terminal and smart card by my self). i'm not sure it is bug in my program or my hardware but when i transfer between pic16f877(Terminal) and pic16f876(smartcard) data transfer is ok but at baud rate 4800 (data is ok)
but i must use pic18f458 instead of pic16f877. because memory in pic16f877 is not enough for my code and when i transfer data to pic18f458. Data transfer from pic18f458 is not receive by pic16f876(Smartcard) and i must connect with pic16f876 at baud rate 9600. because i must follow by ISO7816-3 thank you for your answer. and i will try to solve my problem and please help me or tell me about sourcecode. if you see error in my sourcecode. thank you
http://ucables.com/ref/5SILVCARD/v180,software,download
You will see circuit of smartcard(Silvercard--> PIC16f876+24LC64) at the end of page
Sorry for my english. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Mar 24, 2005 12:42 pm |
|
|
I had been looking the Silvercard schematic.
Why use INT_RB to receive data ? According to your code you have
enough time for polling RB7 using a software uart handler, without
using interrupts.
Just a guide:
Code: |
void main()
{
while(1)
{
do
{
.........
your stuf
}while(!kbhit(MCU_DATA));
if(kbhit(MCU_DATA))
{
buffin[next_in]=fgetc(MCU_DATA);
buffin[next_in+1]='\0';
next_in++;
if(next_in==4)
{
buffin[next_in+1]='\0';
enable_interrupts(INT_RTCC);
next_in=0;
}
}
} // while
} // main
|
hope this help,
Humberto |
|
|
A Guest
|
|
Posted: Fri Mar 25, 2005 9:34 am |
|
|
Thank you Humberto. Now my Test program is OK. but now i will try this code for my real program. thank you very much. and if u don't mind. May i contact U by e-mail. My e-mail is a_assump@hotmail.com |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Mar 25, 2005 9:54 am |
|
|
Quote: |
and if u don't mind. May i contact U by e-mail
|
y're wellcome
Humberto |
|
|
|
|
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
|