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