PDA

View Full Version : Oscillator Switching



Bruce
- 25th July 2010, 02:28
This example shows how to switch between internal & external oscillators, with a POT to adjust cycle speeds of the LED bargraph on the LAB-X1 board.


' Name : BLINK1934X.pbp
' Compiler : PICBASIC PRO Compiler 2.6
' Assembler : MPASM
' Target PIC : 40-pin 16F1934 or similar
' Hardware : LAB-X1 Experimenter Board
' Oscillator : 4MHz external crystal
' Keywords : ADCIN, DO LOOP, EXIT, FOR NEXT, REPEAT UNTIL, STEP
' Description : PICBASIC PRO program to cycle bargraph LEDs on PORTD in different
' patterns, with speed control & oscillator switching from external to internal.
' Uses POT1 on RA0 to set cycle speed, and keypad button #13 to alternate between
' LED patterns & oscillator selection.
'
' Note: Open the 16F1934.INC file in your PBP directory, and comment out the default
' __config settings.

ASM
__config _CONFIG1, _FOSC_XT & _WDTE_SWDTEN & _PWRTE_ON & _MCLRE_ON & _BOREN_ON & _IESO_ON & _FCMEN_ON
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _STVREN_ON & _BORV_25
ENDASM

' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 4 ' Set clock source Fosc/4
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

i Var Byte ' Define loop variable
Delay Var Word ' LED delay time
LEDS Var PORTD ' Alias PORTD to LEDS

Gosub Init ' Hardware initialization routine

mainloop:
LEDS = 1 ' First LED on
Do
For i = 1 To 7 ' Go through For..Next loop 7 times
LEDS = LEDS << 1 ' Shift on LED to the left
Gosub GetDelay ' Get new Delay value
Pause Delay ' Pause for Delay period
Next i

' Exit allows us to exit the loop mid-stream
IF !PORTB.4 Then Exit ' Exit Do Loop if key pressed

For i = 7 To 1 Step-1 ' Go through For..Next loop 7 times
LEDS = LEDS >> 1 ' Shift on LED to the right
Gosub GetDelay ' Get new Delay value
Pause Delay ' Pause for Delay period
Next i
Loop Until PORTB.4 = 0 ' Loop until press on keypad button #13

' Flipping OSCCON.1 to 1 selects the internal oscillator.
' Flipping OSCCON.1 to 0 selects the external oscillator.

OSCCON.1 = 1 ' Switch from 4MHz external to 16MHz internal oscillator
Repeat ' Change LED pattern on keypad button #13 press
Gosub GetDelay
LEDS = %00011000
Gosub GetDelay ' Get new Delay value
Pause Delay
LEDS = %00100100
Gosub GetDelay ' Get new Delay value
Pause Delay
LEDS = %01000010
Gosub GetDelay ' Get new Delay value
Pause Delay
LEDS = %10000001
Gosub GetDelay ' Get new Delay value
Pause Delay
LEDS = %01000010
Gosub GetDelay ' Get new Delay value
Pause Delay
LEDS = %00100100
Gosub GetDelay ' Get new Delay value
Pause Delay
Until PORTB.4 = 1 ' Repeat above until keypad button #13 is released

OSCCON.1 = 0 ' Switch back to external 4MHz oscillator
Goto mainloop ' Blink LEDs forever

GetDelay:
Adcin 0,Delay ' Read POT1 on RA0 input for 10-bit Delay value
Return

Init:
OSCCON = %01111000 ' Internal osc is 16MHz, but using external for now
TRISD = 0 ' Set PORTD to all outputs
TRISB = %11110111 ' RB3 to output, rest inputs
PORTB.3 = 0 ' RB3 low for keypad button #13 low input
ANSELA = 1 ' Set PortA 0 to analog POT1 input, rest digital
ANSELB = 0 ' portb all digital
ANSELD = 0 ' portd all digital
ANSELE = 0 ' porte all digital
ADCON1 = %11000000 ' Right justify for 10-bit, Fosc/4, +Vref/-Vref = Vdd/gnd
OPTION_REG.7 = 0 ' Internal pull-ups on (RB4 is held high until key #13 is pressed)
Return ' Setup complete, return to caller

End