[EasyPIC5] Need an example for 7seg display + Button

General discussion on mikroPascal.
Post Reply
Author
Message
Elder
Posts: 8
Joined: 10 Jul 2005 01:05

[EasyPIC5] Need an example for 7seg display + Button

#1 Post by Elder » 23 Mar 2009 09:33

Hi! I'm starting to develop on EasyPIC5 board with MikroPascal. I've a little problem with the 7seg example (for PIC 16F887): how can I add a Button procedure to the program then, when I click a RC0 button Increment the number on the display, RC1 button Increment the number by 100, RC2 reset to 0 the display? I've tried with this code but unsuccessful:

Code: Select all

program Segnapunti;

var SHIFTER, i, portd_index , VarOut, oldstate0, oldstate1, oldstate2, oldstate3: byte;
    digit, number, tmp : word;
    portd_array: array[4] of Word;

function mask(num: Word): Word;
begin
  case num of
     0 : result:= $3F;
     1 : result:= $06;
     2 : result:= $5B;
     3 : result:= $4F;
     4 : result:= $66;
     5 : result:= $6D;
     6 : result:= $7D;
     7 : result:= $07;
     8 : result:= $7F;
     9 : result:= $6F;
   end; //case end
end;//~


procedure interrupt;
begin

  PORTA := 0;                             // Turn off all 7seg displays
  PORTD := portd_array[portd_index];      // bring appropriate value to PORTD
  PORTA := shifter;                       // turn on appropriate 7seg. display

  // move shifter to next digit
  shifter:= shifter shl 1;
  if (shifter > 8) then
    shifter := 1;

  // increment portd_index
  Inc(portd_index);
  if (portd_index > 3) then
    portd_index := 0;                     // turn on 1st, turn off 2nd 7seg.

 // number:=tmp;
  TMR0   :=   0;                          // reset TIMER0 value
  INTCON := $20;                         	// Clear T0IF

 end;//~

procedure testButton;
begin
varOut:=0;
      if Button(PORTC, 0, 1, 1) then oldstate0 := 255;
      if Button(PORTC, 1, 1, 1) then oldstate1 := 255;
      if Button(PORTC, 2, 1, 1) then oldstate2 := 255;
      if Button(PORTC, 3, 1, 1) then oldstate3 := 255;
      if oldstate0 and Button(PORTC, 0, 1, 0) then
      begin
        //number:=number+1;
        varOut:=VarOut+1;
        oldstate0:=0;
      end;
      if oldstate1 and Button(PORTC, 1, 1, 0) then
      begin
        //number:=number+100;
        varOut:=VarOut+2;
        oldstate1:=0;
      end;
      if oldstate2 and Button(PORTC, 2, 1, 0) then
      begin
        //number:=0;
        varOut:=VarOut+4;
        oldstate2:=0;
      end;
      if oldstate3 and Button(PORTC, 3, 1, 0) then
      begin
        varOut:=VarOut+8;
        oldstate3:=0;
      end;

      case VarOut of
        1: number:=number+1;
        2: number:=number+100;
       // 4: number:=0;
       // 8,0: number:=number;
      end;
end;


begin
  ANSEL       :=    0;                    // Set AN pins to Digital I/O
  ANSELH      :=    0;
  OPTION_REG  := $80;                    	// Timer0 settings
  digit       :=    0;
  portd_index :=    0;
  shifter     :=    1;
  TMR0        :=    0;
  INTCON      := $A0;                    	// Enable GIE, T0IE
  PORTA       :=    0;
  TRISA       :=    0;                    // Set PORTA as output
  PORTD       :=    0;
  TRISD       :=    0;                    // Set PORTD as output
  PORTB       :=    0;
  TRISB       :=    0;
  TRISC := 0xFF;
  
{
  while TRUE do
  begin
    for i := 0 to 9 do
    begin
      PORTA := 0;                 // Turn off all 7seg displays
      PORTD := mask(i);           // bring appropriate value to PORTD
      PORTA := 1;                 // turn on appropriate 7seg. display
      Delay_ms(1000);
    end;
   end;                    //endless loop
   
}
  	
  number      :=   0;              		// some initial value

  oldstate0:=0;
  oldstate1:=0;
  oldstate2:=0;
  oldstate3:=0;

  while TRUE do
  begin

    testButton;
    portb:=varout;

    digit := number / 1000 ;             	// extract thousands digit
    portd_array[3] := mask(digit);        // and store it to PORTD array
    digit := (number / 100) mod 10;       // extract hundreds digit
    portd_array[2] := mask(digit);        // and store it to PORTD array
    digit := (number / 10) mod 10;        // extract tens digit
    portd_array[1] := mask(digit);        // and store it to PORTD array
    digit := number mod 10;               // extract ones digit
    portd_array[0] := mask(digit);        // and store it to PORTD array

{    PORTA := 0;                             // Turn off all 7seg displays
    PORTD := portd_array[portd_index];      // bring appropriate value to PORTD
    PORTA := shifter;

    shifter:= shifter shl 1;
    if (shifter > 8) then
      shifter := 1;

    // increment portd_index
    Inc(portd_index);
    if (portd_index > 3) then
      portd_index := 0;
  }
    
   end;                            				// endless loop
end.
Thank you!

M.P. Conte
Elder

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 23 Mar 2009 09:49

Which part of the code is not working well?
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Elder
Posts: 8
Joined: 10 Jul 2005 01:05

#3 Post by Elder » 23 Mar 2009 10:51

I presume the procedure testButton: if I click on a button the display will not change (a little flickering on first display and not other)

Post Reply

Return to “mikroPascal General”