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.

setting weak pull ups on portb

Status
Not open for further replies.

MrDEB

Well-Known Member
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
 
found this but not sure if it is correct?
intcon2bits.rbpu=0
Not sure where you found that, but that syntax won't work w/SF unless you have something to define it that way.

the setalldigital should disable ADC
It does.

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
 
will give it a go.
assembled this morning an 8 tactile keypad so I can get the correct melodies for different tunes.
 
Apologies... I saw Intcon2 and assumed... !WPUB was always in the option reg... Once again sorry MrDeb..

You will still need the WPUB to set them all on..

However PBADEN is in the Config setup... I don't know whether AllDigital switches it off or not!
 
I saw Intcon2 and assumed. ..
It is an odd place to put the WPUB control, but that's Microchip for ya.

However PBADEN is in the Config setup... I don't know whether AllDigital switches it off or not!
It doesn't.

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




End While[/CODE]
 
MrDeb,

You should check out post #46 in your other topic
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top