## simulation behaves unexpected-motor control

12 posts / 0 new
Author
Message

this is the continuation of my previous post motor switching control.
i write a code in avr studio4.
i load the hex file into proteus isis professional 7.6
but the o/p of lcd is unexpected.
wasting so much time to debug the problem,but no use
i attached the full files below.

## Attachment(s): motor.rar

My copy of ark (Linux archive extractor) says that .rar is corrupt.

Anyway all I was going to check was which standard LCD library it is you are trying to use. Perhaps you can just say?

Also how sure are you that any fault in simulation is not simply a fault in the simulator itself?

Quote:
Also how sure are you that any fault in simulation is not simply a fault in the simulator itself?

no , error can be occur due to the error in program logic

the lcd code i used is from exteemelectronics.com

i just re attach the post again

Try the LCD code that came with your C compiler:

http://www.nongnu.org/avr-libc/u...

Quote:
Try the LCD code that came with your C compiler:

http://www.nongnu.org/avr-libc/u... ... odemo.html

yes i will try

but i feel the below attached is more easy to use for a beginer like me.

## Attachment(s): plz gothrough the attachment

Quote:

below attached is more easy to use for a beginer like me.

Not if it doesn't work.

As JFK almost once said:

Quote:
We choose to use the AVR-LibC LCD code. We choose to use the AVR-LibC LCD code in this project and do the other things, not because they are easy, but because they are hard

my 1 sec timer is r is as follows

```ISR ( TIMER1_COMPA_vect )
{
if((clock_minute==00)&&(clock_second==00))
{
clock_hour--;
}

clock_second--;

if(clock_second==00)
{
clock_minute--;
clock_second=59;
}
if(clock_minute==00)
{
clock_hour--;
clock_minute=59;
}
if((clock_hour==00)&&(clock_minute==00)&&(clock_second==00))
{
change_motor();
}

```

but when the sec reaches 0, it restart from 99 instead of 59

## Attachment(s): motor_controller.zip

So what happens when clock_second is zero and you do clock_second-- ?

Do you think it changes to 59?

Suggest you trace, with a pencil and paper, what your code does for different values of clock_xxx values...

MartinM57

are you simulate my code
i already atttach the complete simulation files(proteus)

no one interested in lcd program

i just put down the full code here.
the problem is
for testing i put 2 min as timer value
it start as i expected

Quote:
Motor Change in
00:01:59

but when second comes to 0 then
it shows like below
Quote:
Motor Change in
55:00:59

and count down.

icant find out the problem.
plz help.

```#include "template.h"
#include "lcd.h"

char n;
volatile char  clock_second;
volatile char  clock_minute=2;
volatile char  clock_hour;

void init_ports(void)
{
//motor ports
OUT(C,0);
OUT(C,1);
OUT(C,2);
OUT(C,3);
//button
IN(B,0);
IN(B,1);
}

void motor_off(void)
{
CLR(C,0);
CLR(C,1);
CLR(C,2);
CLR(C,3);
}

void init_timer(void)
{
TCCR1B |= (1 << WGM12 ); // Configure timer 1 for CTC mode
TIMSK1 |= (1 << OCIE1A ); // Enable CTC interrupt
OCR1A = 15624; // Set CTC compare value to 1Hz at 1 MHz AVR clock , with a prescaler of 64
TCCR1B |= ((1 << CS10 ) | (1 << CS11 )); // Start timer at Fcpu /64
}

void start_motor(void)
{
switch(n)
{
case 0:
SET(C,0);
break;
case 1:
SET(C,1);
break;
case 2:
SET(C,2);
break;
case 3:
SET(C,3);
break;
}
}

//Change Motor
void change_motor(void)
{
switch(n)
{
case 0:
CLR(C,0);
SET(C,1);
n++;
break;
case 1:
CLR(C,1);
SET(C,2);
n++;
break;
case 2:
CLR(C,2);
SET(C,3);
n++;
break;
case 3:
CLR(C,3);
SET(C,0);
n=0;
break;
}
}
//LCD Display function
void show_lcd_hour(void)
{
LCDWriteIntXY(4,1,clock_hour,2);
LCDWriteString(":");
}
void show_lcd_minute(void)
{
LCDWriteIntXY(7,1,clock_minute,2);
LCDWriteString(":");
}
void show_lcd_second(void)
{
LCDWriteIntXY(10,1,clock_second,2);
}

void initial_lcd_show(void)
{
clock_second=59;
if((clock_minute==0)&&(clock_second==0))
{
clock_hour--;
}
clock_minute--;
show_lcd_hour();
show_lcd_minute();
show_lcd_second();
}

ISR ( TIMER1_COMPA_vect )
{
clock_second--;

if((clock_hour==0)&&(clock_minute==0)&&(clock_second==0))
{
change_motor();
cli;
}

show_lcd_second();
if(clock_second==-1)
{
clock_minute--;
clock_second=59;
show_lcd_minute();
if(clock_minute==-1)
{
clock_hour--;
clock_minute=59;
show_lcd_hour();
}

}

}

void main()
{
unsigned char i;
//Initialize LCD module
//Clear the screen
LCDClear();
//Simple string printing
LCDWriteString("    Welcome   ");
//A string on line 2
//Print some numbers
for (i=0;i<100;i+=1)
{
LCDWriteIntXY(9,1,i,3);
LCDWriteStringXY(12,1," % ");
_delay_ms(100);
}
LCDClear();
LCDWriteString("     Start    ");
LCDWriteStringXY(0,1,"  Motor Timer  ");

init_ports();
motor_off();
init_timer();
sei();
start_motor();
LCDClear();
LCDWriteString(" Motor Change in");
initial_lcd_show();
while(1)
{

}
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.89842445,"math_prob":0.8894394,"size":2572,"snap":"2021-31-2021-39","text_gpt3_token_len":630,"char_repetition_ratio":0.18068536,"word_repetition_ratio":0.13052632,"special_character_ratio":0.22822706,"punctuation_ratio":0.10223048,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95381856,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-23T06:38:54Z\",\"WARC-Record-ID\":\"<urn:uuid:3bcfcab2-033a-497d-9022-702a09761172>\",\"Content-Length\":\"92635\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d66837c2-4848-400a-b805-07aee2639453>\",\"WARC-Concurrent-To\":\"<urn:uuid:4aea8b19-1d55-42fc-abfb-299bfab523b8>\",\"WARC-IP-Address\":\"50.112.125.251\",\"WARC-Target-URI\":\"https://www.avrfreaks.net/comment/840821\",\"WARC-Payload-Digest\":\"sha1:CLF7OEWGZOEMVPV47QXSKHI4EHODPAZN\",\"WARC-Block-Digest\":\"sha1:X6MICCXVDXZ6W7KEXG5GEYENRUBL4BTG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057417.10_warc_CC-MAIN-20210923044248-20210923074248-00477.warc.gz\"}"}