milan.rajik
Banned
- Joined
- Apr 1, 2013
- Messages
- 2,524
- Helped
- 540
- Reputation
- 1,078
- Reaction score
- 524
- Trophy points
- 1,393
- Activity points
- 0
/*******************************************************************************
* 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
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;
The suggestion was to zip your project before attaching it.And its saying attachment not supported while trying to attach DNS file.
There are no relays in the schematic you posted. What's the purpose of the schematic if not showing the actual circuit?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.
It's at least helpful to understand the peripheral (relays, triac) connection.Anyway, here is the PCB diagram. I hope you'll understand it.
#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)
Were there freewheeling diodes in parallel to relay coils ? Your PCB layout doesn't show freewheeling diode.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?