# Rs232 MAXSONAR to de2

module testc (CLOCK_50, KEY, UART_RXD, LEDR);

parameter IDLE = 1'b0;
parameter RECEIVING = 1'b1;

input CLOCK_50, UART_RXD;
input [0:0] KEY;
output [7:0] LEDR;
wire [7:0] RxData;

reg shift;
reg [1:0] state, nextstate;
reg [3:0] bitcounter;
reg [3:0] samplecounter;
reg [12:0] counter;
reg [10:0] rxshiftreg;
reg clear_bitcounter, inc_bitcounter, inc_samplecounter, clear_samplecounter;

wire reset;

assign reset = KEY[0];
assign RxData = rxshiftreg[8:1];
assign LEDR = RxData;

always @ (posedge CLOCK_50)
begin
if (!reset)
begin
state <= IDLE;
bitcounter <= 0; counter <= 0;
samplecounter <= 0;
end
else
begin
counter <= counter+1;
if (counter>=1736)
begin
counter<=0;
state<=nextstate;
if (shift)
rxshiftreg<={UART_RXD, rxshiftreg[10:1]};
if (clear_samplecounter)
samplecounter<=0;
else if (inc_samplecounter)
samplecounter<=samplecounter+1;
if (clear_bitcounter)
bitcounter <=0;
if (inc_bitcounter)
bitcounter<=bitcounter+1;
end
end
end

always @ (state or UART_RXD or bitcounter or samplecounter)
begin
shift = 0;
clear_samplecounter = 0;
inc_samplecounter = 0;
clear_bitcounter = 0;
inc_bitcounter = 0;

case (state)
IDLE:begin
if (UART_RXD)
nextstate = IDLE;
else begin
nextstate = RECEIVING;
clear_bitcounter = 1;
clear_samplecounter = 1;
end
end
RECEIVING:begin
if (samplecounter == 1) shift = 1;
if (samplecounter == 3) begin
if (bitcounter == 10) begin
nextstate = IDLE;
end
inc_bitcounter = 1;
clear_samplecounter = 1;
end
else inc_samplecounter = 1;
end
endcase
end
endmodule


This is the verilog module i am using to get my rxdata ... When i connect the tx pin of the sensor to the rx pin of the de2 the leds just keep blinking without pattern and also the rxd led on the de2 keeps blinking which means it is getting the data. I give a outside 5v source to the sensor and ground the 5th pin of the rs232

If anyone can help it qould be great

-
If you've connect true RS232 voltages directly to your FPGA (as your mention of pin 5 suggests you may have), you will probably kill it. And it won't work anyway, since the polarity of RS232 is opposite that of logic-level decoders. You need a level shifter circuit which will include an inverter. You may also want to do your debugging using a PC terminal program as the data source instead of the sensor. Do your have you clock rates and baud rates worked out? – Chris Stratton Nov 17 '11 at 17:53
I don't think the sensor is true RS232, more like 0-to-VCC RS232 (see i.stack.imgur.com/YnVGu.jpg), so 5V in this. But still, the I/O pins on Cyclone II FPGA probably aren't 5V tolerant. The DE2 does have 47-ohm resistors in-line between the expansion headers and the FPGA I/O pins, so that might be helping somewhat. – Craig Nov 17 '11 at 18:22
i have connected the sensors to an outside 5v voltage i just connect the tx of the sensor to the rx of the fpga. The gnd actually does not matter it gives me the same output anyways. I am still not sure if they are compatible. If there is anyother way i can achieve this please let me know – user591124 Nov 17 '11 at 19:15
it is running on 9600 baud, 8bit no parity and 1 stop bit the sensor gives .. R (Ascii) and a binary for 0-255 – user591124 Nov 17 '11 at 19:21

Based on the linked data sheet in comments, at a minimum, you need to invert the signal. That you can do in the FPGA code, electrical protection beyond the 47 ohm resistors apparently already present would have to be external. At 9600 baud a resistive voltage divider will probably work.

"If standard RS232 voltage level is desired, invert, and connect an RS232 converter such as a MAX232"

Your FPGA code is almost certainly designed for logic-level serial communication like the MAX232 accepts on its logic side, which is inverted in sense from the "lower voltage RS232" output of this sensor and the true spec-compliant RS232 it hopes to be accepted in place of.

It may still not work even after you have the logic sense sorted out. As I said more briefly in my comment, you have to be sure that the FPGA is generating the right 8x baud or 16x or whatever sampling clock (if you got this from a source that also publishes a transmitter, building that too and setting it to repeat some character can be a simple check). And you may want to do your debugging with a pc terminal program instead as the source, so that you can send one known value at a time. As you get closer to it working you should at least start to see some sort of pattern between what key you push and what output you get, thinking about those numbers in binary may lead to identifying any remaining problems.

(Incidentally, ordinarily a microcontrollor would output serial data with the customary logic level polarity, and it would require an external inverter to get to the same sense as RS232 in order to try the undervoltage trick they have employed. However, they don't seem to be using a hardware UART but instead bit-banging the serial output from software, at which point it's easy for them to produce it with whichever polarity they desire. If problems persist, it may be worth keeping in mind that you basically have do-it-yourself serial engines on both ends of the link rather than more thoroughly validated UARTs implemented at silicon level. One might as a result expect a slightly higher probability of having minor implementation problems at each end which only become a real issue when they are connected to each other).

-
My sampling is 3x and i think the de2 fpga already has a max232 converter included – user591124 Nov 17 '11 at 19:45
3x sampling is probably not sufficient - 8x or 16x is traditional. If this is something you wrote from scratch you might want to try one known to work, see for example fpga4fun.com Did you connect to the DE2 through a 9pin "serial port" type connector present on the board? If so then yes you probably have a level shifter and corresponding inverter already there. – Chris Stratton Nov 17 '11 at 19:47
I connected the tx pin of the sensor to the 9pin serial port (3rd pin) rx of the de2 – user591124 Nov 17 '11 at 22:53