I would like to convert my code that uses an array that's constantly allocated to hold the converted data from an analog to digital converter into a method where the result of the sprintf conversion would be stored in a pointer as long as I need it. Below shows two versions of my code and the red highlighted line shows the task that I'm having trouble with. I apologize for the lackluster tab formatting of the code in advance. Thanks.
explicitly allocated char array version:
Code:
// Data Logging Scale <[email protected]>
// Created: Nov 3, 2007
// Revised: Nov 15, 2007
#include <built_in.h>
#include "typedefs.h"
#include "usart.h"
#include "LCD4.h"
#define BUFFER_SIZE 5
void delay(uint number)
{
while(--number > 0);
}
void main() {
uint temp_res, last_temp_res;
int n;
char adc[BUFFER_SIZE]; // temporary adc string value
OSCCON = 0x70; /* internal oscillator at 8 Mhz, system clock = internal oscillator*/
OSCTUNE = 0x00; /* oscillator tuning register at max frequency */
CMCON = 7; // turn off comparators
Delay_ms(20); // system wide delay of 20 ms
ADCON1 = 0x0e; // Configure Vref and AN0
TRISA = 0x01; // PORTA is output except RA0 (AN0)
TRISB = 0x00; // all PORTB pins are output
TRISC = 0x00; // PORTC is outputs
LCD4_INIT(); // initialize the LCD
LCD4_OUT("Weight (kg) : "); // output some text
while(1)
{
temp_res = Adc_Read(0);
n = sprintf(adc, "%u", temp_res);
if (n < 0 || n >= BUFFER_SIZE)
break;
if (abs(temp_res - last_temp_res) < 10)
{
LCD4_CMD(142); // move cursor position to character #15
LCD4_OUT(adc); // output the a/d result
}
last_temp_res = temp_res;
}
}
malloc for a char pointer version:
Code:
// Data Logging Scale <[email protected]>
// Created: Nov 3, 2007
// Revised: Dec 15, 2007
#include <built_in.h>
#include "typedefs.h"
#include "usart.h"
#include "LCD4.h"
#define BUFFER_SIZE 5
void delay(uint number)
{
while(--number > 0);
}
void main() {
uint temp_res, last_temp_res;
char *temp_res_text; // dynamic character array pointer
int n;
OSCCON = 0x70; /* internal oscillator at 8 Mhz, system clock = internal oscillator*/
OSCTUNE = 0x00; /* oscillator tuning register at max frequency */
CMCON = 7; // turn off comparators
Delay_ms(20); // system wide delay of 20 ms
ADCON1 = 0x0e; // Configure Vref and AN0
TRISA = 0x01; // PORTA is output except RA0 (AN0)
TRISB = 0x00; // all PORTB pins are output
TRISC = 0x00; // PORTC is outputs
LCD4_INIT(); // initialize the LCD
LCD4_OUT("Weight (kg) : "); // output some text
while(1)
{
temp_res = Adc_Read(0); // sample a/d converter
temp_res_text = (char *) malloc(5); // allocate 5 character bytes
// How would i use sprintf function to convert temp_res data into string format
// in resultant pointer temp_res_text?
LCD4_CMD(142); // move cursor position to character #15
LCD4_OUT(temp_res); // output the a/d result
free(temp_res_text); // free allocated character bytes
}
}
Please let me know if there are any functions that you don't understand as those functions have been tested to work up to the specification so far, hence why I didn't include them b/c they are not the source of the problem.