|
|
View previous topic :: View next topic |
Author |
Message |
neverlog
Joined: 02 May 2010 Posts: 36
|
1 software uart and 1 hardware uart |
Posted: Tue Jul 06, 2010 5:43 am |
|
|
Dear All,
I am new to PIC and CCS.
I have one project as below:
RFID Reader <===>PIC<====>PC
My RFID reader needs to get command from PIC then it will return data to PIC, at the same time I need to display it on the PC, and PIC will be getting command from PC for other purpose.
I don't need full-duplex, half duplex will be just fine.
I have problem using 1 software uart and 1 hardware UART. I am using 16F877A.
I am trying to use #int_rda and #int_tbe for hardware uart,
I am using #int_ext (PIN_B0) for my receive (Software UART), does there have any solution for transmit buffer(software uart)?
my communication for reader to PIC and PIC to PC need to be 2 way, most of the thread I read is one way.
Can anybody please help? Thanks in advance.
My next phase is, I will need to link all the PIC through RS485 because there will be total of 16 (reader+PIC) need to be controlled by a PC. |
|
|
neverlog
Joined: 02 May 2010 Posts: 36
|
Re: 1 software uart and 1 hardware uart |
Posted: Tue Jul 06, 2010 6:04 am |
|
|
This is my code, I just can't make it running. At the PC side, it won't display the return data from reader.
Code: |
//Auto Mode Software and Hardware UART for EMR/DUR300 by Lam
#include <16F877a.h>
#fuses hs,noprotect,nowdt,nolvp
//#device adc=8
#use delay(clock=20000000) //20mhz
#use rs232(baud=19200, xmit=PIN_B1, rcv=PIN_B0, stream=reader)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, errors,stream=pc)
//#include <stdlib.h>
//#include <stdio.h>
//#include <string.h>
#pragma zero_ram //interesting command
//Software UART================================================
#define SU_BUFFER_SIZE 32
BYTE SU_buffer[SU_BUFFER_SIZE];
BYTE SU_next_in = 0;
BYTE SU_next_out = 0;
#int_ext
void SU_serial_isr() {
int SU_t;
SU_buffer[SU_next_in]=fgetc(reader);
SU_t=SU_next_in;
SU_next_in=(SU_next_in+1) % SU_BUFFER_SIZE;
if(SU_next_in==SU_next_out)
SU_next_in=SU_t; // Buffer full !!
}
#define SU_bkbhit (SU_next_in!=SU_next_out)
BYTE SU_bgetc() {
BYTE SU_c;
while(!SU_bkbhit) ;
SU_c=SU_buffer[SU_next_out];
SU_next_out=(SU_next_out+1) % SU_BUFFER_SIZE;
return(SU_c);
}
//HArdware UART================================================
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void HU_serial_isr() {
int t;
buffer[next_in]=getc();
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);
}
//main program================================================
void main() {
int i;
char c;
enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(int_ext);
ext_int_edge(H_to_L);
printf("\r\n\Running...\r\n");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
//RF ON Command
do {
delay_ms(2000);
fputc(0x40,reader);
delay_ms(1);
fputc(0x02,reader);
delay_ms(1);
fputc(0x00,reader);
delay_ms(1);
fputc(0x41,reader);
delay_ms(1);
fputc(0x01,reader);
delay_ms(1);
fputc(0x42,reader);
delay_ms(1);
delay_ms(50);
for (i=0;i<5;i++) { //Reader will return 5 bytes
printf("%02x", SU_bgetc());
}
} while (TRUE);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 06, 2010 1:57 pm |
|
|
Quote: | #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, errors,stream=pc)
#int_rda
void HU_serial_isr()
{
buffer[next_in]=getc();
.
.
.
}
printf("%02x", SU_bgetc());
|
In the code above, you have a specified a stream for the PC side,
but you are using non-stream functions. You need to use fgetc()
and fprintf().
Quote: | enable_interrupts(global);
enable_interrupts(int_rda);
enable_interrupts(int_ext);
ext_int_edge(H_to_L); |
Here you are setting the interrupt edge after you have enabled INT_EXT
interrupts. Ideally, the edge should be set up before you enable
interrupts. |
|
|
neverlog
Joined: 02 May 2010 Posts: 36
|
|
Posted: Tue Jul 06, 2010 7:35 pm |
|
|
Thank you for your help, PCM Programmer.
I have changed it, but it still doesn't work.
At least I can see it print out "Running" on the screen,
Are we allow to use more than 1 interrupts in the PIC?
I am using compiler version 3.181 |
|
|
neverlog
Joined: 02 May 2010 Posts: 36
|
|
Posted: Tue Jul 06, 2010 7:51 pm |
|
|
I need to put the delay_ms(1) after each command, I wonder why I must?
Do software uart have something like #int_TBE in hardware uart?
Please help me. Thank you. |
|
|
neverlog
Joined: 02 May 2010 Posts: 36
|
|
Posted: Wed Jul 07, 2010 10:53 pm |
|
|
My program is running, but now I have new problem.
When I turn on my PIC, the PIC will transmit FF00. It happens everytime I start the PIC. It is ok when I reset the PIC.
Does anyone know how to clear the transmit buffer for the first time the PIC is on?
Please help. Thanks in advance. |
|
|
|
|
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
|