Files
SyncHome/trunk/workspace/gcb/Interrupt driving LED via PWM - 16F886.gcb
2023-03-09 12:51:54 +00:00

82 lines
2.1 KiB
Plaintext

' ########### start ###########
' pic:PIC16F886
' ########### end ###########
; FILE: Interrupt driving LED via PWM - 16F886.gcb
; DATE: 31.01.2015
; VERSION: 1.0a
; AUTHOR: Evan R. Venn based on works of others
;
; Description.
' A demonstration program for GCGB and GCB.
' This adjusts the power to the LED over 2.5 seconds, then two zero power again over 2.5 seconds.
' The LED is connected to Portb.0 via appropiate resistors.
' The LED will reflect the value of AN0 in terms of brightness
; This file is part of the Great Cow Basic compiler.
;
; This demonstration code is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
#chip 16F886,16
; ----- Define Hardware settings
Dir Portb.0 out
; ----- Constants
#define LED Portb.0
; ----- Main body of program commences here.
'Call the initialisation routine
InitLEDConrol
Do
'Increase power to full over 2.5 seconds
For LEDPower = 0 to 100
LEDlum = LEDPower
Wait 25 ms
Next
'Hold LEDPower
Wait 1 s
'Decrease power to zero over 2.5 seconds
For LEDPower = 100 to 0
LEDlum = LEDPower
Wait 25 ms
Next
'Hold LEDPower
Wait 1 s
Loop
; ----- Support methods. Subroutines and Functions
'Setup routine
Sub InitLEDConrol
'Clear variables
LEDlum = 0
PWMCounter = 0
'Add a handler for the interrupt
On Interrupt Timer0Overflow Call PWMHandler
'Set up the timer
InitTimer0 Osc, PS0_2
'Timer 0 starts automatically on a PIC
End Sub
'This will be called when Timer 0 overflows
Sub PWMHandler
If LEDlum > PWMCounter Then
Set LED On
Else
Set LED Off
End If
PWMCounter += 1
If PWMCounter = 100 Then PWMCounter = 0
End Sub