#include <p18cxxx.h>
// PIC18F25K22 Configuration Bit Settings
// CONFIG1H
#pragma config FOSC = INTIO67 // Oscillator (LP,XT,HSHP,HSMP,RC,RCIO6,ECHP,
//#pragma config WDT = OFF // ECHPIO6,INTIO67,INTIO7,ECMPIO6,ECLP,ECLPIO6)
//#pragma config PLLCFG = OFF // 4X PLL Enable
//#pragma config PRICLKEN = ON // Primary clock enable
//#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable
//#pragma config IESO = OFF // Internal/External Oscillator Switchover
#define USE_OR_MASKS
#define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)
void ftoa(float value, char *string) ;
void delay();
void delay_d();
float read_battery_voltage(void);
#include<delays.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "image_logo.h"
#include "GLCD.h"
#include <adc.h>
#include "sys_init.h"
#include <math.h>
#include <string.h>
unsigned int ADCResult=0;
float tx_voltage;
float voltage;
void ftoa(float value, char *string)
{
if (value < 0) {
*string++ = '-';
value = -value;
}
sprintf(string, (const far rom char *) "%lu.%03u",
(long) value,
(int) ((value - (long) value) * 1000. + 0.5));
}
// convert float to string one decimal digit at a time
// assumes float is < 65536 and ARRAYSIZE is big enough
// problem: it truncates numbers at size without rounding
// str is a char array to hold the result, float is the number to convert
// size is the number of decimal digits you want
void FloatToStringNew(char *str, float f, char size)
{
char pos; // position in string
char len; // length of decimal part of result
char* curr; // temp holder for next digit
int value; // decimal digit(s) to convert
pos = 0; // initialize pos, just to be sure
value = (int)f; // truncate the floating point number
itoa(value,str); // this is kinda dangerous depending on the length of str
// now str array has the digits before the decimal
if (f < 0 ) // handle negative numbers
{
f *= -1;
value *= -1;
}
len = strlen(str); // find out how big the integer part was
pos = len; // position the pointer to the end of the integer part
str[pos++] = '.'; // add decimal point to string
while(pos < (size + len + 1) ) // process remaining digits
{
f = f - (float)value; // hack off the whole part of the number
f *= 10; // move next digit over
value = (int)f; // get next digit
itoa(value, curr); // convert digit to string
str[pos++] = *curr; // add digit to result string and increment pointer
}
}
void ftostring (void)
{
float fval;
signed int ival;
char string[10];
char i;
signed int max_int = 32767;
signed int min_int = -32768;
fval = 1234.56;
ival = 0;
for (i = 0; i < 10; i++) string[i] = '\0';
if (fval > max_int) ; // overflow - do something
if (fval < min_int) ; // underflow - do something
ival = fval + 0.5;
sprintf(string, "%+1.1d", ival);
ival = 0;
}
void delay()
{
int i;
for (i=0;i<200;i++)
{
Nop();
}
}
void main ()
{
unsigned char j,x,len;
int i;
i = 0;
sys_init();
loadimg(&toplogo_bmp[0], 1024);
delay();
ClearScreen();
while(1)
{
read_battery_voltage();
PutMessage((rom char*)"\x16\x2\x19 ADC Convesrion ends");
delay();
delay();
}
}
/*********************************************************************
* File: adc.c
* Use: Holds battery display routines. Measures actual battery voltage
and indicator is then based on a multimetric reading.
**********************************************************************/
void delay_d()
{
int i;
for (i=0;i<50000;i++)
{
Nop();
}
}
/************************************************************************
* Function: read_battery_voltage
* Arguments: None
* Use: Acts as a multimeter to measure input battery voltage
************************************************************************/
float read_battery_voltage(void)
{
int v;
int i;
unsigned char p;
char var = 2;
char* str;
int buff[10];
unsigned long temp = 108;
unsigned char buffer[11];
char buf[8];
unsigned int s[20];
unsigned int *ptr;
int *ptr1;
char snum[5];
//char buff[24];
TRISA = 0;
OSCCON = 0x72; //8MHz clock
while(!OSCCONbits.IOFS); //Wait for OSC to become stable
ADCPin = 1; //AN0 = Input
ADCON0 = 0x00; //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
//BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
ADCON1 = 0x0E; //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
//BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
ADCON2 = 0b00001100; //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
//BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
ADCON0bits.ADON = 1; //Turn on the A/D module
while(1)
{
for(i=0;i<16;i++)
{
Delay10TCYx(20);
ConvertADC();
while(BusyADC());
ADCResult = ReadADC();
SetPos(64,32);
voltage = (ADCResult*5.0)/1024;
PORTA = voltage;
Delay10TCYx( 100 ); // Delay for 50TCY (precaution)
}
ftoa(voltage,str);
while(*str++ !=0)
PutChar(*str);
Delay10TCYx(2000);
}
}