NVM and DAC

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

NVM and DAC

Post by Ondra »

Good day all. I am trying to use the Flowcode SPI interface component and need some assistance. The microcontroller has SPI connections SDO,SDI, SCK, SS. The flowcode componet List NVM and DAC enable pins. I'm not using the eblocks so what happens to the NVM and DAC enable pins? Thanks in advance for your assistance.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

The NVM and DAC enable pins are essentially the same as the SS for your device. If you are not using the functions for the NVM or DAC then you will have no problem. If you are using the NVM or DAC macros then you will have to make sure that the NVM or DAC enable pins correspond to the SS pin of your SPI device.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Ben. I was thinking that the SPI componet would automatly impement the SS pin. i.e I would just connect the SS pins from both units along with the SCK, SDI, SDO and just start sending data with the SPI component handling all the protocol stuff.
This is the instructions for using a device that will convert SPI to USB by a company vinculum. Could you please have a look and break this down in Flowcode language for me. Maybe even give an example working with the instructions below of sending "Hello world" using the SPI component. Thanks for all your help.

Vinculum SPI instructions.

From Start - SPI CS must be held high for the entire read cycle, and must taken low at least one clock period after the read is completed. The first bit on Data In R/W bit - inputting a ‘1’ here allows data to be from the chip. The next bit is the address bit, ADD, which is used to indicate whether the data register (‘0’) or the status register (‘1’) is read from. During the read cycle a byte of data will start being output on Data Out on the next clock cycle address bit, MSB first. After has been clocked out of chip, status should be checked to see if the data read is new data. A ‘0’ level here on SPI Data means that the data read is new data. 1’ indicates that the data read is old data, and the read cycle should repeated to get new data. Remember that CS must held low for at least one clock period before taken high again to continue with the next read or write cycle.

From Start - SPI CS must be held high for the entire write cycle, and must taken low at least one clock period the write completed. The bit SPI In R/W bit - inputting ‘0’ here allows be written to the chip. The next bit is the address bit, ADD, which is used to indicate whether the data register (‘0’) or the status register (‘1’) is written to. During the SPI write cycle a byte of data can be input to Data In on the next clock cycle after the address bit, MSB first. After data has been clocked in to chip, status of Data Out should be checked to see if the data read was accepted. A ‘0’ level on Data Out means that the data write was accepted. 1’ indicates that the internal buffer is full, and the write should repeated. Remember that CS must held low for at least one clock period before being taken high again to continue with the next read or write cycle.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

I have had a look at the Vinculum device datasheet and it seems that the SPI interface is not 8 bit and as such is not the standard SPI communications bus supported by Flowcode. It would probably be easier for you to bit bang out the SPI rather then using the PIC 8-bit hardware SPI module.

To do this simply create a macro called toggle clock. This will output a 1 to an I/O pin, wait a little while and then output a 0.

Then set or clear bits as needed to establish the comms with a call to the toggle clock macro after every bit. Data can be read out of or into a variable by bit shifting the variables one bit at a time.

If you need further help let me know.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Here is a simple write example that will send out the character H to the SPI module.

You should be able to see what is going on using the SPI_Write macro and toggle_clock macro.

SCK = PortB 0
SDO = PortB 1
SDI = PortB 2
CS = PortB 3
Attachments
BitBang SPI.fcf
(5.5 KiB) Downloaded 740 times

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Ben.
I spent some time over the weekend reading what I could find on the topic of bit banging. I think I understand what's going on but I would like to learn more. I couldn't get the device working. But that may be a device issue. I will talk with the FTDI tech people today. Back to the bit banging. The reason I ended up with this SPI to USB device is that I needed 2 com port for the pic and Flowcode only supports one RS232 component. After looking through the forum I discovered bit banging can be used to simulate RS232 communication. My question is: -
1) Where can I get some examples of using bit banging to simulate RS232. Flowcode style.
2) Do you have a few examples of RS232 transmitting and receiving using the bit banging approach.
3) Using the SPI example you gave me could you give another example sending and receiving the words "Hello World". I'm sure I can play around with the code changing it to work with what I'm doing. I feel like I'm asking a lot, but I do hope you can find the time to get me straight on this bit banging approach.

Thanks for your help.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Here is an example of the bit-banging algorythm for sending RS232 data.
Receiving RS232 data would be very similar but you would read the pin rather then send out a bit.

create 2 byte variables = data and maskdata

calc - load data into byte variable
output - send start bit - 0
delay - delay for 1/baud rate
loop - loop 8 times
{
calc - maskdata = data & 0x01

output - send maskdata

calc - data = data >> 1

delay - delay for 1/baud rate
}

output - send stop bit - 1
delay - delay for 1/baud rate

To get delays shorter then 1ms you can use a C code icon with the following line of code.

delay_10us(1); //Delays for multiples of 10 micro seconds depending on the value in the brackets

or

delay_us(1); //Delays for multiples of micro seconds depending on the value in the brackets (will not work for some crystal frequencies)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

I have created an example bitbanging RS232 program here.

http://www.matrixmultimedia.com/softwar ... BRS232.fcf

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Benj.
Two things.
1)In both example you show outputing a single letter. If I wanted to output a word or phrase would it be as simple as
Data = "whatever word, phrase or number" or would there be other manippulations needed.
2) What is masking off a single bit. What does "maskdata = data & 0x01" do?

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

If you wanted to send more then one character then you would have to create an array or string variable and then step through the variable in a loop pulling out a byte of data and sending it then moving to the next byte. The string functions can help with doing this.

maskdata = data & 0x01 masks off a single bit of the data held in variable data.

The & is used as a logical AND.

so data xxxxxxxx and 00000001 is equal to 0000000x - Where x can be a 0 or a 1

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Benj. I set the code to transmitting at 2400bps and it works fine. To take it further here's what I want to do. I want to use the RS232 component to receive data that I send from Hyper terminal and then bit bang out the data using the Bit bang RS232 Code thats working. I took your advice and tried to create an array and then using a variable, but as much as I tried to make something happen I just couldn't get it working. It's easier for me to grasp something when I see an example. Do you have anything you can send me to further help me along, and if you have any RS232 receiving/sending .fcf files could you please send them to.
I'll work with them over the weekend. Thanks again for all your help.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Basically all you have to do is use a RS232 hardware macro to receive a byte from the RS232 port. Then you would load this byte into the transmit variable and run the transmit macro as shown in the above example. Then repeat.

You may have to play around with the delays in the transmit macro to acheive the baud rate you require.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thats what I thought, and I did try it. Maybe I'm not understanding how to use it. In the RS232 Receive macro I put for parameters = 1 and for return
variable = Data. Is that right? If it is, it didn't work. I even tried to putting the macro call in a loop. No luck. I tried converting the byte data into a string creating an index var as part of loop calling the bit bang macro still not working. I sure it simple but I just don't see it. If you ever decide to write a book called flowcode for dummies Ill take the first one off the press.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Ok here's more detail.
In a loop I setup an RS232 component to receive into a variable named dataout.
I put the bit bang macro in the loop with the RS232 Receive macro with dataout as it parameter.
I connect hyper term to both in and out DB9 conn and what happens is that the key press are passed through
but the cursor does not stop scrolling across the page.
2) I also tried to index the data but the same thing happens.
? Is there a way to call the bit bang macro only when data is received or var dataout has a value.
What I want to have happen are.... as I receive characters using the RS232 component they are bit banged out.


Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Try a parameter of 200 for the RS232 receive. This will allow the macro to wait longer for an incoming message before continuing.

If the data is invalid then it will equal 255. You can therefore do a decision to check if the incoming data is less then 255. If it is then you know it is valid data.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Tanks Benj it's working. I'm not finishes though. I'm now trying to reverse the data flow. I want to receive on the bit bang port and transmit through the
RS232 component. Here's what I have. I tried to follow the instructions you gave for bit bang receive. I changed it up many time as it made sense to me with no good results.

Main macro

Loop - while 1
Input - with var= starttbit
condition - StartBit = 0 then Call BB_RS232
else - Data_IN = 255
condition - Data_IN < 255 then macro call RS232(0)::SendRS232char(Data_IN)
loop - end

BB_RS232 Macro

cal - count = 0
Loop - count<10
input - port B4 var = Data_IN
cal - Data_IN = Data_IN << 1
C code delay - delay_10us(41);
cal - count = count + 1
loop - end

I no it's wrong. What else am I missing?


Thanks for all your help.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Benj it's working. I'm not finishes though. I'm now trying to reverse the data flow. I want to receive on the bit bang port and transmit through the
RS232 component. Here's what I have. I tried to follow the instructions you gave for bit bang receive. I changed it up many time as it made sense to me with no good results.

Main macro

Loop - while 1
Input - with var= starttbit
condition - StartBit = 0 then Call BB_RS232
else - Data_IN = 255
condition - Data_IN < 255 then macro call RS232(0)::SendRS232char(Data_IN)
loop - end

BB_RS232 Macro

cal - count = 0
Loop - count<10
input - port B4 var = Data_IN
cal - Data_IN = Data_IN << 1
C code delay - delay_10us(41);
cal - count = count + 1
loop - end

I no it's wrong. What else am I missing?


Thanks for all your help.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Hello, is anybody out there.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Well for a start you are looping 10 times. In each loop you are shifting bits of data but there is only 8 bits of data available in a byte.

Do a little at a time. Eg get the bit bang reception sussed first.

Here is a basic example.

Read Input Pin
While - Input pin = high //Wait for start bit
{
Read Input Pin
}

delay for a bit length and a half

count = 0

While - count < 8 //Reception loop
{
Read Input Pin

Append inoput pin data to variable

rotate variable

delay for a bit length

Incrment count
}

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Hi Benj thanks again for your help and pleaseee don't get tired of my slowness. I'm an electronic hobbiest. I'm an electrician by trade and new to microcontrollers. I've been at this bit bang receive since last Friday. Every day for at least 2hrs a day reading everything I could find on the forum and the www. I'v tried looping 8 time 9 time 10 times. I know its only eight bits but how do I handle the stop bit and start bit. I'm sure someone will want to try this in the future and it would I'm sure take a lot less time for MMM to upload a Flowcode example, one that works so that in the future you could just point someone to it. I'll pay for your time if I have to. I know what I am tring to do won't take you long. But I don't want to try any more. Pleaseeeee could give me or sell me a working .fcf bit bang RS232 receive file. Pleaseeeeeeeeeee. It took me a week just to get the bit bang RS232 transmit working and that all ended when you took the time to give me the .fcf file. I appreciate all the help you have and are giving me. It's just that somethings just take longer for me to comprehend. And with this bit banging and bit shifting I just don't get it. I love the Product and it has open to me a lot of possibilities to do things with electronic components I just couldn't do before. This bit banging all came about because I wanted to have two RS232 comports connected to my project and flowcode doesn't support more that one, otherwise I would have never at this point gone down this road. So if you can or can't help to this end please let me know. So I can end this topic. Thanks either way.


Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

Your probably right that this will just come around again in a few months.

So I have made a bit banged input example for you now.

It is available with the receive example in the Flowcode examples section.

http://www.matrixmultimedia.com/Flowcode_Examples.php

Let me know if you have any problems.

You will probably have to edit the delays to get the timings correct. Remember to always have a bit and a half delay after detecting the start bit. This skips the start bit and then gets half way into the next incoming bit so the input sample should be in a stable state.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thank you so much Oh Great wizard of the Flowcode realm. :lol:
I'll let you know how I get along.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Ok. Sorry I'm still on this topic, but here's the situation.
Using the RS232_RX.fcf example I set the bit delay
to delay_10us(41); that's to receive at 2400bps.
I set the delay and a half to delay_10us(62); in the decision "In < 255"
of the main macro I put an RS232 send macro call with the parameter
variable"In". I am using a PIC16F690 a 19660800 crystal, receiving on
port pin RB4.
What's happening is this. When I press a character on the keyboard
I get a different character coming through i.e
I press an f I get 3
I press an e I get 2
I press an s I get 9
I press an a I get 0
an the results are consistent. every press of the f key give a 3 result.
What am I missing?

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: NVM and DAC

Post by Benj »

Hello Ondra

The ascii code for an 'f' is 01100110 in binary. The ascii code for a '3' is 00110011 in binary.

As you can see the data coming in is correct but being read in slightly incorrectly.

The problem was an oversight on my part im afraid. The data is correct but is being shifted one too many times.

If you move the "Shift the return value" block up above the "Read Input Pin" block in the "Collect Byte" loop then your program should work correctly.

The program on the example page should also work correctly now.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: NVM and DAC

Post by Ondra »

Thanks Benj. I made the change and it works. Another issue though. I got he bit bang Tx and Rx working separately but they don' t work together. Here what I have.
In main I have:
loop while 1
Data=RS232(0)::receivRS232Char(200)
if Data<255
Call macro BB_Tx
end if
BB_Rx return val = In
if In<255
Call macro
RS232(0)::SendRS232Char(In)
end while

Any one of these calls will work when uses by itself but when put together only one works.
What I was hoping for, was: - Using 2 terminal emulator windows to monitor ports. I want to type in to window 1 and have the data print out in window 2 then type in window 2 and have the data print in the first window. The PIC is used in this case like a conduit. Data flows in and pass out in both direction between the bit banged serial and the flowcode RS232 component. What am I not understanding in this situation?

Ondra

Post Reply