Microcontroller › PIC › Convert ‘ unsigned long int ‘ to ‘string’ without using inbuilt function for UART
- This topic has 5 replies, 3 voices, and was last updated 9 years, 9 months ago by Sasa.
-
AuthorPosts
-
December 31, 2014 at 10:10 am #3378Jayant GuptaParticipant
hello everybody
i m a newbie at microcontrollers. i have started making projects for my own learning.
i am using PIC18F1330 for UART communication. i am trying to send unsigned long int over UART without interrupts, for which i need to convert it string, which i am unable to do it although i have gone through some help on other websites but nothing works. UART is working fine but as for the conversion part not able to figure out its working. kindly help!!
IDE- MPLAB and Compiler- HTC for PIC18
this is my code
=============================== main.c ==========================================
#include<htc.h>
#include”uart.h”
#include”delay_custom.h”unsigned long int data0=7654321; // for example data to be sent on UART
unsigned char *t_buffer[8]; // buffer in which conversion will be stored and transmittedvoid pic_init()
{
OSCCON=0xFF;
PORTA=0x00;
ADCON1=0xFF;
}void main()
{
pic_init();UART_Init();
DelayMs(5);
while(1)
{
UART_Write_Text(“HELLO !!”);
DelayMs(5);
UART_Write_Byte(‘r’);
UART_Write_Byte(‘n’);
*t_buffer=data0;
UART_Write_Text(t_buffer);
DelayMs(5);
UART_Write_Byte(‘r’);
UART_Write_Byte(‘n’);
}
}======================== uart.h =======================================
#define _XTAL_FREQ 8000000
#define BAUDRATE 9600void UART_Init(void);
void UART_Write_Byte(unsigned char);
void UART_Write_Text(const unsigned char*);void UART_Write_Byte(unsigned char Byte)
{
//TXEN=1;
while(!TRMT);
TXREG=Byte;
}void UART_Write_Text(const unsigned char *st)
{
while(*st)
{
UART_Write_Byte(*st++);
}
}void UART_Init()
{
TRISA3=1;
TRISA2=1;SPBRG=((_XTAL_FREQ/16)/BAUDRATE)-1;
BRGH=1; // higher baud rate
SYNC=0; // asynchronous
SPEN=1; // enable serial port
CREN=1; // reciever enable
SREN=0; // no efftect
TXIE=0; // disable transmit interrupt
RCIE=0; // disable receive interrupt
TX9=0; // 8 bit transmission
RX9=0; // 8 bit reception
TXEN=0; // reset transmitter
TXEN=1; // enable transmitter
}============================== delay.h ============================
void DelayMs(unsigned char cnt)
{
unsigned char i;
while (cnt–) {
i=4;
while(i–) {
DelayUs(uS_CNT); /* Adjust for error */
} ;
} ;
}December 31, 2014 at 10:26 am #12477Ashutosh BhattParticipantwhat is the meaning of
*t_buffer=data0;
what u want to do?
do u think this well saparate digits of integer number?
you have to write saparate program to do this
December 31, 2014 at 11:51 am #12484Jayant GuptaParticipant*t_buffer= data0;
means nothing its a part of the previous method i tried which didnt work.
pls IGNORE this part!!!!
i simply want to sent the content of variable data0 over uart right after i send hello over uart.
do i need to write separate program for separating the digits and send them over uart one by one as char values???
January 5, 2015 at 9:20 pm #12499SasaParticipantHello,
Firstly, you didn’t declared buffer in a correct way:
Instead of “unsigned char *t_buffer[8];” use “unsigned char t_buffer[8];”
Secondly, you have to fill into t_buffer ascii representation for each digit. There are two ways for do this:
1. using sprintf command (header “stdio.h” must be previously included into project) :
sprintf((char*)t_buffer,”%lu”,data0);
2. step by step:
t_buffer[0] = (data0 / 1000000ul) + 0x30;
data0 /= 1000000ul;
t_buffer[1] = (data0 / 100000ul) + 0x30;
data0 /= 100000ul;
t_buffer[2] = (data0 / 10000ul) + 0x30;
data0 /= 10000ul;
t_buffer[3] = (data0 / 1000ul) + 0x30;
data0 /= 1000ul;
t_buffer[4] = (data0 / 100ul) + 0x30;
data0 /= 100ul;
t_buffer[5] = (data0 / 10ul) + 0x30;
t_buffer[6] = (data0 % 10ul) + 0x30;
t_buffer[7] = 0;
January 7, 2015 at 10:50 am #12500Jayant GuptaParticipantthanks Sasa.. ur post provided me a lot of help and i implemented it sucessfully
i had to make certain small changes to get it working .. i really appreciate ur help without it would have been lil difficult understanding further step.
i am posting the code here jus incase someone might need it.
void convert_l2s(unsigned long int data0, unsigned char temp_buffer[8])
{
temp_buffer[0]=(data0/1000000)+0x30;
data0%=1000000;
UART_Write_Byte(temp_buffer[0]);
temp_buffer[1]=(data0/100000)+0x30;
data0%=100000;
UART_Write_Byte(temp_buffer[1]);
temp_buffer[2]=(data0/10000)+0x30;
data0%=10000;
UART_Write_Byte(temp_buffer[2]);
temp_buffer[3]=(data0/1000)+0x30;
data0%=1000;
UART_Write_Byte(temp_buffer[3]);
temp_buffer[4]=(data0/100)+0x30;
data0%=100;
UART_Write_Byte(temp_buffer[4]);
temp_buffer[5]=(data0/10)+0x30;
UART_Write_Byte(temp_buffer[5]);
temp_buffer[6]=(data0%10)+0x30;
UART_Write_Byte(temp_buffer[6]);
temp_buffer[7]=0;
UART_Write_Byte(temp_buffer[7]);
}i am trying to generalize it using loop for n digits int type!!
do i need to use a similar approach for float data as well???
January 7, 2015 at 10:34 pm #12501SasaParticipantYou are welcome,
Sorry, I also made a mistake accidentally in prevoius post (divide instead of moduo), but you made it correct.
You can do the same thing with float values, but before converting to ascii simply multiply float value in order to lose decimal places and print it via uart digit by digit.
Example:
If you want to print float value 32.768 , multiply it by 1000 (or some other factor depending on decimal places you want to show) to get integer 32768 and print it digit by digit as in previous case.
-
AuthorPosts
- You must be logged in to reply to this topic.