PIC USART Transmission

Thread Starter

crazyengineer

Joined Dec 29, 2010
156
So I'm having some difficulty trying to transmit intergers though USART. I thought if I use sprintf it'll be able to transmit the data sucessfully. However, I'm getting this garbage



Here's the code
Rich (BB code):
#include <p18F2420.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF 
int current_angle=15;
char message[10];

void CONFIG(void);





#pragma interrupt ISR
void ISR ()
{ 	/*	Checks the current rotation */
	if(INTCON3bits.INT1IF==1||INTCONbits.INT0IF==1)										
	{
		if(PORTBbits.RB0==0 && PORTBbits.RB1==0)
		{
			current_angle=0;
		}
		else if(PORTBbits.RB0==0 && PORTBbits.RB1==1)
		{
			current_angle=90;
		}
		else if(PORTBbits.RB0==1 && PORTBbits.RB1==1)
		{
			current_angle=180;
		}
		else
		{
			current_angle=270;
		}
		
		/* Resets the external interrupt flags */
		INTCON3bits.INT1IF=0;
		INTCONbits.INT0IF=0;
	}
}


#pragma code InterruptVectorHigh = 0x08				//	This function simply jumps to the ISR code shown above.
void InterruptVectorHigh (void)
{
	_asm
	goto ISR //jump to interrupt routine
	_endasm
}
#pragma code

void main(void)
{
	CONFIG();

	while(1)							//	Run the program forever and let the interrupts take over.
	{
		sprintf(message,"%d",current_angle);
		putrsUSART(message);
		Delay1KTCYx(125);
	}

}



void CONFIG(void)
{
	/* Code for handling all of the interrupt */
	RCONbits.IPEN=1;					//	Enable interrupt priority
	INTCONbits.GIE=1;					//	Enable global interrupts
	INTCONbits.INT0IE=1;				//	Enable INT0 interrupts
	INTCONbits.INT0IF=0;				//	Set external interrupt flag to zero
	INTCON2bits.RBPU=1;					// Disable Pull up
	INTCON2bits.INTEDG0=1;				// 	Interrupt enable on rising edge
	INTCON3bits.INT1IE=1;				//	Enable External interrupt 1
	INTCON2bits.INTEDG1=1;				//	Enable External interrupt on rising edge
	INTCON3bits.INT1IF=0;				//	Set flag to zero
	TRISBbits.TRISB0=1;					//	Set PORTB0 to input	
	TRISBbits.TRISB1=1;					//	Set PORTB1 to input
	TRISBbits.TRISB2=0;					// 	Set PORTB2 to output
	PORTB=0x03;							// Enable high on inputs

	/* USART Configuration */
	TRISC=0x00;
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 129);
}
 

t06afre

Joined May 11, 2009
5,936
What did you expect as output? Your terminal program take 8 and 8 bytes and print them out as ASCII characters. You may end up all kind of strange characters. That is because your micro use the binary number system not the decimal. If you want readable numbers in a simple ASCII terminal program. You have to convert that number into a string.
By the way I have asked you several time about this. But you have ignored my request. So for the last time. Which C compiler and version do use.
 

CVMichael

Joined Aug 3, 2007
419
Just do something like putrsUSART("test1234"); and then see if you still get invalid data.

I am guessing the baud rate in your PIC is different than the baud rate selected in your terminal window.

Actually I don't see in your code where you specify the baud rate.
 

Markd77

Joined Sep 7, 2009
2,796
Try realterm or some other terminal software that can display the actual data without converting it to ASCII text.
Or convert the data to ASCII text before sending.
<ed>I now realise that the %d converts to string. It might still help to have a better terminal.</ed>
 
Last edited:

Thread Starter

crazyengineer

Joined Dec 29, 2010
156
What did you expect as output? Your terminal program take 8 and 8 bytes and print them out as ASCII characters. You may end up all kind of strange characters. That is because your micro use the binary number system not the decimal. If you want readable numbers in a simple ASCII terminal program. You have to convert that number into a string.
By the way I have asked you several time about this. But you have ignored my request. So for the last time. Which C compiler and version do use.
It works when I submit simple text, but the error in the reading when I try to convert the number to a string. Btw, I'm sorry I did not respond to your question. I really did not see them :( . I'm using MPLAB with the C18 compiler.

I am guessing the baud rate in your PIC is different than the baud rate selected in your terminal window.
I'm pretty sure I configured it to a baud rate of 9600 as shown in the Arduino terminal.
 

t06afre

Joined May 11, 2009
5,936
@crazyengineer Are you using a paid version of the C18 compiler? If not can you use the free CX8 from Microchip compiler instead. The C18 compiler is kind of outdated.
 
Top