how does one create multi-port arrays?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
MrDEB
Posts: 12
Joined: Fri Apr 24, 2009 1:45 am
Location: Salmon, Idaho

how does one create multi-port arrays?

Post by MrDEB » Fri Apr 24, 2009 7:15 pm

am trying to intergrate 4 multi port variables using a 4 pos dip switch to change the variable SPEED
Thanks to Pommie, Blueroom and be80be over at electrotech I have gotton this far. Please note that this programming is all new to me. Did some on the Comodore computers years ago. I want to add to SPEED a value of 50 per dip switch setting=all on = 175 where SPEED starts at 25
50 x 3 + 25=175 if I did my math right. have ra0,rb2 &rb3 already in use. Rb1 is presently working to change speed to 25

Code: Select all

DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip3 AS PORTB.1
DIM dip3TRIS AS TRISB.1 //changes delay
DIM dip4 AS PORTB.0 //changes # of tones
DIM dip4TRIS AS TRISB.0
DIM PIR AS PORTB.2 // turns on PIR
DIM PIRTris AS TRISB.2
DIM speed AS BYTE
DIM TUNE AS BYTE


//global variables


DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE
    
//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)

//interrupt routine

INTERRUPT MyInt()
    T1CON.0=0 //stop timer
    TMR1=-Tones(Tone) //reset period
    T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    PIR=1 //PIR off
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    toggle(amp)//turn amp on
    ENDIF
    TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
    FOR i = 0 TO 7 //generate 8 bits
    Feed = Seed.30 XOR Seed.27 //make new bit
    Seed=Seed*2+Feed //shift seed left and add new bit
    NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
    END FUNCTION

//main code starts here

OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    WHILE(TRUE) 			//repeat forever
    if PORTB.1=0 then  // dip switch setting
        speed=25
    else
        speed=200
    endif
WHILE(TRUE) //repeat forever
    
    if PORTb.0=0 then
        speed=25
    portb.1=0 then speed+50
        else
        speed=25
    endif
    IF PORTB.2=0 THEN 		//if PIR activated portRB2 is low
        FOR i = 1 TO 200 	//play 20 tones
            Tone=Rand(5) 	//each tone is random frequency
            DELAYMS(speed) 	//and for 25-200 seconds here is where I want to change
        NEXT 			//end for loop
    ELSE 			//otherwise
        Tone=5 			//silence
        i=Rand(255) 		//make rand more random
    ENDIF 			//end if condition

   
   Tone=5 //silence
   i=Rand(255) //make rand more random
   

WEND //end of while loop

   
   

MrDEB
Posts: 12
Joined: Fri Apr 24, 2009 1:45 am
Location: Salmon, Idaho

try something different??

Post by MrDEB » Sat Apr 25, 2009 3:01 am

Code: Select all

DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip1 AS PORTa.1
DIM dip1TRIS AS TRISa.1 //changes speed
DIM dip2 AS PORTa.2
DIM dip2TRIS AS TRISa.2 //changes speed
DIM dip3 AS PORTa.3
DIM dip3TRIS AS TRISa.3 //changes speed
DIM dip4 AS PORTa.4 //changes # of tones
DIM dip4TRIS AS TRISa.4
DIM PIR AS PORTB.2 // turns on PIR
DIM PIRTris AS TRISB.2
DIM speed AS BYTE



//global variables


DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE
    
//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)
[b]Const 
      constArray(4) as byte=(25,40,55,70)
      for array = 0 to bound(4)
      dip1=25  //dip switch values
      dip2=40
      dip3=55
      dip4=70
      speed()=dip1+dip2+dip3+dip4
      next
     [/b]
      
end
      
      
//interrupt routine

INTERRUPT MyInt()
    T1CON.0=0 //stop timer
    TMR1=-Tones(Tone) //reset period
    T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    PIR=1 //PIR off
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    TOGGLE(Amp)//turn amp on
    ENDIF
    TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
    FOR i = 0 TO 7 //generate 8 bits
    Feed = Seed.30 XOR Seed.27 //make new bit
    Seed=Seed*2+Feed //shift seed left and add new bit
    NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
    END FUNCTION

//main code starts here

OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    WHILE(TRUE) 			//repeat forever
    speed=dip1+dip2+dip3+dip4
        
    ELSE
        speed=200
    ENDIF
WHILE(TRUE) //repeat forever
    
    IF PORTB.0=0 THEN
        speed=25
    PORTB.1=0 THEN speed+50
        ELSE
        speed=25
    ENDIF
    IF PORTB.2=0 THEN 		//if PIR activated portRB2 is low
        FOR i = 1 TO 200 	//play 20 tones
            Tone=Rand(5) 	//each tone is random frequency
            DELAYMS(speed) 	//and for 25-200 seconds here is where I want to change
        NEXT 			//end for loop
    ELSE 			//otherwise
        Tone=5 			//silence
        i=Rand(255) 		//make rand more random
    ENDIF 			//end if condition

   
   Tone=5 //silence
   i=Rand(255) //make rand more random
   

WEND //end of while loop

   
   
not sure of what I am doing??

Post Reply