Kylobeetle
Member
Hello everyone, Im playing with a PIC16F887 to practice my coding skills and I managed to connect it to my computer via UART. I made a simply code to
Send a text to my pc and then answer back anything inserted, then procced to show it back and confirm what i wrote. But im facing something weird when i do this because after the word is completed, next to it appears a chain of garbage chracters, i have tried to limit the array and set a null value to the last digit of the array to let it know that the word will end there. I dont know if you have give me a hand to succesfull enter any word of X characters and then show it back with no garbage in it. Excuse me if some part of my redaction doesnt make sense at some parts, Im not a native english speaker. thanks !
Info:
Compiler XC8
Config.h = are the config bits made into that file for quick working (PIC16F887 , using inner osc)
https://prntscr.com/j4lpsc (image of the test result)
Main code (for any reason i couldnt not use the insert code command, it just hangs and does nothing so i ll paste the code here.
#include <xc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "uart.h"
#define _XTAL_FREQ 4000000
int main() {
TRISB = 0x00; //All B ports as output
PORTB = 0x01; // b0 on as ON pilot led.
char block[]; //test purposes
char stone[3]; // test purposes
stone[3] = "\0"; // assign null to the last space of the array stone
usart_init();
__delay_ms(500);
UART_Write_Text("Insert something to display\r\n");
UART_Read_Text(block, 5); // using word ERROR
UART_Write_Text(block); // shows ERROR+garbage
__delay_ms(800);
UART_Write_Text("Insert something to display\r\n");
UART_Read_Text(stone, 2); // OK as test word
UART_Write_Text(stone); gets OK+garbage
__delay_ms(800);
UART_Write_Text("This is a test\r\n"); // nothing special
return 0;
}
UART.H
void usart_init(){
TRISCbits.TRISC7=1;//pin RX as digital input
TRISCbits.TRISC6=0;//pin TX as digital output
TXSTA=0b00100110;// settings
RCSTA=0b10010000;// settings
SPBRG=25;// 9600 bauds for 4mhz osc.
}
char UART_TX_Empty()
{
return TRMT;
}
char UART_Data_Ready()
{
return RCIF;
}
char UART_Read()
{
while(!RCIF);
return RCREG;
}
void UART_Read_Text(char *Output, unsigned int length)
{
int i;
for(int i=0;i<length;i++)
Output = UART_Read();
}
void UART_Write(char data)
{
while(!TRMT);
TXREG = data;
}
void UART_Write_Text(char *text)
{
int i;
for(i=0;text!='\0';i++)
UART_Write(text);
}
Send a text to my pc and then answer back anything inserted, then procced to show it back and confirm what i wrote. But im facing something weird when i do this because after the word is completed, next to it appears a chain of garbage chracters, i have tried to limit the array and set a null value to the last digit of the array to let it know that the word will end there. I dont know if you have give me a hand to succesfull enter any word of X characters and then show it back with no garbage in it. Excuse me if some part of my redaction doesnt make sense at some parts, Im not a native english speaker. thanks !
Info:
Compiler XC8
Config.h = are the config bits made into that file for quick working (PIC16F887 , using inner osc)
https://prntscr.com/j4lpsc (image of the test result)
Main code (for any reason i couldnt not use the insert code command, it just hangs and does nothing so i ll paste the code here.
#include <xc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "uart.h"
#define _XTAL_FREQ 4000000
int main() {
TRISB = 0x00; //All B ports as output
PORTB = 0x01; // b0 on as ON pilot led.
char block[]; //test purposes
char stone[3]; // test purposes
stone[3] = "\0"; // assign null to the last space of the array stone
usart_init();
__delay_ms(500);
UART_Write_Text("Insert something to display\r\n");
UART_Read_Text(block, 5); // using word ERROR
UART_Write_Text(block); // shows ERROR+garbage
__delay_ms(800);
UART_Write_Text("Insert something to display\r\n");
UART_Read_Text(stone, 2); // OK as test word
UART_Write_Text(stone); gets OK+garbage
__delay_ms(800);
UART_Write_Text("This is a test\r\n"); // nothing special
return 0;
}
UART.H
void usart_init(){
TRISCbits.TRISC7=1;//pin RX as digital input
TRISCbits.TRISC6=0;//pin TX as digital output
TXSTA=0b00100110;// settings
RCSTA=0b10010000;// settings
SPBRG=25;// 9600 bauds for 4mhz osc.
}
char UART_TX_Empty()
{
return TRMT;
}
char UART_Data_Ready()
{
return RCIF;
}
char UART_Read()
{
while(!RCIF);
return RCREG;
}
void UART_Read_Text(char *Output, unsigned int length)
{
int i;
for(int i=0;i<length;i++)
Output = UART_Read();
}
void UART_Write(char data)
{
while(!TRMT);
TXREG = data;
}
void UART_Write_Text(char *text)
{
int i;
for(i=0;text!='\0';i++)
UART_Write(text);
}