ModelSim unexpected z input - verilog

I'm building a simple 7-segment display.
I didn't have errors when I was compiling the module and testbench.
But, when I'm simulating, I keep getting z value as input. Why do I get the z?
Verilog code as below :
module dec_7seg(d, seg);
input [3:0]d;
output [7:0]seg;
assign seg[1]= (d[1]&~d[2]) | d[0] | (~d[1]&d[2]) | (d[2]&~d[3]);
assign seg[2]= (~d[0]&d[1]) | (~d[1]&~d[2]&~d[3]) | (d[1]&d[2]) | (d[0]&~d[1]&d[3]);
assign seg[3]= (~d[1]&~d[3]) | (d[0]&d[1]) | (d[2]&~d[3]) | (d[0]&d[2]);
assign seg[4]= (~d[1]&~d[3]) | (d[1]&~d[2]&d[3]) | (~d[1]&d[2]) | (d[2]&~d[3]) | (d[0]&~d[3]);
assign seg[5]= (~d[2]&d[3]) | (~d[0]&d[1]) | (~d[0]&~d[2]) | (d[0]&~d[1]) | (~d[0]&d[3]);
assign seg[6]= (~d[1]&~d[2]) | (~d[0]&~d[2]&~d[3]) | (~d[0]&~d[1]) | (~d[0]&d[2]&d[3]) | (d[0]&~d[2]&d[3]) | (d[0]&d[2]&~d[3]);
assign seg[7]= (~d[1]&~d[3]) | (~d[0]&d[2]) | (d[1]&d[2]) | (~d[0]&d[1]&d[3]) | (d[0]&~d[1]&~d[2]);
endmodule
Testbench :
`timescale 1ns/1ps
module tb_dec_7seg_selfchecking();
reg [3:0] d;
wire [7:0] seg;
dec_7seg U0(.d(d),.seg(seg));
initial begin
d=4'b0000; #10; //0000
if(seg !== 8'b11111100)$display("0 fail");
d=4'b1000; #10; //0001
if(seg !== 8'b01100000)$display("1 fail");
d=4'b0100; #10; //0010
if(seg !== 8'b11011010)$display("2 fail");
d=4'b1100; #10; //0011
if(seg !== 8'b11110010)$display("3 fail");
d=4'b0010; #10; //0100
if(seg !== 8'b01100110)$display("4 fail");
d=4'b1010; #10; //0101
if(seg !== 8'b10110110)$display("5 fail");
d=4'b0110; #10; //0110
if(seg !== 8'b10111110)$display("6 fail");
d=4'b1110; #10; //0111
if(seg !== 8'b11100100)$display("7 fail");
d=4'b0001; #10; //1000
if(seg !== 8'b11111110)$display("8 fail");
d=4'b1001; #10; //1001
if(seg !== 8'b11100110)$display("9 fail");
d=4'b0101; #10; //1010
if(seg !== 8'b11111010)$display("a fail");
d=4'b1101; #10; //1011
if(seg !== 8'b00111110)$display("b fail");
d=4'b0011; #10; //1100
if(seg !== 8'b00011010)$display("c fail");
d=4'b1011; #10; //1101
if(seg !== 8'b01111010)$display("d fail");
d=4'b0111; #10; //1110
if(seg !== 8'b11011110)$display("e fail");
d=4'b1111; #10; //1111
if(seg !== 8'b10001110)$display("f fail");
end
endmodule

You do not drive seg[0]. An output port is implicitly declared as type wire, and wire types default to z when undriven. To get rid of the z, since your testbench expects bit 0 to be 0, you could add this line inside your dec_7seg module:
assign seg[0]= 0;

Related

MSP432 GPIO Output Voltage Too Low

I'm an undergraduate student. We have an assignment to use Energia and the MSP432-P401R microcontroller to create an OR gate and AND gate IC tester.
You place the IC in the tester circuit and the tester will first determine if the IC is an AND or OR IC. Then, it will indicate which gates on the chip are functioning properly.
My issue is regrading the logical HIGH output voltage of the MSP432. My input signals, a and b, are passed to the IC gates. When my "a" signal is logical HIGH, its voltage is 2.8V. When my "b" signal is logical HIGH, its voltage is 0.7V. This 0.7V is too low for my ICs to register as a HIGH input. I am reading the voltage directly from the MSP432 pin.
I have tried using different GPIO pins and resetting the board. However, the "b" signal is still 0.7V.
My code for setting up "a" and "b" are similar, so I'm not sure why they are outputting different voltages.
My code:
//IC TEST
//Gate inputs pin assignment
int APin=11;
int BPin=8; //other pins tried: 12, 18, 5
//Gate output read pins
int output1Pin=38;
int output2Pin=37;
int output3Pin=36;
int output4Pin=35;
//LED pins
int gate1LEDPin=31;
int gate2LEDPin=32;
int gate3LEDPin=33;
int gate4LEDPin=34; //Gate functioning indicators
int orLEDPin = 40;
int andLEDPin= 39; //Logic of IC indicators
//Truth Table Arrays with inputs a and b
int aValue[]={0, 0, 1, 1};
int bValue[]={0, 1, 0, 1};
int orTrue[]={0, 1, 1, 1};
int andTrue[]={0, 0, 0, 1};
//Function Declaration
int workTest(int,int); //checks if gate is working
//by comparing gate output to
//expected truth result
int logicTest(); //Returns 0 for OR IC, 1 for AND IC
//Progrram variables
int i; //for loop counter
int gate1Result;
int gate2Result;
int gate3Result;
int gate4Result; //Stores number of times gate outputs
//correct value for each ab input
int logicRead; //0 is OR gate. 1 is AND gate
//--------------SETUP-----------------
void setup()
{
Serial.begin(9600);
pinMode(APin, OUTPUT);
pinMode(BPin, OUTPUT);
pinMode(output1Pin, INPUT);
pinMode(output2Pin, INPUT);
pinMode(output3Pin, INPUT);
pinMode(output4Pin, INPUT); //gate output reads
pinMode(orLEDPin, OUTPUT);
pinMode(andLEDPin, OUTPUT);
pinMode(gate1LEDPin, OUTPUT);
pinMode(gate2LEDPin, OUTPUT);
pinMode(gate3LEDPin, OUTPUT);
pinMode(gate4LEDPin, OUTPUT);
}
//END SETUP
//-----------------LOOP---------------------
void loop()
{
gate1Result = 0;
gate2Result = 0;
gate3Result = 0;
gate4Result = 0;
//test IC for its logic function
logicRead = logicTest(); //logicTest returns 0 for OR, 1 for AND
if(logicRead == 9) //logic of IC cannot be determined
{
Serial.println("Try again");
}
if(logicRead != 9)
{
for(i=0; i<4; i++)
{
digitalWrite(APin, aValue[i]); //Load ith value of aValue array
digitalWrite(BPin, bValue[i]); //Load ith value of bValue array
Serial.print("AB = ");
Serial.print(aValue[i]);
Serial.println(bValue[i]);
delay(4000); //Stabilize input signals
if(logicRead == 0) //OR Testing
{
Serial.print("OR Output should be ");
Serial.println(orTrue[i]);
Serial.print("Gate 1: ");
gate1Result = gate1Result + workTest(orTrue[i], output1Pin);
Serial.print("Gate 2: ");
gate2Result = gate2Result + workTest(orTrue[i], output2Pin);
Serial.print("Gate 3: ");
gate3Result = gate3Result + workTest(orTrue[i], output3Pin);
Serial.print("Gate 4: ");
gate4Result = gate4Result + workTest(orTrue[i], output4Pin);
}
if(logicRead == 1) //AND Testing
{
Serial.print("AND Output should be ");
Serial.println(andTrue[i]);
Serial.print("Gate 1: ");
gate1Result = gate1Result + workTest(andTrue[i], output1Pin);
Serial.print("Gate 2: ");
gate2Result = gate2Result + workTest(andTrue[i], output2Pin);
Serial.print("Gate 3: ");
gate3Result = gate3Result + workTest(andTrue[i], output3Pin);
Serial.print("Gate 4: ");
gate4Result = gate4Result + workTest(andTrue[i], output4Pin);
}
}
//Write gate 1 LED
if(gate1Result == 4)
{
digitalWrite(gate1LEDPin, HIGH);
Serial.println("Gate 1 works");
}
else
{
digitalWrite(gate1LEDPin, LOW);
Serial.println("Gate 1 FAIL");
}
//Write gate 2 LED
if(gate2Result == 4)
{
digitalWrite(gate2LEDPin, HIGH);
Serial.println("Gate 2 works");
}
else
{
digitalWrite(gate2LEDPin, LOW);
Serial.println("Gate 2 FAIL");
}
//Write gate 3 LED
if(gate3Result == 4)
{
digitalWrite(gate3LEDPin, HIGH);
Serial.println("Gate 3 works");
}
else
{
digitalWrite(gate3LEDPin, LOW);
Serial.println("Gate 3 FAIL");
}
//Write gate 4 LED
if(gate4Result == 4)
{
digitalWrite(gate4LEDPin, HIGH);
Serial.println("Gate 4 works");
}
else
{
digitalWrite(gate4LEDPin, LOW);
Serial.println("Gate 4 FAIL");
}
}
Serial.println();
Serial.println();
delay(10000); //Wait 10 sec before running code again
}//End void loop
//-----------Function Definitions-----------------------
//This function tests if all gates of the IC are working
//Returns 1 if gate is functioning
//Returns 0 if gate is not functioning
int workTest(int truth, int gateNum)
{
int gateRead = 0; //Updates for each gate output
int result = 0; //TotResult is 1 for all gates pass, 0 for any gate fail
gateRead= digitalRead(gateNum);
if(gateRead == truth)
{
result = 1;
}
else
{
result = 0;
}
Serial.println(gateRead);
return result; //1 for pass, 0 for gate fails
}
//This function tests the logic of the IC
//Returns 1 if IC is an AND gate
//Returns 0 if IC is an OR gate
//Allows for 1 broken gate
//If more than 1 gate broken, logic cannot
//be determined
int logicTest()
{
int aVal[]={0, 1};
int bVal[]={1, 0};
int outputCount = 0;
int logicGate = 0;
int c;
int gate1;
int gate2;
int gate3;
int gate4;
for(c=0; c<2; c++) //only testing ab = 01 and ab = 10
{
digitalWrite(APin, aVal[c]);
digitalWrite(BPin, bVal[c]);
delay(1000); //Stabilize input delay
gate1=digitalRead(output1Pin);
gate2=digitalRead(output2Pin);
gate3=digitalRead(output3Pin);
gate4=digitalRead(output4Pin);
outputCount = outputCount + gate1 + gate2 + gate3 + gate4;
}
if(outputCount < 3) //IC is likely AND gate w/ 1 broken gate
{
logicGate = 1;
Serial.println("IC is an AND gate");
digitalWrite(andLEDPin, HIGH);
digitalWrite(orLEDPin, LOW);
}
else if(outputCount > 5 && outputCount < 9) //IC is likely OR gate w/ 1 broken gate
{
logicGate = 0;
Serial.println("IC is an OR gate");
digitalWrite(andLEDPin, LOW);
digitalWrite(orLEDPin, HIGH);
}
else
{
logicGate = 9;
digitalWrite(andLEDPin, LOW);
digitalWrite(orLEDPin, LOW);
Serial.println("Logic of IC could not be determined");
Serial.println("IC could be broken. Make sure IC is connected properly.");
Serial.println();
}
return logicGate;
}
Here is the circuit setup:

How to send Data over Bluetooth Module HC-05 using Arduino?

/*
== MASTER CODE ==
*/
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ledPin 9
int state = 0;
int Vry = 0;
int Vrx = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop() {
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
// Controlling the LED
/*if (state == '1') {
digitalWrite(ledPin, HIGH); // LED ON
state = 0;
}
else if (state == '0') {
digitalWrite(ledPin, LOW); // LED ON
state = 0;
}
*/
// Reading the potentiometer
//Vry = analogRead(A0);
/*
Vrx = analogRead(A1);
int VrxMapped = map(Vrx, 0, 1023, 0, 255);
//int Vry_mapped = map(Vrx, 0, 1023, 0, 255);
//int Vrx_mapped = map(Vry, 0, 1023, 0, 255);
Serial.print("Vrx");
Serial.println(VrxMapped);
//Serial.print("Vry");
//Serial.println(Vry);
*/
Vrx = analogRead(A1);
BTSerial.write(Vrx);
Serial.print("Vrx: ");
Serial.println(Vrx);
//BTSerial.write(Vry);
delay(2000);
}
and the slave code is as follows,
/*
== SLAVE CODE ==
*/
#include <SoftwareSerial.h>
#define button 8
SoftwareSerial BTSerial(5, 3); // RX | TX
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
int enB = 11;
int in3 = 7;
int in4 = 6;
int jx = A0;
int jy = A1;
int mx = 0; //right motor
int my = 0; //left motor
int state = 0;
int i = 0;
int buttonState = 0;
int ledPin = 13;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
BTSerial.begin(38400); // HC-05 default speed
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
//my = analogRead(BTSerial.read());
// mx = analogRead(BTSerial.read());
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
int state = BTSerial.read(); // Reads the data from the serial port
//Serial.print('y');
//Serial.println(my+100,DEC);
Serial.print('x');
Serial.println(state);
}
if (i == 2){
i=0;
}
/*int mapx = map(mx,0,1023,0,255);
int mapy = map(my,0,1023,0,255);
if (mapx>127)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA,mapx);
}
else
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA,127-mapx);
}
if (mapy>127)
{
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB,mapy);
}
else
{
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB,127-mapy);
}
delay(1000);
*/
/*Serial.println(state);
if(BTSerial.available() > 0){ // Checks whether data is comming from the serial port
state = BTSerial.read(); // Reads the data from the serial port
}
if (state > 120){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000);
}
else{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
}
// Controlling the servo motor
// Reading the button
buttonState = digitalRead(button);
if (buttonState == HIGH) {
BTSerial.write('1'); // Sends '1' to the master to turn on LED
}
else {
BTSerial.write('0');
}
*/
delay(2000);
}
The problem here is following,
if I send 1 from master I get 130 at slave end, I have no idea how Serial communication works and how data can be received over bluetooth devices!
The serial of bluetooth in read function returns character.

Select in loop - work all the time - linux

I got next question about select:
How to make select in loop ?
I try to do like that:
struct timeval timeout;
int sel;
size_t rozmiar = sizeof(pid_t);
char buf[rozmiar];
int i;
FD_ZERO(&set);
for(i = 0; i< val; i++)
{ FD_SET(fd[i][0], &set); // val -> N pipe2
}
timeout.tv_sec = 2;
timeout.tv_usec = 0;
while(1)
{
sel = select(val+1,&set,NULL,NULL,&timeout);
if(sel < 0)
perror("select");
else if(sel == 0)
printf("No communicate \n");
else{
for(i = 0; i < val; i++)
{
if(FD_ISSET(fd[i][0],&set))
{
while(read(fd[i][0],&buf,rozmiar) > 0)
write(1,&buf,rozmiar);
} // check if exist and write to stdout
}
} // end SELECT
timeout.tv_sec = 2;
timeout.tv_usec = 0;
}
But there all the time show: ,, no communicate". Is it the correct way to create select which work all the time? I am not sure so I prefer to ask. I try to find information in books but with no lucky.
The set is changed by select, you need to refill it each time

How to use TSL2301 and make clock delay in usart

I am working on a line scan ccd sensor named TSL2301 .I want to read pixels by USART of stm32f103 but always i just could receive 0xFF , did anybody work with this sensor to help me?
I used Usart synchronous mode of STM32f10x , I sent some order to sensor by Usart and want to make 8 clock delay after each order . how can i do it?
int count=0;
int i=0;
uint8_t data[102]={0};
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 2200000;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTy, &USART_InitStructure);
/* Configure the USARTy */
USART_Init(USARTy, &USART_InitStructure);
/* Enable the USARTy */
USART_Cmd(USARTy, ENABLE);
while(1)
{
while(count < 3)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count=0;
USART_SendData(USARTy,0x1b); //RESET command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
USART_SendData(USARTy,0x1b);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
USART_SendData(USARTy,0x1b);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
USART_SendData(USARTy,0x08); //StartInt Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
while(count < 20 )//DelayIntegrationTime() ;
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;
USART_SendData(USARTy,0x10); //SampleInt Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
USART_SendData(USARTy,0x02); //ReadPixel Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
while(count < 2)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;
USART_Cmd(USARTy, DISABLE);
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_Init(USARTy, &USART_InitStructure);
USART_Cmd(USARTy, ENABLE);
/*read pixels*/
for (i = 0; i < 102; i++)
{
while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET);
data[i] = USART_ReceiveData(USARTy);
while(count < 1)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;
}
USART_Cmd(USARTy, DISABLE);
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_Init(USARTy, &USART_InitStructure);
USART_Cmd(USARTy, ENABLE);
}
There is different USART timing diagrams in STM32F103 reference manual and TAOS datasheet.
I think they will not work together in USART mode.
As an Idea, you can try to use UART instead of USART. And clock the device using MCO or other clock source. I think it should work.
It this case all delays you can do with timer.
PS: But there is one more problem you should solve, is to synchronize output of UART with clock source. I need to read stm32 manual more carefuly to tell you how to do it. I'll do it later

not a valid l-value - verilog compiler error

module fronter ( arc, length, clinic ) ;
input [7:0] arc;
output reg [7:0] length ;
input [1:0] clinic;
input en0, en1, en2, en3; // 11
// clock generator is here
g_cal A( en0) ;
g_cal B( en1) ;
g_cal C( en2) ;
g_cal D( en3) ;
always #( negedge arc, posedge clk )
case ( clinic )
2'b00 : { en3, en2, en1, en0 } = 4'b0001; // 23
2'b01 : { en3, en2, en1, en0 } = 4'b0010; // 24
2'b10 : { en3, en2, en1, en0 } = 4'b0100; // 25
2'b11 : { en3, en2, en1, en0 } = 4'b1000; // 26
default : { en3, en2, en1, en0 } = 4'bxxxx; // 27
endcase
// I am trying to change value of en to call corresponding intance with that
//corresponding en value
endmodule
module g_cal ( en ) ;
input en ;
// some other jobs, calling another instances after making some job
endmodule
when I compile, compiler gives me ;
verilog.v:23: error: en0 is not a valid l-value in Numerator.
verilog.v:11: : en0 is declared here as wire.
verilog.v:24: error: en1 is not a valid l-value in Numerator.
verilog.v:11: : en1 is declared here as wire.
verilog.v:25: error: en2 is not a valid l-value in Numerator.
verilog.v:11: : en2 is declared here as wire.
verilog.v:26: error: en3 is not a valid l-value in Numerator.
verilog.v:11: : en3 is declared here as wire.
verilog.v:27: error: en3 is not a valid l-value in Numerator.
verilog.v:11: : en3 is declared here as wire.
segmentation fault
How can I fix it ?
Why it gives error?
EDIT:
I have solved problem as ;
// I erased that line "input en0, en1, en2, en3; // 11"
// clock generator is here
g_cal A( 1'b0) ;
g_cal B( 1'b0) ;
g_cal C( 1'b0) ;
g_cal D( 1'b0) ;
always #( negedge arc, posedge clk )
/* erasing all those line
case ( clinic )
2'b00 : { en3, en2, en1, en0 } = 4'b0001; // 23
2'b01 : { en3, en2, en1, en0 } = 4'b0010; // 24
2'b10 : { en3, en2, en1, en0 } = 4'b0100; // 25
2'b11 : { en3, en2, en1, en0 } = 4'b1000; // 26
default : { en3, en2, en1, en0 } = 4'bxxxx; // 27
endcase
I will use if and else structure, and calling corresponding instance with 1'b1*/
// I am trying to change value of en to call corresponding intance with that
//corresponding en value
endmodule
You're trying assign to an input (which is bad). Change input en0, en1, en2, en3; to output reg en0, en1, en2, en3;. The reg is necessary since you are assigning to that variable within a procedural block (ie, an always or initial). The "not a valid l-value" message is trying to tell you this.
Also, I'm assuming that the 11, 23, 24, etc are stray line numbers from a copy-paste...
Problem has solved when I write ;
reg en0, en1, en2, en3 ;
initial begin
en0 <= 1'b0; en1 <= 1'b0;
en2 <= 1'b0; en3 <= 1'b0;
end
g_cal A( en0) ;
g_cal B( en1) ;
g_cal C( en2) ;
g_cal D( en3) ;
#Marty have emphasized important thing "The reg is necessary since you are assigning to that variable within a procedural block (ie, an always or initial)."

Resources