tigerclaw
Newbie level 6
dose this code seem ok to you guys?
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <stdlib.h>
#pragma config FOSC = INTOSCIO_EC //Internal oscillator,
#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, rev;
unsigned long int Cnt;
unsigned char delay = 200;
TRISC = 0; // PORTC are outputs
TRISB = 0; // PORTC are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
DIGIT3 = 0; // Disable digit 3
revdigit = 0;
LATCbits.LATC0 = 0;
for(;
{
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_2_TAD,
ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_REF_VDD_VREFMINUS , 2 );
ConvertADC();
while(BusyADC());
Cnt = ReadADC(); //5V = 1022 0.005v = 1 vstah mezi rychlosti je 3.785(maxV 270km/h)
Cnt = Cnt / 3.785;
//Cnt = 129;
//
//OpenTimer0( TIMER_INT_OFF &
//T0_8BIT &
//T0_SOURCE_INT &
//T0_PS_1_256 );
//
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
//
//DIGIT1 = 1;
//DIGIT2 = 1;
//DIGIT3 = 1;
//PORTB = 0x7f;
//Delay1KTCYx(200);
//DIGIT1 = 0;
//DIGIT2 = 0;
//DIGIT3 = 0;
CloseADC();
// Engne Rev
//ConvertADC();
//while(BusyADC());
//rev = ReadADC();
//rev = rev * 4.892;
// if(rev >= 1000);
//
// PORTB = 0x0FF;
// PORTC = 0x0D;
// revdigit = 1;
// Delay1KTCYx(delay);
// revdigit = 0;
// else if (rev > 1300) break;
//
// else if (rev > 1600) break;
}
}
It should display a value 0-270 on a 3x 7seg displays(Anode) connected directly to port B pins, based on the voltage on the analog input chanel 0. But the displays show two zeros, i think the problem is the ADC conversion bicose when i specify the value of Cnt directly it shows properly on the displays.
Any sugestions?
---------- Post added at 19:21 ---------- Previous post was at 19:18 ----------
Oh and again in Proteus it works perfectly.
#include <p18f2550.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <stdlib.h>
#pragma config FOSC = INTOSCIO_EC //Internal oscillator,
#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, rev;
unsigned long int Cnt;
unsigned char delay = 200;
TRISC = 0; // PORTC are outputs
TRISB = 0; // PORTC are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
DIGIT3 = 0; // Disable digit 3
revdigit = 0;
LATCbits.LATC0 = 0;
for(;
{
OpenADC( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_2_TAD,
ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_REF_VDD_VREFMINUS , 2 );
ConvertADC();
while(BusyADC());
Cnt = ReadADC(); //5V = 1022 0.005v = 1 vstah mezi rychlosti je 3.785(maxV 270km/h)
Cnt = Cnt / 3.785;
//Cnt = 129;
//
//OpenTimer0( TIMER_INT_OFF &
//T0_8BIT &
//T0_SOURCE_INT &
//T0_PS_1_256 );
//
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
//
//DIGIT1 = 1;
//DIGIT2 = 1;
//DIGIT3 = 1;
//PORTB = 0x7f;
//Delay1KTCYx(200);
//DIGIT1 = 0;
//DIGIT2 = 0;
//DIGIT3 = 0;
CloseADC();
// Engne Rev
//ConvertADC();
//while(BusyADC());
//rev = ReadADC();
//rev = rev * 4.892;
// if(rev >= 1000);
//
// PORTB = 0x0FF;
// PORTC = 0x0D;
// revdigit = 1;
// Delay1KTCYx(delay);
// revdigit = 0;
// else if (rev > 1300) break;
//
// else if (rev > 1600) break;
}
}
It should display a value 0-270 on a 3x 7seg displays(Anode) connected directly to port B pins, based on the voltage on the analog input chanel 0. But the displays show two zeros, i think the problem is the ADC conversion bicose when i specify the value of Cnt directly it shows properly on the displays.
Any sugestions?
---------- Post added at 19:21 ---------- Previous post was at 19:18 ----------
Oh and again in Proteus it works perfectly.