LCD shows funny character when timer is on

Status
Not open for further replies.
Here is the code:
Code:
/*******************************************************************************
*                   Program for 'Battery Charger, 12V/20A and others'          *
*                          Compiler: MicroC Pro for PIC v.5.6.1                *
*                         Program Written by Engr. Mithun K. Das               *
*                        e-mail: mithun060@gmail.com; 01722448270              *
*                          MCU: PIC16F690; X-Tal:8MHz(internal)                *
*                                     23-Dec,2014                              *
*******************************************************************************/
// LCD module connections
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RA4_bit;
sbit LCD_D7 at RA5_bit;

sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA5_bit;
// End LCD module connections

float Battery = 12.00;
int Current = 0, Current_Target = 0;
short Charge = 0;
unsigned int adc_rd = 0,cnt=0,Clock_target=35,Clock_target1 = 35;
unsigned int Temp, POT;
int Charging_Data = 0,miains_delay_cnt=0;
unsigned int count_Display=0;
short mode = 0;


#define       Mains_Relay_ON             PORTC = (PORTC & 0b11111011) | (1<<2)
#define       Mains_Relay_OFF            PORTC = (PORTC & 0b11111011) | (0<<2)
#define       Tapping_Relay_ON           PORTB = (PORTB & 0b11101111) | (1<<4)
#define       Tapping_Relay_OFF          PORTB = (PORTB & 0b11101111) | (0<<4)
#define       Buzzer_ON                  PORTC = (PORTC & 0b11111101) | (1<<1)
#define       Buzzer_OFF                 PORTC = (PORTC & 0b11111101) | (0<<1)
#define       Cooling_Fan_ON             PORTC = (PORTC & 0b11111110)|(1)
#define       Cooling_Fan_OFF            PORTC = (PORTC & 0b11111110)|(0)

#define       Charing_Mode               RB6_bit
#define       ON                         1
#define       OFF                        0


void Get_CT_Reading(void);
void Get_Battery(void);
void Get_LM35_Reading(void);
void Get_POT_Variable(void);
void Charging(void);

void Lcd_COut(char row, char col, const char *cptr)
{
      char chr = 0;             //first, it is used as empty string
      Lcd_Out(row, col, &chr);  //nothing to write but set position.
      for ( ; chr = *cptr ; ++cptr ) Lcd_Chr_CP(chr); //out in loop
}

void interrupt()
{
   if(T0IF_bit)
   {
       T0IF_bit = 0;//clear flag
       cnt++;
       TMR0 = 55;
       if(cnt>Clock_target1) PORTB = (PORTB & 0b01111111) | (1<<7);

   }
   if(INTF_bit)
   {
       INTF_bit = 0;//clear flag
       PORTB = (PORTB & 0b01111111) | (0<<7);
       cnt = 0;//clear count
       if(Temp>35)Cooling_Fan_ON;
   }

}

void InitTimer0()
{
     OPTION_REG  = 0x88;
     TMR0 = 50;
     INTCON  = 0xA0;
}

void main()
{
  TRISA0_bit = 1;//set as input
  TRISA1_bit = 1;//set as input
  TRISA2_bit = 1;//set as input
  TRISC0_bit = 0;//set as output
  TRISC1_bit = 0;//set as output
  TRISC2_bit = 0;//set as output
  TRISC7_bit = 1;//set as input

  TRISB4_bit = 0;//set as output
  TRISB5_bit = 1;//set as input
  TRISB6_bit = 1;//set as input
  TRISB7_bit = 0;//set as output
  ANSEL = 0x03;
  ANSELH = 0x0A;
  ADCON1 = 0x00;//max frequency
  C1ON_bit = 0;
  C2ON_bit = 0;// comparator off

  Buzzer_ON;
  //Mains_Relay = OFF;
  Mains_Relay_OFF;
  Tapping_Relay_OFF;
  



  Lcd_Init();// LCD initialization
  Lcd_Cmd(_LCD_CLEAR);//clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);// cursor off
  Delay_ms(200);
  Buzzer_OFF;
  Lcd_Out(1,1,"   WELCOME TO   ");
  Lcd_Out(2,1,"    M'S LAB     ");
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);//clear LCD
  
  InitTimer0();//Timer 0 initialization

  INTE_bit = 1;//enavle hardware interrupt
  INTEDG_bit = 0;// Falling edge
  INTF_bit = 0;//clear flag

  Buzzer_OFF;
  Get_Battery(void);
  Get_CT_Reading(void);
  while(1)
  {

        Get_Battery(void);
        if(Battery>7.00 && Battery<17.8)
        {
            //Mains_Relay = ON;
            Mains_Relay_ON;
            if(miains_delay_cnt<5)miains_delay_cnt++;
            if(miains_delay_cnt>2)
            {
                Charging(void);
            }
            else
            {
                Tapping_Relay_OFF;//select 220V Tap
                count_Display = 0;
                if(TMR0<220)Lcd_COut(2,1," INITIALIZING...");
            }
        }
        else
        {
           miains_delay_cnt = 0;//clear count
           if(Battery<17.8)
           {
               if(TMR0<220)Lcd_COut(2,1,"NO BATTERY FOUND");
               //Mains_Relay = OFF;
               Mains_Relay_OFF;
               Tapping_Relay_OFF;
               Delay_ms(1000);
           }
           else
           {
               Tapping_Relay_OFF;
               //Mains_Relay = OFF;
               Mains_Relay_OFF;
               if(TMR0<220)Lcd_COut(2,1,"BATT._OVER SIZE");
               Delay_ms(1000);
           }
        }
        Get_CT_Reading(void);
        Get_LM35_Reading(void);
        count_Display++;
        if(count_Display>0 && count_Display<2) mode = 0;
        else if(count_Display>=2 && count_Display<4) mode = 1;
        else if(count_Display>=4 && count_Display<6) mode = 2;
        else count_Display = 0;


  }//while
}//mains

void Charging(void)
{

    if(Charing_Mode)
    {
         if(Battery>14.5) Charge = 1;//charging mode boost
         if(Battery<=13.25)Charge = 0;//charging mode bulk
         if(mode==1 && TMR0<220)Lcd_COut(2,1,"   MODE: AUTO   ");
    }
    else
    {
       Charge = 0;//charging mode bulk
       if(mode==1 && TMR0<220)Lcd_COut(2,1,"MODE: CNST.Curr.");
    }
            
    if(Charge)//tricle mode
    {
        Tapping_Relay_OFF;
        if(mode==0 && TMR0<220)Lcd_COut(2,1,"CHARGING_TRICKLE");
        Clock_target1 = 35;
    }
    else // bulk mode
    {
         if(mode==0 && TMR0<220)Lcd_COut(2,1," CHARGING_BOOST ");
         Tapping_Relay_ON;
         Charging_Data = 100;
         if(Charging_Data<190)
         {
                 Get_POT_Variable(void);
                 if(Current_Target>=0 && Current_Target<100) Clock_target = 30;
                 else if(Current_Target>=100 && Current_Target<200) Clock_target = 29;
                 else if(Current_Target>=200 && Current_Target<300) Clock_target = 28;
                 else if(Current_Target>=300 && Current_Target<400) Clock_target = 27;
                 else if(Current_Target>=400 && Current_Target<500) Clock_target = 26;
                 else if(Current_Target>=500 && Current_Target<600) Clock_target = 25;
                 else if(Current_Target>=600 && Current_Target<700) Clock_target = 24;
                 else if(Current_Target>=700 && Current_Target<800) Clock_target = 23;
                 else if(Current_Target>=800 && Current_Target<900) Clock_target = 22;
                 else if(Current_Target>=900 && Current_Target<1000) Clock_target = 21;
                 else Clock_target = 20;
                 
                 if(Clock_target1>Clock_target)
                 {
                     Clock_target1--;
                 }
                 else if(Clock_target1==Clock_target)
                 {
                     Clock_target1==Clock_target;
                 }
                 else
                 {
                    if(Clock_target1<Clock_target) Clock_target1++;
                 }
         }
         else
         {
            Tapping_Relay_OFF;
            Clock_target = 30;
            if(TMR0<220)Lcd_COut(2,1," SYSTEM ERROR  ");
            Buzzer_ON;
            Delay_ms(1000);
            Buzzer_OFF;
            Delay_ms(5000);
         }
    }
}

void Get_Battery(void)
{
     char Battery_Voltage[] = "B:  . V ";
     int i;
     int B_Data = 0;
     ADCON0 = 0b00100101;//select channel 9
     adc_rd = 0;
     for(i=0;i<20;i++)
     {
         adc_rd += ADC_Read(9);
         Delay_ms(2);
     }
     adc_rd/=20;//get the average value
     Battery = adc_rd*0.039;
     B_Data = (int)adc_rd*0.39;
     Battery_Voltage[2] = B_Data/100 + 48;
     Battery_Voltage[3] = (B_Data/10)%10 + 48;
     Battery_Voltage[5] = (B_Data)%10 +48;
     if(TMR0<220)Lcd_Out(1,1,Battery_Voltage);
     Delay_ms(10);
}

void Get_CT_Reading(void)
{
     char Charging_Current[] = "I:  . A";
     int j,ii;
     unsigned int adc_rd1=0,temp = 0,max_point = 0;

     ADCON0 = 0b00101101;//select channel 11
     adc_rd = 0;//clear previous data
     
     for(j=0;j<30;j++)
     {
        for(ii=0;ii<1000;ii++)
        {
            if(temp = ADC_Read(11),temp>max_point)
            {
               max_point = temp;
            }
            Delay_us(5);
        }
          adc_rd1+= max_point;
          Delay_ms(2);
     }
     adc_rd1/=30;

     adc_rd1 = (int)adc_rd1*4.88;// Convert into mV
     Charging_Data = (int)(0.0002*adc_rd1*adc_rd1 + 0.205*adc_rd1 + 1.3953);

     Charging_Data /= 6;

     Charging_Current[2] = Charging_Data/100 + 48;
     Charging_Current[3] = (Charging_Data/10)%10 + 48;
     Charging_Current[5] = Charging_Data%10 +48;
     if(TMR0<220)Lcd_Out(1,9,Charging_Current);
     Delay_ms(10);
     adc_rd1 = 0;
}

void Get_LM35_Reading(void)
{
     int k;
     char Temperature[] = "  SYS_TEMP:   C  ";
     adc_rd = 0;//clear previous data
     ADCON0 = 0b00000001;//select channel 0
     for(k=0;k<20;k++)
     {
        adc_rd += ADC_Read(0);
        Delay_ms(1);
     }
     adc_rd/=40;// get the average value
     Temp = (int)adc_rd -1;
     Temperature[11] = Temp/10 + 48;
     Temperature[12] = Temp%10 +48;
     Temperature[13] = 223;
     if(mode==2 && TMR0 < 220)Lcd_Out(2,1,Temperature);
     if(Temp>35)
     {
         Cooling_Fan_ON;
         if(Temp>65)
         {
             Mains_Relay_OFF;
             Clock_target = 100;
             Lcd_Cmd(_LCD_CLEAR);//clear LCD
             for(k=0;k<20;k++)
             {
                  if(TMR0<200)Lcd_COut(1,5,"WARNING!!!");
                  if(TMR0<200)Lcd_COut(2,3,"SYS_TEMP HIGH");
                  Buzzer_ON;
                  Delay_ms(1000);
             }
             Buzzer_OFF;
             Lcd_Cmd(_LCD_CLEAR);//clear LCD
         }
     }
     else
     {
       if(Temp<30) Cooling_Fan_OFF;
     }
}

void Get_POT_Variable(void)
{
     ADCON0 = 0b00000101;//select channel 1
     Current_Target = (int)ADC_Read(1);
}






//end


And its saying attachment not supported while trying to attach DNS file. So I'm sending you an image of the circuit.


Note: When the simulation is going on, there is no trouble. But when I'm running it in the hardware, its trouble. Also I added a capacitor next to the LCD VDD && VSS, but same result.
 

hello,



Mistake in Output affectation to LCD

Mix data and controle
Code:
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RA4_bit;
sbit LCD_D7 at RA5_bit;

half PORTC for DATA

sbit LCD_RS at RA4_bit;
sbit LCD_EN at RA5_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
 

So you are suggesting to keep all the pins related with LCD in a same PORT? If so, then let me give a try again.

- - - Updated - - -

I'm taking a hardware interrupt at RA2. Will it be a trouble for LCD?

What is shadow mask register? and how to use it in pin/port declaration?
 

Tried. But says not supported. Let me do it RAR.

Also note again, I'm testing it for a long time. Even now. Anyway, Simulation says all ok. But in real hardware I'm getting that funny character problem.

Still now what I found is, There is a relay in the circuit. When this relay turns on/off... LCD goes funny character. Is it a serious case for LCD?

The relay is being driven by ULN2003. No capacitor with relay coil.
 

Still now what I found is, There is a relay in the circuit. When this relay turns on/off... LCD goes funny character. Is it a serious case for LCD?

The relay is being driven by ULN2003. No capacitor with relay coil.
There are no relays in the schematic you posted. What's the purpose of the schematic if not showing the actual circuit?

Do you say that the "funny characters" are exclusively related to relay switching? That's somehow surprizing, after nearly one year.
 

Anyway, here is the PCB diagram. I hope you'll understand it.
It's at least helpful to understand the peripheral (relays, triac) connection.

Relay coil interface is state-of-the art, utilizing the ULN2003 free wheeling diode. There's nothing to improve in this regard.

Display artefacts during relay switching are most likely caused by contact arcing. In this case, they won't happen if the 230V circuit is disconnected from mains.
 

Few hours ago, I made some changes. Added a capacitor 100uF/50V with each of the relays. And modified the code a littel bit.

here:
Code:
#define       Mains_Relay_ON             PORTC = (PORTC & 0b11111011) | (1<<2)
#define       Mains_Relay_OFF            PORTC = (PORTC & 0b11111011) | (0<<2)
#define       Tapping_Relay_ON           PORTB = (PORTB & 0b11101111) | (1<<4)
#define       Tapping_Relay_OFF          PORTB = (PORTB & 0b11101111) | (0<<4)
#define       Buzzer_ON                  PORTC = (PORTC & 0b11111101) | (1<<1)
#define       Buzzer_OFF                 PORTC = (PORTC & 0b11111101) | (0<<1)
#define       Cooling_Fan_ON             PORTC = (PORTC & 0b11111110)|(1)
#define       Cooling_Fan_OFF            PORTC = (PORTC & 0b11111110)|(0)

To protect R-M-W problem. Still now that funny character problem is not occurring. But I want to run the system for 1/2 days more to see what happen.
 

Were there freewheeling diodes in parallel to relay coils ? Your PCB layout doesn't show freewheeling diode.
 

Were there freewheeling diodes in parallel to relay coils ? Your PCB layout doesn't show freewheeling diode.

I hope you know about ULN2003. If not please check the datasheet. If you use ULN2003, you have already have individual freewheeling diode inside built in.
 

The final result, I kept that system running all the day long. And asked my boys to drive the unit at various conditions. The final results comes out as, LCD is still ok and working well.

But the problem still remained hidden to me. Because it is still not clear to me what the actual problem was. So I don't know what to say about this trouble.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…