Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART source code in Hi-tech C!

Status
Not open for further replies.

bing2005

Junior Member level 2
Joined
Feb 21, 2005
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,486
hi-tech usart

Code:
#include <pic.h>
#include <stdio.h>

void config(void);
void srl(void);
void start(void);

void main(void)
{
	config();
	srl();
	while(1)
	{	
		start();
	}
}

void config(void)
{
	STATUS |= 0x20;
	TRISC7	= 1;
	TRISC6	= 0;
	STATUS &= 0xdf;
}

void srl(void)
{
	BRGH = 1;		/* high baud rate */
	SPBRG = 25;		/* set the baud rate */
	SYNC = 0;		/* asynchronous */
	SPEN = 1;		/* enable serial port pins */
	CREN = 1;		/* enable reception */
	SREN = 0;		/* no effect */
	TXIE = 0;		/* disable tx interrupts */
	RCIE = 0;		/* disable rx interrupts */
	TX9  = 0;		/* 8- or 9-bit transmission */
	RX9  = 0;		/* 8- or 9-bit reception */
	TXEN = 1;		/* enable the transmitter */
}

void start(void)
{	
	printf("The distance is 5cm.");
}
I am using the PIC16f877 to write the source code above.
But, i cannot get the desire output.
Please help me guys.
Thanks a lot.[/code]
 

uart source code

Hi,

If you are using Hi-T*ch compiler then go to the \\HT-PIC\samples\usart directory and see the file serial.c and serial.h. There you will find all your answers.
Also, its not enough to use printf, you have to direct printf to some where, and in your case to the TXREG. That is done usigng the stdio.h that redirects it to the putch().
The printf routing takes about 500 words from your PIC, so you better build your own small file and use the putch() and puts() to send strings to the UART...

Good luck.
 

putrs1usart

#include <pic.h>


void putrs1USART(const char *data);
void putrsUSART(const char *data);
void putByteUSART(unsigned char data);
unsigned char a=33,b=0,udata;

main()
{
SPBRG = 12; // 19200 baud @ 4MHz
TXSTA = 0x24; // setup USART transmit
RCSTA = 0x90; // setup USART receive
PORTC = 0; // Clear PORTC
TRISC = 0x80; //

putrsUSART("\r\n\ 1234567890ABCDEFGHIJKLMNOPRSTVZXYQW");
putrs1USART("\r\n\ 1234567890abcdefghijklmnoprstvzxyqw");
putrs1USART("\r\n\ ASCI from 33 to 255");


putByteUSART(10);
putByteUSART(13);

for(a=33;a<254;a++){ //send ASCI table
putByteUSART(a);
b++;
if(b>25){

putByteUSART(10);

putByteUSART(13);
b=0;

}
}

while(1){

if(RCIF)
{
udata = RCREG;
putByteUSART(udata); //send char back

}

}





}

void putrsUSART(const char *data)
{
do
{
while(!(TXSTA & 0x02));
TXREG = *data;
} while( *data++ );
}

void putrs1USART(const char *data)
{
do
{
while(!TXIF);
TXREG = *data;
} while( *data++ );
}
void putByteUSART(unsigned char data)
{


while(!(TXSTA & 0x02));
TXREG = data;

}
 

hi tech usart

Thank you so much, guys!!! :)
 

Good afternoon everyone ...
I am programming the PIC16F883 and did what grabik now, but I'm having some problems:
By the time I'll send putrsUSART ("\ r \ n \ 1234567890ABCDEFGHIJKLMNOPRSTVZXYQW");

In RealTerm hours appears: ABCQW in 234QY again, I am not able to send complete.

Below is the code I'm using to make the configuration

Code:
/***** Common Code ****
	 *  Portbit7:4 interrupt-on-change disabled
	 *  Timer 0 interrupt enabled.
	 *  Peripheral interrupts enabled
	 *  Global interrupt disabled during initialization
	 *  PortB interrupt enabled
	 */
	INTCON	= 0b01101000;
	
	/*
	 *  Timer 0 is prescaled by 1:256 (65.536 mSec)
	 *  Timer 0 is clocked internally.
	 *  Weak pullup on PORT disabled
	 */
	OPTION	= 0b10000111;
	
	/*
	 *  Usart TX interrupt disabled.
	 *  Usart RX interrupt enabled.
	 */
	PIE1	= 0b00100000;
	
	/*
	 *  Port directions: 1=input, 0=output
	 */
	TRISC	= 0b10000000;
	PORTC   = 0b00000000;
	
	/***** 16F883 Code ****
	 *  Internal oscillator set to 4MHz
	 */
	OSCCON	= 0b01100000;
	
	/***** PortA Code ****
	 *  Port directions: 1=input, 0=output
	 */
	TRISA	= 0b00000000;
	PORTA   = 0b00000000;
	
	/***** PortB Code ****
	 *  Port directions: 1=input, 0=output
	 */
	TRISB 	= 0b00001111;
	PORTB   = 0b00000000;
	
	// ports digital
	ANSELH = 0x00; //Puts AN 13-8 to Digital 
	ANSEL  = 0x00; //Puts AN 7-0 to Digital
	
	// enabled interrupt pins
	IOCB   = 0b11111111;
	
	/***** Usart Code ****
	 *  High speed baud rate generator enabled
	 *  Usart in Asynchronous mode
	 *  Usart transmission enabled
	 *  TX in eight bit format
	 */
	TXSTA	= 0b00100100;
	
	/*
	 *  Usart reception enabled
	 *  RX in eight bit format
	 *  Usart module enabled
	 */
	RCSTA	= 0b10010000;
	
	/*
	 *  Baud rate is 9600
	 */
	SPBRG	= 0b00011001;
	
	TXEN=0;	//reset transmitter
	TXEN=1;	//enable the transmitter
		
	for (int i = 0; i <= 9; i++)
	{
		msgSend[i] = '0';
		msgRec[i]  = '0';
	}
	
	msgSend[0] = '#';
	msgSend[9] = '$';
	
	PWR = 1;
	
	ei();	// Global interrupts enabled

Each interrupt port B, sending data over serial

Tanks.
 

Any one has similar code for PIC18F1220. I am interfacing the PIC18F1220 with MAX233A.
 

Hey when I'm using PIC16f877, I don't have to explicitly mention the TXEN ,the baud and stuff right? I mean I can simply use -> #use rs232(baud=9600,xmin=c.6,rcv=c.7,parity=N,stream=rs232,bits=8)
Am I write?
 

Hey when I'm using PIC16f877, I don't have to explicitly mention the TXEN ,the baud and stuff right? I mean I can simply use -> #use rs232(baud=9600,xmin=c.6,rcv=c.7,parity=N,stream=rs232,bits=8)
Am I write?

The following compiler directive is for CCS not Hi-Tech C:

#use rs232(baud=9600,xmin=c.6,rcv=c.7,parity=N,stream=rs232,bits=8)

Which compiler are you actually utilizing?

Even if you use a C library to configure and utilize the PICs UART, properly configuring the BAUD rate and other relevant settings using the library would be necessary.

BigDog
 

The following compiler directive is for CCS not Hi-Tech C:



Which compiler are you actually utilizing?

Even if you use a C library to configure and utilize the PICs UART, properly configuring the BAUD rate and other relevant settings using the library would be necessary.

BigDog

Okay I've downloaded a CCS C compiler, do I need to download a C30 compiler? It's a PIC mid range MCU PIC16f877 that I'm using..

---------- Post added at 16:15 ---------- Previous post was at 16:13 ----------

I've written the program in MPLABX the free version, I have MIKRO PRO C too..

---------- Post added at 16:26 ---------- Previous post was at 16:15 ----------

And it's a CCS C lite compiler..
 

I had simulate the program in proteus simulation software its working. But PIC16f877 hardware is not transmit a data,
what is the problem?
 

I had simulated a UART reciever/Tranmittior in PIC18f46K22 . Transmitter is perfectly working.But reciever is not working. :oops:
Can that be a hardware problem??
code for recuiever :-

Code CSS - [expand]
1
2
3
4
5
6
7
while(PIR1bits.RC1IF == 1)
                {
                    ch=RCREG1;
                    TXREG1=ch;
                    blink_LED();
 
                }

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top