Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

128x64 GLCD sprintf problem PIC18F45K22

Status
Not open for further replies.
eTech,

I have attached all the files related to project.

Cheers

Hi

I haven't been able to compile the code (I normally use the C30 but switched to the XC8 to try to help).
There's some code changes that have to be made to migrate the code from C18 to xc8.
I haven't much experience with this migration...so struggling some..
At the moment I'm getting "Bad storage class" errors from GLCD.h.
I believe is related to the "rom" qualifers used in the code..

sorry...

eT
 
Code:
#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);
	}

}

@Ian, Thanks for your help....
photo.JPGyou mean, there is no need of math.h ? and which float to string function is good (ftoa)?

This is the final main code I am using. This time the problem is with the GLCD update. I can see the PORTA LED's changing according to ADC change but the GLCD got stuck.

I have also attached snap of GLCD.
 
Thanks Ian..

Carry on with your work.If you get time, please have a look..
Have a nice evening !!!
Cheers
 
I managed to get working.

Could you please help me with the PutChar32x20 function. I want to print the value with big font. This(PutChar32x20) is able to print the numeric value (with garbage) hoe to modify to display or alphabet and special characters. It would be great if you recommend any website to generate the code for big font.

I have attached the photograph showing the numeric value with noise(garbage).
fphoto (6).JPG

Cheers
 
Code:
void PutChar32x20 (unsigned char data)
{
unsigned char i,d = 0x0;
unsigned char j,k,count = 0;
unsigned char test;
unsigned char* arr_test[20];
unsigned char data_test;

data_test = data - 48;
for (i= 0; i <20;i++)
{
	arr_test[i] = & num_data[data_test][i];
}

for (k =0;k<6;k++)
{
	if (k ==5) // It goes into loop when K becomes 5
	{
		for (i=0;i<4;i++)
		{
			WritePosition();
			GLCD_Write_Data(0xff);
			YPos+=8;
		}
	}
	else  // enters into loop for k=1,2,3,4,6
	{
		for (j=0;j<4;j++) // loops until 3
		{
			for (i=0;i<4;i++)
			{
				WritePosition();
				d = *arr_test[count];
				GLCD_Write_Data(d);
				XPos++;
			}
			YPos+=8;
			XPos-=8;
			count++;
		}
	}
	YPos-=32;
	if (k==5)XPos+=1;
	else XPos+=4;
}

Nop();

	if(data<32)
	{
		switch(data)
		{
			case 13:
				XPos=0;
			case 10:
				XPos=0;
				YPos+=8;
				YPos=YPos&63;
		}
		WritePosition();
	}
	else
	{
		if (data > 44 || data == 32)
		{
			for (k = 0;k<6;k++)
			{
				if (k == 5)
				{
					for (i=0;i<4;i++)
					{
						WritePosition();
						GLCD_Write_Data(0xff);
						YPos+=8;
					}
				}
				else
				{
					for (j = 0;j<4;j++)
					{	
						for (i = 0;i<4;i++)
							{
								WritePosition();
								if (data!=46)
								{
									if (data == 32)d = num_data1[1][count];
									else
										{
											if (data == 45)
											{
												 d = num_data1[13][count]; 
											}
											else 
											{
												d = num_data1[data-48][count];
												//test = num_data1[count][2];
												//d = *arr_test[count];
											}
										}
								}
								else
									d = num_data1[10][count];
								GLCD_Write_Data(d);
								XPos++;
							}
							count++;
							YPos+=8;
							XPos-=4;
					}
				}
				YPos-=32;
				if (k==5)XPos+=1;
				else XPos+=4;
			}
			if (data ==46)
			{
				XPos -=8;
			}
		}
	}
	
}

PuChar32x20 function
 
It looks like you are printing the number too close to the edge of the screen.....

Please can you stop posting in threads all over the place... I just found a posting in another KS0107 thread... I'm going to delete it
 
Thanks Ian.. and I do apologize for the post.

Could you help me to change my this function to print alphabet please ?

Where can I find the bigger font and what changes need to do ? I have to finish this project by end of march.This is the last task. I need a bigger font.

Cheers
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top