Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
16596 Discussions

I need help with coding...

assn1
Novice
902 Views

I'm making a digital watch code.
There is no problem with the code itself, but as I input things like BUTTON_SW and BUS_SW, I didn't have enough buttons in my kit to run the hour, minute, second, and mode conversion…

So I want to allocate BUTTON_SW to work as much as the DIP button in the kit is turned on. But that code failed.
Could you please help me?

 

<code>

always @ (negedge RESETN or posedge CLK_100HZ) begin
if (~RESETN)
DIP = 1'b0;
else begin
if (DIP < 17)
if (DIP == 4'b0000)
DIP = BUTTON_SW[0];
else if (DIP == 4'b0001)
DIP = BUTTON_SW[1];

else if (DIP == 4'b0010)
DIP = BUTTON_SW[2];

else if (DIP == 4'b0011)
DIP = BUTTON_SW[3];

else if (DIP == 4'b0100)
DIP = BUTTON_SW[4];

else if (DIP == 4'b0101)
DIP = BUTTON_SW[5];

else if (DIP == 4'b0110)
DIP = BUTTON_SW[6];

else if (DIP == 4'b0111)
DIP = BUTTON_SW[7];

else if (DIP == 4'b1000)
DIP = BUTTON_SW[8];

else if (DIP == 4'b1001)
DIP = BUTTON_SW[9];

else if (DIP == 4'b1010)
DIP = BUTTON_SW[10];

else if (DIP == 4'b1011)
DIP = BUTTON_SW[11];

else if (DIP == 4'b1100)
DIP = BUTTON_SW[12];

else if (DIP == 4'b1101)
DIP = BUTTON_SW[13];

else if (DIP == 4'b1110)
DIP = BUTTON_SW[14];

else if (DIP == 4'b1111)
DIP = BUTTON_SW[15];
end
end 

0 Kudos
9 Replies
ak6dn
Valued Contributor III
886 Views

I really have no idea what the question is that you are asking. Can you be more specific?

There is no problem with the code itself, but as I input things like BUTTON_SW and BUS_SW, I didn't have enough buttons in my kit to run the hour, minute, second, and mode conversion…

So I want to allocate BUTTON_SW to work as much as the DIP button in the kit is turned on. But that code failed.

By the way, I would have written your code like this...

always @(posedge CLK_100HZ or negedge RESETN)
    begin
    if (~RESETN)
        DIP <= 1'b0;
    else
        if (DIP < 17) DIP <= BUTTON_SW[DIP[3:0]];
    end

which is much more succinct.

I also prefer to use a non-blocking register assignment (<=) as opposed to a blocking (=) assignment UNLESS you specifically need the functionality of a blocking assignment (ie, if you are computing temporary intermediate values).

And I always put the clock edge first in the always @() block to make it clear what clock edge is providing state transition.

assn1
Novice
861 Views

All right, let's go into more detail. Here's my kit (QB-EP2C208-BK).

 

1.png

And BUTTON_SW [15:0] is the code that you enter to raise the time, minutes, and seconds of the digital clock.
So I have to assign it to the button. That way I can enter the time manually.
But as you can see in the picture, the number of buttons is small....

KakaoTalk_20220605_125256523.png

Therefore, when DIP switch 1 above is turned on, button 1 with BUTTON_SW assigned will operate as BUTTON_SW [1], When DIP switches 1 and 2 are turned on, the switch is operated as BUTTON_SW[2]. I want to continue this method and operate it until BUTTON_SW[15]. 

Do you need the full code for your digital watch?

0 Kudos
ak6dn
Valued Contributor III
858 Views

Well, I am not to write your code for you.

If you want to ask questions about some code you have written that is one thing.

We are not here to do your project for you.

 

That being said, I would typically implement a data entry function for setting a clock (in fact I have for my nixie clock) using a sequential state machine:

Press button1 to get into set hours tens digit mode.
Then press button 2 to increment the selected digit (ie, hours 10s)
Press button 1 to save the selected digit, and move to the next digit (ie, hours unit).
Then press button 2 to increment the selected digit (ie, hours 1s).

Keep going until get down to seconds 1s digit.

Last press of button 1 sets the seconds 1s digit and enables the clock to start running.

So all you really need is two buttons, no other switches.

0 Kudos
assn1
Novice
851 Views

You're right. I also want to take care of it on my own, but... It was so hard. Sorry.

I want to try as you said, so should I remove the DIP, and move the code corresponding to the hour, minute, and second using the case(BUTTON_SW) Like this?

always @(posedge CLK_100HZ or negedge RESETN)
begin
if (~RESETN)
BUTTON_SW <= 1'b0;
else begin
case(BUTTON_SW)
0 : BUTTON_SW = hour10_up_in;
1 : BUTTON_SW = hour1_up_in;
.
.
.

0 Kudos
ak6dn
Valued Contributor III
821 Views

Well, by your above module definition, BUTTON_SW is an INPUT to your module, so you can't assign to it.

You need to define a multi-bit reg which is your state variable, and based on button pushes, transition between states.

Based on the state you are in (ie, hr10s, hr1s, min10s, min1s, etc) you would use the second button push to increment the selected digit.

The final state would be the ClockRun state.

0 Kudos
assn1
Novice
804 Views

All right, I gave it a try.
I added sw_time to the input (pressing this to move the position) and moved the corresponding time, minute, and second to reg, and added a reg called sw_t to hold it.
And so I made the code, and I succeeded in compiling it, but it works strangely in the kit.
I'm sorry I kept asking.....

 

always@(posedge CLK_1KHz) begin
case(sw_time)
0 : sw_t <= c_10h;
1 : sw_t <= c_1h;
2 : sw_t <= c_10m;
3 : sw_t <= c_1m;
4 : sw_t <= c_10s;
5 : sw_t <= c_1s;
endcase
end

0 Kudos
ak6dn
Valued Contributor III
795 Views

Well, sorry, I don't see a question there.

I don't know if much more help can be gotten thru this forum for you.

I think you need to find someone who can sit down next to you and go over your design's structure and examine your code.

assn1
Novice
770 Views
Yes... I think you're right. Actually, I don't even know if I delivered the question correctly...
But it helped a lot. Thank you.
0 Kudos
ShengN_Intel
Employee
760 Views

Seems like no further help is needed on this thread. I'll now transition this thread to community support. If you have a new question, feel free to open a new thread to get the support from Intel experts.


Thank you.


0 Kudos
Reply