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
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);
}