Advertisement
MBJ

IR frequency counter

MBJ
Apr 21st, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. `timescale 1ns / 1ps
  2.  
  3. // add in the no data display
  4. // Infrared Frequency detection and display
  5.  
  6. // Version 4: removed HZLED funcionality
  7. // -changed freq_confirm to 2 bit var (from 1 bit), keeps track of which mailbox as well as confirms
  8. // -changed else block to conditional upon RM state. Makes sure freq_confirm keeps driving until mail is delivered
  9. // -ADDED ANOTHER IRsignal input!!
  10. // also, saved over V3... might need to go back to group folder and redownload
  11. // MAJOR CHOICE: keep L and R identification here
  12. // OR: revert back to only one sided inputs and let state machine decide which side!!!!!!
  13. module IR_frequency(
  14. input clk,
  15. //input [1:0] whisker, // MAYBE NEED THIS FOR DETERMINING WHICH SIGNAL TO TAKE IN
  16. input [1:0] IRsignal, // MSB = left side, LSB = right side!!!!!!
  17. input [1:0] state,
  18. //output reg LED10, LED100, LED1k, // to differentiate which frequency easily
  19. //output reg [2:0] HzLED, // 000 off, 001 10Hz, 010 100 Hz, 100 1kHz
  20. output reg [1:0] freq_confirm // 0 = off, 1 = 10 Hz, 2 = 100 Hz, 3 = 1Khz
  21. );
  22.  
  23.  
  24. // Going to confirm the mailbox by polling twice.
  25. // 10-1k Hz going to be chosen from 3 different ranges.
  26.  
  27.  
  28. // previous variables: no _L or _R, IRsignal was 1 bit, so was prevIR
  29.  
  30. reg [26:0] fr_count; // counting up to 100 million
  31. reg [1:0] last;
  32. reg [10:0] data_store; // register to count data, data/8 = hertz/8
  33. reg [16:0] data; // LSB side of IRsignal input
  34. // changed mailbox/prevmailbox from 3 bit to 2 bit
  35. reg [1:0] mailbox;
  36. reg [1:0] prev_mailbox; // to compare current mailbox with for confirmation
  37. reg IR;
  38. reg prevIR;
  39.  
  40. initial begin
  41. {fr_count, last, data_store, data, mailbox, prev_mailbox, IR, prevIR} = 0;
  42. end
  43.  
  44. always@(posedge clk)
  45. begin
  46. if(state == 1) begin
  47. //freq_confirm = 0; // always zero unless confirm var.
  48. fr_count <= fr_count + 1;
  49. prevIR <= IR;
  50. IR <= IRsignal;
  51. if(IR && !prevIR)
  52. data <= data + 1;
  53. //data <= (IR && !prevIR)? data + 1 : data;
  54. data_store = data*5;
  55. //if(fr_count == 100000000) begin // 100 MHz
  56. if(fr_count == 20000000) begin // 100 MHz / 8 --->!!!1 maybe switch to 1 fifth!!
  57. //poll_flag = 1; // with it edited out, don't gotta worry about 7-seg
  58. fr_count <= 0;
  59. // NOTE: data_store, mailbox, and prev_mailbox all must have blocking assignments
  60. data_store = data * 5;
  61. //data_store_R = data_R * 5;
  62. data <= 0;
  63. // mailbox is current selection
  64. prev_mailbox = mailbox;
  65. mailbox = ((data_store >= 8) && (data_store <= 20)) ? 1 :
  66. ((data_store >= 80) && (data_store <= 200)) ? 2 :
  67. ((data_store >= 800) && (data_store <= 1200)) ? 3 : 0;
  68. if((prev_mailbox == mailbox) && mailbox != 0) begin
  69. freq_confirm = mailbox; // frequency has been confirmed
  70. // remobed HzLED variable, freq_confirm does all of it now
  71. //HzLED <= mailbox; // 3 bit mailbox feeds directly into HzLED's to show user
  72. end
  73. else
  74. freq_confirm = 0;
  75. end
  76. end // end IR state
  77. else if(state == 0) begin // in RM state
  78.  
  79. //HzLED <= HzLED; // keeps on previously confirmed lights
  80. // HZLED TYPE VARIABLE REQUIRED TO KEEP DRIVING AN LED
  81. // SINCE FREQ CONFIRM MUST GO BACK TO 0 AFTER CONFIRMATION!!!
  82. freq_confirm <= 0; // confirm flag needs to be reset for next IR state
  83. end
  84.  
  85. end
  86.  
  87.  
  88. endmodule
  89.  
  90.  
  91. // instead of rising/falling edge, every clock tick check IR signal
  92. // previous IR signal, current IR signal.
  93. // set both to zero
  94. // if(IRsignal && !prevIR) --> data <= data + 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement