pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Advertising: Renegade Motorhomes - Cheap Car Insurance - Cheap Flights - Phoenix Pools
QBasic/QuickBASIC paste-bin - collaborative debugging tool View Help

Posted by Anonymous on Tue 29th Apr 21:13 (modification of post by Anonymous view diff)
View followups from Anonymous | diff | download | new post

  1. '****************************************************************
  2. '*  Name    : PCCLD106.bas                                      *
  3. '*  Author  : Shannon Wynter (http://fremnet.net)               *
  4. '*  Notice  : Copyright (c) 2008 Shannon Wynter                 *
  5. '*          : All Rights Reserved                               *
  6. '*  Date    : 28/04/2008                                        *
  7. '*  Version : 1.0                                               *
  8. '*  Notes   :                                                   *
  9. '*          :                                                   *
  10. '****************************************************************
  11.  
  12. ' Configure the FUSE options for the compiler
  13. @     device pic16F628A, hs_osc, wdt_off, pwrt_on, lvp_off, protect_off, bod_on, cpd_off, pwrt_off, mclr_off
  14.  
  15. ' Running a 20Mhz clock
  16. DEFINE OSC 20
  17.  
  18. ' Setup some EEPROM values
  19. EEPROM 0, [255, 255, 127, 254]
  20.  
  21. ' Constants
  22. cfgLength CON 4
  23.  
  24. ' Setup the USART
  25. DEFINE HSER_RCSTA 90h     ' Receive register to receiver enabled
  26. DEFINE HSER_TXSTA 20h     ' Transmit register to transmitter enabled
  27. DEFINE HSER_BAUD 2400     ' Set baud rate
  28.  
  29. ' Define some aliases for USART
  30. OERR VAR RCSTA.1 ' Over-run bit
  31. CREN VAR RCSTA.4 ' Continuous receive enable bit
  32. RCIF VAR PIR1.5  ' Received character interrupt flag bit
  33.  
  34. ' Define some aliases for PINS
  35. OVERFLOW VAR PORTB.5  ' Alias pin 11 to indicate an overflow state
  36. HEARTBEAT VAR PORTB.4 ' Alias pin 10 to indicate heartbeat
  37. LDR1 VAR PORTA.0      ' Alias pin 17 for reading LDR1
  38. LDR2 VAR PORTA.1      ' Alias pin 18 for reading LDR2
  39.  
  40. CMCON = 7 ' Turn off the comparators
  41.  
  42. ' Set the direction of pins
  43. INPUT LDR1             ' Set Pin 17 for INPUT
  44. INPUT LDR1             ' Set Pin 18 for INPUT
  45.  
  46. ' Make some storage
  47. myState VAR byte           ' A state holder
  48. pwmLevel VAR BYTE          ' Current dutycycle for the HWPWM
  49. pwmFrequency VAR WORD      ' The frequency to run the PWM at
  50. serialByte1 VAR BYTE       ' A serial byte holder
  51. LDR2Value VAR BYTE         ' LDR1 value
  52. LDR1Value VAR BYTE         ' LDR2 value
  53. LDR1Scale VAR BYTE         ' LDR1 Scale byte
  54. LDR2Scale VAR BYTE         ' LDR2 Scale byte
  55. heartBeatRate VAR Byte     ' Helps us limit the amount of flashes per second
  56. cfgData VAR BYTE[cfgLength]' Used while reading config data from serial
  57. tmp VAR BYTE               ' A throw away byte
  58.  
  59. ' Configure the interrupt handler
  60. ON INTERRUPT GOTO interrupt_handler ' Where to go
  61. INTCON = %11000000                  ' Enable the PIE & Global interrupt handler
  62. PIE1.5 = 1                          ' Enable interrupt on USART Receive
  63.  
  64. ' Set up default values
  65. myState = 0        ' Should be off
  66. pwmlevel = 0       ' Should be off
  67. heartBeatRate = 50 ' Beat twice per second
  68.  
  69. GOSUB reload_eeprom
  70.  
  71. ' Turn the heartbeat led on
  72. HIGH HEARTBEAT
  73.  
  74. main_loop:
  75.  
  76. ' Manage the heartbeat
  77. heartBeatRate = heartBeatRate - 1 ' Decrement counter
  78. IF heartBeatRate = 0 THEN
  79.     TOGGLE HEARTBEAT   ' Toggle the heartbeat led
  80.     LOW OVERFLOW       ' Reset the overflow led
  81.    
  82.     ' We throw the LDR readings in here also, not much point in checking light
  83.     ' levels more then twice per second
  84.     POT LDR1, LDR1Scale, LDR1Value
  85.     POT LDR2, LDR2Scale, LDR2Value
  86.     ' ToDo: add code to manage logic behind light levels
  87.  
  88.     heartBeatRate = 50 ' Reset the Heartbeat rate
  89. endif
  90.  
  91. ' Manage states
  92. IF mystate = 1 THEN             ' Fading down?
  93.     IF pwmLevel = 0 THEN
  94.         mystate = 0             ' Set state to off(0) if finished fading
  95.     ELSE
  96.         pwmLevel = pwmLevel - 1 ' Continue fading
  97.     ENDIF
  98. ENDIF
  99. IF mystate = 2 THEN             ' Fading up?
  100.     IF pwmLevel = 255 THEN
  101.         mystate = 3             ' Set state to on(3) if finished fading
  102.     ELSE
  103.         pwmLevel = pwmlevel + 1 ' Continue fading
  104.     ENDIF
  105. ENDIF
  106.    
  107. ' Manage the hardware PWM. Should probably completely turn it off/on when in
  108. ' the off/on state to save power
  109. HPWM 1,pwmLevel,pwmFrequency
  110.  
  111. ' Wait 10 ms (The fade looks best at 10 ms increments)
  112. PAUSE 10
  113. ' Go back through the main program loop
  114. GOTO main_loop
  115.  
  116. ' The interrupt handler
  117. DISABLE ' Disable interrupts while in this code block
  118. interrupt_handler:
  119.     WHILE RCIF                                 ' While there are bytes to be read
  120.         serialByte1 = RCREG                    ' Read a byte
  121.         HSEROUT [1, serialByte1] ' Tell the sender what byte was received
  122.         IF serialByte1 = 1 AND myState > 1 THEN myState = 1 ' If A: Check and set state to fade off
  123.         IF serialByte1 = 2 AND myState < 2 THEN myState = 2 ' If B: Check and set state to fade on
  124.         IF serialByte1 = 3 THEN                             ' If C: Print back the LDR values
  125.             HSEROUT [LDR1Value, LDR2Value]
  126.         ENDIF
  127.         IF serialByte1 = 254 THEN
  128.             HSEROUT [LDR1Scale, LDR2Scale, pwmFrequency]
  129.         ENDIF
  130.         IF serialByte1 = 255 THEN
  131.             HSERIN 10000, cancel_set, [WAITSTR cfgData\cfgLength]
  132.             HSEROUT [STR cfgData\cfgLength]
  133.             FOR tmp = 0 TO 10
  134.                 WRITE tmp, cfgData[tmp]
  135.             NEXT tmp
  136.             GOSUB reload_eeprom
  137.             GOTO done_set
  138. cancel_set:
  139.             HSEROUT [REP 0\cfgLength]
  140. done_set:
  141.         ENDIF
  142.            
  143.     WEND
  144.  
  145.     ' Clear overruns if present
  146.     IF OERR = 1 THEN
  147.         CREN = 0
  148.         CREN = 1
  149.         HIGH OVERFLOW ' Light the overflow led
  150.     ENDIF
  151.     RESUME
  152.  
  153. ' Load EEPROM values
  154. reload_eeprom:
  155.     READ 0, LDR1Scale
  156.     READ 1, LDR2Scale
  157.     READ 3, tmp
  158.     pwmFrequency.HighByte = tmp
  159.     READ 4, tmp
  160.     pwmFrequency.LowByte = tmp
  161.     RETURN
  162. 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.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me
: