Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

can't increment int values greater than 255 in mikroc for pic

Status
Not open for further replies.

renga92

Member level 5
Joined
Jun 21, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
india
Activity points
2,084
hello everyone,
i am doing a project on countdown timer (0 - 99) min and i am also counting the total hours of its working time. i am saving the no of hours and mins in eeprom for every count down of the timer.
i ran into a problem when i want to increment value greater than 255 (no of hours) and i end up getting a weird value[ the increment function fine till i shutdown the timer and turn it on,after turning it on i get weird values ]. i got the reason for getting that weird value...

if the no is 312 after saving it to eeprom. then i did turn it off and then i turned it on ,checked the value stored in the eeprom. it was still 312.
then i set the timer for any time after turning on (for example : 4 mins)
after 4 mins value turns out to be 057

the reason was that

312 binary equivalent - 1 00111000
57 binary equivalent - 0 00111001

so the ninth bit is set in the first bit.
the reason is i am working with 8 bit controller...

now my question is
1)how to work with nos greater than 255 ?
2)will that overflow in w register call any stack or will it cause any interrupt ?
i am asking this because i am using 5 to 6 functions which might also occupy most of the stack all the time...

please help me to solve this problem..
thanks in advance

renga92
 

how to work with nos greater than 255 ?

Sounds as though you need an additional byte.

You assign two bytes to a counter variable.

The first (smallest) byte is the least significant byte. You have just added a next most significant byte.

When the first byte reaches 255, you increment the second byte. Set the first byte to 0. Continue from there.

This expands the range of numbers you can obtain. With two bytes it is 0 to 65535.

If you are counting down: when the LSB is at zero, you set it to 255, and decrement the adjacent byte.

All of your arithmetic will be more complicated. If you store a value on the stack, you store it in 2 steps. Each step will store 1 byte.

When pulling the number off the stack, you pull 2 bytes. Always in the proper sequence.

If you need negative numbers then there is a method to do this. It's a matter of whether the last bit is 0 or 1. Now when using 2 bytes, your range becomes -32768 to +32767.
 
MikroC huh?

I presume you are using a 'int' value for your number... a 'int' variable is 16 bits and can reach up to ~32767 if signed or 65535 if unsigned...
(a 'char' variable or a 'short int' can count up to 255 as it only have 8 bits)

AS you stated, it works ok until you save to eeprom...

also I presume you have used the Eeprom_Write() function...

you have to note tha this function ONLY WRITES 1 byte (8bits) of the value...

say:

we have a variable called... mmmm... var1

Code:
unsigned int var1;

...

...

Eeprom_Write(0x00,var1);

THIS will not work... you have only saved the lower 8 bits of the 16 bit number...

you should implement something like this:


Code:
unsigned int var1;

...

...

Eeprom_Write(0x00,var1); //lower byte
Delay_ms(20);
Eeprom_Write(0x01,var1>>8); //upper byte
Delay_ms(20);

now you are using 2 bytes of the eeprom, and save both bytes of your number...

to read the 16 bit number, you should


Code:
unsigned int var1;

...

...

Eeprom_Write(0x00,var1); //lower byte
Delay_ms(20);
Eeprom_Write(0x01,var1>>8); //upper byte
Delay_ms(20);


...
//read 16 bits from eeprom addres 0x01 and 0x00
var1 = (Eeprom_Read(0x01)<<8); //upper byte
var1 |= Eeprom_Read(0x00); // lower byte
 
thanks all for your reply....
i ll check n see if its working and get back to you people...

i have another doubt...
i want to use a button called reset button which will reset the total no of hours after pressing it continuously for 10 secs....
so i connected monostable configuration of ic 555 to genetrate square wave of 10secs on time...
so i used that reset button to connect 5 volts to ic 555 timer....if i press it for 10 secs to power the ic555 then the c will get charged up by then and generate a square wave which can be connected to the input pin of the pic to check when it does go high....

the code i wrote for this is
Code:
 do {

     if(!Unit_Button)
     {
     Delay_300();
     unit ++;
     if(unit==10)
     {
      ten++;
      unit=0;
     }
     Display_Digits();
    } // If !Unit_Button

    if(!Ten_Button){
     Delay_300();
     if(ten == 0 && unit == 0 )
     {
      ten=0;
      unit=0;
      k = 1;
     }
     if(ten!=0 && unit == 0)
     {
      ten--;
      unit = 9;
      m = 1;
     }
     if(k == 0 )
     {
      if(unit != 9 || m == 0)
       {
        unit--;
       }
      else
      {
       m = 0;
      }
      k = 0 ;
     }
     Display_Digits();
    } // If !Ten_Button
    if(!SS_Select)
    {
     Delay_300();
     time = (ten*10)+unit ;
     if(time > 0)
     {
      start_timer();
     }
     goto start;
    } // If !SS_Select
    if(reset)
    {
     delay_ms(1000);
     if(reset)
         n++;
     if(n == 10)
     {
      n = 0;
      a1 = '0';
      a2 = '0';
      a3 = '0';
      a4 = '0';
      a5 = '0';
      a6 = '0';
     }
     else
      continue ;
    }
  } while(1);

do while will check for the pressing of the button ,for which reset is ra4 pin which is configured for input and it is connected to monostable multivibrator...
i am not able to reset the total no of hrs....

could you please point out the mistake i have made....

thanks in advance
renga92
 
Last edited:

do while will check for the pressing of the button ,for which reset is ra4 pin which is configured for input and it is connected to monostable multivibrator...
i am not able to reset the total no of hrs....

could you please point out the mistake i have made....

Did you first test whether you can get the reset to work when you do it directly, without the delay-producing circuitry?
 
i didn't yet...
ll check n get back to you

thanks
renga92

- - - Updated - - -

i didn't yet...
ll check n get back to you

thanks
renga92
 

all your suggestions has helped...thanks all...

- - - Updated - - -

all your suggestions has helped...thanks all...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top