Serially reading into PIC

Thread Starter

QMD

Joined Jun 30, 2010
30
One of the aspects of my project is to have an RFID reader which will ouput an RFID tag's ID. I want to serially read this into a PIC 16F877A. Any tips? The manual is rather confusing :(
 

be80be

Joined Jul 5, 2008
2,395
RFID reader are really not that hard to use there serial devices. Most use a baud rate of 2400 or 9600.

Set the pic to receive at the right baud and enable the card reader is about all there is to it.

You can test the card with a max232 to find the cards number.

Here a look at it working http://www.youtube.com/watch?v=I4-TXLs4lbk
 

Thread Starter

QMD

Joined Jun 30, 2010
30
Thanks. I was having difficulty understanding the PIC's manual. Do you know if there is any dummy code available for a simple serial read?

In the meanwhile, I'll check out this video. Thanks
 

Thread Starter

QMD

Joined Jun 30, 2010
30
Another issue we are currently facing is that our RFID reader is not reading as far as we desired...we are trying to read ~1m...but we are only getting 1 foot, at max. Any suggestions on places where one can get a UHF antenna? We need to change the antenna to one with a higher gain, in efforts to get more range
 

be80be

Joined Jul 5, 2008
2,395
This should give you a good idea of how to do this The code is by ericgibbs

You'll have to change to match your carder baud rate

Rich (BB code):
	list		p=16f877A	; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	errorlevel -302, -207

	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

bank0	macro
	bcf	03h,5
	bcf	03h,6		; ram page 0
	endm



bank1	macro
	bsf	03h,5
	bcf	03h,6		; ram page 1
	endm

;---------------------------------------------------------------------------  



;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set  


	bank0
	bsf	18h,7
	bsf	07h,6

	bank1


	movlw	00h
	movwf	88h
	bsf	87h,7




;---------------------------------------------------------------------------  

;1. initialize the spbrg register for the appropriate  
;baud rate. if a high speed baud rate is desired,  
;set bit brgh (section 10.1).  
	bank1
	bsf	98h,2

	movlw	0ch		;baud rate = 19,2 k
	movwf	99h		;4 mhz


;---------------------------------------------------------------------------  

;2. enable the asynchronous serial port by clearing  
;bit sync and setting bit spen.  

	bank1
	bcf	98h,4

	bank0
	bsf	18h,7

;---------------------------------------------------------------------------  

;3. If interrupts are desired, then set enable bit  
;RCIE..


	bank1
	bcf	8ch,5

;---------------------------------------------------------------------------  

;4. 4. If 9-bit reception is desired, then set bit RX9.  

	bank0
	bcf	18h,6


;---------------------------------------------------------------------------  


; 5. Enable the reception by setting bit CREN.  
	bank0
	bsf	18h,4



;---------------------------------------------------------------------------  

;6. Flag bit RCIF will be set when reception is complete  
;and an interrupt will be generated if enable  
;bit RCIE is set.  



;---------------------------------------------------------------------------  

;7. Read the RCSTA register to get the ninth bit (if  
;enabled) and determine if any error occurred  
;during reception.  



;---------------------------------------------------------------------------  


;8. Read the 8-bit received data by reading the  
;RCREG register.  

vrti
	bank0
	movf	RCREG,0		;byte that I send to LED on PORTD
	movwf	PORTD		;txreg



	;bank1

cekaj	btfss	PIR1,5
	goto	cekaj



	goto	vrti

;---------------------------------------------------------------------------  

;8. if using interrupts, ensure that gie and peie  
;(bits 7 and 6) of the intcon register are set.  



	end
 

Thread Starter

QMD

Joined Jun 30, 2010
30
This should give you a good idea of how to do this The code is by ericgibbs

You'll have to change to match your carder baud rate

Rich (BB code):
    list        p=16f877A    ; list directive to define processor
    #include    <p16f877A.inc>    ; processor specific variable definitions
    errorlevel -302, -207

    __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

bank0    macro
    bcf    03h,5
    bcf    03h,6        ; ram page 0
    endm



bank1    macro
    bsf    03h,5
    bcf    03h,6        ; ram page 1
    endm

;---------------------------------------------------------------------------  



;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set  


    bank0
    bsf    18h,7
    bsf    07h,6

    bank1


    movlw    00h
    movwf    88h
    bsf    87h,7




;---------------------------------------------------------------------------  

;1. initialize the spbrg register for the appropriate  
;baud rate. if a high speed baud rate is desired,  
;set bit brgh (section 10.1).  
    bank1
    bsf    98h,2

    movlw    0ch        ;baud rate = 19,2 k
    movwf    99h        ;4 mhz


;---------------------------------------------------------------------------  

;2. enable the asynchronous serial port by clearing  
;bit sync and setting bit spen.  

    bank1
    bcf    98h,4

    bank0
    bsf    18h,7

;---------------------------------------------------------------------------  

;3. If interrupts are desired, then set enable bit  
;RCIE..


    bank1
    bcf    8ch,5

;---------------------------------------------------------------------------  

;4. 4. If 9-bit reception is desired, then set bit RX9.  

    bank0
    bcf    18h,6


;---------------------------------------------------------------------------  


; 5. Enable the reception by setting bit CREN.  
    bank0
    bsf    18h,4



;---------------------------------------------------------------------------  

;6. Flag bit RCIF will be set when reception is complete  
;and an interrupt will be generated if enable  
;bit RCIE is set.  



;---------------------------------------------------------------------------  

;7. Read the RCSTA register to get the ninth bit (if  
;enabled) and determine if any error occurred  
;during reception.  



;---------------------------------------------------------------------------  


;8. Read the 8-bit received data by reading the  
;RCREG register.  

vrti
    bank0
    movf    RCREG,0        ;byte that I send to LED on PORTD
    movwf    PORTD        ;txreg



    ;bank1

cekaj    btfss    PIR1,5
    goto    cekaj



    goto    vrti

;---------------------------------------------------------------------------  

;8. if using interrupts, ensure that gie and peie  
;(bits 7 and 6) of the intcon register are set.  



    end

Thanks..is there a C version available somewhere?
 

Thread Starter

QMD

Joined Jun 30, 2010
30
My friend and I figured out that this will output values from pin C6

Rich (BB code):
#use rs232(baud=9600, xmit=PIN_C6, invert)
Does anyone know the similar function that is used to read via an RX pin?

Thanks
 

be80be

Joined Jul 5, 2008
2,395
If you would tell what C your using hi-tech, PWC, boostC,

That sets the pin to sends data

You want to receive data

This sets both

Rich (BB code):
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream=PC)
 
Last edited:

Thread Starter

QMD

Joined Jun 30, 2010
30
If you would tell what C your using hi-tech, PWC, boostC,
Oh my bad...I am using PIC-C

I am under the assumption that I will still need a max232 IC for proper rs232 communication

For output, I've been told that it is via a printf function, outputting via the specified pin
Would I use a scanf to read data?
 

be80be

Joined Jul 5, 2008
2,395
You need the max from the pic to the PC I only have the demo of PIC-C and it ran out years ago I would use MikroC it doesn't have a time limit and is way easier to use.
Rich (BB code):
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  UART1_Write_Text("Start");
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     //   read the received data,
      UART1_Write(uart_rd);       //   and send data via UART
    }
  }
}
 

Thread Starter

QMD

Joined Jun 30, 2010
30
Okay...so I have moved over to the dsPIC30f4011, as I need to do some serial I/O from two ports

Here is some sample code I found online....but I get many errors...

Rich (BB code):
/////////////////////////////////////////////////////////////////////////
////                             EX_SQW.C                            ////
////                                                                 ////
////  This program displays a message over the RS-232 and waits for  ////
////  any keypress to continue.  The program will then begin a 1khz  ////
////  square wave over I/O pin B4.                                   ////
////                                                                 ////
////  Comment out the printf's and getc to eliminate the RS232 and   ////
////  just output a square wave.                                     ////
////                                                                 ////
////  Change both delay_us to delay_ms to make the frequency 1 hz.   ////
////  This will be more visable on a LED.                            ////
////                                                                 ////
////                                                                 ////
////  Change the device, clock and RS232 pins for your hardware if   ////
////  needed.                                                        ////
/////////////////////////////////////////////////////////////////////////

#include <p30F4011.h>
#include <stdio.h>
//#device ICD=TRUE  // For using the debugger, un-comment

#use delay(crystal=20mhz)



// UART1A specifies the alternate UART pins Pin_C13, Pin_C14
// use UART1 to sprcify UART for pins Pin_F3, Pin_F2

#use rs232(baud=9600, UART1A)


int main() {

   printf("Press any key to beginnr");
   getc();
   printf("1 khz signal activatednr");

   while (1) {
     output_high(PIN_B4);
     delay_us(500);
     output_low(PIN_B4);
     delay_us(500);
   }
return 0;

}
I also tried my own test code, with no avail:

Rich (BB code):
#include "p30f4011.h" #include <stdio.h> #use rs232(baud=9600, xmit=U1TX, rcv=U1RZ) //serial port output pin & baud rate // Set up the serial connection to use 9600 buad rate void setupUSART1() { U1BRG = 11; //set baud rate to ~9600bps at 7.37MHz (FRC oscillator) U1MODEbits.STSEL = 0; //1 stop bit U1MODEbits.PDSEL = 0; //8-bit, no parity U1MODEbits.UARTEN = 1; //enable UART1 U1STAbits.UTXEN = 1; //enable UART1 transmitter U1STAbits.URXISEL = 3; //Enable 4 character } int main(void) { setupUSART1(); printf("Hello"); return 0; }​
Both gave me an error regarding "heap" and that "#use" was an invalid preprocessing directive
 

Thread Starter

QMD

Joined Jun 30, 2010
30
ok i figured out the issue regarding heap

this program compiles, but no output..

Rich (BB code):
#include "p30f4011.h"
#include <stdio.h>

void setupUSART1()
{
       U1BRG = 11;                                        //set baud rate to ~9600bps at 7.37MHz (FRC oscillator)        
       U1MODEbits.STSEL = 0;                //1 stop bit
       U1MODEbits.PDSEL = 0;                //8-bit, no parity
       U1MODEbits.UARTEN = 1;                //enable UART1
       U1STAbits.UTXEN = 1;                //enable UART1 transmitter
       U1STAbits.URXISEL = 3;                //Enable 4 character
}

int main()
{
       //Initialization
     //  setupADC();
   //    setupPWMs();
          setupUSART1();
       
       printf("Starting up...\n\r");
   //    delay();
       
       TRISBbits.TRISB2 = 0; //set Port B pin 2 to output
       

       

       //main loop        
       while (1)
   {        printf("Hello");
}
return 0;
}
 

Thread Starter

QMD

Joined Jun 30, 2010
30
Got it to work on the PIC16, will probably just stick to that for now.

Any advice on reading? (as of now, I am planning to use the getc() function)
 

Thread Starter

QMD

Joined Jun 30, 2010
30
Got it to work on the PIC16, will probably just stick to that for now.

Any advice on reading? (as of now, I am planning to use the getc() function)

The getc() function compiles, but it kills the program...others have had trouble with this as well it seems...any advice?
 
Top