PROGRAM Series_RLC_Circuit

   LIBRARY "CIRLIB*"
   SET MODE "LACEHIGH8"
   DIM ZRect(2), ZPolar(2)

   LET R     =  50       ! Ohms
   LET C     =  1E-6     ! Farads
   LET L     =  2E-3     ! Henrys
   LET F1    =  100      ! Hertz
   LET F2    =  100000   ! Hertz
   LET ZMIN  =  0        ! Ohms
   LET ZMAX  =  1000     ! Ohms
   LET AMIN  = -90       ! Degrees
   LET AMAX  =  90       ! Degrees
   LET Zo    =  50       ! Ohms
   LET ZPlot =  1        ! Windor number
   LET APlot =  2        ! Window number
   LET SPlot =  3        ! Window number
   
   OPEN #ZPlot : screen .55, 1, .55, 1
   CALL Generate_Log_Chart(F1, F2, ZMIN, ZMAX)
   SET COLOR "RED"
   
   OPEN #APlot : screen .55, 1, 0, .45
   CALL Generate_Log_Chart(F1, F2, AMIN, AMAX)
   SET COLOR "GREEN"
   
   OPEN #SPlot : screen 0, .5, .3, 1
   CALL Generate_Smith_Chart(Zo)
   SET COLOR "YELLOW"
 
   WINDOW #0  
   CALL Page_Title(F1, F2, ZMIN, ZMAX, AMIN, AMAX)
   
   ! Calculate and present impedance plot
   !
   WINDOW #ZPlot
   LET Freq = F1
   DO
      CALL Calc_Series_RLC((R), (L), (C), (Freq), ZRect, ZPolar)
      PLOT log10(Freq),ZPolar(1);
      LET Freq = Freq * 1.1
   
   LOOP until Freq > F2
   
   ! Calculate and present phase angle plot
   !
   WINDOW #APlot
   LET Freq = F1
   DO
      CALL Calc_Series_RLC((R), (L), (C), (Freq), ZRect, ZPolar)
      PLOT log10(Freq),ZPolar(2);
      LET Freq = Freq * 1.1
   
   LOOP until Freq > F2

   ! Calculate and present Smith plot
   !
   WINDOW #SPlot
   LET Freq = F1
   DO
      CALL Calc_Series_RLC((R), (L), (C), (Freq), ZRect, ZPolar)
      CALL SmithPlot(ZRect(1),ZRect(2))
      LET Freq = Freq * 1.1
   
   LOOP until Freq > F2

   WINDOW #0
   ASK WINDOW left,right,bottom,top
   SET TEXT JUSTIFY "CENTER", "BASE"
   PLOT TEXT, AT 12/48*(right-left),3/96*(top-bottom): "R =" & Str$(R) & " L =" & Str$(L) & " C =" & Str$(C)
   
END

SUB Calc_Series_RLC(R, L, C, F, ZRect(), ZPolar())

   OPTION ANGLE DEGREES

   ! Solve inductive reactance
   !
   LET XL = 2*PI*F*L

   ! Trap numerical overflow
   !
   IF F = 0 OR C = 0 THEN
      LET ZRect(1)  =  R
      LET ZRect(2)  = -MAXNUM
      LET ZPolar(1) =  MAXNUM
      LET ZPolar(2) = -90
      EXIT SUB
   END IF

   ! Solve capacitive reactance
   !
   LET XC = 1/(2*PI*F*C)

   ! Solve reactance
   !
   LET X  = XL - XC

   ! Solve impedance, rectangluar and polar forms
   !
   IF R >= 0 THEN
      LET ZRect(1)  = R
      LET ZRect(2)  = X
      LET ZPolar(1) = SQR(R^2 + X^2)
      IF R = 0 THEN
         IF X < 0 THEN
            LET ZPolar(2) = -90
         ELSEIF X > 0 THEN
            LET ZPolar(2) = 90
         ELSE
            LET ZPolar(2) = 0
         END IF
      ELSE
         LET ZPolar(2) = ATN(X/R)
      END IF
   ELSE
      ! We don't handle negative resistances
      LET ZRect(1) = 0
      LET ZRect(2) = 0
      LET ZPolar(1) = 0
      LET ZPolar(2) = 0
   END IF

END SUB


SUB Page_Title(F1, F2, ZMIN, ZMAX, AMIN, AMAX)

    ASK WINDOW left,right,bottom,top
    SET TEXT JUSTIFY "CENTER", "BASE"

    PLOT TEXT, AT 24/48*(right-left),93/96*(top-bottom): USING$(">######",ZMAX)
    PLOT TEXT, AT 25/48*(right-left),26/48*(top-bottom): USING$("#",0)
    PLOT TEXT, AT 37/48*(right-left),92/96*(top-bottom): "IMPEDANCE"

    PLOT TEXT, AT 37/48*(right-left),40/96*(top-bottom): "PHASE ANGLE"
    PLOT TEXT, AT 25/48*(right-left),40/96*(top-bottom): "+90"
    PLOT TEXT, AT 25/48*(right-left),0/96*(top-bottom): "-90"

    PLOT TEXT, AT 12/48*(right-left),20/96*(top-bottom): "Series RLC circuit"
    PLOT TEXT, AT 12/48*(right-left),15/96*(top-bottom): "Frequency Domain Analysis"
    PLOT TEXT, AT 12/48*(right-left),10/96*(top-bottom): "in True BASIC"
    PLOT TEXT, AT 26/48*(right-left),48/96*(top-bottom): str$(F1)
    PLOT TEXT, AT 37/48*(right-left),48/96*(top-bottom): "Frequency"
    PLOT TEXT, AT 46/48*(right-left),48/96*(top-bottom): str$(F2)

END SUB
