Abhilashhegde94
Member level 1

Hey guys,
need help urgently on this one
i am getting garbage values in the terminal when i send a single character from controller uart to pc terminal.
The code is as follows:
I am getting this a garbage value printed in the terminal sir,
Pls help
need help urgently on this one
i am getting garbage values in the terminal when i send a single character from controller uart to pc terminal.
The code is as follows:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 #include <stdio.h> #include <pic.h> #include <xc.h> #include<htc.h> #define _XTAL_FREQ 8000000 #define baud_rate 9600 /* A simple demonstration of serial communications which * incorporates the on-board hardware USART of the Microchip * PIC16Fxxx series of devices. */ // PIC16F886 Configuration Bit Settings // 'C' source line config statements // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. // CONFIG1 #pragma config "FOSC = HS" // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN) #pragma config "WDTE = OFF" // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config "PWRTE = OFF" // Power-up Timer Enable bit (PWRT disabled) #pragma config "MCLRE = OFF" // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config "CP = OFF" // Code Protection bit (Program memory code protection is disabled) #pragma config "CPD = OFF" // Data Code Protection bit (Data memory code protection is disabled) #pragma config "BOREN = ON" // Brown Out Reset Selection bits (BOR disabled) #pragma config "IESO = OFF" // Internal External Switchover bit (Internal/External Switchover mode is disabled) #pragma config "FCMEN = OFF" // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) #pragma config "LVP = OFF" // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) // CONFIG2 #pragma config "BOR4V = BOR40V" // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V) #pragma config "WRT = OFF" // Flash Program Memory Self Write Enable bits (Write protection off) char UART_Init(const long int baudrate) { unsigned int x; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate if(x>255) //If High Baud Rate required { x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate BRGH = 1; //Setting High Baud Rate } if(x<256) { SPBRG = x; //Writing SPBRG register SYNC = 0; //Selecting Asynchronous Mode SPEN = 1; //Enables Serial Port TRISC7 = 1; TRISC6 = 1; CREN = 1; //Enables Continuous Reception TXEN = 1; //Enables Transmission return 1; } return 0; } void UART_Write(char data) { while(!PIR1bits.TXIF); while(!TRMT); //Waiting for Previous Data to Transmit completly TXREG = data; //Writing data to Transmit Register, Starts transmission } char UART_TX_Empty() { return TRMT; //Returns Transmit Shift Status bit } void UART_Write_Text(const char *text) { int i; for(i=0;text[i]!='\0';i++) UART_Write(text[i]); } //recive part char UART_Data_Ready() { return RCIF; } char UART_Read() { while(!RCIF); //Waits for Reception to complete return RCREG; //Returns the 8 bit data } void UART_Read_Text(char *Output, unsigned int length) { int i; for(int i=0;i<length;i++) Output[i] = UART_Read(); } void putch(unsigned char byte) { /* output one byte */ while(!TXIF) /* set when register is empty */ continue; TXREG = byte; } unsigned char getch() { /* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; return RCREG; } unsigned char getche(void) { unsigned char c; putch(c = getch()); return c; } void main(void){ const unsigned char arr='B'; INTCON=0; // purpose of disabling the interrupts. UART_Init(baud_rate); __delay_ms(300);// set up the USART - settings defined in usart.h TRISB=0x00; // Output a message to prompt the user for a keypress //printf("\rPress a key and I will echo it back:\n"); // read a response from the user UART_Write(arr); // __delay_ms(1000); // PORTB=0xFF; // __delay_ms(1000); // PORTB=0X00; // echo it back }
I am getting this a garbage value printed in the terminal sir,
Pls help
Last edited by a moderator: