using an 18f43k22 and need to set the portb weak pullups as I am using tactile switches on all 8 pins of port b.
found this but not sure if it is correct? intcon2bits.rbpu=0
SOMETHING WRONG?
the setalldigital should disable ADC
I have the switch connected to grd
the other side of the switch is connected to portb.0
pressing the button has no effect?
portb pullups configured correctly?
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 7/18/2021 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
// tone generator using CCP compare
Device = 18F43K22
Clock = 64
Include "intosc.bas"
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
// specify tone library output pin
#option _TONE_OUTPUT_PIN = PORTD.2
Include "tone.bas"
Dim ix As Byte
Dim note As Word
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
INPUT(SWT1)
WPUB=255
main:
// init tone library module
tone.init()
IF SWT1=0
THEN
TONE.PLAY(NOTE_F4)
DELAYMS(1000)
tone.stop()
ENDIF
// play A (440) until stop()
tone.play(NOTE_B4)
DelayMS(1000)
tone.stop()
// play all 88 notes for 100msecs each
For ix = 0 To Bound(tone.notes)
note = tone.notes(ix) // current note
tone.play(note, 100)
While(tone.isPlaying())
End While
DelayMS(10) // short delay between notes
Next
While (true)
'IF SWT1=0
'THEN
'TONE.PLAY(NOTE_F4)
'DELAYMS(1000)
'tone.stop()
'ENDIF
End While
WPUB sets a pullup enable mask, but it doesn't turn them on.
To do that, you have to set INTCON2.bits(7) = 0 (bit 7 is the RBPU bit).
See datasheet section 10.3
Try:
Code:
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 7/18/2021 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
// tone generator using CCP compare
Device = 18F43K22
Clock = 64
Include "intosc.bas"
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
// specify tone library output pin
#option _TONE_OUTPUT_PIN = PORTD.2
Include "tone.bas"
Dim ix As Byte
Dim note As Word
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
' make all of PORTB inputs
TRISB = $FF
' set pullup enable mask for all PORTB pins
WPUB = 255
' and turn on the pullups (RBPU=0)
INTCON2.bits(7) = 0
main:
// init tone library module
tone.init()
// play A (440) until stop()
tone.play(NOTE_B4)
DelayMS(1000)
tone.stop()
// play all 88 notes for 100msecs each
For ix = 0 To Bound(tone.notes)
note = tone.notes(ix) // current note
tone.play(note, 100)
While(tone.isPlaying())
End While
DelayMS(10) // short delay between notes
Next
While (true)
IF SWT1=0 THEN
TONE.PLAY(NOTE_F4)
DELAYMS(1000)
tone.stop()
ENDIF
End While
The SetAllDigital() routine sets the port analog control bits for all the pins to digital mode (ANSELx=0 for the K22).
It does a few other things as well, such as turn off the comparators and set pin slew rate to normal.
When I said it disables the ADC that wasn't really true...
It was in OPTION_REG back in the 16F877 (and 16C(F)84) days, but most remotely modern devices have optional pullups on all port pins, and individually switchable.
Well now to fine tune the melody to twinkle twinkle little star and adjust for proper notes. It sounds like the twinkle melody but needs work.
Need to add code for LCD and sub routines for HAPPY BIRTHDAY, JINGLE BELLS, MARY HAD A LITTLE LAMB AND LONDON BRIDGES. Almost forgot BABY SHARK. Then add in LEDs.
[CODE Swordfish basic]
// tone generator using CCP compare
Device = 18F43K22
Clock = 64
Include "intosc.bas"
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"
// specify tone library output pin
#option _TONE_OUTPUT_PIN = PORTD.2
Include "tone.bas"
Dim ix As Byte
Dim note As Word
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
'input(swt1)
' make all of PORTB inputs
TRISB = $FF
' set pullup enable mask for all PORTB pins
WPUB = 255
' and turn on the pullups (RBPU=0)
INTCON2.bits(7) = 0
main:
// init tone library module
tone.init()
// play all 88 notes for 100msecs each
//INTRO PLAY ALL 88 NOYES
For ix = 0 To Bound(tone.notes)
note = tone.notes(ix) // current note
tone.play(note, 100)
While(tone.isPlaying())
End While
DelayMS(10) // short delay between notes
Next
While (true)
'If SWT1=0 Then
tone.play(NOTE_C4)
DelayMS(1000)
tone.stop()
' EndIf
' If SWT2=0 Then
tone.play(NOTE_c4)
DelayMS(1000)
tone.stop()
' EndIf
' If SWT3=0 Then
tone.play(NOTE_g4)
DelayMS(1000)
tone.stop()
' EndIf
'If SWT1=0 Then
tone.play(NOTE_g4)
DelayMS(1000)
tone.stop()
'EndIf
'If SWT2=0 Then
tone.play(NOTE_a4)
DelayMS(1000)
tone.stop()
'EndIf
' If SWT3=0 Then
tone.play(NOTE_a4)
DelayMS(1000)
tone.stop()
'EndIf
'If SWT1=0 Then
tone.play(NOTE_g4)
DelayMS(1000)
tone.stop()
'EndIf
'If SWT2=0 Then
tone.play(NOTE_f4)
DelayMS(1000)
tone.stop()
' EndIf
' If SWT3=0 Then
tone.play(NOTE_f4)
DelayMS(1000)
tone.stop()
'EndIf
' If SWT1=0 Then
tone.play(NOTE_d4)
DelayMS(1000)
tone.stop()
'EndIf
'If SWT2=0 Then
tone.play(NOTE_d4)
DelayMS(1000)
tone.stop()
'EndIf
' If SWT3=0 Then
tone.play(NOTE_c4)
DelayMS(1000)
tone.stop()
'EndIf
I have version 2230 1200 sent email to website but? I copied and repasted the two codes but still get the same errors. Won't compile AFTER I added a new copy of the tone.bas. need to figure out a work around