Serialize bits from input/output pin with VHDL

Status
Not open for further replies.

Dijskstra

Newbie level 5
Joined
Jun 28, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
93
The code below reads 40 bits of data sent in serial from a DHT-11 temperature/humidity sensor and
stores the data in a 5 byte array of RAM.

The code is:

Code:
// Return values:

// DHTLIB_OK              0 => OK
// DHTLIB_ERROR_CHECKSUM -1 => Checksum error
// DHTLIB_ERROR_TIMEOUT  -2 => Timeout


int read(int pin)
{
	
	uint8_t bits[5]; // BUFFER TO RECEIVE
	uint8_t cnt = 7; // bit counter 0..7
	uint8_t idx = 0; // buffer index

	// EMPTY BUFFER
	for (int i=0; i<5; i++) bits[i] = 0;

	// REQUEST SAMPLE
	pinMode(pin, OUTPUT);    // set pin for output
	digitalWrite(pin, LOW);  // write '0'
	delay(18);               // delay 18ms
	digitalWrite(pin, HIGH); // write '1'
	delayMicroseconds(40);   // delay 40 microseconds

	pinMode(pin, INPUT);     // set pin for input

	// ACKNOWLEDGE or TIMEOUT
	unsigned int loopCnt = 10000; 
	while(digitalRead(pin) == LOW)
		if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

	loopCnt = 10000;
	while(digitalRead(pin) == HIGH)
		if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

	// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
	for (int i=0; i<40; i++)
	{
		loopCnt = 10000;
		while(digitalRead(pin) == LOW)
			if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

		unsigned long t = micros();  // this function returns the timestamp in microseconds

		loopCnt = 10000;
		while(digitalRead(pin) == HIGH)
			if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

		if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
		if (cnt == 0)   // next byte?
		{
			cnt = 7;    // restart at MSB
			idx++;      // next byte!
		}
		else cnt--;
	}

	// WRITE TO RIGHT VARS
              // as bits[1] and bits[3] are allways zero they are omitted in formulas.
	humidity    = bits[0]; 
	temperature = bits[2]; 

	uint8_t sum = bits[0] + bits[2];  

	if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
	return DHTLIB_OK;
}

I'm beginning to FPGA developing, but I can't catch some concepts of VHDL, such as poling
a pin of a FPGA.
For testing, I'm using a Spartan 3E with a clock of 50 MHz.

The C function that returns an int (Ok, checksum error, or timeout error).

The equivalente VHDL code doesn't do that, of course, but instead, stores the int in a variable.


Can you please, convert the code above to VHDL?


Thank you.



Anders Dijskstra
 

Then post the code you tried to make work and we can tell you what is wrong with it. Don't expect anyone to just do it for you. Most of us disapprove of that type of laziness.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…