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 Wed 30th Apr 09:12 (modification of post by Anonymous view diff)
View followups from Anonymous | diff | download | new post

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