home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 93win / data1.cab / Basic_Plus_Examples / FUNCGEN < prev    next >
Encoding:
Text File  |  2005-03-02  |  10.3 KB  |  250 lines

  1. 10    ! *********************************************************************
  2. 20    ! Example: Function Generator
  3. 30    !
  4. 40    ! This program shows how a panel, menu, and pushbuttons can be
  5. 50    ! used to construct a front panel for a function generator.
  6. 60    !
  7. 70    ! *********************************************************************
  8. 80    !
  9. 90    ! This part of the program creates a panel with one menu selection.
  10. 100    ! Four menu buttons are put into the menu. When any of these menu
  11. 110    ! items is selected, an appropriate subroutine is called.
  12. 120   !
  13. 130  DIM Attr$(3)[15],Attr(3)
  14. 140  COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  15. 150  ASSIGN @Fgen_panel TO WIDGET "PANEL";SET ("TITLE":" Example: Function Generator","X":150,"Y":80,"WIDTH":240,"HEIGHT":200,"MAXIMIZABLE":0,"RESIZABLE":0)
  16. 160  CONTROL @Fgen_panel;SET ("SYSTEM MENU":"Quit")
  17. 170  ON EVENT @Fgen_panel,"SYSTEM MENU" GOTO Finis
  18. 180   !
  19. 190  ASSIGN @Fgen_main TO WIDGET "PULLDOWN MENU";SET ("LABEL":"Control Menu"),PARENT @Fgen_panel
  20. 200  ASSIGN @Frequency TO WIDGET "MENU BUTTON";SET ("LABEL":"Frequency"),PARENT @Fgen_main
  21. 210  ASSIGN @Amplitude TO WIDGET "MENU BUTTON";SET ("LABEL":"Amplitude"),PARENT @Fgen_main
  22. 220  ASSIGN @Dc_offset TO WIDGET "MENU BUTTON";SET ("LABEL":"DC Offset"),PARENT @Fgen_main
  23. 230  ASSIGN @Function TO WIDGET "MENU BUTTON";SET ("LABEL":"Function"),PARENT @Fgen_main
  24. 240  ASSIGN @Modulation TO WIDGET "CASCADE MENU";SET ("LABEL":"Modulation"),PARENT @Fgen_main
  25. 250  ASSIGN @Am TO WIDGET "MENU TOGGLE";SET ("LABEL":"AM"),PARENT @Modulation
  26. 260  ASSIGN @Pm TO WIDGET "MENU TOGGLE";SET ("LABEL":"PM"),PARENT @Modulation
  27. 270   !
  28. 280   !  Initial values for the controls
  29. 290   !
  30. 300  Freq=1000
  31. 310  Ampl=.001
  32. 320  Ampl_unit=0
  33. 330  Offset=0
  34. 340  Func=0
  35. 350   !
  36. 360   ! Displays for the values of the controls. The displays are actually
  37. 370   ! pushbuttons and the label for each button is used to display its
  38. 380   ! current value.
  39. 390   !
  40. 400  Attr$(0)="X"
  41. 410  Attr$(1)="Y"
  42. 420  Attr$(2)="WIDTH"
  43. 430  Attr$(3)="HEIGHT"
  44. 440  Attr(0)=30                    ! X position
  45. 450  Attr(1)=8                     ! Y position
  46. 460  Attr(2)=178                   ! width
  47. 470  Attr(3)=33                    ! height
  48. 480   !
  49. 490  ASSIGN @Freq_disp TO WIDGET "PUSHBUTTON";SET (Attr$(*):Attr(*),"LABEL":VAL$(Freq)&" Hz"),PARENT @Fgen_panel
  50. 500  Attr(1)=Attr(1)+Attr(3)
  51. 510  ASSIGN @Ampl_disp TO WIDGET "PUSHBUTTON";SET (Attr$(*):Attr(*),"LABEL":VAL$(Ampl)&" V p-p"),PARENT @Fgen_panel
  52. 520  Attr(1)=Attr(1)+Attr(3)
  53. 530  ASSIGN @Offs_disp TO WIDGET "PUSHBUTTON";SET (Attr$(*):Attr(*),"LABEL":VAL$(Offset)&" V offset"),PARENT @Fgen_panel
  54. 540  Attr(1)=Attr(1)+Attr(3)
  55. 550  ASSIGN @Func_disp TO WIDGET "PUSHBUTTON";SET (Attr$(*):Attr(*),"LABEL":"SINE"),PARENT @Fgen_panel
  56. 560   !
  57. 570   ! When either the menu is pulled down, or the display is clicked, call
  58. 580   ! a routine to change that control.
  59. 590   !
  60. 600  ON EVENT @Frequency,"ACTIVATED",1 CALL Set_frequency
  61. 610  ON EVENT @Function,"ACTIVATED",1 CALL Set_function
  62. 620  ON EVENT @Amplitude,"ACTIVATED",1 CALL Set_amplitude
  63. 630  ON EVENT @Dc_offset,"ACTIVATED",1 CALL Set_offset
  64. 640  ON EVENT @Freq_disp,"ACTIVATED",1 CALL Set_frequency
  65. 650  ON EVENT @Func_disp,"ACTIVATED",1 CALL Set_function
  66. 660  ON EVENT @Ampl_disp,"ACTIVATED",1 CALL Set_amplitude
  67. 670  ON EVENT @Offs_disp,"ACTIVATED",1 CALL Set_offset
  68. 680   !
  69. 690   ! Event handling for modulation toggles in the menu
  70. 700   !
  71. 710  ON EVENT @Am,"CHANGED",1 CALL Toggle_am
  72. 720  ON EVENT @Pm,"CHANGED",1 CALL Toggle_pm
  73. 730   !
  74. 740  LOOP
  75. 750      WAIT FOR EVENT
  76. 760  END LOOP
  77. 770 Finis: END
  78. 780  SUB Set_frequency
  79. 790   !
  80. 800   ! This routine sets the value of frequency.
  81. 810   !
  82. 820      COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  83. 830     !
  84. 840     ! The user is allowed to enter the new frequency in any of three units.
  85. 850     ! A cancel button is included in case the user makes a mistake.
  86. 860     !
  87. 870      DIM Btn$(3)[6],Freq$[20]
  88. 880      Btn$(0)="Hz"
  89. 890      Btn$(1)="kHz"
  90. 900      Btn$(2)="MHz"
  91. 910      Btn$(3)="Cancel"
  92. 920     !
  93. 930     ! Use a STRING dialog to get the new frequency. This dialog returns
  94. 940     ! a string so a conversion must be done.
  95. 950     !
  96. 960      DIALOG "STRING","",Btn;SET ("VALUE":VAL$(Freq),"TITLE":"Enter Frequency","DIALOG BUTTONS":Btn$(*),"DEFAULT BUTTON":0),RETURN ("VALUE":Freq$)
  97. 970     !
  98. 980      IF Btn=3 THEN SUBEXIT             ! Cancel was clicked
  99. 990      ON ERROR GOTO Cant_convert
  100. 1000     Freq=VAL(Freq$)                   ! convert to a number
  101. 1010     OFF ERROR
  102. 1020    !
  103. 1030    ! The value is checked for validity. The valid range might depend on
  104. 1040    ! function, but that case is not handled.
  105. 1050    !
  106. 1060     Freq=DROUND(Freq*10^(3*Btn),11)   ! apply the suffix and round
  107. 1070     IF Freq>2.0E+7 THEN Frequ=2.0E+7
  108. 1080     CONTROL @Freq_disp;SET ("LABEL":VAL$(Freq)&" Hz")
  109. 1090    !
  110. 1100    ! If actual instruments were being controlled, the OUTPUT statement
  111. 1110    ! would be here.
  112. 1120    !
  113. 1130     SUBEXIT
  114. 1140 Cant_convert:   !
  115. 1150    !
  116. 1160    ! The string could not be converted to a number. Display an error
  117. 1170    ! message and ask again.
  118. 1180    !
  119. 1190     DIALOG "ERROR",Freq$&CHR$(10)&"is not recognizable as a number.";SET ("TITLE":"Can't convert to a number")
  120. 1200     OFF ERROR
  121. 1210     GOTO 960
  122. 1220 SUBEND
  123. 1230 SUB Set_function
  124. 1240     COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  125. 1250     DIM Func$(5)[10]
  126. 1260     Func$(0)="SINE"
  127. 1270     Func$(1)="SQUARE"
  128. 1280     Func$(2)="TRIANGLE"
  129. 1290     Func$(3)="POS RAMP"
  130. 1300     Func$(4)="NEG RAMP"
  131. 1310     Func$(5)="DC only"
  132. 1320    !
  133. 1330    ! The button value is placed in a temporary variable in case Cancel
  134. 1340    ! is clicked. Only if OK is clicked is the value transferred to
  135. 1350    ! the actual variable which contains the function.
  136. 1360    !
  137. 1370     DIALOG "LIST","",Btn;SET ("TITLE":"Select Function","ITEMS":Func$(*),"SELECTION":Func,"DEFAULT BUTTON":0),RETURN ("SELECTION":A)
  138. 1380     IF Btn=1 THEN SUBEXIT
  139. 1390     Func=A
  140. 1400     CONTROL @Func_disp;SET ("LABEL":Func$(Func))
  141. 1410    !
  142. 1420    ! If DC only function is selected then the amplitude control has
  143. 1430    ! no effect.  The button and menu are deactivated.
  144. 1440    !
  145. 1450     IF Func=5 THEN
  146. 1460         CONTROL @Ampl_disp;SET ("SENSITIVE":0)
  147. 1470         CONTROL @Amplitude;SET ("SENSITIVE":0)
  148. 1480     ELSE
  149. 1490         CONTROL @Ampl_disp;SET ("SENSITIVE":1)
  150. 1500         CONTROL @Amplitude;SET ("SENSITIVE":1)
  151. 1510     END IF
  152. 1520 SUBEND
  153. 1530 SUB Set_amplitude
  154. 1540     COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  155. 1550     DIM Btn$(5)[7],Ampl$[20]
  156. 1560     Btn$(0)=" V p-p"
  157. 1570     Btn$(1)=" mV p-p"
  158. 1580     Btn$(2)=" V RMS"
  159. 1590     Btn$(3)=" mV RMS"
  160. 1600     Btn$(4)=" dBm"
  161. 1610     Btn$(5)="Cancel"
  162. 1620     DIALOG "STRING","",Btn;SET ("VALUE":VAL$(Ampl),"TITLE":"Enter Amplitude","DIALOG BUTTONS":Btn$(*),"DEFAULT BUTTON":0),RETURN ("VALUE":Ampl$)
  163. 1630     IF Btn=5 THEN SUBEXIT            ! Cancel was clicked
  164. 1640     ON ERROR GOTO Cant_convert
  165. 1650     Ampl=VAL(Ampl$)
  166. 1660     OFF ERROR
  167. 1670     Ampl_unit=Btn
  168. 1680     IF Btn=1 OR Btn=3 THEN
  169. 1690         Ampl=Ampl*.001               ! A mV button was selected
  170. 1700         Ampl_unit=Ampl_unit-1        ! Change to V unit
  171. 1710     END IF
  172. 1720     IF Btn<4 THEN
  173. 1730      !
  174. 1740      ! A volt button was used. Round and check for limits. The limit
  175. 1750      ! might also depend on the dc offset.
  176. 1760      !
  177. 1770         Ampl=ABS(PROUND(Ampl,-3))
  178. 1780         IF Ampl>5 THEN Ampl=5
  179. 1790     ELSE
  180. 1800      !
  181. 1810      ! dBm was selected. Round and check for limits.
  182. 1820      !
  183. 1830         Ampl=PROUND(Ampl,-2)
  184. 1840         IF Ampl>13 THEN Ampl=13
  185. 1850         IF Ampl<-60 THEN Ampl=-60
  186. 1860     END IF
  187. 1870     CONTROL @Ampl_disp;SET ("LABEL":VAL$(Ampl)&Btn$(Ampl_unit))
  188. 1880     SUBEXIT
  189. 1890 Cant_convert:   !
  190. 1900     DIALOG "ERROR",Ampl$&CHR$(10)&"is not recognizable as a number.";SET ("TITLE":"Can't convert to a number")
  191. 1910     GOTO 1620
  192. 1920 SUBEND
  193. 1930 SUB Set_offset
  194. 1940     COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  195. 1950     DIM Btn$(2)[6],Offs$[20]
  196. 1960     Btn$(0)=" V"
  197. 1970     Btn$(1)=" mV"
  198. 1980     Btn$(2)="Cancel"
  199. 1990     DIALOG "STRING","",Btn;SET ("VALUE":VAL$(Offset),"TITLE":"Enter DC Offset","DIALOG BUTTONS":Btn$(*),"DEFAULT BUTTON":0),RETURN ("VALUE":Offs$)
  200. 2000     IF Btn=2 THEN SUBEXIT ! Cancel was selected
  201. 2010     ON ERROR GOTO Cant_convert
  202. 2020     Offset=VAL(Offs$)
  203. 2030     OFF ERROR
  204. 2040     IF Btn=1 THEN Offset=Offset*.001           ! mV button
  205. 2050     Offset=PROUND(Offset,-3)                   ! Round to 3 digits
  206. 2060    !
  207. 2070     IF ABS(Offset)>5 THEN Offset=SGN(Offset)*5 ! no more than 5 V offset
  208. 2080     CONTROL @Offs_disp;SET ("LABEL":VAL$(Offset)&" V offset")
  209. 2090     SUBEXIT
  210. 2100 Cant_convert:   !
  211. 2110     DIALOG "ERROR",Offs$&CHR$(10)&"is not recognizable as a number.";SET ("TITLE":"Can't convert to a number")
  212. 2120     GOTO 1990
  213. 2130 SUBEND
  214. 2140 SUB Toggle_am
  215. 2150     COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  216. 2160  !
  217. 2170  ! The menu widgets handle displaying whether modulation is on or
  218. 2180  ! off automatically. This routine determines what the state is and
  219. 2190  ! does the appropriate I/O.
  220. 2200  !
  221. 2210     STATUS @Am;RETURN ("VALUE":State)
  222. 2220     IF State THEN
  223. 2230  !
  224. 2240  ! Send the command to turn AM on
  225. 2250  !
  226. 2260     ELSE
  227. 2270  !
  228. 2280  ! Send the command to turn AM off
  229. 2290  !
  230. 2300     END IF
  231. 2310 SUBEND
  232. 2320 SUB Toggle_pm
  233. 2330     COM /Fgen/Freq,Ampl,Ampl_unit,Func,Offset,@Freq_disp,@Ampl_disp,@Offs_disp,@Func_disp,@Amplitude,@Am,@Pm
  234. 2340  !
  235. 2350  ! The menu widgets handle displaying whether modulation is on or
  236. 2360  ! off automatically. This routine determines what the state is and
  237. 2370  ! then does the appropriate I/O.
  238. 2380  !
  239. 2390     STATUS @Pm;RETURN ("VALUE":State)
  240. 2400     IF State THEN
  241. 2410  !
  242. 2420  ! Send the command to turn PM on
  243. 2430  !
  244. 2440     ELSE
  245. 2450  !
  246. 2460  ! Send the command to turn PM off
  247. 2470  !
  248. 2480     END IF
  249. 2490 SUBEND
  250.