Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Adding string with variable in MikroC

Status
Not open for further replies.

anishpsla

Member level 2
Joined
Dec 15, 2013
Messages
44
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
402
How to concat string with variable in MikroC.

I want to display something like


Code C - [expand]
1
UART1_Write_Text("Fan"+fan+" speed : "+fan_rpm+" RPM");

 

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,299
hello

what PIC family you are using ...
sprintf can be used for 18F...

and what unit for other info like RPM , Fan value
is it integer, long or float ?
 

anishpsla

Member level 2
Joined
Dec 15, 2013
Messages
44
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
402
I am using PIC16F877.

RPM and Fan are int type. I want to send something like "Fan0 speed : 1200 RPM"
 

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,454
Helped
295
Reputation
590
Reaction score
278
Trophy points
1,363
Location
France 01800
Activity points
10,299
So,you can prepare a text in RAm as "Fan 0 speed : 1200 RPM"
and after just change the value 0 for the fan number ( is it less than 10 ? only one digit ?)
and convert your speed measure wit WordToStr
and place the result direct inside the text.

result on terminal (UART)
<0><0>16F877 Test string
Freq kHz= 20000
Fan 0 speed: xxxxx RPM
Fan 0 speed: 0 RPM
Fan 1 speed: 980 RPM
Fan 2 speed: 1960 RPM
Fan 3 speed: 2940 RPM
Fan 4 speed: 3920 RPM
Fan 5 speed: 4900 RPM
Fan 6 speed: 5880 RPM
Fan 7 speed: 6860 RPM
Fan 8 speed: 7840 RPM
Fan 9 speed: 8820 RPM

MikroC prog

Code:
// 14-01-2014
// Project : 16F877_Irdaboard_ADC.mcppi
// PIC16F877 sur carte Microchip IRDA Demo Board 1  102-00032
// Q=20MHz
// CONFIG   :$2007 : 0x3B3A
// uses  library  string,uart,
sbit Led_Verte_D0 at RD0_bit ;
sbit Led_Verte_D1 at RD1_bit  ;
 //             12345678901234567890
char ConvTable[6];
char CRam1[32];
unsigned char uart_rd;
unsigned int i,j,k,m;
unsigned long L1;
unsigned int Fan,Speed;

void Tempo(unsigned long LT);
void CRLF(void);

 void Tempo(unsigned long LT)
 {
 while (LT-->0);
 }
 
void CRLF()
 {
 UART1_Write(13);
UART1_Write(10);
}

void main()
{
  ADCON1=0x0E;
  TRISA=0b11110001 ;
  ADCON1=0x0E;      // RA0=Analog other as digital portA
  TRISC=0b10111111 ;
  TRISD=0b00000000 ;
  TRISB=0b11110000 ;
  PORTB=0;
  OPTION_REG=128  ;
  PORTA=0;
 
  Led_Verte_D1=1;
  Tempo(10000L);

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(500);                  // Wait for UART module to stabilize
  UART1_Write_Text("16F877  Test string\r\n");
  L1=Get_Fosc_kHz();
  UART1_Write_Text("Freq kHz= ");
  LongToStr(L1, CRam1);
  UART1_Write_Text(CRam1);
  CRLF();
  strcpy(CRam1,"Fan 0 speed: xxxxx RPM");
  UART1_Write_Text(CRam1);
  CRLF();

 ADC_Init();
 Led_Verte_D1=0;
 Fan=0;
 do
 { 
//Speed m=ADC_Get_Sample(0);
// WordToStr(Speed,ConvTable);
 //UART1_Write(9); // tabulation
 Speed= 980*Fan;
 CRam1[4]=Fan+48;   // update fan number
 WordToStr(Speed,CRam1+13);
 CRam1[18]=32;   // replace the zero given by WordToStr by a space
 UART1_Write_Text(CRam1);
 CRLF();
 Led_Verte_D1=!Led_Verte_D1;
 Tempo(150000L);
 Fan++;
 }
 while (Fan<10);
 do
 {}
 while(1);
}

- - - Updated - - -

better, with right justied result for speed value
but need another table to know the lenght of data to convert in ascii

Test_String.jpg

Code:
 Fan=0;
 do
 { 
 Speed= 256*Fan;
 CRam1[4]=Fan+48;   // update fan number
 WordToStr(Speed,ConvTable);
 k=strlen(ConvTable);
 i=0;
 while(k<5)
 {
  CRam1[13+i]=' ';   // add space to respect the format 5 digits right justified
  i++;k++;
 }
 memcpy(CRam1+13+i,ConvTable,5-i);
 CRam1[18]=32;   // replace the zero given by WordToStr by a space
 UART1_Write_Text(CRam1);
 CRLF();
 Led_Verte_D1=!Led_Verte_D1;
 Tempo(150000L);
 Fan++;
 }
 while (Fan<10);
 

Max Jahnke

Newbie level 4
Joined
Jan 13, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Pelotas/RS - Brazil
Activity points
52
I think you can declare two vectors of a certain size and use sprinti() to convert int to char. After that you just use lcd_out("vector you've created").
 

anishpsla

Member level 2
Joined
Dec 15, 2013
Messages
44
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
402
Thanks for your help. It extremely helped me.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top