SneaKSz
Member
Hello , I've can read successfully the value of a ADC input and also write data to the LCD, only the binary to decimal is the part where I am getting mad.
The delay routine works fine , its only the binary to decimal routine : the while (1) and Conversion ( ADCV). The LCD_Char() routine works perfect , I think the conversion() routine doesnt work , I want to get each decimal of the 10 bit ADC value by using INT's and store them in Dx , and then use that number to locate the correct hex number in the array lcdc . Then I send this number to LCD_Char().
When I use this program I get a weird characterson the output that doesn't change when I change the voltage on the ana input.
Can someone spot this minor mistake ?
Thanks in advance !
Code:
#include <p18f14k50.h>
#include <xlcd.h>
#include <delays.h>
//#include <usart.h>
//#include <htc.h>
//#include <pic18f1xk50.h>
# include <string.h>
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = ON
#pragma config BOREN = ON
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 1
#pragma config MCLRE = ON
#pragma config HFOFST = ON
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config BBSIZ = OFF
#pragma config XINST = OFF
#pragma config CP0 = ON
#pragma config CPB = ON
#pragma config CPD = ON
#pragma config WRT1= ON
#pragma config WRTB = ON
#pragma config WRTC = ON
#pragma config WRTD = ON
#pragma config EBTRB = ON
#define LCD_PORT LATC // ;set constant LEDPORT = 'PORTB'
#define LCD_TRIS TRISC // ;set constant for TRIS register
//;LCD handshake lines
#define LCD_RS LATCbits.LATC4
#define LCD_RW LATCbits.LATC6
#define LCD_E LATCbits.LATC7
unsigned char D0;// low decimal of the decimal value
unsigned char D1;
unsigned char D2;
unsigned char D3;
unsigned char lcdc[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
// array with correct hex code for 0,1,2,3,4,5,6,7,8,9
unsigned int H; // high byte ADRESH
unsigned int L;
void Conversion( float );
void Output();
void LCD_Init();
void ADC_Init();
void ADC_Go();
void LCD_Char(unsigned char);
void LCD_Cmd(unsigned char);
void writeEE(unsigned char addrH,unsigned char addrL,unsigned char data);
void main(void)
{
unsigned long ad_val;
float ADCV;
OSCCON =62; //set internal clock to 4MHz
LCD_TRIS = 0; // set PortC all outputs
LCD_PORT = 0; //all outputs are low
TRISBbits.TRISB4=1;// input
ANSELHbits.ANS10=1;////Set RB4 to analog (ANS10)
Delay10KTCYx(30); // wait 100ms ,t = 333ns => 100ms = 300.000 cycles
LCD_Init(); // init the LCD
ADC_Init(); // init the ADC
while(1)
{
ADC_Go(); // get binary value
ad_val = H;// store high byte
ad_val = (H << 8 )| L; // add both bytes together
ADCV = (float) (ad_val )*(4.8828125); // get a correct value with reference to 5V.(5000/1024)
Conversion ( ADCV); // get each decimal of ADCV
Output();
// LCD_Char(0x56);'V'
}
}
void Conversion ( float value)
{
unsigned int test = value; // float => int
D3=value/1000; // high decimal
D2 = (test - (D3*1000.0))/100;// secong highest decimal
D1= (test) - ((D3*1000.0)+(D2*100.0));
D1=D1/10;//second lowest
D0= (test) - ((D3*1000.0)+(D2*100.0)+(D1*10)); // lowest decimal
}
void Output()
{
LCD_Char(lcdc[D3]); // get the correct LCD char end send it to the LCD
LCD_Char(lcdc[D2]);
LCD_Char(lcdc[D1]);
LCD_Char(lcdc[D0]);
}
void ADC_Init()
{
ADCON2 = 0b10101110 ;//right justify, Frc/64 , 12Tad
ADCON1=0b00000000 ;//ADC ref = Vdd,Vss boven- en onderste venster
// LATBbits.LATB4=1;//Set RB4 to analog (ANS10)
ADCON0 = 0b00101001 ;//AN10, ADC on
}
void ADC_Go()
{
ADCON0bits.GO =1 ;//Start conversion
//ADCPoll:
while(ADCON0bits.DONE==1); //Is conversion done?
// ; Result is complete - store 2 MSbits in
H=ADRESH;
L=ADRESL;
}
void writeEE(unsigned char addrH,unsigned char addrL,unsigned char data)
{
EEADRH = addrH;
EEADR =addrL;
EEDATA = data;
//following as per spec. sheet
EECON1bits.EEPGD =0;
EECON1bits.CFGS =0;
EECON1bits.WREN =1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
}
The delay routine works fine , its only the binary to decimal routine : the while (1) and Conversion ( ADCV). The LCD_Char() routine works perfect , I think the conversion() routine doesnt work , I want to get each decimal of the 10 bit ADC value by using INT's and store them in Dx , and then use that number to locate the correct hex number in the array lcdc . Then I send this number to LCD_Char().
When I use this program I get a weird characterson the output that doesn't change when I change the voltage on the ana input.
Can someone spot this minor mistake ?
Thanks in advance !