Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Input and output signal for controller

Status
Not open for further replies.

Parth86

Member
hello experts
I want to write input and output for microcontroller in verilog langugae (8051)

I have tried to write code


Code:
Module mcu (clk, rst, en, p_in, p_out, t_in, t_out, i_in,i_out, rx_in, tx_out ,a0,a1,a2,d0,d1,d2,d3,d4,d5,d6,d7,a,b,s0,s1,s2,f,........etc);
Rst= reset input
Clk=  clock input
En= enable input

Port
P_in=port input
P_out= port output

Timer
T_in = timer input
T_out = timer output

Interrupt
i_in= input for interrupt
i_out,= output for interrupt

ALU
A=input
B=input
So=input
S1=input
S2=input
F=output

Decoder
opcode_in
opcode_out

ram
wr
wd

ram
read
write
enable
anyone can tell me how to start ?

I have read about pin diagram but I don't understand which pins is used for ALU, decoder rom and ram memory

I think each pin is used for input and output. I have to define in top line . I have write some input and output for controller
 
Last edited by a moderator:
Why 8051? Have you ever used it in a circuit? Have you ever written a program for it? If you dont know which pins are used for ALU (none are) then you are doing it the wrong way.
You cannot start your design with the top level when you have no idea what should be in there.
You need to start with the basic parts - ALU, program counter, registers and program memory. Do these three, make them work with a subset of the instuctions, then start thinking about the rest of the stuff.

Also, it might be easier to download the whole 8051 soft core from opencores.org, learn how it works, and then start doing your own version.
 
Why 8051? Have you ever used it in a circuit? Have you ever written a program for it? If you dont know which pins are used for ALU (none are) then you are doing it the wrong way.
You cannot start your design with the top level when you have no idea what should be in there.
You need to start with the basic parts - ALU, program counter, registers and program memory. Do these three, make them work with a subset of the instuctions, then start thinking about the rest of the stuff.

Also, it might be easier to download the whole 8051 soft core from opencores.org, learn how it works, and then start doing your own version.

I have done some work
look at here
I have made sample code for 4 bit ALU and 3 to 8 decoder to make 4 bit processor . as designer we can design anything so I have started to design processor with two function ALU and decoder

specification
4 bit processor
4 bit ALU
3 to 8 decoder

4 bit ALU verilog code

Code:
Module alu (a,b,s0,s1,s2 f);
Input a,b,s0,s1,s2;
Output f;
Reg [3:0];
Always @(s0,s1,s2);
Begian
Case (s0,s1,s2);
3b’000 :f=(a&b);
3b’001:f= (a|b);
3b’010 :f= ~(a&b);
3b’011 :f= ~ (a|b);
3b’100:f=(a^b);
3b’101: f=(a*b);
3b’110: f=(a+b);
3b’111:f=(a-b );
End case
End module

3 to 8 decoder

Code:
Module decoder (a2,a1,a0, d7,d6,d5,d4,d3,d2,d1,d0);
Input a2,a1,a0;
Output d7,d6,d5,d4,d3,d2,d1,d0;
Wire [7:0];
Always @(a2,a1,a0);
Begin
Case (a2,a1,a0);
4’b000:( d7,d6,d5,d4,d3,d2,d1,d0)=00000001;
4’001: (d7,d6,d5,d4,d3,d2,d1,d0)=00000010;
4’b010: (d7,d6,d5,d4,d3,d2,d1,d0)=00000100;
4’b011: (d7,d6,d5,d4,d3,d2,d1,d0)=00001000;
4’b100:( d7,d6,d5,d4,d3,d2,d1,d0)=00010000;
4’b101:( d7,d6,d5,d4,d3,d2,d1,d0)=00100000;
4’b110: (d7,d6,d5,d4,d3,d2,d1,d0)=01000000;
4’b111: (d7,d6,d5,d4,d3,d2,d1,d0)=10000000;
Endcase
Endmodule

thanks for quick response I know how does we write input and output for gates , flip flop adder, decoder counter, alu ,....etc but I don't understand for microcontroller
 
Aaand the question is? Have you tried to compile what you have written? You sure do have quite a few errors in your code.
Remeber that verilog is a quite high-level language, so you can use its contructs to make your code much simpler. You could replace the whole case statement with a simple D=1<<A; (that is shift 1 to the left A times)
Also you could write this code inside the ALU and get rid of the decoder alltogether.
this is the way I think it should be done, also remember that verilog is case sensitive, so writing Begin or Input will likely not work.

Code:
module decoder (A, D);
input [2:0] A;
output [7:0] D;
always @(A)
begin: 
 
Case (A);
0:D=1;
1:D=2;
2:D=4;
3:D=8;
4:D=16;
5:D=32;
6:D=64;
7:D=128;
endcase

end
endmodule
 
Another way to do the module definition is like this:
Code:
module decoder
(
input [2:0] A,
output [7:0] D
);

always @(A)
begin:
Case (A);
0:D=1;
1:D=2;
2:D=4;
3:D=8;
4:D=16;
5:D=32;
6:D=64;
7:D=128;
endcase

end
endmodule
 
Have you tried to compile
no I made for temporary

I just want to know the way how we start to write code for micro controller
look at my post #1

I don't say that write all code for me .
if you can help with this code. Its grate help for me .
welcome for your any advice
 
I just want to know the way how we start to write code for micro controller
Normally you would start by defining how exactly should it work, which instructions and capabilities it should have etc. That would be called a top down design, you define the top - the inputs and outputs of the whole thing, and then design all the inner parts as you go along.

But in your case, since you seem to have no idea what youre going into, you should start with something very simple, and doing it bottom up will be a lot easier - startign from pieces and building the whole thing.
The ALU you did is a good start to get you going. Next you need to make one register that will store your results so that you can use them in the next operation. Then you need a program counter so that you can load each instruction from the program memory, and a controller that will decide what goes where.

And the most important thing, get some IDE like the Xilinx ide RIGHT NOW and start testing and using the code immediately, otherwise you will write lots of code that has hundreds of syntax errors and then you will spend hours rewriting the code properly so that it can be compile.
 
That would be called a top down design, you define the top - the inputs and outputs of the whole thing, and then design all the inner parts as you go along.
I just want to confirm that I am going in write way

.
The ALU you did is a good start to get you going. Next you need to make one register that will store your results so that you can use them in the next operation.

. firs I just want make sample code to better understanding so I made some code . I know codes are not correct but can you tell how to connect alu , decoder in my verilog code for microcontroller. then I will write code for program counter , register and others

thank you very much in advance for every help
 
Last edited by a moderator:
Like I said, it is much easier to write correct code the first time, than to redo everything later. It will teach you the syntax of verilog right away, so all the code you write later will have less and less errors in it.
 
Last edited:
you define the top - the inputs and outputs of the whole thing, and then design all the inner parts as you go along
first I have define input output example ALU, DECODER, PORT ....etc
you should start with something very simple, and doing it bottom up will be a lot easier - startign from pieces and building the whole thing.
The ALU you did is a good start to get you going
then I made code for inner part example ALU, decoder

verilog code
Code:
Module mcu (clk, rst, en, p_in, p_out, t_in, t_out, i_in,i_out, rx_in, tx_out ,a0,a1,a2,d0,d1,d2,d3,d4,d5,d6,d7,a,b,s0,s1,s2,f,........etc);
Rst= reset input
Clk=  clock input
En= enable input
Port
P_in=port input
P_out= port output

Timer
T_in = timer input
T_out = timer output

Interrupt
i_in= input for interrupt
i_out,= output for interrupt

ram
wr
wd

ram
read
write
enable

Module alu (a,b,s0,s1,s2 f);
Input a,b,s0,s1,s2;
Output f;
Reg [3:0];
Always @(s0,s1,s2);
Begian
Case (s0,s1,s2);
3b’000 :f=(a&b);
3b’001:f= (a|b);
3b’010 :f= ~(a&b);
3b’011 :f= ~ (a|b);
3b’100:f=(a^b);
3b’101: f=(a*b);
3b’110: f=(a+b);
3b’111:f=(a-b );
End case
End module



Module decoder (a2,a1,a0, d7,d6,d5,d4,d3,d2,d1,d0);
Input a2,a1,a0;
Output d7,d6,d5,d4,d3,d2,d1,d0;
Wire [7:0];
Always @(a2,a1,a0);
Begin
Case (a2,a1,a0);
4’b000:( d7,d6,d5,d4,d3,d2,d1,d0)=00000001;
4’001: (d7,d6,d5,d4,d3,d2,d1,d0)=00000010;
4’b010: (d7,d6,d5,d4,d3,d2,d1,d0)=00000100;
4’b011: (d7,d6,d5,d4,d3,d2,d1,d0)=00001000;
4’b100:( d7,d6,d5,d4,d3,d2,d1,d0)=00010000;
4’b101:( d7,d6,d5,d4,d3,d2,d1,d0)=00100000;
4’b110: (d7,d6,d5,d4,d3,d2,d1,d0)=01000000;
4’b111: (d7,d6,d5,d4,d3,d2,d1,d0)=10000000;
Endcase
Endmodule
how to route alu with decoder . I am asking for sample code ( handy code )
 
Last edited by a moderator:
For example like this:
Code:
module tx_top(
    input clk,
    input reset_count,
    input last_bit,
    input txrx,
    input ld,
    input [8:0] data_in,
    output tx_out,
    output tx_finished,    
    output tx_p);

    wire clk8r, clk64r;
    wire [3:0] n;
    wire [7:0] data8;
    wire [7:0] d;
    wire [7:0] d1;
    wire [3:0] dn;
    wire [3:0] dn1;   

    shifter shifter(.clk(clk),  .txrx(txrx), .ld(ld), .data(data8),.n(n),.tx_bit(tx_bit),.shift_pulse(tx_p), .tx_finished(tx_finished), .loaded_d1(loaded_d1), .d(d), .d1(d1), .dn(dn), .dn1(dn1), .ld_old(ld_old) );
    timer timer(.clk(clk), .reset_count(reset_count),.last_bit(last_bit),.clk8(clk8r),.clk64(clk64r),.tx_pulse(tx_p), .tx_loaded(tx_loaded));
    waiter waiter(.clk(clk), .txrx(txrx), .ld(ld), .tx_loaded(tx_loaded), .tx_finished(tx_finished));
   
    decoder decoder(.data_in(data_in),.n(n),.data8(data8));
    tx_logic txlogic(.clk8r(clk8r), .clk64(clk64r),.tx_finished(tx_finished), .tx_bit(tx_bit), .tx_out(tx_out), .tx_loaded(tx_loaded));
endmodule
 
This is the absolute basics, have you tried reading some tutorial or a book prehaps?
An engineer should be able to find the information he needs all by himself, not asking basic questions that can be answered in five seconds on google.
 
This is the absolute basics, have you tried reading some tutorial or a book prehaps?
An engineer should be able to find the information he needs all by himself, not asking basic questions that can be answered in five seconds on google.
I have spend lot of time. I am not designing for company.
i am student I don't care what I will make. I just want to learn way ,
I know designing controller is not easy . It need more reading and practice
I did search more but I could not find how to add alu , decoder , program counter and other
so I just wrote code for alu, and decoder
so I did ask here
 
Code:
shifter shifter(.clk(clk),  .txrx(txrx), .ld(ld), .data(data8),.n(n),.tx_bit(tx_bit),.shift_pulse(tx_p), .tx_finished(tx_finished), .loaded_d1(loaded_d1), .d(d), .d1(d1), .dn(dn), .dn1(dn1), .ld_old(ld_old) );
    timer timer(.clk(clk), .reset_count(reset_count),.last_bit(last_bit),.clk8(clk8r),.clk64(clk64r),.tx_pulse(tx_p), .tx_loaded(tx_loaded));
    waiter waiter(.clk(clk), .txrx(txrx), .ld(ld), .tx_loaded(tx_loaded), .tx_finished(tx_finished));
 
    decoder decoder(.data_in(data_in),.n(n),.data8(data8));
    tx_logic txlogic(.clk8r(clk8r), .clk64(clk64r),.tx_finished(tx_finished), .tx_bit(tx_bit), .tx_out(tx_out), .tx_loaded(tx_loaded));
endmodule

I think you are using gate level modeling but I want to use behavioral modeling or structural modeling because its easy to understand
 
I think you are using gate level modeling but I want to use behavioral modeling or structural modeling because its easy to understand
Why do you think that? I dont see any way you could tell from this what is inside shifter, timer or any other of the components used. This is just the instantiation of the components used inside tx_top and it defines how they are wired together. It has nothing to do with any modelling paradigm.
 
Why do you think that? I dont see any way you could tell from this This is just the instantiation of the components used inside tx_top and it . It has nothing to do with any modelling paradigm.
sorry but still I don't understand following
what is inside shifter, timer or any other of the components used.
defines how they are wired together
 
Last edited by a moderator:
The code I posted before uses many components, like shifter, timer, tx_logic etc. These components are defined in some other file, just like your ALU is.
In the tx_top you define how are these components connected - where do ins and outs from each component connec to.
 
The code I posted before uses many components, like shifter, timer, tx_logic etc. These components are defined in some other file, just like your ALU is.
In the tx_top you define how are these components connected - where do ins and outs from each component connec to.
ok what do you think about this code
Code:
Module mcu (clk, rst, en, p_in, p_out, t_in, t_out, i_in,i_out, rx_in, tx_out ,a0,a1,a2,d0,d1,d2,d3,d4,d5,d6,d7,a,b,s0,s1,s2,f,........etc);

//Input, output  declaration
Rst= reset input
Clk=  clock input
En= enable input
Port
P_in=port input
P_out= port output
Timer
T_in = timer input
T_out = timer output
Interrupt
i_in= input for interrupt
i_out,= output for interrupt
ram
wr
wd
ram
read
write
enable

//Interconnect signals
wire alu [3:0]
wire decoder [7:0]
reg counter [3:0]
// add alu
Mcu_alu (a,b,s0,s1,s2 f);
Input a,b,s0,s1,s2;
Output f;
Reg [3:0];
Always @(s0,s1,s2);
Begian
Case (s0,s1,s2);
3b’000 :f=(a&b);
3b’001:f= (a|b);
3b’010 :f= ~(a&b);
3b’011 :f= ~ (a|b);
3b’100:f=(a^b);
3b’101: f=(a*b);
3b’110: f=(a+b);
3b’111:f=(a-b );
End case;

//Add decoder
Mcu_decoder (a2,a1,a0, d7,d6,d5,d4,d3,d2,d1,d0);
Input a2,a1,a0;
Output d7,d6,d5,d4,d3,d2,d1,d0;
Wire [7:0];
Always @(a2,a1,a0);
Begin
Case (a2,a1,a0);
4’b000:( d7,d6,d5,d4,d3,d2,d1,d0)=00000001;
4’001: (d7,d6,d5,d4,d3,d2,d1,d0)=00000010;
4’b010: (d7,d6,d5,d4,d3,d2,d1,d0)=00000100;
4’b011: (d7,d6,d5,d4,d3,d2,d1,d0)=00001000;
4’b100:( d7,d6,d5,d4,d3,d2,d1,d0)=00010000;
4’b101:( d7,d6,d5,d4,d3,d2,d1,d0)=00100000;
4’b110: (d7,d6,d5,d4,d3,d2,d1,d0)=01000000;
4’b111: (d7,d6,d5,d4,d3,d2,d1,d0)=10000000;
Endcase

//add counter 
module up_counter(current state, next state ,clk)
input current state ;
input clk;
output next state;
reg 3:0
always @ (posedge clk);
begin
next state <= current state +1 ;
end
endmodule
Endmodule
 
Last edited by a moderator:
look at this code there is another way to connect one component to other
Is it correct way to connect with another ,
Code:
module mcu_8051 (rst, clk ,

// interrupt interface
                int0_i,
                int1_i,
// port interface
  `ifdef mcu_port
        `ifdef mcu_port0
                p0_i,
                p0_o,
        `endif
        `ifdef mcu_port1
                p1_i,
                p1_o,
        `endif
        `ifdef mcu_port2
                p2_i,
                p2_o,
        `endif
        `ifdef mcu_port3
                p3_i,
                p3_o,
        `endif
  `endif
// serial interface
        `ifdef mcu_uart
                rxd_i, txd_o,
        `endif
// counter interface
        `ifdef mcu_tc01
                t0_i, t1_i,
        `endif
        `ifdef OC8051_tc2
                t2_i, t2ex_i,
        `endif
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top