Continue to Site

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.

[SOLVED] [MOVED] Mikro C UART1 TEMPERATURE VALUES

Status
Not open for further replies.

KiuFelix

Newbie level 5
Joined
Apr 24, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
Hi all,
I am new in mikro-c compiler. I am trying to read temperature values and pass it to rs232 for logging. In my code, I am able to do so through UART1_WRITE() or even UART1_WRITE_TEXT(). I can out-put text well through UART1_WRITE_TEXT() though. Like UART1_WRITE_TEXT("test"). Data sent is test.Successful in this case. But passing values like UART1_WRITE_TEXT(12) or UART1_WRITE(12) I get pointer errors. How do one concatenate values in this case? Somebody help.Here is my code.

Code:
unsigned int t;
float temp;
void main(){
ADCON1 = 0x00;// Set all PORTA pins as analog
TRISA = 0xFF; // PORTA is input
TRISB = 0x00; // PORTB is output
PORTA=0; //clear PORTA
PORTB=0; //clear PORTB
PORTC=0; //clear PORTC
UART1_Init(9600);
Delay_ms(100);

while(1) {
    PORTB=0; //clear PORTB
    PORTC=0; //clear PORTC
    t=ADC_Read(0);
    Delay_ms(100);
    temp=(t*500)/1023;
    delay_us(100);
    UART1_Write_Text(temp);                                //Output temperature values
    delay_us(100);
         if(temp>35){
              PORTB.RB0=1;
              UART1_Write_Text("_H");
              }
         else if(temp<10)
         {
              PORTB.RB0=0;
              UART1_Write_Text("_L");
         }
         else
               {
                PORTB.RB0=0;
                UART1_Write_Text("_N");

               }
               Delay_ms(400);
}
}

Kindly Locate
Code:
 UART1_Write_Text(temp);  //Output temperature values
That is the area of interest.
Expected output is something like 20_N21_N22_N for normal temperature for example. 22,21 etc are the calculated temperature values.
Thanks in advance.
 

You need to convert the value into a string.
If your variable was an integer, you could use sprintf.

char buffer[24];

sprintf(buffer, "Temperature is %d", value);
UART1_Write_Text(buffer);

But your value is a float. I doubt if mikro-c supports full printf formatting?

You will have to write a function to convert it to a string.

Do you need to use a float?

unsigned int temp; /* will it overflow? */

temp = (t * 500) / 1024; /* 1024 not 1023? */
 
Last edited:

hello,

- add Conversions library in your project to use float conversion to string
- declare a buffer ( ex: text)
- cast your "t" reading ADC from 16bits to float.
- Use decimal in your floating fix value to be sure to work with float (not integer) ,
it is a good rule to avoid mistake or to clarifie your code.


UART1_WRITE(12) I get pointer errors.

I allready uses direct code invoice on uart with this function without problems.
example :
UART1_Write(9); // (9=Tabulation) to separate the value
don't use UART1_Write_Text(9); for that !
i also uses
UART1_Write(12); to erase the page on terminal Vbray at starting program (like CLS in screen)

and try this:
Code:
// include in library   Conversions


unsigned int t;
float temp;
char text[15];		// add a buffer for float conversion



void main(){
ADCON1 = 0x00;// Set all PORTA pins as analog
TRISA = 0xFF; // PORTA is input
TRISB = 0x00; // PORTB is output
PORTA=0; //clear PORTA
PORTB=0; //clear PORTB
PORTC=0; //clear PORTC
UART1_Init(9600);
Delay_ms(100);

while(1) {
    PORTB=0; //clear PORTB
    PORTC=0; //clear PORTC
    t=ADC_Read(0);
    Delay_ms(100);
    temp=(float)t*500.0/1023.0;
    delay_us(100);
    FloatToStr(temp, text); 	// fill the buffer
    UART1_Write_Text(text);   //Output temperature values
    delay_us(100);
         if(temp>35){
              PORTB.RB0=1;
              UART1_Write_Text("_H");
              }
         else if(temp<10)
         {
              PORTB.RB0=0;
              UART1_Write_Text("_L");
         }
         else
               {
                PORTB.RB0=0;
                UART1_Write_Text("_N");

               }
               Delay_ms(400);
}
 
Hi btbass,
Thanks for your contribution. I think the srprintf() works only in native C. In mikroc most of this functions have been changed and assigned different names/identifications. Not like in CCS C compiler which is almost similar to native C.
The temp calculation I think I am right. I get a good simulation value in Protuse. I considered 0-1023 for 0=0v and 5 to 1023. I am still going to take a deep look on that though.
Thanks

---------- Post added at 13:22 ---------- Previous post was at 13:20 ----------

Hey paulfjujo,
Thanks for the assistance. Your code works like a charm. This is exactly what I wanted. Your explanation is good to. I have gained something from it. Thanks again.
 

10 bits is actually 1024 values. 0 is a value.

0-1023 is 1024 different values.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top