OpenCores
URL https://opencores.org/ocsvn/oscilloscope/oscilloscope/trunk

Subversion Repositories oscilloscope

[/] [oscilloscope/] [trunk/] [adc.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 olegodints
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company:  Bauman Moscow University
4
// Engineer: Oleg A. Odintsov
5
// 
6
// Create Date:    21:44:06 02/28/2012 
7
// Design Name: 
8
// Module Name:    Main module
9
// Project Name:   Oscilloscope
10
// Target Devices: Xilinx Spartan 3E
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module adc(input clk,
22
                        input[1:0] flags, // enable or disable phases
23
                        input[3:0] gain_A,
24
                        input[3:0] gain_B,
25
                        output reg[13:0] val_A,
26
                        output reg[13:0] val_B,
27
                        output reg upd,
28
                        output SPI_MOSI,
29
                        output AMP_CS,
30
                        output SPI_SCK,
31
                        output AMP_SHDN,
32
                        input  AMP_DOUT,
33
                        output AD_CONV,
34
                        input  SPI_MISO);
35
 
36
        parameter
37
                STATE_IDLE = 2'd0,
38
                STATE_G = 2'd1,
39
                STATE_V  = 2'd3,
40
                FLAG_G = 0,
41
                FLAG_V = 1;
42
 
43
        reg[1:0] state = STATE_IDLE;
44
        reg[8:0] val;
45
        reg[13:0] out;
46
        integer cnt = 0;
47
 
48
        wire state_amp = (state == STATE_G);
49
        wire state_adc = (state == STATE_V);
50
 
51
        assign AD_CONV = state_adc && (cnt == 34);
52
        assign SPI_MOSI = state_amp?val[8]:1'bZ;
53
        assign AMP_CS = ~state_amp;
54
        assign SPI_SCK = state_amp?((cnt==9||cnt==0)?1'b0:~clk): state_adc?((cnt == 34)?1'b0:~clk): 1'bZ;
55
        assign AMP_SHDN = 1'b0;
56
 
57
 
58
 
59
        function[1:0] next_state;
60
                input[1:0] state;
61
                input[2:0] flags;
62
                case (state)
63
                STATE_IDLE:
64
                        next_state = flags[FLAG_G]?STATE_G:(flags[FLAG_V]?STATE_V:STATE_IDLE);
65
                STATE_G:
66
                        next_state = flags[FLAG_V]?STATE_V:(flags[FLAG_G]?STATE_G:STATE_IDLE);
67
                STATE_V:
68
                        next_state = flags[FLAG_G]?STATE_G:(flags[FLAG_V]?STATE_V:STATE_IDLE);
69
                endcase
70
        endfunction
71
 
72
        always @(posedge clk) begin
73
                if (cnt) begin
74
                        cnt <= cnt - 1;
75
                        case (state)
76
                        STATE_G: val <= {val[7:0], 1'b0};
77
                        STATE_V:        begin
78
                                if (cnt <= 31 && cnt >= 18) out <= {out[12:0], ~SPI_MISO};
79
                                else if (cnt == 17) val_A <= out;
80
                                else if (cnt <= 15 && cnt >= 2) out <= {out[12:0], ~SPI_MISO};
81
                                else if (cnt == 1) begin val_B <= out; upd <= 1; end
82
                                end
83
                        endcase
84
                end else begin
85
                        state = next_state(state, flags);
86
                        upd <= 0;
87
                        case (state)
88
                        STATE_G: begin cnt <= 9; val <= {1'b0, gain_B, gain_A}; end
89
                        STATE_V:  begin cnt <= 34; end
90
                        endcase
91
                end
92
        end
93
endmodule
94
 
95
 
96
module clk_div(input clk, output clk1);
97
        parameter divide = 16;
98
        wire clk0;
99
 
100
   DCM_SP #(
101
      .CLKDV_DIVIDE(divide) // Divide by: 1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0,6.5
102
                          //   7.0,7.5,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0 or 16.0
103
   ) DCM_SP_inst (
104
      .CLKDV(clk1),   // Divided DCM CLK out (CLKDV_DIVIDE)
105
      .CLKIN(clk),   // Clock input (from IBUFG, BUFG or DCM)
106
                .CLK0(clk0),
107
                .CLKFB(clk0),
108
                .RST(0)
109
   );
110
 
111
endmodule
112
 
113
module my_clk_div(input clk, output reg clk1 = 0);
114
        parameter divide = 16;
115
        integer cnt = 0;
116
        always @(posedge clk) begin
117
                cnt <= (cnt?cnt:(divide/2)) - 1;
118
                if (!cnt) clk1 <= ~clk1;
119
        end
120
 
121
endmodule
122
 
123
 
124
module video_counters(
125
                input clk,
126
                output reg video_vsync = 1,
127
                output reg video_hsync = 1,
128
                output video_on,
129
                output reg [10:1] hpos = 0,
130
                output reg [9:1] vpos = 0);
131
 
132
        integer hcnt = 0, vcnt = 0;
133
 
134
        reg video_von = 0, video_hon = 0;
135
        assign video_on = video_von & video_hon;
136
 
137
        always @(posedge video_hsync) begin
138
                vcnt <= vcnt + 1;
139
                vpos <= video_von?vpos + 1: 0;
140
                case (vcnt)
141
                2: video_vsync = 1;
142
                31: video_von = 1;
143
                511: video_von = 0;
144
                521: begin vcnt <=0; video_vsync = 0; end
145
                endcase
146
        end
147
 
148
        always @(posedge clk) begin
149
                if (!video_hon) hcnt <= hcnt - 1;
150
                else hpos <= hpos + 1;
151
 
152
                if (hpos == 639) video_hon <= 0;
153
 
154
                if (hpos == 640) begin
155
                        if (!hcnt) begin
156
                                hcnt <= 96;
157
                                video_hsync <= 0;
158
                                hpos <= 0;
159
                        end
160
                end else if (!hcnt) begin
161
                        if (!video_hsync) begin
162
                                video_hsync <= 1;
163
                                hcnt <= 48;
164
                        end else if (!video_hon) begin
165
                                video_hon <= 1;
166
                                hcnt <= 16;
167
                        end
168
                end
169
        end
170
endmodule
171
 
172
 
173
 
174
module rot_driver(input clk,
175
                                input rot_a, input rot_b,
176
                                output wire rot_dir, output wire rot_event_out);
177
 
178
        reg rot_a_latch = 0, rot_b_latch = 0;
179
        assign rot_dir = rot_b_latch, rot_event_out = rot_a_latch;
180
        always @(posedge clk) begin
181
                case ({rot_a, rot_b})
182
                2'b00: rot_a_latch <= 1;
183
                2'b11: rot_a_latch <= 0;
184
                2'b10: rot_b_latch <= 1;
185
                2'b01: rot_b_latch <= 0;
186
                endcase
187
        end
188
endmodule
189
 
190
module btn_driver(input clk, input btn, output reg sig = 0);
191
        parameter nskip = 'hfff;
192
        integer counter = 0;
193
        wire lock = counter?1:0;
194
 
195
        always @(posedge clk) begin
196
                if (counter) counter <= counter - 1;
197
                if (!lock && sig != btn) begin
198
                        sig <= btn;
199
                        counter <= nskip;
200
                end
201
        end
202
endmodule
203
 
204
 
205
module main(input CLK_50MHZ,
206
                        output SPI_MOSI,
207
                        output AMP_CS,
208
                        output SPI_SCK,
209
                        output AMP_SHDN,
210
                        input  AMP_DOUT,
211
                        output AD_CONV,
212
                        input  SPI_MISO,
213
                        output SPI_SS_B,
214
                        output DAC_CS,
215
                        output SF_CE0,
216
                        output FPGA_INIT_B,
217
                        output[7:0] LED,
218
                        output VGA_RED, VGA_GREEN, VGA_BLUE,
219
                        output VGA_HSYNC, VGA_VSYNC,
220
                        input ROT_A, ROT_B,
221
                        input BTN_EAST, BTN_NORTH, BTN_SOUTH, BTN_WEST
222
                        );
223
 
224
        reg[3:0] amp_A = 4'b0001, amp_B = 4'b0001;
225
        wire[13:0] val_A, val_B;
226
        wire upd;
227
 
228
        assign SPI_SS_B = 1'b1, DAC_CS = 1'b1, SF_CE0 = 1'b1, FPGA_INIT_B = 1'b0;
229
 
230
        assign LED = val_A[13:6];
231
        //assign LED = {SPI_SCK, AMP_CS, SPI_MOSI, AMP_DOUT, AD_CONV, SPI_MISO, 2'b0};
232
 
233
        wire CLK, CLK_25MHZ, CLK_5HZ;
234
        wire video_on;
235
        wire[10:1] hpos;
236
        wire[9:1] vpos;
237
        reg[2:0] color;
238
        assign {VGA_RED, VGA_GREEN, VGA_BLUE} = video_on?color: 3'b0;
239
 
240
        reg[13:0] memA[0:639];
241
        reg[13:0] memB[0:639];
242
        reg[10:1] regpos = 0;
243
 
244
        reg[13:0] curA, curB;
245
        reg[13:0] lastA, lastB;
246
        wire[13:0] ncurA = ~curA, ncurB = ~curB;
247
        wire[13:0] nlastA = ~lastA, nlastB = ~lastB;
248
        wire[9:1] posA, posB, lposA, lposB;
249
 
250
        reg[10:0] div = 1, dcnt = 0;
251
        reg CLK1 = 0;
252
        assign CLK = CLK1;
253
 
254
        assign posA = curA[13]? (241 + ncurA[12:6]): (240 - curA[12:6]);
255
        assign posB = curB[13]? (241 + ncurB[12:6]): (240 - curB[12:6]);
256
 
257
        assign lposA = lastA[13]? (241 + nlastA[12:6]): (240 - lastA[12:6]);
258
        assign lposB = lastB[13]? (241 + nlastB[12:6]): (240 - lastB[12:6]);
259
 
260
//      wire a_on = (vpos - 240 == val_A[13:7]);
261
//      wire b_on = (vpos - 240 == val_B[13:7]);
262
        wire a_on = ((vpos >= posA) && (vpos <= lposA)) || ((vpos >= lposA) && (vpos <= posA));
263
        wire b_on = ((vpos >= posB) && (vpos <= lposB)) || ((vpos >= lposB) && (vpos <= posB));
264
        wire x_on = (vpos == 240);
265
 
266
        clk_div#2.0 div2(CLK_50MHZ, CLK_25MHZ);
267
        clk_div#5  div5(CLK_50MHZ, CLK0);
268
        my_clk_div#2000000  div5hz(CLK0, CLK_5HZ);
269
 
270
        wire btns[3:0];
271
 
272
        btn_driver b0(CLK0, BTN_EAST, btns[0]);
273
        btn_driver b1(CLK0, BTN_NORTH, btns[1]);
274
        btn_driver b2(CLK0, BTN_SOUTH, btns[2]);
275
        btn_driver b3(CLK0, BTN_WEST, btns[3]);
276
 
277
        always @(posedge CLK0) begin
278
                dcnt <= (dcnt?dcnt:div) - 1;
279
                if (!dcnt) CLK1 <= ~CLK1;
280
        end
281
 
282
        always @(posedge CLK_5HZ) begin
283
                if (btns[3]) if (amp_A != 4'b0001) amp_A <= amp_A - 1;
284
                if (btns[0]) if (amp_A != 4'b0111) amp_A <= amp_A + 1;
285
                if (btns[2]) if (amp_B != 4'b0001) amp_B <= amp_B - 1;
286
                if (btns[1]) if (amp_B != 4'b0111) amp_B <= amp_B + 1;
287
        end
288
 
289
 
290
//      my_clk_div#25000000 div16_1(CLK_25MHZ, CLK);
291
 
292
        always @(negedge upd) begin
293
                memA[regpos] <= val_A;
294
                memB[regpos] <= val_B;
295
                regpos <= (regpos == 639)?0:(regpos + 1);
296
        end
297
 
298
        video_counters cnt(CLK_25MHZ, VGA_VSYNC, VGA_HSYNC, video_on, hpos, vpos);
299
        always @(posedge CLK_25MHZ) begin
300
                curA <= memA[hpos];
301
                curB <= memB[hpos];
302
                if (hpos) begin
303
                        lastA <= curA;
304
                        lastB <= curB;
305
                end else begin
306
                        lastA <= memA[hpos];
307
                        lastB <= memB[hpos];
308
                end
309
                color <= {a_on, x_on, b_on};
310
        end
311
 
312
        wire rot_dir, rot_event;
313
 
314
        rot_driver rot(CLK_25MHZ, ROT_A, ROT_B, rot_dir, rot_event);
315
 
316
        always @(posedge rot_event) begin
317
                div <= rot_dir?(div + 1): ((div>1)?(div - 1):div);
318
        end
319
 
320
        adc a1(CLK, 2'b11, amp_A, amp_B, val_A, val_B, upd,
321
                        SPI_MOSI, AMP_CS, SPI_SCK, AMP_SHDN, AMP_DOUT, AD_CONV, SPI_MISO);
322
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.