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.

[SOLVED] mikroC Increment and decrement

Status
Not open for further replies.

IcyWolfBrain

Newbie level 4
Newbie level 4
Joined
Dec 1, 2021
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
52
Hey guys, i need help in MikroC programming PIC16F877A processor. So i need do to increment and decrement 7 segment display with two diferrent buttons (one to increment/ other decrement) (Shematic in photo). Increment for me is working but decrement i dont know how to implement.

My Code:
Code:
bit state, prev_state;
int i=0;
void main(){

 portd=0;
 trisd=0b00000000;

 while(1){

  state=portb.b0;
  if(prev_state!=state){

  if(state==0)i++;
   if(i>9) i=0;
   //portb=i;
   prev_state=state;

  }
 

 switch(i){
           case 0: portd=0b00111111; break;
           case 1: portd=0b00000110; break;
           case 2: portd=0b11011011; break;
           case 3: portd=0b01001111; break;
           case 4: portd=0b01100110; break;
           case 5: portd=0b01101101; break;
           case 6: portd=0b11111101; break;
           case 7: portd=0b00000111; break;
           case 8: portd=0b11111111; break;
           case 9: portd=0b11101111; break;

           }

  }
}
 

Attachments

  • Screenshot 2021-12-01 132628.png
    Screenshot 2021-12-01 132628.png
    47.5 KB · Views: 166
Last edited by a moderator:

Code:
while(1)
{
  state=portb.b0;       // read the current port state
  if(prev_state!=state) // see if its contents have changed
  {
    if(portb.b0 == 0) i++;  // increment i if b0 switch is closed
    if(portb.b7 == 0) i--;  // decrement it if b7 switch is closed

    if(i < 0) i = 0;    // restrict the range if necessary
    if(i > 9) i = 9;

    prev_state=state;   // only allow one pass until switch changes again
  }
}

I don't use MikroC to check it but the code above should give you some ideas. Note that you might have to debounce the switches to avoid one press appearing to repeat.

Brian.
 

Here's my idea:
C:
unsigned char state, prev_state;
unsigned char i;

void main(){

 portd = 0;
 trisd = 0b00000000;
 i = 0;
 prev_state = 0;

while(1){

  state = 0;

  if(portb.b0 == 0) state = state + 1;
  if(portb.b7 == 0) state = state + 2;

  if(prev_state!=state){

    switch(state){
           case 0: /* no buttons pressed */
            break;

           case 1: /* increment button pressed */
            if(i < 9) i++;
            else i = 0;
            break;

           case 2: /* decrement button pressed */
            if(i > 0) i--;
            else i = 9;
            break;

           case 3: /* both buttons pressed */
            break;
    }

    prev_state=state;

    switch(i){          /* .gfedcba */
           case 0: portd=0b00111111; break;
           case 1: portd=0b00000110; break;
           case 2: portd=0b01011011; break;
           case 3: portd=0b01111001; break;
           case 4: portd=0b01100110; break;
           case 5: portd=0b01101101; break;
           case 6: portd=0b01111101; break;
           case 7: portd=0b00000111; break;
           case 8: portd=0b01111111; break;
           case 9: portd=0b01101111; break;
    }
  }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top