tigerclaw
Newbie level 6
Hi i am having trouble reading from two analog channels from my PIC18F2550.
I am new to PIC programming.
I know how to set up ADC on one channel and read from it but when i try do the same thing on the second channel i get 0 zero result.
my code:
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <stdlib.h>
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, HS
#pragma config WDT = OFF //Disable watchdog timer
#define DIGIT1 LATCbits.LATC0
#define DIGIT2 LATCbits.LATC6
#define DIGIT3 LATCbits.LATC7
#define revdigit LATCbits.LATC1
//
// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char DisplayV(unsigned char noV)
{
unsigned char PatternV;
unsigned char SEGMENTV[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F};
PatternV = SEGMENTV[noV]; // Pattern to return
return (PatternV);
}
//
// Start of MAIN Program
//
void main()
{
unsigned char Msd, Midle, Lsd, tmr;
unsigned long int Cnt, rev;
unsigned char delay = 1;
TRISC = 0; // PORTC are outputs
TRISB = 0; // PORTB are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
DIGIT3 = 0; // Disable digit 3
revdigit = 0;
for(;
{
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_0_TAD,
ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS , 13 );
ConvertADC();
while(BusyADC());
Cnt = ReadADC(); //5V = 1022 0.005v = 1 vstah mezi rychlosti je 3.785(maxV 270km/h)
Cnt = Cnt / 3.785;
if(Cnt > 99) goto xxx;
else if (Cnt < 100) goto xx;
else if (Cnt < 10) goto x;// ukazuje se mi stejne nula pred jednomistnim cilsem, musim mrknout na Lsd
xxx:
Msd = Cnt / 100; // MSD digit
PORTB = DisplayV(Msd); // Send to PORTB
DIGIT1 = 1; // Enable digit 1
Delay1KTCYx(delay); // Wait a while
DIGIT1 = 0; // Disable digit 1
xx:
Midle = Cnt % 100;
Midle = Midle / 10;
PORTB = DisplayV(Midle);
DIGIT2 = 1;
Delay1KTCYx(delay);
DIGIT2 = 0;
Midle = Midle * 10;
x:
Lsd = Cnt % 10; // LSD digit
PORTB = DisplayV(Lsd); // Send to PORTB
DIGIT3 = 1; // Enable digit 2
Delay1KTCYx(delay); // Wait a while
DIGIT3 = 0; // Disable digit 2
CloseADC();
// Engne Rev
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_0_TAD,
ADC_CH1 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS , 13 );
//SetChanADC( ADC_CH1 );
ConvertADC();
while(BusyADC());
rev = ReadADC();
rev = rev * 4.892;
//rev = rev + 2000;
if (rev >= 3000)
{
PORTB = 0b01111111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2700)
{
PORTB = 0b00111111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2400)
{
PORTB = 0b00011111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2100)
{
PORTB = 0b00001111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1800)
{
PORTB = 0b00000111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1600)
{
PORTB = 0b00000011;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1300)
{
PORTB = 0b00000001;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1000)
{
PORTB = 0b00000000;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
CloseADC();
}
}
I know i am doing something wrong But WHAT???
Thanks for the help and cheers
I am new to PIC programming.
I know how to set up ADC on one channel and read from it but when i try do the same thing on the second channel i get 0 zero result.
my code:
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <stdlib.h>
#pragma config FOSC = INTOSCIO_EC //Internal oscillator, HS
#pragma config WDT = OFF //Disable watchdog timer
#define DIGIT1 LATCbits.LATC0
#define DIGIT2 LATCbits.LATC6
#define DIGIT3 LATCbits.LATC7
#define revdigit LATCbits.LATC1
//
// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char DisplayV(unsigned char noV)
{
unsigned char PatternV;
unsigned char SEGMENTV[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F};
PatternV = SEGMENTV[noV]; // Pattern to return
return (PatternV);
}
//
// Start of MAIN Program
//
void main()
{
unsigned char Msd, Midle, Lsd, tmr;
unsigned long int Cnt, rev;
unsigned char delay = 1;
TRISC = 0; // PORTC are outputs
TRISB = 0; // PORTB are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
DIGIT3 = 0; // Disable digit 3
revdigit = 0;
for(;
{
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_0_TAD,
ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS , 13 );
ConvertADC();
while(BusyADC());
Cnt = ReadADC(); //5V = 1022 0.005v = 1 vstah mezi rychlosti je 3.785(maxV 270km/h)
Cnt = Cnt / 3.785;
if(Cnt > 99) goto xxx;
else if (Cnt < 100) goto xx;
else if (Cnt < 10) goto x;// ukazuje se mi stejne nula pred jednomistnim cilsem, musim mrknout na Lsd
xxx:
Msd = Cnt / 100; // MSD digit
PORTB = DisplayV(Msd); // Send to PORTB
DIGIT1 = 1; // Enable digit 1
Delay1KTCYx(delay); // Wait a while
DIGIT1 = 0; // Disable digit 1
xx:
Midle = Cnt % 100;
Midle = Midle / 10;
PORTB = DisplayV(Midle);
DIGIT2 = 1;
Delay1KTCYx(delay);
DIGIT2 = 0;
Midle = Midle * 10;
x:
Lsd = Cnt % 10; // LSD digit
PORTB = DisplayV(Lsd); // Send to PORTB
DIGIT3 = 1; // Enable digit 2
Delay1KTCYx(delay); // Wait a while
DIGIT3 = 0; // Disable digit 2
CloseADC();
// Engne Rev
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_0_TAD,
ADC_CH1 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS , 13 );
//SetChanADC( ADC_CH1 );
ConvertADC();
while(BusyADC());
rev = ReadADC();
rev = rev * 4.892;
//rev = rev + 2000;
if (rev >= 3000)
{
PORTB = 0b01111111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2700)
{
PORTB = 0b00111111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2400)
{
PORTB = 0b00011111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 2100)
{
PORTB = 0b00001111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1800)
{
PORTB = 0b00000111;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1600)
{
PORTB = 0b00000011;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1300)
{
PORTB = 0b00000001;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
else if (rev >= 1000)
{
PORTB = 0b00000000;
revdigit = 1;
Delay1KTCYx(delay);
revdigit = 0;
}
CloseADC();
}
}
I know i am doing something wrong But WHAT???
Thanks for the help and cheers