Two ADC codes merge to one code

Status
Not open for further replies.

Muhammad Saad

New Member
hi
I just want to know how to manage two different codes to one code, as i have one code for digital voltmeter and second for voltage limits. but I want to make both of them working on same controller. the code is pasted below:
code # 1:
//Function Prototypes
void floattostring(double FP_NUM);
double FP_NUM, volt;
char string[6];
//Sub Function
void floattostring(double FP_NUM) {
double fpnumber;
long int befdec, aftdec;
fpnumber = FP_NUM;
befdec = fpnumber; // Fractional part is truncated
// 12.163456 becomes 12
aftdec = fpnumber * 100; // 12.163456 becomes 1216
aftdec = aftdec - (befdec * 100); // 1216 - 1200 = 16
}
void main() {
TRISA = 0b00000001;
PORTA = 0b00000000;
TRISC = 0b00000111;
PORTC = 0b00000000;
TRISD = 0b00000000;
PORTD = 0b00000000;
ADCON1 = 0b10001110;
CMCON = 0b00000111;
while(1) {
volt = ADC_Read(0);
volt = volt * 0.0305498981670061;
volt = volt * 0.9612303748798462;
floattostring(volt);
if(volt > 27.0) { // Change value here
PORTD.F0 = 1;
}
else if(volt < 27.0) { // Change value here
PORTD.F0 = 0;
}
if(volt > 9.25) { // Change value here
PORTD.F4 = 0;
}
else if(volt < 9.25) { // Change value here
PORTD.F4 = 1;
}
}
}
code # 2
unsigned int adc_rd0,tlong;
unsigned short mask(int num)
{
switch (num)
{
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xD8;
case 8 : return 0x80;
case 9 : return 0x90;
case 10: return 0x40;
case 11: return 0x79;
case 12: return 0x24;
case 13: return 0x30;
case 14: return 0x19;
case 15: return 0x12;
case 16: return 0x02;
case 17: return 0x78;
case 18: return 0x00;
case 19: return 0x10;
}
}
unsigned short shifter, portb_index;
unsigned int digit, number;
unsigned short portb_array[4];
void interrupt()
{
PORTC = 0;
PORTB = portb_array[portb_index];
PORTC = shifter;
shifter <<= 1;
if(shifter > 8u)
shifter = 1;
portb_index ++ ;
if (portb_index > 3u)
portb_index = 0;
TMR0 = 0;
INTCON = 0x20;
}
void display()
{
digit = number % 10u;
portb_array[0] = mask(digit);
digit = (number / 10u) % 10u;
portb_array[1] = mask(digit);
digit = (number / 100u) % 10u+10;
portb_array[2] = mask(digit);
digit = number / 1000u;
portb_array[3] = mask(digit);
}
void main()
{
// port initialization...
TRISB = 0x00; // Set PORTB direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISC= 0x00; // Set PORTB direction to be output
PORTC = 0x00;
TRISA = 0xFF; // all input
digit = 0;
portb_index = 0;
shifter = 1;
number = 0; //initial value;
// tiemr0 settings...
OPTION_REG = 0x80; // Set timer TMR0;
TMR0 = 0;
INTCON = 0xA0; // Disable interrupt PEIE,INTE,RBIE,T0IE
while(1)
{
// Read Battery voltage
ADCON0 = 0b00000001;
adc_rd0 = ADC_Read(0); // A/D conversion. Pin RA2 is an input.
tlong = (float)adc_rd0 *1.96078431372549; // Convert the result in millivolts
number = tlong;
display();
}//Endless loop;
}//End.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…