05/17/16 12:36:51 C:\goku\DE2-115_Media_Computer\verilog\DE2_115_Media_Computer.v
   1 
// DE2_115_Media_Computer.v
   2 
// Authors: Ryan Land, Gautham Ponnu
   3 
// ECE 5760 - Final Project
   4 
// May 16, 2016
   5 
 
   6 
// TOP LEVEL MODULE
   7 
module DE2_115_Media_Computer (
   8 
 
   9 
 
  10 
    // Inputs
  11 
    CLOCK_50,
  12 
    TD_CLK27,
  13 
    KEY,
  14 
    SW,
  15 
 
  16 
    //  Communication
  17 
    UART_RXD,
  18 
   
  19 
    // Audio
  20 
    AUD_ADCDAT,
  21 
   
  22 
    // IrDA
  23 
    IRDA_RXD,
  24 
 
  25 
    // Video In
  26 
    TD_DATA,
  27 
    TD_HS,
  28 
    TD_VS,
  29 
   
  30 
    // USB
  31 
    OTG_INT,
  32 
 
  33 
/*****************************************************************************/
  34 
    // Bidirectionals
  35 
    GPIO,
  36 
 
  37 
    // Memory (SRAM)
  38 
    SRAM_DQ,
  39 
   
  40 
    // Memory (SDRAM)
  41 
    DRAM_DQ,
  42 
 
  43 
    // PS2 Port
  44 
    PS2_KBCLK,
  45 
    PS2_KBDAT,
  46 
    PS2_MSCLK,
  47 
    PS2_MSDAT,
  48 
   
  49 
    // Audio
  50 
    AUD_BCLK,
  51 
    AUD_ADCLRCK,
  52 
    AUD_DACLRCK,
  53 
   
  54 
    // Char LCD 16x2
  55 
    LCD_DATA,
  56 
 
  57 
    // AV Config
  58 
    I2C_SDAT,
  59 
   
  60 
    // SD Card
  61 
    SD_CMD,
  62 
    SD_DAT,
  63 
   
  64 
    // Flash
  65 
    FL_DQ,
  66 
   
  67 
    //USB
  68 
    OTG_DATA,
  69 
   
  70 
/*****************************************************************************/
  71 
    // Outputs
  72 
   
  73 
    //     Simple
  74 
    LEDG,
  75 
    LEDR,
  76 
 
  77 
    HEX0,
  78 
    HEX1,
  79 
    HEX2,
  80 
    HEX3,
  81 
    HEX4,
  82 
    HEX5,
  83 
    HEX6,
  84 
    HEX7,
  85 
   
  86 
    //     Memory (SRAM)
  87 
    SRAM_ADDR,
  88 
 
  89 
    SRAM_CE_N,
  90 
    SRAM_WE_N,
  91 
    SRAM_OE_N,
  92 
    SRAM_UB_N,
  93 
    SRAM_LB_N,
  94 
   
  95 
    //  Communication
  96 
    UART_TXD,
  97 
   
  98 
    // Memory (SDRAM)
  99 
    DRAM_ADDR,
 100 
   
 101 
    DRAM_BA,
 102 
    DRAM_CAS_N,
 103 
    DRAM_RAS_N,
 104 
    DRAM_CLK,
 105 
    DRAM_CKE,
 106 
    DRAM_CS_N,
 107 
    DRAM_WE_N,
 108 
    DRAM_DQM,
 109 
   
 110 
    // Audio
 111 
    AUD_XCK,
 112 
    AUD_DACDAT,
 113 
   
 114 
    // VGA
 115 
    VGA_CLK,
 116 
    VGA_HS,
 117 
    VGA_VS,
 118 
    VGA_BLANK_N,
 119 
    VGA_SYNC_N,
 120 
    VGA_R,
 121 
    VGA_G,
 122 
    VGA_B,
 123 
 
 124 
    // Char LCD 16x2
 125 
    LCD_ON,
 126 
    LCD_BLON,
 127 
    LCD_EN,
 128 
    LCD_RS,
 129 
    LCD_RW,
 130 
   
 131 
    // AV Config
 132 
    I2C_SCLK,
 133 
   
 134 
    // SD Card
 135 
    SD_CLK,
 136 
   
 137 
    // Flash
 138 
    FL_ADDR,
 139 
   FL_CE_N,
 140 
    FL_OE_N,
 141 
    FL_RESET_N,
 142 
    FL_WE_N,
 143 
       
 144 
    // Video In
 145 
    TD_RESET_N,
 146 
   
 147 
    // USB
 148 
    OTG_ADDR,
 149 
    OTG_CS_N,
 150 
    OTG_OE_N,
 151 
    OTG_RST_N,
 152 
    OTG_WE_N,
 153 
);
 154 
 
 155 
/*****************************************************************************
 156 
*                           Parameter Declarations                          *
 157 
*****************************************************************************/
 158 
 
 159 
 
 160 
/*****************************************************************************
 161 
*                             Port Declarations                             *
 162 
*****************************************************************************/
 163 
 
 164 
 165 
// Inputs
 166 
input                CLOCK_50;
 167 
input                TD_CLK27;
 168 
input        [ 3: 0]    KEY;
 169 
input        [17: 0]    SW;
 170 
 
 171 
 
 172 
//  Communication
 173 
input                UART_RXD;
 174 
 
 175 
//  Audio
 176 
input                AUD_ADCDAT;
 177 
 
 178 
//  IrDA
 179 
input                IRDA_RXD;
 180 
 
 181 
// Video In
 182 
input     [7:0]    TD_DATA;
 183 
input               TD_HS;
 184 
input              TD_VS;
 185 
 
 186 
// USB
 187 
input       [ 1: 0] OTG_INT;
 188 
 
 189 
// Bidirectionals
 190 
inout   [35:0]   GPIO;
 191 
 
 192 
//     Memory (SRAM)
 193 
inout        [15: 0]    SRAM_DQ;
 194 
 
 195 
//  Memory (SDRAM)
 196 
inout        [31: 0]    DRAM_DQ;
 197 
 
 198 
//  PS2 Port
 199 
inout                PS2_KBCLK;
 200 
inout                PS2_KBDAT;
 201 
inout                PS2_MSCLK;
 202 
inout                PS2_MSDAT;
 203 
 
 204 
//  Audio
 205 
inout                AUD_BCLK;
 206 
inout                AUD_ADCLRCK;
 207 
inout                AUD_DACLRCK;
 208 
 
 209 
//  AV Config
 210 
inout                I2C_SDAT;
 211 
 
 212 
//  Char LCD 16x2
 213 
inout        [ 7: 0]    LCD_DATA;
 214 
 
 215 
// SD Card
 216 
inout             SD_CMD;
 217 
inout     [ 3: 0]  SD_DAT;
 218 
 
 219 
// Flash
 220 
inout     [ 7: 0]  FL_DQ;
 221 
 
 222 
// USB
 223 
inout    [15: 0]  OTG_DATA;
 224 
 
 225 
// Outputs
 226 
output                TD_RESET_N;
 227 
 
 228 
//     Simple
 229 
output        [ 8: 0]    LEDG;
 230 
output        [17: 0]    LEDR;
 231 
 
 232 
output        [ 6: 0]    HEX0;
 233 
output        [ 6: 0]    HEX1;
 234 
output        [ 6: 0]    HEX2;
 235 
output        [ 6: 0]    HEX3;
 236 
output        [ 6: 0]    HEX4;
 237 
output        [ 6: 0]    HEX5;
 238 
output        [ 6: 0]    HEX6;
 239 
output        [ 6: 0]    HEX7;
 240 
 
 241 
//     Memory (SRAM)
 242 
output        [19: 0]    SRAM_ADDR;
 243 
 
 244 
output                SRAM_CE_N;
 245 
output                SRAM_WE_N;
 246 
output                SRAM_OE_N;
 247 
output                SRAM_UB_N;
 248 
output                SRAM_LB_N;
 249 
 
 250 
//  Communication
 251 
output                UART_TXD;
 252 
 
 253 
//  Memory (SDRAM)
 254 
output        [12: 0]    DRAM_ADDR;
 255 
 
 256 
output        [ 1: 0]    DRAM_BA;
 257 
output                DRAM_CAS_N;
 258 
output                DRAM_RAS_N;
 259 
output                DRAM_CLK;
 260 
output                DRAM_CKE;
 261 
output                DRAM_CS_N;
 262 
output                DRAM_WE_N;
 263 
output        [ 3: 0]    DRAM_DQM;
 264 
 
 265 
//  Audio
 266 
output                AUD_XCK;
 267 
output                AUD_DACDAT;
 268 
 
 269 
//  VGA
 270 
output                VGA_CLK;
 271 
output                VGA_HS;
 272 
output                VGA_VS;
 273 
output                VGA_BLANK_N;
 274 
output                VGA_SYNC_N;
 275 
output        [ 7: 0]    VGA_R;
 276 
output        [ 7: 0]    VGA_G;
 277 
output        [ 7: 0]    VGA_B;
 278 
 
 279 
//  Char LCD 16x2
 280 
output                LCD_ON;
 281 
output                LCD_BLON;
 282 
output                LCD_EN;
 283 
output                LCD_RS;
 284 
output                LCD_RW;
 285 
 
 286 
//  AV Config
 287 
output                I2C_SCLK;
 288 
 
 289 
//  SD Card
 290 
output                 SD_CLK;
 291 
 
 292 
//  Flash
 293 
output         [22: 0]    FL_ADDR;
 294 
output                 FL_CE_N;
 295 
output                FL_OE_N;
 296 
output                FL_RESET_N;
 297 
output                FL_WE_N;
 298 
 
 299 
//USB
 300 
output      [ 1: 0]  OTG_ADDR;
 301 
output                OTG_CS_N;
 302 
output                OTG_OE_N;
 303 
output                OTG_RST_N;
 304 
output                OTG_WE_N;
 305 
 
 306 
/*****************************************************************************
 307 
*                 Internal Wires and Registers Declarations                 *
 308 
*****************************************************************************/
 309 
// Internal Wires
 310 
 
 311 
// Internal Registers
 312 
 
 313 
// State Machine Registers
 314 
 
 315 
/*****************************************************************************
 316 
*                         Finite State Machine(s)                           *
 317 
*****************************************************************************/
 318 
 
 319 
 
 320 
/*****************************************************************************
 321 
*                             Sequential Logic                              *
 322 
*****************************************************************************/
 323 
 
 324 
 
 325 
/*****************************************************************************
 326 
*                            Combinational Logic                            *
 327 
*****************************************************************************/
 328 
 
 329 
// Output Assignments - set GPIO safely as input.
 330 
assign GPIO[ 0]        = 1'bZ;
 331 
assign GPIO[ 2]        = 1'bZ;
 332 
assign GPIO[ 4]        = 1'bZ;
 333 
//assign GPIO[ 6]        = 1'bZ;
 334 
 
 335 
/*****************************************************************************
 336 
*                              Lab2 + NIOS                             *
 337 
*****************************************************************************/
 338 
 339 
 
 340 
 
 341 
wire signed    [17:0]x1_output;
 342 
wire signed  [17:0]x2_output;
 343 
 
 344 
wire [10:0] kp_wire,ki_wire,kd_wire;   
 345 
 
 346 
///////////////////// INSTANTIATE NIOS ///////////////////////////////////////
 347 
 
 348 
nios_system NiosII (
 349 
    // 1) global signals:
 350 
    .clk                                    (CLOCK_50),
 351 
    .clk_27                                    (TD_CLK27),
 352 
    .reset_n                                (KEY[0]),
 353 
    .sys_clk                                (),
 354 
    .vga_clk                                (),
 355 
    .sdram_clk                                (DRAM_CLK),
 356 
    .audio_clk                                (AUD_XCK),
 357 
 
 358 
    // the_AV_Config
 359 
    .I2C_SDAT_to_and_from_the_AV_Config        (I2C_SDAT),
 360 
    .I2C_SCLK_from_the_AV_Config            (I2C_SCLK),
 361 
   
 362 
    // the_SDRAM
 363 
    .zs_addr_from_the_SDRAM                    (DRAM_ADDR),
 364 
    .zs_ba_from_the_SDRAM                    (DRAM_BA),
 365 
    .zs_cas_n_from_the_SDRAM                (DRAM_CAS_N),
 366 
    .zs_cke_from_the_SDRAM                    (DRAM_CKE),
 367 
    .zs_cs_n_from_the_SDRAM                    (DRAM_CS_N),
 368 
    .zs_dq_to_and_from_the_SDRAM            (DRAM_DQ),
 369 
    .zs_dqm_from_the_SDRAM                    (DRAM_DQM),
 370 
    .zs_ras_n_from_the_SDRAM                (DRAM_RAS_N),
 371 
    .zs_we_n_from_the_SDRAM                    (DRAM_WE_N),
 372 
   
 373 
    // the_SRAM
 374 
    .SRAM_DQ_to_and_from_the_SRAM            (SRAM_DQ),
 375 
    .SRAM_ADDR_from_the_SRAM                (SRAM_ADDR),
 376 
    .SRAM_LB_N_from_the_SRAM                (SRAM_LB_N),
 377 
    .SRAM_UB_N_from_the_SRAM                (SRAM_UB_N),
 378 
    .SRAM_CE_N_from_the_SRAM                (SRAM_CE_N),
 379 
    .SRAM_OE_N_from_the_SRAM                (SRAM_OE_N),
 380 
    .SRAM_WE_N_from_the_SRAM                (SRAM_WE_N),
 381 
 
 382 
    .VGA_CLK_from_the_VGA_Controller        (VGA_CLK),
 383 
    .VGA_HS_from_the_VGA_Controller            (VGA_HS),
 384 
    .VGA_VS_from_the_VGA_Controller            (VGA_VS),
 385 
    .VGA_BLANK_from_the_VGA_Controller        (VGA_BLANK_N),
 386 
    .VGA_SYNC_from_the_VGA_Controller        (VGA_SYNC_N),
 387 
    .VGA_R_from_the_VGA_Controller            (VGA_R),
 388 
    .VGA_G_from_the_VGA_Controller            (VGA_G),
 389 
    .VGA_B_from_the_VGA_Controller            (VGA_B),
 390 
   
 391 
 
 392 
  
 393 
    .x1_output_export(unscaled_pwm_result_from_pid_controller[15:0]),
 394 
   
 395 
    ////////////// EXPORT TO NIOS ///////////////////////////////////
 396 
    //////// x2_output as Median Filter
 397 
    .x2_output_export(median_filtered_proximity_value_to_nios),
 398 
 
 399 
    .kp_export                              (kp_wire),                              //                                 kp.export
 400 
    .ki_export                              (ki_wire),                              //                                 ki.export
 401 
       .kd_export                              (kd_wire)                               //                                 kd.export
 402 
);
 403 
 
 404 
 
 405 
 
 406 
 
 407 
wire reset;
 408 
assign reset=~KEY[0];
 409 
 
 410 
wire [15:0] median_filtered_proximity_value_to_nios;
 411 
wire signed [16:0] unscaled_pwm_result_from_pid_controller;
 412 
 
 413 
//wire [3:0] kp, ki, kd;
 414 
 
 415 
 
 416 
spi_controller spiread (
 417 
 
 418 
    // INPUTS (SPI buses)
 419 
    .cs(GPIO[0]),
 420 
    .sck(GPIO[2]),
 421 
    .sda(GPIO[4]),
 422 
    .kp(kp_wire),
 423 
    .ki(ki_wire),
 424 
    .kd(kd_wire),
 425 
    // OUTPUTS
 426 
    .median_filtered_proximity_value(median_filtered_proximity_value_to_nios),
 427 
    .unscaled_pwm_result_from_pid_controller(unscaled_pwm_result_from_pid_controller)
 428 
);
 429 
 
 430 
pwm_output gpio_pwm_controller (
 431 
 
 432 
    // INPUTS
 433 
    .clock50(CLOCK_50),
 434 
    .switches(SW[17:0]),
 435 
    .unscaled_pwm_result_from_pid_controller(unscaled_pwm_result_from_pid_controller),
 436 
   
 437 
    // OUTPUTS
 438 
    .gpio_to_the_opamp(GPIO[35])
 439 
);
 440 
 
 441 
 
 442 
endmodule
 443 
 
 444 
/////////////////    SPI_CONTROLLER MODULE     ///////////////////////////////////
 445 
 
 446 
 
 447 
module spi_controller(cs, sck, sda, kp, ki, kd, median_filtered_proximity_value, unscaled_pwm_result_from_pid_controller);
 448 
 
 449 
    //////////////////////// Inputs and Outputs ////////////////////
 450 
 
 451 
    input cs, sck, sda;
 452 
    input [9:0] kp, ki, kd;
 453 
    output [15:0] median_filtered_proximity_value;
 454 
    output signed [16:0] unscaled_pwm_result_from_pid_controller;
 455 
   
 456 
    //////////////////////////////////////////////////////////////
 457 
       
 458 
    /////////////////// SPI Transaction Registers //////////////////
 459 
   
 460 
    //Register used for sample storage upon positive edge of every sample clock.
 461 
    reg [15:0] prox_out_reg;
 462 
   
 463 
    //Register for storage of data bits read during the SPI transaction.
 464 
    //The proximity value is located at the 2nd and 3rd bytes of the transaction.
 465 
    reg [47:0] raw_read_bits;
 466 
   
 467 
    // Proximity value history for median filter.
 468 
    reg [15:0] prox_out1,prox_out2,prox_out3;//,prox_out4;
 469 
   
 470 
    //////////////////////////////////////////////////////////////
 471 
    //////////////// Median Filter Registers /////////////////////
 472 
   
 473 
    reg [15:0] median_filtered_proximity_value_reg;
 474 
   
 475 
    //////////////////////////////////////////////////////////////
 476 
    ///////////////// PID Registers and Paramaters ////////////////
 477 
   
 478 
    parameter ldc1000_setpoint = 16'd32768-16'd14800; // 32768 is MAX
 479 
   
 480 
    reg signed [16:0] pid_fractional_output;
 481 
    reg signed [16:0] pid_integral;
 482 
    reg signed [16:0] current_error_reg;
 483 
    reg signed [16:0] previous_error_reg;
 484 
    reg signed [16:0] pid_integral_accumulator;
 485 
    reg signed [16:0] unscaled_pwm_result_from_pid_controller_reg;
 486 
    reg signed [16:0] value_test;
 487 
 
 488 
 
 489 
    /////////////////////////////////////////////////////////////////   
 490 
   
 491 
    // This is the i2c CLK. This is for every bit wise read
 492 
   
 493 
    always @ (negedge sck) begin
 494 
   
 495 
        //Read into LSB, then shift by 1.
 496 
        raw_read_bits[0]=sda;
 497 
        raw_read_bits <= raw_read_bits << 1;
 498 
       
 499 
    end
 500 
   
 501 
    // This is when CS is ready. This means that one sample is ready for us to read.
 502 
 
 503 
    always @ (posedge cs) begin
 504 
   
 505 
        ///////////////////////SPI READING BLOCK////////////////////
 506 
       
 507 
        prox_out_reg = {raw_read_bits[39:24]};
 508 
 
 509 
        // Assigning the right Median Filter Values
 510 
        prox_out3 <= prox_out2;
 511 
        prox_out2 <= prox_out1;
 512 
        //We have to reverse bytes 2 and 3 because they are transmitted as {LSB,MSB}.
 513 
        prox_out1 <= (32768-{prox_out_reg[7:0], prox_out_reg[15:8]});
 514 
       
 515 
        ////////////////////////////////////////////////////////////
 516 
 
 517 
        //////////////////////MEDIAN FILTER BLOCK//////////////////
 518 
       
 519 
        // The Media Filter block reads the samples with a window size of 3 and selects the median value.
 520 
        // This is to avoid noise spikes
 521 
 
 522 
        if ((prox_out1 <= prox_out2) && (prox_out1 <= prox_out3)) begin
 523 
            median_filtered_proximity_value_reg <= ( prox_out2 <= prox_out3) ? prox_out2 : prox_out3;           
 524 
        end
 525 
        else if ((prox_out2 <= prox_out1) && (prox_out2<=prox_out3)) begin
 526 
            median_filtered_proximity_value_reg <= (prox_out1 <= prox_out3) ? prox_out1 : prox_out3;           
 527 
        end
 528 
        else begin
 529 
            median_filtered_proximity_value_reg <= (prox_out1 <= prox_out2) ? prox_out1 : prox_out2;
 530 
        end
 531 
       
 532 
        //////////////////////////////////////////////////////////////
 533 
       
 534 
        /////////////////////PID CALCULATION STUFF///////////////////////////
 535 
       
 536 
        current_error_reg <= ((ldc1000_setpoint - median_filtered_proximity_value_reg)); // This is the current error
 537 
   
 538 
        if ((current_error_reg * previous_error_reg) > 0 ) begin
 539 
            pid_integral_accumulator = pid_integral_accumulator + current_error_reg;
 540 
        end else begin
 541 
       
 542 
            //pid_integral_accumulator = pid_integral_accumulator * 0.96875
 543 
            pid_integral_accumulator = (pid_integral_accumulator >> 1) +
 544 
                                       (pid_integral_accumulator >> 2) +
 545 
                                       (pid_integral_accumulator >> 3) +
 546 
                                       (pid_integral_accumulator >> 4) +
 547 
                                       (pid_integral_accumulator >> 5);
 548 
                                      
 549 
        end
 550 
   
 551 
        value_test <= (kp * current_error_reg + ki * pid_integral + kd * (current_error_reg - previous_error_reg));
 552 
       
 553 
        if ((value_test) < 0) begin
 554 
            unscaled_pwm_result_from_pid_controller_reg <= 0;
 555 
        end
 556 
        else if ((value_test) > 32767 ) begin
 557 
            unscaled_pwm_result_from_pid_controller_reg <= 32767;
 558 
        end
 559 
        else begin
 560 
            unscaled_pwm_result_from_pid_controller_reg <= value_test;
 561 
        end
 562 
       
 563 
       
 564 
        //unscaled_pwm_result_from_pid_controller_reg <= median_filtered_proximity_value_reg;
 565 
   
 566 
        previous_error_reg <= current_error_reg;
 567 
       
 568 
        /////////////////////////////////////////////////////////////////////////
 569 
       
 570 
    end
 571 
   
 572 
    assign unscaled_pwm_result_from_pid_controller = unscaled_pwm_result_from_pid_controller_reg ;
 573 
    assign median_filtered_proximity_value = median_filtered_proximity_value_reg;
 574 
 
 575 
    // We read LSB, then MSB. Now combine them together properly as {MSB,LSB}
 576 
    // 32767 - the value, because when ball goes up, prox output should go up. It is more intuitive
 577 
    // because prox output will change monotonically, and with same sign as height.
 578 
    //assign prox_output = (32767-{prox_out_reg[7:0], prox_out_reg[15:8]});
 579 
   
 580 
endmodule
 581 
 
 582 
///////////////////////////////////////////////////////////////////////////
 583 
 
 584 
module pwm_output(clock50, switches, unscaled_pwm_result_from_pid_controller, gpio_to_the_opamp);
 585 
 
 586 
input clock50;
 587 
input [17:0] switches;
 588 
input signed [16:0] unscaled_pwm_result_from_pid_controller;
 589 
 
 590 
output gpio_to_the_opamp;
 591 
 
 592 
// Variables
 593 
reg signed [31:0] frequency_counter;
 594 
reg signed [31:0] duty_cycle_counter;
 595 
reg signed [16:0] scaled_pwm_result_from_pid_controller;
 596 
reg gpio_to_the_opamp_reg;
 597 
 
 598 
always @ (posedge clock50) begin
 599 
 
 600 
 
 601 
    if (frequency_counter < 0 || frequency_counter == 0) begin
 602 
   
 603 
        //Finished counting? Load counter back with 50 million again.
 604 
        frequency_counter <= 32'b10111110101111000010000000;
 605 
       
 606 
        //Set output = 1 because we're at the start of our cycle.
 607 
        gpio_to_the_opamp_reg <= 1;
 608 
       
 609 
        //Read the PID output only at the beginning of the cycle.
 610 
        //If the PWM value changes dynamically in the middle of the cycle, we might
 611 
        //have some weird unwanted effects.
 612 
 
 613 
        scaled_pwm_result_from_pid_controller <= unscaled_pwm_result_from_pid_controller>>7;
 614 
 
 615 
    end else begin
 616 
 
 617 
        //Decrement as a multiple of 5000
 618 
        frequency_counter <= frequency_counter - ((switches[9:0]) * (32'b101));
 619 
       
 620 
        //Since the PWM frequency is dependent on the switches, we can play around with
 621 
        // the frequency values during operation of the levitator. That way we can tell
 622 
        // what the optimal PWM frequency is for our application.
 623 
 
 624 
    end
 625 
   
 626 
    if(scaled_pwm_result_from_pid_controller[7:0]==0) begin
 627 
        gpio_to_the_opamp_reg <= 0;
 628 
    end
 629 
    // Replacing the switches with the output from the PID
 630 
    else if (frequency_counter < (32'd196078 * (255-scaled_pwm_result_from_pid_controller[7:0]))) begin
 631 
 
 632 
        //This maps to a ~0.5% to 100% duty cycle.
 633 
        //It will allow the FPGA to finely tune the duty cycle to whatever we need.
 634 
        //If we need finer tuning, we can cut "32'd196078" in half, and then we'd have a
 635 
        // multiplier going from 1 to 511. We can basically tune this as fine as we want it.
 636 
        //Probably the finer it is, the better.
 637 
       
 638 
        gpio_to_the_opamp_reg <= 0;
 639 
       
 640 
    end
 641 
   
 642 
   
 643 
end
 644 
 
 645 
assign gpio_to_the_opamp = gpio_to_the_opamp_reg;
 646 
 
 647 
endmodule