software usart PIC18f452 help.

Status
Not open for further replies.

kristt2001

New Member
Hi all, i am trying to write a software usart for the PIC18f452. the usart on the pic is already being used. however i am having trouble understanding the difference between the original usart code and the software one to be implemented.
this is to be connected to a receipt printer, so only transmitting alone would be used. no receiving.
Can you please help?
 
Code:
 #include "p18f452.h"
#include <usart.h>
#pragma config OSC = XT     //Sets the oscillator mode to XT for cristal up to 4MHz or HS=>4MHz */
#pragma config WDT = OFF    //Turns the watchdog timer off */
#pragma config LVP = OFF    //Turns low voltage programming off */
#pragma config DEBUG = OFF  // Compiles without extra debug code */


void read_scanner (void)
{
	unsigned char id[10];
	
	
    RCSTAbits.SPEN=1; 
	
/*	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             
*/  
  // test light led
	TRISDbits.TRISD0 = 1;
       
		while(BusyUSART()){  //if the usart is transmitting 
			getsUSART(id,10);
			PORTDbits.RD0 = 0;  // take off led 
			
			}
			             
			 CloseUSART();
			 }
			 
	unsigned char GetRS232(void){
    while(!PIR1bits.RCIF);	//wait for char to be received
    return(RCREG);		//and return it
}		 
			 
			 
void main (void){
read_scanner();
}
 
basically what that does is, it configures the usart, and puts an led on. when data is received, it is turned off. this is the usart code enabled on the PIC's usart itself. the input to this is a barcode scanner, so that no transmission would be necessary. how do i need to change this to make it a software usart for sending to a receipt printer?
 
Hi,

Cannot help you with the C code as I only do assembler - but sure others will come in and help you.

Basically the Pics Hardware Usart does all the work for you on dedicated output ports - you just give it the parameters and the data stream and it takes over, doing all the timing etc for you.

A 'software usart' can be run on almost any pins, but all the timing and control is done by all your own software, which means it must do all the timings for every bit sent and received at whatever buad rate you set it to.

Perfectly possible, but its a long winded way, hence the creation of the inbuilt Usart module.
 
Hi this i a software UART demonstration that i have made using C18 libraries part of my tutorials on "Junebug with C18".Its simple and will work with 18f452.Just recalculate the the delays..Use Proteus to use demo simulation.Any Comments(for improvement)
 

Attachments

  • software serial.zip
    3.2 KB · Views: 441
  • 18F1320Basic.zip
    18 KB · Views: 306
Last edited:
hi wond3rboy, for some reason my compiler doesnt allow _asm and _endasm functions. im using the HItechC compiler. do i need to include something else for this?
 
Hi Kristt2001, i have used C18 as my compiler and hence its libraries. Dont think it will run on Hi TECHC you will need to add the asm files in your project.Do you want me to upload a hex file? Download the student version of C18 compiler.Its free.
 
And its still causing problems? Put this hex file in to the simulation.You might not be adding all the files in your project.I am also uploading a picture showing the files that are to be added.You need to recalculate the delays as specified in the C18 libraries document.This simulation is for an 18F1320

PS: One thing thing that i always take care about is to install MPLAB and C18 in their default paths i-e in C...
 

Attachments

  • Projectfiles..JPG
    18.5 KB · Views: 292
  • serialsoftware..zip
    902 bytes · Views: 224
Last edited:
i installed the MCC in the Third Party folder and Microchip in program files. could that be the error? yes i have all the necessary files.
 
The only reason i said that install in C drive is that i always tend to get the error "Could not find C18io or something...". If that is the error you are getting then you need to set the language locations for "include files", "library files" for MPLAB it self and the current project(the first one that you are building).
 
Last edited:
yes.

Project>Set Language Tool Locations>MPLAB C18 Toolsuite>Default Search Paths and Directories>

For Include Search Path C:\MCC18\h

For Libraries Search Path C:\MCC18\lib



This is if you have installed C18 in C:\MCC18(it is the default folder)

Do this for the first project(the current one in Project>Build Options...)
 
okay. i got the USART functioning. thanks for the help.
however, i am now stuck with interfacing the pic with a SD card. all i want to do is read and write to a text file.
i am using SPI mode, with
DI -> RC5
DO -> RC4
SCK -> RC3


code so far:

void MMC (void){
unsigned char SPI_data;
unsigned char SPI_Read[10];
unsigned char SPI_Write[10] = "MICROCHIP";



TRISCbits.TRISC3 = 1;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 1;

PORTCbits.RC3 = 1;
PORTCbits.RC4 = 1;
PORTCbits.RC5 = 1;

Nop();

OpenSPI(SPI_FOSC_4,MODE_01,SMPMID);
SSPCON1 |= SPI_FOSC_16;
SSPBUF = 0x00;
while(1)
{// SPI_data = ReadSPI();
putsSPI(SPI_Write);
if(DataRdySPI())
{
getsSPI(SPI_Read,10);
// CloseSPI();
}

}
CloseSPI();
}


however, it isnt writing to the SD.
can anyone help.
 
You are welcome. For the SPI thing, i havent interfaced an SD card with a PIC micro. Search through the Forum a member here "Atomsoft" has done that, look at his post.Also if you start a new thread for the SD card thing, you will get more replies.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…