+assign dbg = ssp_frame;\r
+
+// Divide the clock to be used for the ADC
+reg [7:0] pck_divider;
+reg clk_state;
+\r
+always @(posedge pck0)\r
+begin\r
+ if(pck_divider == divisor[7:0])\r
+ begin\r
+ pck_divider <= 8'd0;
+ clk_state = !clk_state;\r
+ end\r
+ else\r
+ begin\r
+ pck_divider <= pck_divider + 1;\r
+ end\r
+end\r
+
+assign adc_clk = ~clk_state;
+
+// Toggle the output with hysteresis
+// Set to high if the ADC value is above 200
+// Set to low if the ADC value is below 64
+reg is_high;
+reg is_low;
+reg output_state;
+
+always @(posedge pck0)\r
+begin\r
+ if((pck_divider == 8'd7) && !clk_state) begin
+ is_high = (adc_d >= 8'd200);
+ is_low = (adc_d <= 8'd64);
+ end
+end
+
+always @(posedge is_high or posedge is_low)
+begin
+ if(is_high)
+ output_state <= 1'd1;
+ else if(is_low)
+ output_state <= 1'd0;
+end
+
+assign ssp_frame = output_state;