• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16f877a gettin string from uart and need to convert them through ascii[SOLVED]

qwertyqwq

New Member
hello everyone;
im going to build a code file for my pic16f877a but i stacked and i need some help :)

im want to build a code for get ascii via uart from pc.
for example; im want to read a string from pc which is like "Q45" and then i want to convert it to ascii codes[example:Q-->81 , 4-->52 , 5-->53 and i need to see on UART 815253 ]
ineed this cause i ll gonna process after all.
here is my some codes;

char q[5]={'ef'};
char isascii[8] ;
int c=5;
int convint=5;

itoa (isascii,c,10); // convert integer to string
UART_send_string(isascii);// i can see "5" on my terminal
UART_send_string("__");

convint = atoi( q ); // convert string to integer
itoa (k,convint,10);
UART_send_string(k); // i can only see "0" on my terminal


the code is so much ruined, i know. i tried every possible thing and now here i am. i m a newbie in this forum guys :)
i can also be grateful if you guys have any idea or codes for me . im using mplab xc8 compiler for this project.
thanks in advance and sorry for my english ;)
 

Pommie

Well-Known Member
Most Helpful Member
It already is ascii code. If you have a C string that contains "Q45" then the first char is 81 and if the string is called q then q[0] will equal 85 etc. Note your line char q[5]={'ef'}; is not good as single quotes normally don't add an ending zero. Better q[5]="ef"; - assuming you want your string to contain ef.

Mike.
Edit, Can't follow what you are trying to do in your code.
 

qwertyqwq

New Member
thanks for reply pommie, hopefully you have a great day :)
Better q[5]="ef";
yes i know i tried it like a thousend time.i have changed my codes .
now this is my new code; (its all code)

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 16000000L
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <xc.h>

/**********************************************************************
HEADER FILE
***********************************************************************/
#include<pic.h>

/**********************************************************************
MAIN FUNCTION
***********************************************************************/
void main()
{
int a=63;
TRISC = 0x80; /* RC6(TX)-O/P, RC7(RX)-I/P */
SPBRG = 0x81; /* Serial Baud Rate Generator for 9600 */
TXSTA = 0X24; /* TXEN=1, SYNC=0, BRGH=1 */
RCSTA = 0X90; /* Reception Enable (SPEN=1,CREN=1) */
TXREG = 'A'; /* Load the character to be transmitted */

RCREG=a; /* Retransmit the received character */
TXREG=RCREG;
while(!TRMT); /* Wait here till transmission is complete */

while(1)
{
if(RCIF==1)
{
RCIF = 0; /* Clear Receive interrupt flag bit */
RCREG=a; /* Retransmit the received character */
while(!TRMT); /* Wait here till transmission is complete */
if (RCREG==69){ /* E in ascii */
TXREG=0X46; /* F in hex */
TXREG=a+10; /* I in hex */
}
}
}
}

/************************ END OF PROGRAM ********



those codes are working fine. i have change my codes cause of the i have changed my mind, i didnt want to receive or read any string, so now i told you guys im a newbie so i need read ascii like here;
for example; im want to read a string from pc which is like "Q45" and then i want to convert it to ascii codes[example:Q-->81 , 4-->52 , 5-->53 and i need to see on UART 815253 ]
but the problem is when i type it down i can only get "81" which is ascii code of "Q"...
ineed some help ,thanks in advance ...:)
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Have a look at this. Press run and tell me if this helps.

Mike.
Edit, whoops, put G45 instead of Q45. Press "Fork it" to be able to change it.
 

qwertyqwq

New Member
many thanks Pommie :D

its working ,a got "71,52,53," . you gave an idea thanks for helping. im goint to try it this evening :)
 

Pommie

Well-Known Member
Most Helpful Member
Glad you got a result. That is a good site to just try stuff and see if it works.

Mike.
BTW, if you hit the "Fork this" button then you can edit it and play.
 

rjenkinsgb

Active Member
Just for info, do not use the rcreg more than once - most PICs have a FIFO buffer and if another character is waiting, it will be read the second time, resulting in a confused result and missed characters.

In other words, if you check RCIF and it is set, copy the received character to another variable and do not touch the rcreg again until you have checked the RCIF flag for another character.

Also in some places you seem to be writing to the rcreg??
 

Pommie

Well-Known Member
Most Helpful Member
rjenkinsgb

Agree, I'll just add, reading RCREG automatically clears RCIF. You should never write to either RCREG or RCIF.

Mike.
 

sagor1

Active Member
rjenkinsgb

Agree, I'll just add, reading RCREG automatically clears RCIF. You should never write to either RCREG or RCIF.

Mike.
Just to clarify, RCIF will be clear if there is no more data in RCREG. If there is a second byte in the FIFO, RCIF remains set until the FIFO of RCREG is empty.
Best logic is to always check RCIF before exiting an interrupt routine, as there may be two bytes to read. RCREG is a two byte FIFO register.
Section 10.2.2 of the 16F877 datasheet:
Flag bit RCIF is a read-only bit which is
cleared by the hardware. It is cleared when the RCREG
register has been read and is empty
Key word is "empty"...
 

Pommie

Well-Known Member
Most Helpful Member
No need to check it twice in an interrupt, if there are two bytes then there will be two interrupts.

Mike.
 

Latest threads

EE World Online Articles

Loading

 
Top