- Code: Select all
#pragma config CONFIG1 = 0x2CD2
#pragma config CONFIG2 = 0x0700
#include <stdio.h>
#include <stdlib.h>
//LCD module connections
#define LCD_RS RD0
#define LCD_EN RD1
#define LCD_D4 RD2
#define LCD_D5 RD3
#define LCD_D6 RD4
#define LCD_D7 RD5
#define LCD_RS_DIR TRISD0
#define LCD_EN_DIR TRISD1
#define LCD_D4_DIR TRISD2
#define LCD_D5_DIR TRISD3
#define LCD_D6_DIR TRISD4
#define LCD_D7_DIR TRISD5
//End LCD module connections
#include <xc.h>
#define _XTAL_FREQ 20000000
#include <stdint.h> // include stdint header
#include "LCD_Lib.c" // include LCD driver source file
uint16_t AN0RES=0;
float Temperature, Voltage;
char* TempSTR[16];
char* ADCN[16];
void InitADC(void);
uint16_t ADC_Read(uint8_t ANC);
void InitADC(void)
{
ADCON0 = 0x91;
ADCON1 = 0x80;
PORTA = 0x00;
TRISA = 0x20;
ANSEL = 0x10;
}
uint16_t ADC_Read(uint8_t ANC)
{
/*if(ANC<0 || ANC>7) // Check Channel Number Validity
{ return 0;}*/
ADCON0 = 0x91;
__delay_us(30);
GO_DONE = 1; // Start A/D Conversion
while(ADCON0bits.GO_DONE); // Polling GO_DONE Bit
return ((ADRESH<<8)+ADRESL);
}
void main(void)
{
LCD_Begin();
InitADC();
LCD_Cmd(LCD_CLEAR);
LCD_Goto(1, 1);
LCD_Print("Temperature");
while(1)
{
AN0RES = ADC_Read(4); // Read Analog Channel 4
Voltage = AN0RES * 0.0048828;
Temperature = Voltage / 0.01; // Calculate The Temperature
sprintf(TempSTR, " %.2f", Temperature); // Convert The Temperature From Float To String
LCD_Goto(1, 2);
LCD_Print(TempSTR);
__delay_ms(500);
}
}
I checked that this part of the code here takes 84.9% of my Flash.
- Code: Select all
while(1)
{
/*AN0RES = ADC_Read(4); // Read Analog Channel 4
Voltage = AN0RES * 0.0048828;
Temperature = Voltage / 0.01; // Calculate The Temperature
sprintf(TempSTR, " %.2f", Temperature); // Convert The Temperature From Float To String
LCD_Goto(1, 2);
LCD_Print(TempSTR);
__delay_ms(500);*/
}
I think the major problem is this line.
- Code: Select all
sprintf(TempSTR, " %.2f", Temperature);
It takes 63.5% of my FLASH, how do I make it more efficient.
Thank you very much in advance!