Continue to Site

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.

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

PortB pullups not working correctly (using Swordfish basic)

MrDEB

Well-Known Member
The code runs except it keeps blinking led7 in time with the matrix blinking.
Something wrong with the code, as I have two boards that are the same and both blink led7
I checked the switches and they are not shorted.
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/19/2016                                                     *
*  Version : 1.0                                                            *
*  Notes   :  COMMON CATHODE MATRIX                                                              *
*          :                                                                *
*****************************************************************************
}


//xxxxxxxxxxxxxxxxxxxxxxxxxx

Device = 18F43k22 ' Tell the compiler what chip we are using
Clock = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
//CONFIG MCLRE = OFF
Config FOSC = INTIO67 ' Internal oscillator, IO on pins 6 and 7
#option DIGITAL_INIT = true
Include "SetDigitalIO.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
Include "utils.bas"
Include "convert.bas"
Include "intosc.bas"
Include "ISRTimer.bas"   

Dim y As Byte
Dim x As Byte
Dim counter As Byte
Dim Switch As PORTE.0 //a.3
'Dim led1a As PORTD.3
'Dim led1c As PORTB.3
//Dim adval As Word
//Dim adcout As Word

//PLAYER LEDS
Dim led1 As PORTA.0
Dim led2 As PORTA.1
Dim led3 As PORTA.2
Dim led4 As PORTA.3
Dim led5 As PORTA.4
Dim led6 As PORTA.5
Dim led7 As PORTA.6
Dim led8 As PORTA.7

//PLAYER SWITCHES
Dim swt1 As PORTB.0
Dim swt2 As PORTB.1
Dim swt3 As PORTB.2
Dim swt4 As PORTB.3
Dim swt5 As PORTB.4
Dim swt6 As PORTB.5
Dim swt7 As PORTB.6
Dim swt8 As PORTB.7


Dim KEY_BOUNCE_DELAY As Byte
//Dim Battery As P8RTA.2  'hlvd
//Dim Power As PORTA.1     'hlvd

Interrupt press()

   DelayMS(50)

End Interrupt


// CONST Cathodes_Data(9)AS BYTE = (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,%00000000)                               
                      
Const Cathodes_Data(8)As Byte = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                                 %10111111,        ' this is for scanning the rows
                                 %11011111,        //cathodes
                                 %11101111,
                                 %11110111,
                                 %11111011,
                                 %11111101,
                                 %11111110)
                                
                                
                                
Const Cat_Data(8)As Byte =    (%10000000,
                               %01000000,
                               %00100000,
                               %00010000,
                               %00001000,
                               %00000100,
                               %00000010,                                 
                               %00000001)
                              
                              
  Const z_ro(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,                                 
                         %00000000) 

 Const One(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,                                 
                         %00000000)       

' number 2
                                
Const Two(8)As Byte =  (%10000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000001)                 
                                        
Const Three(8) As Byte =(%10000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000001)
                                        
 Const Four(8) As Byte =(%10000001,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)                                       
                                
 
Const Five(8) As Byte = (%10000001,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)   

Const Six(8) As Byte =  (%10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                                                            
Const Seven(8) As Byte =(%10010001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                        
                                                            
Const Eight(8) As Byte =(%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10000010,
                         %00000000)
                        
 Const Nine(8) As Byte= (%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000)
                        
                                                                                                  
 Const Ten(8) As Byte=  (%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                            
Const Eleven(8) As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                        
Const Tweleve(8)As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,                                 
                         %00000000)
// Const KEY_BOUNCE_DELAY(0) = 50     // msec delay for key bounce
                                                                              
// Arrays
 Sub T_12()
 //12
  
Repeat
         For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Tweleve(x)// colums 8 (L-R the X is the scan
             DelayUS(500)
             Next
         // wait for a key press
       If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(200)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
       EndIf
 
          Until Switch=0
          DelayMS(500)
          counter = 11   //11
    End Sub
    
    
    
                 Sub E_11()   
                  //11
                      Repeat
                         For x = 0 To 7
                            PORTC = Cathodes_Data(x) //rows top to bottom
                            PORTD = Eleven(x)// colums 8 (L-R the X is the scan
                             DelayUS(300)
                              //PORTB = %11111111
                             // DelayUS(500)
                             Next
                       If (swt1 = 0) Then
                            Toggle(led1) DelayMS(500)
                        ElseIf (swt2 = 0) Then
                            Toggle(led2)  DelayMS(500)
                        ElseIf (swt3 = 0) Then
                            Toggle(led3)  DelayMS(500)
                        ElseIf (swt4 = 0) Then
                            Toggle(led4)   DelayMS(500)
                        ElseIf (swt5 = 0) Then
                            Toggle(led5)   DelayMS(500)
                        ElseIf (swt6 = 0) Then
                            Toggle(led6)  DelayMS(500)
                        ElseIf (swt7 = 0) Then
                            Toggle(led7)  DelayMS(500)
                        ElseIf (swt8 = 0) Then
                            Toggle(led8)   DelayMS(500)
                       EndIf
                             Until Switch=0
                             DelayMS(500)
                              counter = 10   //10
                    End Sub
  
                                   Sub T_10() 
                                     //10
                                        Repeat
                                         For x = 0 To 7
                                            PORTC = Cathodes_Data(x) //rows top to bottom
                                            PORTD = Ten(x)// colums 8 (L-R the X is the scan
                                             DelayUS(300)
                                              //PORTB = %11111111
                                            //  DelayUS(500)
                                             Next
                                      If (swt1 = 0) Then
                                            Toggle(led1) DelayMS(500)
                                        ElseIf (swt2 = 0) Then
                                            Toggle(led2)  DelayMS(500)
                                        ElseIf (swt3 = 0) Then
                                            Toggle(led3)  DelayMS(500)
                                        ElseIf (swt4 = 0) Then
                                            Toggle(led4)   DelayMS(500)
                                        ElseIf (swt5 = 0) Then
                                            Toggle(led5)   DelayMS(500)
                                        ElseIf (swt6 = 0) Then
                                            Toggle(led6)  DelayMS(500)
                                        ElseIf (swt7 = 0) Then
                                            Toggle(led7)  DelayMS(500)
                                        ElseIf (swt8 = 0) Then
                                            Toggle(led8)   DelayMS(500)
                                       EndIf
                                            Until
                                            Switch = 0
                                            DelayMS(500)
                                              counter = counter - 1   //9
                                    End Sub
                                            
                                            
    
                                                Sub N_9()     
                                                 //9   
                                                   Repeat
                                                     For x = 0 To 7
                                                        PORTC = Cathodes_Data(x) //rows top to bottom
                                                        PORTD = Nine(x)// colums 8 (L-R the X is the scan
                                                        DelayUS(300)
                                                         // PORTB = %11111111
                                                         // DelayUS(500)
                                                         Next
                                                    If (swt1 = 0) Then
                                                        Toggle(led1) DelayMS(500)
                                                    ElseIf (swt2 = 0) Then
                                                        Toggle(led2)  DelayMS(500)
                                                    ElseIf (swt3 = 0) Then
                                                        Toggle(led3)  DelayMS(500)
                                                    ElseIf (swt4 = 0) Then
                                                        Toggle(led4)   DelayMS(500)
                                                    ElseIf (swt5 = 0) Then
                                                        Toggle(led5)   DelayMS(500)
                                                    ElseIf (swt6 = 0) Then
                                                        Toggle(led6)  DelayMS(500)
                                                    ElseIf (swt7 = 0) Then
                                                        Toggle(led7)  DelayMS(500)
                                                    ElseIf (swt8 = 0) Then
                                                        Toggle(led8)   DelayMS(500)
                                                   EndIf
                                                         Until Switch=0
                                                         DelayMS(500)
                                                          counter = counter - 1   //8
                                                End Sub
                                                                            
                                                                            
                                               Sub E_8()                      //8       
                                                                       Repeat
                                                                         For x = 0 To 7
                                                                            PORTC = Cathodes_Data(x) //rows top to bottom
                                                                            PORTD = Eight(x)// colums 8 (L-R the X is the scan
                                                                            DelayUS(300)
                                                                             // PORTB = %11111111
                                                                           //   DelayUS(500)
                                                                             Next
                                                                        If (swt1 = 0) Then
                                                                            Toggle(led1) DelayMS(500)
                                                                        ElseIf (swt2 = 0) Then
                                                                            Toggle(led2)  DelayMS(500)
                                                                        ElseIf (swt3 = 0) Then
                                                                            Toggle(led3)  DelayMS(500)
                                                                        ElseIf (swt4 = 0) Then
                                                                            Toggle(led4)   DelayMS(500)
                                                                        ElseIf (swt5 = 0) Then
                                                                            Toggle(led5)   DelayMS(500)
                                                                        ElseIf (swt6 = 0) Then
                                                                            Toggle(led6)  DelayMS(500)
                                                                        ElseIf (swt7 = 0) Then
                                                                            Toggle(led7)  DelayMS(500)
                                                                        ElseIf (swt8 = 0) Then
                                                                            Toggle(led8)   DelayMS(500)
                                                                       EndIf
                                                                  
                                                                             Until Switch=0
                                                                             DelayMS(500)
                                                                              counter = counter - 1    //7
                                                                    End Sub
    
                
                
                                                                                 Sub S_7()  //7             
                                                                                       Repeat
                                                                                         For x = 0 To 7
                                                                                            PORTC = Cathodes_Data(x) //rows top to bottom
                                                                                            PORTD = Seven(x)// colums 8 (L-R the X is the scan
                                                                                            DelayUS(300)
                                                                                             // PORTB = %11111111
                                                                                           //   DelayUS(500)
                                                                                             Next
                                                                                        If (swt1 = 0) Then
                                                                                            Toggle(led1) DelayMS(500)
                                                                                        ElseIf (swt2 = 0) Then
                                                                                            Toggle(led2)  DelayMS(500)
                                                                                        ElseIf (swt3 = 0) Then
                                                                                            Toggle(led3)  DelayMS(500)
                                                                                        ElseIf (swt4 = 0) Then
                                                                                            Toggle(led4)   DelayMS(500)
                                                                                        ElseIf (swt5 = 0) Then
                                                                                            Toggle(led5)   DelayMS(500)
                                                                                        ElseIf (swt6 = 0) Then
                                                                                            Toggle(led6)  DelayMS(500)
                                                                                        ElseIf (swt7 = 0) Then
                                                                                            Toggle(led7)  DelayMS(500)
                                                                                        ElseIf (swt8 = 0) Then
                                                                                            Toggle(led8)   DelayMS(500)
                                                                                       EndIf
                                                                                             Until Switch=0
                                                                                             DelayMS(500)
                                                                                              counter = counter - 1       //6
                                                                                    End Sub
      
                    
                                                                                                  Sub S_6()    //SIX               
                                                                                                       Repeat
                                                                                                         For x = 0 To 7
                                                                                                            PORTC = Cathodes_Data(x) //rows top to bottom
                                                                                                            PORTD = Six(x)// colums 8 (L-R the X is the scan
                                                                                                             DelayUS(300)
                                                                                                             // PORTB = %11111111
                                                                                                           //   DelayUS(500)
                                                                                                             Next
                                                                                                       If (swt1 = 0) Then
                                                                                                            Toggle(led1) DelayMS(500)
                                                                                                        ElseIf (swt2 = 0) Then
                                                                                                            Toggle(led2)  DelayMS(500)
                                                                                                        ElseIf (swt3 = 0) Then
                                                                                                            Toggle(led3)  DelayMS(500)
                                                                                                        ElseIf (swt4 = 0) Then
                                                                                                            Toggle(led4)   DelayMS(500)
                                                                                                        ElseIf (swt5 = 0) Then
                                                                                                            Toggle(led5)   DelayMS(500)
                                                                                                        ElseIf (swt6 = 0) Then
                                                                                                            Toggle(led6)  DelayMS(500)
                                                                                                        ElseIf (swt7 = 0) Then
                                                                                                            Toggle(led7)  DelayMS(500)
                                                                                                        ElseIf (swt8 = 0) Then
                                                                                                            Toggle(led8)   DelayMS(500)
                                                                                                       EndIf
                                                                                                            Until Switch=0
                                                                                                             DelayMS(500)
                                                                                                              counter = counter - 1          //5
                                                                                                    End Sub
                                                                                                            
      
      
                                                                                                                    Sub F_5()      //5                   
                                                                                                                            Repeat     
                                                                                                                             For x = 0 To 7
                                                                                                                                PORTC = Cathodes_Data(x) //rows top to bottom
                                                                                                                                PORTD = Five(x)// colums 8 (L-R the X is the scan
                                                                                                                                 DelayUS(300)
                                                                                                                               //   PORTB = %11111111
                                                                                                                               //   DelayUS(500)
                                                                                                                                 Next
                                                                                                                           If (swt1 = 0) Then
                                                                                                                                Toggle(led1) DelayMS(500)
                                                                                                                            ElseIf (swt2 = 0) Then
                                                                                                                                Toggle(led2)  DelayMS(500)
                                                                                                                            ElseIf (swt3 = 0) Then
                                                                                                                                Toggle(led3)  DelayMS(500)
                                                                                                                            ElseIf (swt4 = 0) Then
                                                                                                                                Toggle(led4)   DelayMS(500)
                                                                                                                            ElseIf (swt5 = 0) Then
                                                                                                                                Toggle(led5)   DelayMS(500)
                                                                                                                            ElseIf (swt6 = 0) Then
                                                                                                                                Toggle(led6)  DelayMS(500)
                                                                                                                            ElseIf (swt7 = 0) Then
                                                                                                                                Toggle(led7)  DelayMS(500)
                                                                                                                            ElseIf (swt8 = 0) Then
                                                                                                                                Toggle(led8)   DelayMS(500)
                                                                                                                           EndIf
                                                                                                                                 Until Switch=0                    //4
                                                                                                                                 DelayMS(500)
                                                                                                                                  counter = counter - 1
                                                                                                                  End Sub
                            
                                                                                                                                     Sub F_4()       //FOUR                     
                                                                                                                                             Repeat
                                                                                                                                             For x = 0 To 7
                                                                                                                                                PORTC = Cathodes_Data(x) //rows top to bottom
                                                                                                                                                PORTD = Four(x)// colums 8 (L-R the X is the scan
                                                                                                                                                 DelayUS(300)
                                                                                                                                               //   PORTB = %11111111
                                                                                                                                                //  DelayUS(500)
                                                                                                                                                 Next
                                                                                                                                          If (swt1 = 0) Then
                                                                                                                                                Toggle(led1) DelayMS(500)
                                                                                                                                            ElseIf (swt2 = 0) Then
                                                                                                                                                Toggle(led2)  DelayMS(500)
                                                                                                                                            ElseIf (swt3 = 0) Then
                                                                                                                                                Toggle(led3)  DelayMS(500)
                                                                                                                                            ElseIf (swt4 = 0) Then
                                                                                                                                                Toggle(led4)   DelayMS(500)
                                                                                                                                            ElseIf (swt5 = 0) Then
                                                                                                                                                Toggle(led5)   DelayMS(500)
                                                                                                                                            ElseIf (swt6 = 0) Then
                                                                                                                                                Toggle(led6)  DelayMS(500)
                                                                                                                                            ElseIf (swt7 = 0) Then
                                                                                                                                                Toggle(led7)  DelayMS(500)
                                                                                                                                            ElseIf (swt8 = 0) Then
                                                                                                                                                Toggle(led8)   DelayMS(500)
                                                                                                                                           EndIf
                                                                                                                                                Until Switch=0
                                                                                                                                                 DelayMS(500)
                                                                                                                                                  counter = counter - 1        //3
                                                                                                                                        End Sub
          
    Sub T_3() 
             //3                   
         Repeat
         For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Three(x)// colums 8 (L-R the X is the scan
             DelayUS(300)
             // PORTB = %11111111
             // DelayUS(500)
             Next
       If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
       EndIf
             Until Switch=0
             DelayMS(500)
              counter = counter - 1           //2
    End Sub
          
    
          
                    Sub T_2()            //2                         
                              Repeat
                             For x = 0 To 7
                                PORTC = Cathodes_Data(x) //rows top to bottom
                                PORTD = Two(x)// colums 8 (L-R the X is the scan
                                 DelayUS(300)
                                //  PORTB = %11111111
                                //  DelayUS(500)
                                 Next
                          If (swt1 = 0) Then
                                Toggle(led1) DelayMS(500)
                            ElseIf (swt2 = 0) Then
                                Toggle(led2)  DelayMS(500)
                            ElseIf (swt3 = 0) Then
                                Toggle(led3)  DelayMS(500)
                            ElseIf (swt4 = 0) Then
                                Toggle(led4)   DelayMS(500)
                            ElseIf (swt5 = 0) Then
                                Toggle(led5)   DelayMS(500)
                            ElseIf (swt6 = 0) Then
                                Toggle(led6)  DelayMS(500)
                            ElseIf (swt7 = 0) Then
                                Toggle(led7)  DelayMS(500)
                            ElseIf (swt8 = 0) Then
                                Toggle(led8)   DelayMS(500)
                           EndIf
                                 Until Switch=0
                                 DelayMS(500)
                                  counter = counter - 1              //1
                        End Sub
                                                  
                          
 Sub O_1()                         
        Repeat          //1                 
         For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = One(x)// colums 8 (L-R the X is the scan
             DelayUS(300)
             // PORTB = %11111111
           //   DelayUS(500)
             Next
     If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
       EndIf
             Until Switch=0
             DelayMS(500)
              counter = counter - 1     //0  blank display
    End Sub
            
    
     Sub zero() // get the sub one to blink indicating zero                         
        Repeat          //1                       
                  
         For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = One(x)// colums 8 (L-R the X is the scan
             DelayMS(300)
            //  PORTB = %11111111
           //   DelayMS(500)
              //PORTB = %00000000
             Next
       If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
       EndIf
             Until Switch=0
             DelayMS(500)
              counter = counter - 1     //0  blank display
    End Sub
                            
//zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Sub Matrix_sel()

   If  counter = 12
   Then
    T_12()
End If

        If  counter = 11
           Then
           E_11()
          End If

                 If  counter = 10
               Then
               T_10()
              End If
  
                    If  counter = 9
                   Then
                   N_9()
                   End If
                            If  counter = 8
                               Then
                               E_8()
                              End If
                    
                                   If  counter = 7
                                       Then
                                       S_7()
                                       End If
                        
                        
                                           If  counter = 6
                                               Then
                                               S_6()
                                              End If
                            
                                                   If  counter = 5
                                                       Then
                                                       F_5()
                                                       End If
                                
                                
                                                            If  counter = 4
                                                               Then
                                                               F_4()
                                                             End If
                                    
                                    
                                                                    If  counter = 3
                                                                       Then
                                                                       T_3()
                                                                       End If
                                    
                                    
                                    
                                                                            If  counter = 2
                                                                               Then
                                                                               T_2()
                                                                               End If
                                        
                                        
                                                                                    If  counter = 1
                                                                                       Then
                                                                                       O_1()
                                                                                     End If 
                                            
                                            
                                               ' If  counter = 0
                                               '    Then
                                               '    e_12()
                                               '    Until Switch=0
                                               '    DelayMS(500)      ''debounce
                                               ' End If                         
                    End Sub 

 
// Start Of Program
// set switch port to inputs and enable pullups
    TRISB = $ff                // set as inputs
    WPUB = $ff                // enable pullups on all PORTB pins
    INTCON2.bits(7) = 0        // set bit RBPU = 0 turns on pullups


//TRISB = %00000000                   // Make PORTB all inputs               
TRISC = %00000000                   // Make PORTC all outputs
TRISA = %00000000
TRISD = %00000000
x = 0
y = 0



Output (led1)
Output (led2)
Output (led3)
Output (led4)
Output (led5)
Output (led6)
Output (led7)
Output (led8)
'Output (Battery)
//Output (Power)
Input (Switch)

Input(swt1)
Input(swt2)
Input(swt3)
Input(swt4)
Input(swt5)
Input(swt6)
Input(swt7)
Input(swt8)

led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
led7=0
led8=0
SetAllDigital()
counter = 12
KEY_BOUNCE_DELAY=50
While true

  Matrix_sel()  //start out with 12




 
Wend           // Loop back to the while loop as long as we havent finished.
 
Christ MrDeb... How do you follow code like that..

There are better ways to select that oodles of if else statements. I tried to sort through, but I gave up.

It seems like EVERY sub is identical, apart from the portd = x which means call 1 sub with a parameter for the portd= parameter.

Also.. Does Swordfish even allow pointers for ports... I mean portd = somearray(0)???
I see zero through 12 are constant 8 8bit arrays..

Very strange.
 
Last edited:
Here is the code a wee bit more visible
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/19/2016                                                     *
*  Version : 1.0                                                            *
*  Notes   :  COMMON CATHODE MATRIX                                                              *
*          :                                                                *
*****************************************************************************
}


//xxxxxxxxxxxxxxxxxxxxxxxxxx

Device = 18F43k22 ' Tell the compiler what chip we are using
Clock = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
//CONFIG MCLRE = OFF
Config FOSC = INTIO67 ' Internal oscillator, IO on pins 6 and 7
#option DIGITAL_INIT = true
Include "SetDigitalIO.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
Include "utils.bas"
Include "convert.bas"
Include "intosc.bas"
Include "ISRTimer.bas"   

Dim y As Byte
Dim x As Byte
Dim counter As Byte
Dim Switch As PORTE.0 //a.3
'Dim led1a As PORTD.3
'Dim led1c As PORTB.3
//Dim adval As Word
//Dim adcout As Word

//PLAYER LEDS
Dim led1 As PORTA.0
Dim led2 As PORTA.1
Dim led3 As PORTA.2
Dim led4 As PORTA.3
Dim led5 As PORTA.4
Dim led6 As PORTA.5
Dim led7 As PORTA.6
Dim led8 As PORTA.7

//PLAYER SWITCHES
Dim swt1 As PORTB.0
Dim swt2 As PORTB.1
Dim swt3 As PORTB.2
Dim swt4 As PORTB.3
Dim swt5 As PORTB.4
Dim swt6 As PORTB.5
Dim swt7 As PORTB.6
Dim swt8 As PORTB.7


Dim KEY_BOUNCE_DELAY As Byte
//Dim Battery As P8RTA.2  'hlvd
//Dim Power As PORTA.1     'hlvd

Interrupt press()

   DelayMS(50)

End Interrupt


// CONST Cathodes_Data(9)AS BYTE = (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,%00000000)                               
                      
Const Cathodes_Data(8)As Byte = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                                 %10111111,        ' this is for scanning the rows
                                 %11011111,        //cathodes
                                 %11101111,
                                 %11110111,
                                 %11111011,
                                 %11111101,
                                 %11111110)
                                
                                
                                
Const Cat_Data(8)As Byte =    (%10000000,
                               %01000000,
                               %00100000,
                               %00010000,
                               %00001000,
                               %00000100,
                               %00000010,                                 
                               %00000001)
                              
                              
  Const z_ro(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,                                 
                         %00000000) 

 Const One(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,                                 
                         %00000000)       

' number 2
                                
Const Two(8)As Byte =  (%10000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000001)                 
                                        
Const Three(8) As Byte =(%10000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000001)
                                        
 Const Four(8) As Byte =(%10000001,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)                                       
                                
 
Const Five(8) As Byte = (%10000001,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)   

Const Six(8) As Byte =  (%10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                                                            
Const Seven(8) As Byte =(%10010001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                        
                                                            
Const Eight(8) As Byte =(%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10000010,
                         %00000000)
                        
 Const Nine(8) As Byte= (%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000)
                        
                                                                                                  
 Const Ten(8) As Byte=  (%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                            
Const Eleven(8) As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                        
Const Tweleve(8)As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,                                 
                         %00000000)
// Const KEY_BOUNCE_DELAY(0) = 50     // msec delay for key bounce
                                                                              
// Arrays
Sub T_12()
    //12
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Tweleve(x)// colums 8 (L-R the X is the scan
            DelayUS(500)
        Next
        // wait for a key press
           If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(200)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
           EndIf
 
    Until Switch=0
    DelayMS(500)
    counter = 11   //11
End Sub

Sub E_11()    //11

    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Eleven(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            //PORTB = %11111111
            // DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = 10   //10
End Sub

Sub T_10()    //10

    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Ten(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            //PORTB = %11111111
            //  DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until
    Switch = 0
    DelayMS(500)
    counter = counter - 1   //9
End Sub
                                            
Sub N_9()    //9
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Nine(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            // DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1   //8
End Sub
                                                           
Sub E_8()    //8
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Eight(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            //   DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1    //7
End Sub
                
Sub S_7()    //7
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Seven(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            //   DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1       //6
End Sub
       
Sub S_6()    //SIX
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Six(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            //   DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1          //5
End Sub
      
Sub F_5()      //5
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Five(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            //   PORTB = %11111111
            //   DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0                    //4
    DelayMS(500)
    counter = counter - 1
End Sub
                            
Sub F_4()       //FOUR
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Four(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            //   PORTB = %11111111
            //  DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1        //3
End Sub

Sub T_3()    //3
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Three(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            // DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1           //2
End Sub
          
Sub T_2()            //2
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Two(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            //  PORTB = %11111111
            //  DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1              //1
End Sub
                                                  
                          
 Sub O_1()                         
    Repeat          //1                 
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = One(x)// colums 8 (L-R the X is the scan
            DelayUS(300)
            // PORTB = %11111111
            //   DelayUS(500)
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1     //0  blank display
End Sub
            
    
Sub zero()    // get the sub one to blink indicating zero                         
    Repeat          //1                    
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = One(x)// colums 8 (L-R the X is the scan
            DelayMS(300)
            //  PORTB = %11111111
            //   DelayMS(500)
            // PORTB = %00000000
        Next
        If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(500)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
        EndIf
    Until Switch=0
    DelayMS(500)
    counter = counter - 1     //0  blank display
End Sub
                            
//zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Sub Matrix_sel()

   If  counter = 12
        Then
        T_12()
    End If

    If  counter = 11
        Then
        E_11()
    End If

    If  counter = 10
        Then
        T_10()
    End If
  
    If  counter = 9
        Then
        N_9()
    End IF
    
    If  counter = 8
        Then
        E_8()
    End If
    
    If  counter = 7
        Then
        S_7()
    End If
    
    If  counter = 6
        Then
        S_6()
    End If
    
    If  counter = 5
        Then
        F_5()
    End If

    If  counter = 4
        Then
        F_4()
    End If

    If  counter = 3
        Then
        T_3()
    End If

    If  counter = 2
        Then
        T_2()
    End If

    If  counter = 1
        Then
        O_1()
    End If 
                                            
    ' If  counter = 0
    '    Then
    '    e_12()
    '    Until Switch=0
    '    DelayMS(500)      ''debounce
    ' End If                         
End Sub 

 
// Start Of Program
// set switch port to inputs and enable pullups
    TRISB = $ff                // set as inputs
    WPUB = $ff                // enable pullups on all PORTB pins
    INTCON2.bits(7) = 0        // set bit RBPU = 0 turns on pullups


//TRISB = %00000000                   // Make PORTB all inputs               
TRISC = %00000000                   // Make PORTC all outputs
TRISA = %00000000
TRISD = %00000000
x = 0
y = 0



Output (led1)
Output (led2)
Output (led3)
Output (led4)
Output (led5)
Output (led6)
Output (led7)
Output (led8)
'Output (Battery)
//Output (Power)
Input (Switch)

Input(swt1)
Input(swt2)
Input(swt3)
Input(swt4)
Input(swt5)
Input(swt6)
Input(swt7)
Input(swt8)

led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
led7=0
led8=0
SetAllDigital()
counter = 12
KEY_BOUNCE_DELAY=50
While true

  Matrix_sel()  //start out with 12

 
Wend           // Loop back to the while loop as long as we havent finished.

BUT!!! May I show the following... a tad fewer lines..


Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/19/2016                                                     *
*  Version : 1.0                                                            *
*  Notes   :  COMMON CATHODE MATRIX                                                              *
*          :                                                                *
*****************************************************************************
}


//xxxxxxxxxxxxxxxxxxxxxxxxxx

Device = 18F43k22 ' Tell the compiler what chip we are using
Clock = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
//CONFIG MCLRE = OFF
Config FOSC = INTIO67 ' Internal oscillator, IO on pins 6 and 7
#option DIGITAL_INIT = true
Include "SetDigitalIO.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
Include "utils.bas"
Include "convert.bas"
Include "intosc.bas"
Include "ISRTimer.bas"   

Dim y As Byte
Dim x As Byte
Dim counter As Byte
Dim Switch As PORTE.0 //a.3
'Dim led1a As PORTD.3
'Dim led1c As PORTB.3
//Dim adval As Word
//Dim adcout As Word

//PLAYER LEDS
Dim led1 As PORTA.0
Dim led2 As PORTA.1
Dim led3 As PORTA.2
Dim led4 As PORTA.3
Dim led5 As PORTA.4
Dim led6 As PORTA.5
Dim led7 As PORTA.6
Dim led8 As PORTA.7

//PLAYER SWITCHES
Dim swt1 As PORTB.0
Dim swt2 As PORTB.1
Dim swt3 As PORTB.2
Dim swt4 As PORTB.3
Dim swt5 As PORTB.4
Dim swt6 As PORTB.5
Dim swt7 As PORTB.6
Dim swt8 As PORTB.7


Dim KEY_BOUNCE_DELAY As Byte
//Dim Battery As P8RTA.2  'hlvd
//Dim Power As PORTA.1     'hlvd

Interrupt press()

   DelayMS(50)

End Interrupt


// CONST Cathodes_Data(9)AS BYTE = (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,%00000000)                               
                      
Const Cathodes_Data(8)As Byte = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                 %10111111,        ' this is for scanning the rows
                 %11011111,        //cathodes
                 %11101111,
                 %11110111,
                 %11111011,
                 %11111101,
                 %11111110)
                      
Const Cat_Data(8)As Byte =    (%10000000,
                 %01000000,
                 %00100000,
                 %00010000,
                 %00001000,
                 %00000100,
                 %00000010,                                 
                 %00000001)
                              
                              
  Const z_ro(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,                                 
                         %00000000) 

 Const One(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,                                 
                         %00000000)       

' number 2
                                
Const Two(8)As Byte =  (%10000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000001)                 
                                        
Const Three(8) As Byte =(%10000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000001)
                                        
 Const Four(8) As Byte =(%10000001,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)                                       
                                
 
Const Five(8) As Byte = (%10000001,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)   

Const Six(8) As Byte =  (%10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                                                            
Const Seven(8) As Byte =(%10010001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                        
                                                            
Const Eight(8) As Byte =(%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10000010,
                         %00000000)
                        
 Const Nine(8) As Byte= (%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000)
                        
                                                                                                  
 Const Ten(8) As Byte=  (%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                            
Const Eleven(8) As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                        
Const Tweleve(8)As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,                                 
                         %00000000)
// Const KEY_BOUNCE_DELAY(0) = 50     // msec delay for key bounce
                                                                              
// Arrays
Sub number(unit as byte)
    Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            if(units=12) PORTD = Tweleve(x)// colums 8 (L-R the X is the scan
            if(units=11) PORTD = Eleven(x)// colums 8 (L-R the X is the scan
            if(units=10) PORTD = Ten(x)// colums 8 (L-R the X is the scan
            if(units=9) PORTD = Nine(x)// colums 8 (L-R the X is the scan
            if(units=8) PORTD = Eight(x)// colums 8 (L-R the X is the scan
            if(units=7) PORTD = Seven(x)// colums 8 (L-R the X is the scan
            if(units=6) PORTD = Six(x)// colums 8 (L-R the X is the scan
            if(units=5) PORTD = Five(x)// colums 8 (L-R the X is the scan
            if(units=4) PORTD = Four(x)// colums 8 (L-R the X is the scan
            if(units=3) PORTD = Three(x)// colums 8 (L-R the X is the scan
            if(units=2) PORTD = Two(x)// colums 8 (L-R the X is the scan
            if(units=1) PORTD = One(x)// colums 8 (L-R the X is the scan
            if(units=0) PORTD = Zero(x)// colums 8 (L-R the X is the scan
            DelayUS(500)
        Next
        // wait for a key press
           If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
        ElseIf (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
        ElseIf (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
        ElseIf (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
        ElseIf (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
        ElseIf (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
        ElseIf (swt7 = 0) Then
            Toggle(led7)  DelayMS(200)
        ElseIf (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
           EndIf
 
    Until Switch=0
    DelayMS(500)
    counter = counter-1   
End Sub
                            
//zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Sub Matrix_sel()
      
    number(counter)
                        
End Sub 

 
// Start Of Program
// set switch port to inputs and enable pullups
    TRISB = $ff                // set as inputs
    WPUB = $ff                // enable pullups on all PORTB pins
    INTCON2.bits(7) = 0        // set bit RBPU = 0 turns on pullups


//TRISB = %00000000                   // Make PORTB all inputs               
TRISC = %00000000                   // Make PORTC all outputs
TRISA = %00000000
TRISD = %00000000
x = 0
y = 0



Output (led1)
Output (led2)
Output (led3)
Output (led4)
Output (led5)
Output (led6)
Output (led7)
Output (led8)
'Output (Battery)
//Output (Power)
Input (Switch)

Input(swt1)
Input(swt2)
Input(swt3)
Input(swt4)
Input(swt5)
Input(swt6)
Input(swt7)
Input(swt8)

led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
led7=0
led8=0
SetAllDigital()
counter = 12
KEY_BOUNCE_DELAY=50
While true

  Matrix_sel()  //start out with 12
    if counter = 0 then counter = 12    
 
Wend           // Loop back to the while loop as long as we havent finished.

Untested!! But shows the endless if else get toooooo much
 
HERE IS A SHORTEN VERSION BUT NOW LED7 AND 8 COME ON
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/13/2024                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

//xxxxxxxxxxxxxxxxxxxxxxxxxx

Device = 18F43k22 ' Tell the compiler what chip we are using
Clock = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
//CONFIG MCLRE = OFF
Config FOSC = INTIO67 ' Internal oscillator, IO on pins 6 and 7
#option DIGITAL_INIT = true
Include "SetDigitalIO.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
Include "utils.bas"
Include "convert.bas"
Include "intosc.bas"
Include "ISRTimer.bas"   

Dim y As Byte
Dim x As Byte
Dim counter As Byte
Dim Switch As PORTE.0 //a.3
'Dim led1a As PORTD.3
'Dim led1c As PORTB.3
//Dim adval As Word
//Dim adcout As Word

//PLAYER LEDS
Dim led1 As PORTA.0
Dim led2 As PORTA.1
Dim led3 As PORTA.2
Dim led4 As PORTA.3
Dim led5 As PORTA.4
Dim led6 As PORTA.5
Dim led7 As PORTA.6
Dim led8 As PORTA.7

//PLAYER SWITCHES
Dim swt1 As PORTB.0
Dim swt2 As PORTB.1
Dim swt3 As PORTB.2
Dim swt4 As PORTB.3
Dim swt5 As PORTB.4
Dim swt6 As PORTB.5
Dim swt7 As PORTB.6
Dim swt8 As PORTB.7


Dim KEY_BOUNCE_DELAY As Byte
//Dim Battery As P8RTA.2  'hlvd
//Dim Power As PORTA.1     'hlvd

Interrupt press()

   DelayMS(50)

End Interrupt


// CONST Cathodes_Data(9)AS BYTE = (%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,%00000000)                               
                      
Const Cathodes_Data(8)As Byte = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                                 %10111111,        ' this is for scanning the rows
                                 %11011111,        //cathodes
                                 %11101111,
                                 %11110111,
                                 %11111011,
                                 %11111101,
                                 %11111110)
                                
                                
                                
Const Cat_Data(8)As Byte =    (%10000000,
                               %01000000,
                               %00100000,
                               %00010000,
                               %00001000,
                               %00000100,
                               %00000010,                                 
                               %00000001)
                              
                              
  Const z_ro(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,                                 
                         %00000000)

 Const One(8)As Byte =  (%00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,                                 
                         %00000000)       

' number 2
                                
Const Two(8)As Byte =  (%10000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000000,
                        %00000001)                 
                                        
Const Three(8) As Byte =(%10000000,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000001)
                                        
 Const Four(8) As Byte =(%10000001,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)                                       
                                
 
Const Five(8) As Byte = (%10000001,
                         %00000000,
                         %00000000,
                         %00010000,
                         %00000000,
                         %00000000,
                         %00000000,
                         %10000001)   

Const Six(8) As Byte =  (%10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                                                            
Const Seven(8) As Byte =(%10010001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000,
                         %00000000,
                         %10000001,
                         %00000000)
                        
                                                            
Const Eight(8) As Byte =(%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10000010,
                         %00000000)
                        
 Const Nine(8) As Byte= (%10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000,
                         %00000000,
                         %10010010,
                         %00000000)
                        
                                                                                                  
 Const Ten(8) As Byte=  (%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                            
Const Eleven(8) As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10000010,
                         %00000000)
                        
                                                        
Const Tweleve(8)As Byte=(%10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,
                         %00000000,
                         %10010010,                                 
                         %00000000)
// Const KEY_BOUNCE_DELAY(0) = 50     // msec delay for key bounce
                                                                              
// Arrays


 
// Start Of Program
// set switch port to inputs and enable pullups
    TRISB = $ff                // set as inputs
    WPUB = $ff                // enable pullups on all PORTB pins
    INTCON2.bits(7) = 0        // set bit RBPU = 0 turns on pullups


//TRISB = %00000000                   // Make PORTB all inputs               
TRISC = %00000000                   // Make PORTC all outputs
//TRISA = %00000000
TRISD = %00000000
x = 0
y = 0


//PLAYER LEDS PORTA
Output (led1)
Output (led2)
Output (led3)
Output (led4)
Output (led5)
Output (led6)
Output (led7)
Output (led8)
'Output (Battery)
//Output (Power)
Input (Switch)  //COUNTER SWITCH


//PLAYER SWITCHES  PORTB
Input(swt1)
Input(swt2)
Input(swt3)
Input(swt4)
Input(swt5)
Input(swt6)
Input(swt7)
Input(swt8)


//PLAYER LEDS  ALL OFF
led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
led7=0
led8=0


//PORTA = %00000000
//PORTB = %00000000
//PORTC = %00000000
//PORTD = %00000000
//PORTE = %00000000
//SetAllDigital()
counter = 12
KEY_BOUNCE_DELAY=50
While true

 // Matrix_sel()  //start out with 12
 Repeat
        For x = 0 To 7
            PORTC = Cathodes_Data(x) //rows top to bottom
            PORTD = Eleven(x)// colums 8 (L-R the X is the scan
            DelayUS(500)
        Next
        // wait for a key press
           If (swt1 = 0) Then
            Toggle(led1) DelayMS(500)
            End If
        If (swt2 = 0) Then
            Toggle(led2)  DelayMS(500)
            End If
        If (swt3 = 0) Then
            Toggle(led3)  DelayMS(500)
            End If
        If (swt4 = 0) Then
            Toggle(led4)   DelayMS(500)
            End If
       If (swt5 = 0) Then
            Toggle(led5)   DelayMS(500)
            End If
       If (swt6 = 0) Then
            Toggle(led6)  DelayMS(500)
            End If
       If (swt7 = 0) Then
            Toggle(led7)  DelayMS(200)
            End If
       If (swt8 = 0) Then
            Toggle(led8)   DelayMS(500)
           EndIf
 
    Until Switch=0
    DelayMS(500)
    counter = 11   //11
    
 
Wend           // Loop back to the while loop as long as we havent finished.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top