Posted by Anonymous on Wed 30th Apr 10:33 (modification of post by Anonymous view diff)
diff | download | new post
- '****************************************************************
- '* Name : PCCLD106.bas *
- '* Author : Shannon Wynter *
- '* Notice : Copyright (c) 2008 Shannon Wynter *
- '* : All Rights Reserved *
- '* Date : 28/04/2008 *
- '* Version : 1.0 *
- '* Notes : *
- '* : *
- '****************************************************************
- ' Configure the FUSE options for the compiler
- @ device pic16F628A, hs_osc, wdt_off, pwrt_on, lvp_off, protect_off, bod_on, cpd_off, pwrt_off, mclr_off
- ' Running a 20Mhz clock
- DEFINE OSC 20
- ' Setup some EEPROM values
- EEPROM 0, [140, 140, 255, 127]
- ' Constants
- cfgLength CON 4 ' Number of configuration bytes we're using
- versionMajor CON 0
- versionMinor CON 1
- ' Setup the USART
- DEFINE HSER_RCSTA 90h ' Receive register to receiver enabled
- DEFINE HSER_TXSTA 20h ' Transmit register to transmitter enabled
- DEFINE HSER_BAUD 2400 ' Set baud rate
- ' Define some aliases for USART
- OERR VAR RCSTA.1 ' Over-run bit
- CREN VAR RCSTA.4 ' Continuous receive enable bit
- RCIF VAR PIR1.5 ' Received character interrupt flag bit
- ' Define some aliases for PINS
- OVERFLOW VAR PORTB.5 ' Alias pin 11 to indicate an overflow state
- HEARTBEAT VAR PORTB.4 ' Alias pin 10 to indicate heartbeat
- LDR1 VAR PORTA.0 ' Alias pin 17 for reading LDR1
- LDR2 VAR PORTA.1 ' Alias pin 18 for reading LDR2
- REED VAR PORTA.2 ' Alias pin 1 for reading the REED status
- CMCON = 7 ' Turn off the comparators
- ' Set the direction of pins
- INPUT LDR1 ' Set Pin 17 for INPUT
- INPUT LDR1 ' Set Pin 18 for INPUT
- ' Make some storage
- myState VAR byte ' A state holder
- pwmLevel VAR BYTE ' Current dutycycle for the HWPWM
- pwmFrequency VAR WORD ' The frequency to run the PWM at
- serialByte1 VAR BYTE ' A serial byte holder
- LDR2Value VAR BYTE ' LDR1 value
- LDR1Value var BYTE ' LDR2 value
- LDR1Scale VAR BYTE ' LDR1 Scale byte
- LDR2Scale VAR BYTE ' LDR2 Scale byte
- heartBeatRate VAR Byte ' Helps us limit the amount of flashes per second
- cfgData VAR BYTE[cfgLength]' Used while reading config data from serial
- tmp VAR BYTE ' A throw away byte
- ' Configure the interrupt handler
- INTCON = %11000000 ' Enable the PIE & Global interrupt handler
- PIE1.5 = 1 ' Enable interrupt on USART Receive
- ' Set up default values
- myState = 0 ' Should be off
- pwmlevel = 0 ' Should be off
- heartBeatRate = 50 ' Beat twice per second
- GOSUB reload_eeprom
- ' Turn the heartbeat led on
- HIGH HEARTBEAT
- main_loop:
- ' Manage the heartbeat
- heartBeatRate = heartBeatRate - 1 ' Decrement counter
- IF heartBeatRate = 0 THEN
- TOGGLE HEARTBEAT ' Toggle the heartbeat led
- LOW OVERFLOW ' Reset the overflow led
- ' We throw the LDR readings in here also, not much point in checking light
- ' levels more then twice per second
- POT LDR1, LDR1Scale, LDR1Value
- POT LDR2, LDR2Scale, LDR2Value
- ' ToDo: add code to manage logic behind light levels
- heartBeatRate = 50 ' Reset the Heartbeat rate
- endif
- ' Manage states
- IF mystate = 1 THEN ' Fading down?
- IF pwmLevel = 0 THEN
- mystate = 0 ' Set state to off(0) if finished fading
- ELSE
- pwmLevel = pwmLevel - 1 ' Continue fading
- ENDIF
- ENDIF
- IF mystate = 2 THEN ' Fading up?
- IF pwmLevel = 255 THEN
- mystate = 3 ' Set state to on(3) if finished fading
- ELSE
- pwmLevel = pwmlevel + 1 ' Continue fading
- ENDIF
- ENDIF
- ' Manage the hardware PWM. Should probably completely turn it off/on when in
- ' the off/on state to save power
- HPWM 1,pwmLevel,pwmFrequency
- ' Wait 10 ms (The fade looks best at 10 ms increments)
- PAUSE 10
- ' Go back through the main program loop
- GOTO main_loop
- ' The interrupt handler
- DISABLE ' Disable interrupts while in this code block
- interrupt_handler:
- WHILE RCIF ' While there are bytes to be read
- serialByte1 = RCREG ' Read a byte
- HSEROUT [1, serialByte1] ' Tell the sender what byte was received
- IF serialByte1 = 3 THEN ' If C: Print back the LDR values
- HSEROUT [LDR1Value, LDR2Value]
- ENDIF
- IF serialByte1 = 4 THEN HSEROUT [REED]
- IF serialByte1 = 253 THEN
- HSEROUT [LDR1Scale, LDR2Scale, pwmFrequency.LowByte, pwmFrequency.HighByte]
- ENDIF
- IF serialByte1 = 254 THEN
- HSERIN 10000, cancel_set, [STR cfgData\cfgLength]
- HSEROUT [STR cfgData\cfgLength]
- FOR tmp = 0 TO 10
- NEXT tmp
- GOSUB reload_eeprom
- GOTO done_set
- cancel_set:
- HSEROUT [REP 0\cfgLength]
- done_set:
- ENDIF
- IF serialByte1 = 255 THEN HSEROUT [versionMinor, versionMajor]
- WEND
- ' Clear overruns if present
- IF OERR = 1 THEN
- CREN = 0
- CREN = 1
- HIGH OVERFLOW ' Light the overflow led
- ENDIF
- RESUME
- ' Load EEPROM values
- reload_eeprom:
- READ 0, LDR1Scale
- READ 1, LDR2Scale
- READ 2, tmp
- pwmFrequency.LowByte = tmp
- READ 3, tmp
- pwmFrequency.HighByte = tmp
- RETURN
- ENABLE
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.