Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help with USART code.

Status
Not open for further replies.

kristt2001

New Member
Hi, i am trying to communicate between a barcode scanner, Unitech MS180 and a PIC 18f452, using the USART. However, there is nothing received when a scan is done. The led at RD0 lights when information is received. also the contents of the RCSTA register was viewed, which shows no information is received. can anyone help please?


#include "p18f452.h"
#include <usart.h>
#include <stdio.h>

void read_scanner (void)
{
RCSTA=0b10010000;
/* Serial Port &
Continuous Receive enabled */

TRISCbits.TRISC6 = 0; // set TX (RC6) as output
TRISCbits.TRISC7 = 1; // and RX (RC7) as input

/* USART Tx off &
USART Rx on */

// configure USART
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,
25 );
/*
baud rate for asynchronous mode(BRGH=1)@9600bps,
yields lower %error=+0.16
SPBRG value = 25
*/


while(1)
{
while( ! PORTAbits.RA0 ); //wait for RA0 high
putrsUSART( PORTD ); //write value of PORTD
if(PORTD == 0x80) // check for termination
break; // value
}
PORTDbits.RD0 = 0;

// test light led
TRISDbits.TRISD0 = 0;
PORTDbits.RD0 = 1;


CloseUSART();
}

void main (void){
read_scanner();
}
 
while(1)
{
while( ! PORTAbits.RA0 ); //wait for RA0 high
putrsUSART( PORTD ); //write value of PORTD
if(PORTD == 0x80) // check for termination
break; // value
}

Not knowing anything about microcontrollers at this point, I can only guess, but should the while loop not look something like this:
(note the added brackets and removed ";")

while(1)
{
while( ! PORTAbits.RA0 ) //wait for RA0 high
{
putrsUSART( PORTD ); //write value of PORTD
if(PORTD == 0x80) // check for termination { break; }// value
}
}

Actually, I think my above example may be an endless loop and I would shorten it to this:

while( ! PORTAbits.RA0 ) //wait for RA0 high
{}
// if we get to here, it is high
while(1)
{
putrsUSART( PORTD ); //write value of PORTD
if(PORTD == 0x80) // check for termination
{ break; }// value
}

Please note that the second while loop will run forever if portD never equals hex 80 due to a transmission error. A for loop may be better.

(I wish the forum would show code with the indentations.)
 
Last edited:
I don't use the builtin routines and so not sure if it's relevant but you are turning on interrupts for receive and have not got an interrupt handler.

@jack 0987, to keep the formatting of code, type [code] before it and [/code] after it.

IE,
Code:
while(1){
    while( ! PORTAbits.RA0 );   //wait for RA0 high
    putrsUSART( PORTD );        //write value of PORTD
    if(PORTD == 0x80)           //check for termination
    break;                      //value
}

Mike.
 
I do not know enough about the microcontroller code to say. My guess is if you do a gets, then you need to a puts (save it some where). there may still be a problem with the flow of yours and my while loop as well and you need to do some error checking of your results.

Best of luck

as an afterthought, this must be a fairly common operation. Please search for a tutorial. Someone has must likely already written the code.
 
Last edited:
Try using this to get a character,
Code:
unsigned char GetRS232(void)
    while(!PIR1bits.RCIF);	//wait for char to be received
    return(RCREG);		//and return it
}

Mike.
 
Last edited:
I think you may have left out a left brace Mike.
Code:
unsigned char GetRS232(void)
{
    while(!PIR1bits.RCIF);	//wait for char to be received
    return(RCREG);		//and return it
}
I have come across this on this site:

PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
 
Last edited:
You are correct jack. I'm sure it had the opening brace before I posted it. Oh well.

Well spotted. Rep++.

Mike.
 
If the Barcode Scanner is a 232 interface, are you using a 232 - TTL (MAX232) converter chip?
Or is the Barcode Scanner a TTL interface?
 
Perhaps it's best to take a step back and test what's going on in the signal connecting to the uC. If you have a scope or logic, you should be able to see if the waveforms are as expected but if you don't, even a simple multimeter should show you if anything is actually going on in the received signal (perform a scan and see if the voltage of the rx signal fluctuates).
 
yes i am using the max 232 chip. also i used a multimeter, and there is a signal received at pin 25 and 26.the problem is probably in the receiving part of the code.
 
btw pommie, i tried
Code:
unsigned char GetRS232(void)
{
    while(!PIR1bits.RCIF);	//wait for char to be received
    return(RCREG);		//and return it
}
and it doesent work...
 
Hi again, this is another demo from the tutorial series that i am working on "Junebug with C18".ts absed on an 18F1320 but cna easily be changed.Use proteus to see how it works.Comments for improvement?
 

Attachments

  • 18F1320Basic.zip
    18 KB · Views: 122
  • serialtxrrx.c
    1.2 KB · Views: 162
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top