CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help with LCD displaying

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mcataldo



Joined: 04 Jul 2010
Posts: 11
Location: Santiago, Chile

View user's profile Send private message

Help with LCD displaying
PostPosted: Sun Oct 31, 2010 1:55 pm     Reply with quote

Hi everyone, I'm working in a power and energy meter what uses a LCD to display these variables and power factor.
I'm having a problem, because I think display routine is right, but in Proteus and in practice LCD doesn't show any character. I'm using LCD library included in CCS.
Please, help me, I attach code and Proteus schematic image.

Thanks.

Code:

#include <18F452.h>
#device adc=8
#use delay(clock=4000000)
#include <math.h>
#include <lcd.c>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //LOW speed Osc (<= 4mhz)
#FUSES NOCPD                    //No EE protection
#FUSES NOPUT                    //Power up timer
#FUSES NOBROWNOUT               //Reset when Brownout detected

float corriente;
float en_inst;
float energia = 0;
float fp;
float pot_inst[];
float potencia;
float voltaje;

int dt;
int fase;
int i = 0;
int valor1;
int valor2;

short estado = 0;
short flag_1 = 0;
short flag_2 = 0;

#INT_EXT1  // Interrupción de cruce por cero de señal de voltaje
void EXT1_isr()
{         
   if(estado == 0)
   { 
      set_timer0(0);
      estado = 1;
   }
}

#INT_EXT2 // Interrupción de cruce por cero de señal de corriente
void EXT2_isr()
{
   if(estado == 1)
   {
      dt = get_timer0(); // Tiempo de desfase entre voltaje y corriente
      flag_1 = 1;
      estado = 0;
   }
}

#INT_TIMER1 // Interrupción para muestreo de valores de voltaje y corriente
void muestreo()
{
   set_timer1(64536);
   flag_2 = 1;
}

void main()
{
   SET_TRIS_A(0b00011);         //Configuración de puertos         
   SET_TRIS_B(0b00111);
   SET_TRIS_D(0x00);
     
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_INTERNAL);
     
   ext_int_edge(INT_EXT1,L_TO_H);
   ext_int_edge(INT_EXT2,L_TO_H);
     
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL);

   enable_interrupts(GLOBAL);   
   enable_interrupts(INT_EXT1);
   enable_interrupts(INT_EXT2);
   enable_interrupts(INT_TIMER1);
   
   set_timer1(64536);

   if(flag_1 == 1)
   {
      flag_1 = 0;
      if(dt <= 10000)
      {
         fase = 0.018*dt; // Carga capacitiva
      }
      else
      {
         fase = 0.018*(20000-dt); // Carga inductiva
      }
      fp = cos(fase); // Cálculo de factor de potencia
   }
   
   if(flag_2 == 1)
   { 
      i++;
      flag_2 = 0;
      set_adc_channel(0);
      valor1 = read_adc();         
      delay_us(20);
      voltaje = (float)valor1/51;  // Valor de voltaje
     
      set_adc_channel(1);
      valor2 = read_adc();       
      delay_us(20);
      corriente = (float)valor2/51; // Valor de corriente
      pot_inst[i] = voltaje*corriente; // Cálculo de potencia instantánea
     
      if(i == 20)
      {
         for (i=1;i<=20;i++)
    {
            potencia += 0.05*pot_inst[i]; // Cálculo de potencia RMS
    }
         en_inst = potencia/3000; // Cálculo de energía instantánea
         energia = energia + en_inst; // Cálculo de energía acumulada
         i = 0;
      }
   }

   lcd_init();   // Visualización en display de variables
   lcd_putc("\f");
   lcd_gotoxy(1,1);
   printf(lcd_putc,"Pot:%1.1f",potencia);
   lcd_putc("W");
   printf(lcd_putc,"FP:%1.2f",fp);
   lcd_gotoxy(3,2);
   printf(lcd_putc,"En:%3.1f",energia);
   lcd_putc("Wm");
   delay_ms(500);
   while(1)
   {
   }
}


PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 31, 2010 2:51 pm     Reply with quote

Quote:
float pot_inst[];

This array doesn't have any storage locations allocated to it.
If you want 20 elements in the array, then edit the line above so it
declares the 20 elements.


Quote:
for (i=1;i<=20;i++)
{
potencia += 0.05*pot_inst[i]; // Cálculo de potencia RMS
}

Arrays start with an index of 0. For an array of 20 elements, the indexes
will go from 0 to 19. Edit the for() statement above so the first index is 0
and so that it checks for "less than" 20 (not less-than or equal to 20).
Then it will go through elements 0 to 19, which is what you need.

Quote:

SET_TRIS_A(0b00011); //Configuración de puertos
SET_TRIS_B(0b00111);
SET_TRIS_D(0x00);

You don't have to set the TRIS. The compiler will do it for you, provided
that you use CCS functions and library routines.

Quote:
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);

The internal A/D clock is not recommended by the PIC data sheet.
Look in the A/D section of the data sheet, in the section on selecting the
A/D clock. It lists the correct clock divisor to use for 4 MHz. Then look
in the 18F452.h file to see the list of constants used by the setup_adc()
routine. Choose the correct constant for your clock divisor.

Quote:
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);

These two lines are not needed. The PSP and SPI modules are disabled
upon power-on reset of the PIC. You don't need to disable them again.
Also, the setup_spi() line above doesn't disable the SPI port. It actually
enables it. Delete those two lines.

There might be other problems. The major problems are the lack of
declared space for the array, and the for() statement.
mcataldo



Joined: 04 Jul 2010
Posts: 11
Location: Santiago, Chile

View user's profile Send private message

PostPosted: Mon Nov 01, 2010 11:16 pm     Reply with quote

Hi, thanks for help. I've made changes you proposed, but I can't get LCD shows something yet; when running simulation on proteus, LCD pins don't show a color (red or blue) indicating 0 or 1, it's like they have no signal.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 01, 2010 11:36 pm     Reply with quote

The CCS lcd driver expects the PIC pins to be connected to the LCD pins
as shown below. What does your schematic show ?
Code:

//     D0  enable
//     D1  rs
//     D2  rw
//     D4  D4
//     D5  D5
//     D6  D6
//     D7  D7
//
mcataldo



Joined: 04 Jul 2010
Posts: 11
Location: Santiago, Chile

View user's profile Send private message

PostPosted: Tue Nov 02, 2010 12:14 am     Reply with quote

Ok, I changed pins configuration as you show in lcd.c and it worked so good.
Thank you very much PCM programmer.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group