• 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.

What have I done wrong in program code simulation

Eng Emmanuel

New Member
Hello,
I have created a simple circuit of Automatic temperature controller with the keypad interface and the microcontroller (PIC16F877A) output goes to the Fan (whenever temperature is high) and Heater (whenever temperature is low), but the simulation does not work perfectly. I am using a MICROC PRO COMPILER with PROTEUS 8.0.
My code:
C:
char keypadPort at PORTD;

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main() {
unsigned short rd,kp;
unsigned int Temp_Ref;
float Ref_Temp,rd1;
unsigned char TxT[4],TxT1,InTemp;
ADCON1=0b11000000;
  TRISA0_bit=1;
  TRISC0_bit=0;
  TRISC1_bit=0;
  ADC_Init();
  LCD_Init();
  Keypad_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Out(1,4,"AUTOMATIC");
  delay_ms(1000);
  Lcd_Out(2,1,"TEMPERATURE CONTROL");
  Lcd_Cmd(_LCD_SHIFT_LEFT);
  delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);
  PORTC.RC0=0;
  PORTC.RC1=0;
 
  START:
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Enter Temp. Ref");
  Temp_Ref=0;
  Lcd_Out(2,1,"Temp. Ref:");
  while(1){
  do
  kp=Keypad_Key_Click();
  while(!kp);
  if(kp==15)break;
  if(kp>3 && kp<12)kp=kp-2;
  if(kp==14)kp=0;
  if(kp==13) goto START;
  Lcd_Chr_Cp(kp+'0');
  Temp_Ref=(10*Temp_Ref)+kp;}
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1,"Temp. Ref:");
  IntToStr(Temp_Ref,TxT1);
  InTemp=Ltrim(TxT1);
  Lcd_Out_Cp(InTemp);
  Lcd_Out(2,1,"Press # to cont.");
  kp=0;
  while(kp!=15)
  {
  do
  kp=Keypad_Key_Click();
  while(!kp); goto START_PGM;
  }
 
  START_PGM:
  while(1) {
  Lcd_Cmd(_LCD_CLEAR);
  rd=ADC_Read(0);
  rd1=(rd*5000.0)/1024.0;
  Ref_Temp=rd1/10.0;
  FloatToStr(Ref_Temp,TxT);
  Lcd_Out(2,1,"TEMP. READ: ");
  Lcd_Out(1,13,TxT);
  Lcd_Out(1,15,'C');
  if(Ref_Temp>Temp_Ref){
  PORTC.RC0=0,
  PORTC.RC1=1;}
  if(Ref_Temp<Temp_Ref){
  PORTC.RC0=1,
  PORTC.RC1=0;}
  if(Ref_Temp==Temp_Ref){
  PORTC.RC0=0,
  PORTC.RC1=0;}  }
 
}
Simulation:
120498
 
Last edited by a moderator:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
First... Its very bad C practice to use goto and lables... You will also run into trouble in real time with two port writes to inductive loads one after the other. I would use PORTC = 1 or 2 instead.

I'm very surprised that MikroC allows "if(Ref_Temp>Temp_Ref)" as they are different variables, You'll need a cast for one or the other one is a float and the other an int.

try
if((int)Ref_Temp>Temp_Ref){ blah blah }

I also really hate the way MikroC allows non case sensitivity... For portability, you can switch it off.
 

Eng Emmanuel

New Member
First... Its very bad C practice to use goto and lables... You will also run into trouble in real time with two port writes to inductive loads one after the other. I would use PORTC = 1 or 2 instead.

I'm very surprised that MikroC allows "if(Ref_Temp>Temp_Ref)" as they are different variables, You'll need a cast for one or the other one is a float and the other an int.

try
if((int)Ref_Temp>Temp_Ref){ blah blah }

I also really hate the way MikroC allows non case sensitivity... For portability, you can switch it off.
Thanks, let me try it.
 

EE World Online Articles

Loading

 
Top