sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//-----------------------------------------------------
void staticprint(){
LCD_Out(2,1,"TIME");
LCD_Out(2,14,":");
LCD_Out(2,11,":");
LCD_Out(2,8,":");
LCD_Out(1,1,"TEMP");
LCD_Chr(1,15, 223);
LCD_Out(1,16,"F");
}
//--------------------------------------tahmids code for lm35----------------
void readtemp()
{
unsigned long ADRead;
unsigned int vDisp[3];
unsigned char Display[7];
ADRead = (ADC_Get_Sample(0) * 500) >> 10;
vDisp[0] = ADRead / 100;
vDisp[1] = (ADRead / 10) % 10;
vDisp[2] = ADRead % 10;
Display[1] = vDisp[0] + 48;
Display[2] = vDisp[1] + 48;
Display[3] = vDisp[2] + 48;
LCD_Chr(1, 8, Display[0]);
LCD_Chr(1, 9, Display[1]);
LCD_Chr(1, 10, Display[2]);
LCD_Chr(1, 11, Display[3]);
}
//------------------------------------------------------
void mydelay()
{
readtemp();
staticprint();
Delay_ms(250);
readtemp();
staticprint();
Delay_ms(250);
readtemp();
staticprint();
Delay_ms(250);
readtemp();
staticprint();
Delay_ms(250);
}
//------------------------------------------------------
//-----------------------------------------------------------------------------
void main()
{
//-----------------------------
PORTA = 0;
TRISA = 0X01;
PORTB = 0;
TRISB = 0;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
ADCON1 = 0x0E;
ADC_Init();
//----------------------------
//////////////////////////////////////////////////////////////////////////
while (1)
{
//------------variables-----------------------
char secs[7];
char mins[7];
char hours[7];
char days[7];
unsigned int s;
unsigned int m;
unsigned int h;
unsigned int d;
//-------------------------------------------
for(d = 0;d<40;d++)
{
IntToStr(d,days);
if(d<10)
{
LCD_Out(2,7,Ltrim(days));
LCD_Out(2,6, "0");
}
if (d>9)
{
LCD_Out(2,6,Ltrim(days));
}
for(h = 0;h<24;h++)
{
IntToStr(h,hours);
if(h==24)
{
d++;
}
if(h<10)
{
LCD_Out(2,10,Ltrim(hours));
LCD_Out(2,9,"0");
}
if (h>9)
{
LCD_Out(2,9,Ltrim(hours));
}
for(m = 0;m<60;m++)
{
IntToStr(m,mins);
if(m==60)
{
h++;
}
if(m<10)
{
LCD_Out(2,13,Ltrim(mins));
LCD_Out(2,12, "0");
}
if (m>9)
{
LCD_Out(2,12,Ltrim(mins));
}
for(s = 0;s<60;s++)
{
IntToStr(s,secs);
if(s==60)
{
m++;
}
if(s<10)
{
LCD_Out(2,16,Ltrim(secs));
LCD_Out(2,15, "0");
}
if (s>9)
{
LCD_Out(2,15,Ltrim(secs));
}
mydelay();
}
}
}
}
}
}
ADRead = (ADC_Get_Sample(0) * 500) >> 10;
ADRead = (ADC_Get_Sample(0) * 500UL) >> 10;
ADRead = (ADC_Get_Sample(0) * 500UL) >> 10;
vDisp[0] = ADRead / 100;
vDisp[1] = (ADRead / 10) % 10;
vDisp[2] = ADRead % 10;
i dont see sprintf in the library files for pic16f877a im using mikroc is there another way to get the float value of ADRead?
i read a thread in mikroc, sadly pic16 doesn't support sprintf
float ADRead;
char txt[16];
ADRead = ADC_Get_Sample(0);
FloatToStr(ADRead, txt);
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
float voltage;
char voltagestr[23];
void main(){
PORTA = 0;
PORTA = 0;
TRISA = 0X01;
PORTB = 0;
TRISB = 0;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
ADCON1 = 0x0E;
ADC_Init();
Delay_ms(2500);
Lcd_Cmd(_LCD_CLEAR);
while (1)
{
voltage = ADC_Read(0);
//Convert ADC readings to strings for LCD)
floatToStr(voltage, VoltageStr);
//Write Strings to LCD
LCD_Out(1,9,VoltageStr);
//Wait a while
delay_ms(2000);
}
}
Temp= 65 pts soit 65
Temp= 66 pts soit 65.99999
Temp= 27 pts soit 26.99999
Temp= 17 pts soit 16.99999
Temp= 269 pts soit 268.9999
Temp= 519 pts soit 518.9999
Temp= 605 pts soit 604.9999
Temp= 691 pts soit 690.9999
Temp= 840 pts soit 839.9999
Temp= 1021 pts soit 1020.999
// Globales variables
int ARRead;
float f1;
unsignec char TEXTE[60];
char * txt;
unsigned char CRam1[30];
.. other ....
void Float2Ascii (float x, unsigned char *str,char precision)
{
/* converts a floating point number to an ascii string */
/* x is stored into str, which should be at least 30 chars long */
int ie, i, k, ndig;
double y;
ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
ie = 0;
/* if x negative, write minus and reverse */
if ( x < 0)
{
*str++ = '-';
x = -x;
}
/* put x in range 1 <= x < 10 */
if (x > 0.0) while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x/10.0;
ie++;
}
// in f format, number of digits is related to size
ndig += ie; // a la place de =+
//round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y/10.;
x += y/2.;
if (x >= 10.0) {x = 1.0; ie++;}
if (ie<0)
{
*str++ = '0'; *str++ = '.';
if (ndig < 0) ie = ie-ndig;
for (i = -1; i > ie; i--) *str++ = '0';
}
for (i=0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie ) *str++ = '.';
x -= (y=k);
x *= 10.0;
}
*str = '\0';
}
void main()
{
....
init Hardware();
init UART();
init LCD():
.....
ADCON1 = 0x0E;
ADC_Init();
do
{
UART1_Write_CText("Temp= " );
ADRead=ADC_Read(0);
//readtemp();
f1= (float) ADRead * 0.004896; // result 0 to 5.00 volt
WordToStr(ADRead,CRam1);
UART1_Write_Text(CRam1);
UART1_Write_CText(" pts soit " );
UART1_Write(TAB);
Float2Ascii (f1,txt,3); // 3 = 3 digits after decimal point
txt=TEXTE; // init pointeur txt
UART1_Write_Text(txt);
UART1_Write_CText(" Volts\r\n " );
Delay_ms(1500);
}
while(1);
result on terminal
Temp= 1023 pts soit 5.009 Volts
Temp= 772 pts soit 3.780 Volts
Temp= 555 pts soit 2.717 Volts
Temp= 464 pts soit 2.272 Volts
Temp= 346 pts soit 1.694 Volts
Temp= 280 pts soit 1.371 Volts
Temp= 205 pts soit 1.004 Volts
Temp= 93 pts soit 0.455 Volts
Temp= 28 pts soit 0.137 Volts
Temp= 70 pts soit 0.343 Volts
Temp= 69 pts soit 0.338 Volts */
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?