C-code

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

C-code

Post by Dutch »

Hi, for a long time already im struggling to get this done. please any help or advice is deeply appreciated.

i found an C code on line I am free to use. The files where created for, and compiled by an old version of atmel studio. what i want is to have this code converted in an c code icon so i can use that data in a normal FC way . I see in the help file from the c icon there are changes needed. but to start i already have 3 files . how do i put that all in one icon? or is this not even possible?

Angelo.
Attachments
main.c
(21.06 KiB) Downloaded 297 times
j1850.h
(4.16 KiB) Downloaded 291 times
j1850.c
(6.35 KiB) Downloaded 276 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: C-code

Post by LeighM »

Generally, you can add “supplementary” code via the Build->Project Options dialog
All C function files would be included here…
code.jpg
code.jpg (50.9 KiB) Viewed 13656 times
Just the body of your “main” function would then be placed in a C icon in the Flowcode Main(flow)
I’ve tried this with your project code, but there are a lot of missing references so it might be best to try and understand what the C code is doing and then replicating that in Flowcode.

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

Hi Leigh, thank you for your answer!

Ah so there is where i need to put the different files!. ill certain look into that in FC7. but.. if an expert already says its missing all kind of references. i wouldn't mind putting all the time needed in finding and repairing all the links. but without the deeper knowledge im sure i will miss a lot.

For making it in flowcode itself that would be better of coarse!. i tried in my best way to make a FC code and il post it below. maybe the simplicity will make you smile but i did my best! i may approach this very wrong but im learning...

Its all hobby and fun! what i eventually want to decipher and show filtered data from on a display is from a single wire J1850 VPW communication protocol. It actually looks a little bit like the 'one wire interface' already present in flow code. simply said, the signal starts with an 200uS startbit(SOF) than followed by 7 x 8bit signals . the different headers contain Arbitration Data and a Cyclic-Redundancy-Check. i have some links with info.

Anyway. about the code il post below. its far from complete but in theory and simulation it works. in real.. not. i have put this in on an atmega328P just because these are easy.
for start its not detecting the number of loops for the 200uS correct. i think the loop timings might be too small or the calculation goes wrong. if i put an active output in the loop and an inactive on the end it switches perfect on-off . so 51 loops of 4uS makes a nice 204uS signal on the scope.. but no active decision. the loopcounter is very irregular so it never goes by the decision.

Angelo.
Attachments
J1850VPW-DISPLAY_test.fcfx
(33.09 KiB) Downloaded 288 times

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

Leigh, can you tell me if im even close with my FC i posted above?

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Hi Dutch,

I had a little play with this after your previous post..

Can I suggest that you investigate arrays - rather than saving to 8 individual variables. It will make your code much easier to follow (and shorter to enter too - always good)

I'd suggest using a property for the pins too - will make life easier if you rewire things!

I also changed the switch statements to a calculation.

I had a little tidy :
J1850VPW.fcfx
(17.63 KiB) Downloaded 301 times
- but like most people - I'm hamstrung by mot having the hardware to test here! (and hopefully this will work as well as your current code - ie probably not at all)

Hopefully we can be of some help though.

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

Hi Martin, I highly appreciate your help and input!

It took me evenings to get this working and its partly the fun. you might know the 'yesss' effect when you accomplish a piece of code and does what it needs to. i realize its not the best type of code but I learn from examples and what you made for me.

I have my unit back the weekend that sends the VPW signal. I also have the scope back so I can try again and see what is needed to have the processor at least determine its a 200uS signal and needs to proceed a other flow. il try to make a few pictures. last time I could not understand why it would turn an output very precise on and off but not count the number of loops and made a calculation. lets see.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Hi,

Yes, sorry i didn't mean to criticise - I'd hesitated to post when you first asked the question.
So, yes - I program because it is interesting and fun! Flow code is good because it has a friendly (honestly, we try) forum - and, speaking for myself, I enjoy chatting and thinking about programming here...

So, I posted because you asked the question again - and shorter code is easier to debug for other people. The techniques I mentioned will make your life easier in the long run too - so I do recommend them strongly.... There is always another way to do things - look back at an old program and you'll always think - why did I do that when .... is so much better!

Thinking more about the display - I think in the first instance, I'd try and read the SOF signal (200us) - only. Get that to work - turn on an led on success then move on to reading the data.....

I didn't manage to find any on eBay to try though.

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

fully appreciate all the help and idea's!. I don't see all the deeper things you did yet but I imagine its hard to re-write something without being able to test or knowing what it does. but im deciphering the steps!

I had my test stuff back and simplified the code back to the first decision . I could not see why it would not pass but I found it. I made a simple test prog 1uS on 1uSec off block signal and measured it with the scope. it turned out the duration is 1,26 uS for the on and for the off so there is an oscillator way off :?
I lowered the SOF number for the decision and all later decisions and it passed and runs tru the code.. but still then it does not 'decode' accurate at all .. im trying to think out a way to make a visible '1' and '0' separate on my scope to see if the long and short 0's and 1' are detected.
the picture I show have a pin switched on in all the loops and off there after. this shows it switches fine...

I know its possible to get a accurate data stream because the C code I posted above I have in an simple pic controller I did buy ready and tru a terminal program on the pc by an rs232 I see all the signals... maybe I still need to find someone to get that c code to work too.
Attachments
IMG_9116.jpg
IMG_9116.jpg (106.72 KiB) Viewed 13464 times
IMG_9115.jpg
IMG_9115.jpg (101.04 KiB) Viewed 13464 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

I took a quick look at using a timer component - and it's opened a bit of a can of worms...

So - in the first instance I try to pulse a pin a 200us intervals using a timer and a delay. (Note that this is effectively how the C code you posted measures the length of SOF pulses.)

- and there seems to be a lot of overhead in the component macros (they use FP math - eek) So a count waiting 200us (using a simple loop - see code below) - actually gives a 246us pulse (according to my oscilloscope)

Using a delay macro of 200us gives a 262us pulse..... This is quite a large error! - event allowing for loop / pin setting etc overheads.

timer_test.fcfx
(9.2 KiB) Downloaded 289 times
LabNation_Screenshot1.png
(189.07 KiB) Downloaded 3077 times
A bit more play - and using a timer component actually makes the delays more inaccurate - taking out the timer and the 200us delay clocks in at 202us which is more reasonable....

Martin

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: C-code

Post by Benj »

Hi Martin,

The GetCountMicroSeconds uses floating point maths and hence the problem in terms of stopping on a fixed delay.

Normally you would time how long something takes e.g. start the timer wait for something to happen, stop the timer and then collect the time elapsed and this will provide a highly accurate reading.

For what you need a different approach might be nessisary. I'll make available the GetRAWCount macro as this returns an integer value containing the number of instructions with no floating point scaling which should allow you to get much more accurate timings.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

- a bit more playing and I converted the C code (above) using Timer1.. - Little conversion was necessary -just added to supplementary code.

This generates a steady 202us (probably within the realms of crystal errors on Arduinos?) pulse - and can be used as a starting point for a conversion.
timer_test2.fcfx
(8.74 KiB) Downloaded 235 times
See supplementary code(project options)

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

Thanks Gentlemen!

Martin, il test the codes on my unit and scope to see what happens! interesting!

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

That works Martin. pretty close to the 200 now.

I looked for the supplementary code to see how I could to change it to a shorter time for the loop I had but I saw you prepared a few definitions with timings. not sure how you want to approach this. can we measure time length of a pulse in stead of counting loops?
Attachments
IMG_9158.jpg
IMG_9158.jpg (114.04 KiB) Viewed 13372 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Yes, we can use this to measure a pulse, we start the timer when the pin is 'set' and then record the time when it changes state again - then compare this with the low and high bound of the expected pulse length.

We could probably also use the GetRawCount or GetMicroSecs - from the timer component - calling it multiple times to generate the pulse didn't work - but just calling it to get the pulse length would probably be good - on reflection:-)

It's a job for tomorrow though!

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Ok - a simple 'proof of concept' test (after a very long day at work :-( )

A simple circuit - two arduino nanos - ground and 5v connected (I only found one USB lead :-( ) and D3 connected to D3 (signal)

One nano outputting 200us pulses (as per above but at a 1s interval) and one receiving - flashing the inbuilt LED and outputting some info to UART if the pulse is in range..

I measuring a 'high' pulse - and I guess the j1850 will use low - but it is easy to swap the required state.

It works! It outputs the range RX_SOF_MIN, the actual count and RX_SOF_MAX as:
Got SOF 2608 3211 3824
(So some leeway in timings here) + note the timings are in 'ticks'

I'd go some tidying - move timings etc to macros + needs timeouts as per C.

timer_test2.fcfx
(8.62 KiB) Downloaded 189 times
time_pulse.fcfx
(13.85 KiB) Downloaded 215 times
Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

Sounds very promising... Glad you want to help !

finding all about the signal is interesting. when I see it simplified the first bit (bites 1-8)holds the arbitration for sending, so not interesting for only reading the signals. the second byte(9-16) is the header i need telling me the sort of data. next two (17-32) is part of the header but not needed so far. byte 5 and 6 is the data I want to show on a display. needs some calculation but . the last byte is an CRC number im not fully sure of how it is put together but there is a calculation needed from all bytes combined and the outcome is fixed (have seen it somewhere. looking for it).. I cant decipher it from the c-code but its there too. but if we can get some stabil data streams it creates a good base for the next steps! stoked.

the signal stream after the SOF of 200uS is 56 bits long. (7 bytes of 8 bit). A passive logic 1 is sent as a 128uS low level, an active logic 1 is sent as a 64uS high. A passive logic 0 is sent as a 64uS low level, an active logic 0 is sent as a 128uS high. that way one transition of the signal provides a logic 0 or 1.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

I converted the CRC routine (probably should have left this as C - although it is only short - going for the easy targets first!)

I used a string as the message to CRC - and really should have used an array of bytes (although is string is just an array of bytes :-) ) - FC doesn't let you change the type back from string :? so to save some work I've left it as it is.... I'd use an array of bytes elsewhere though.
J1850_CRC.fcm
(3.21 KiB) Downloaded 196 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Okay - created a receive message macro.

If you could try it with your hardware - there is a 50:50 chance it will work (it either will work or it won't :) )

I can't test it until I do a Send macro :( and second nano can pretend.

I've put a very simple test into main - tries to receive a message and outputs a success message to UART (doesn't output anything on failure - as there will be lots!)
time_pulse.fcfx
(31.32 KiB) Downloaded 201 times
Note this doesn't work - needed a few mods - and version below does....
I've hidden the 'C' away (and worry that the extra overhead might cause problems) and haven't done lots of the niceties (enabling pullups for example - so you might need to add real ones)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

Now done send too.

Output looks reasonable on the oscilloscope - but receive not happy.

I'll post when it works!

Oops - defined tcnt1_buf as a byte rather than 16 bit. Now receives something reliably (ie the same every time) just not the same as the message sent :(

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

im very eager to see what you come up with..

because i sadly cannot be of any help i guess i worked on some display for a friend. im now using a sensor to show pressure data on a 16x2 screen but if this decoder your working on is oke i can try to pick the signals from the bus too...! again i really hope this works later on. i ordered some oled displays to play with now too.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

- And it works !!!

So one Arduino acting as a J1850 sender and one as receiver....

Note that the transmit and receive programs haven't been merged (so use Send_msg from transmit and Receive.... you get the idea.)

And because it should always be done so:

'Hello World' (rotating here to keep the scope traces interesting)
j1850_receive.fcfx
(42.02 KiB) Downloaded 205 times
j1850_transmit.fcfx
(39.28 KiB) Downloaded 191 times
Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

looks promising Martin! i see all the terms in there. got to ask you to explain it more to me what you did. but maybe that later..
il try to figure out what connections i need to make for bus in and see if i can hook up the terminal program by UART rx tx.
to show my data i think i need to do a few things or had you put an input in it?

can you show me later how i can get the different bites out? when i have that separate i can get it filtered and show that on an lcd.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: C-code

Post by mnf »

The receiving end displays data to uart..

Getting data out - bytes 5 and 6.

Use ( in a calculation block)

Code: Select all

.x = .m[4] 
 .y = .m[5]
Note that array indexes start at 0.

If the data is a word then

Code: Select all

.x = (.m[4] << 8) + .m[5]
where x is a 16 bit variable - and assuming that data is sent high byte then low (this might be reversed)

The bus seems to run at approx 10k baud - so i2c etc have major speed advantages - second wire with clock signal. But, pretty good for one wire!

Note there is a slight bug in the above, send message doesn't wait for tx_eof after sending - but it doesn't make a difference as the pin is passive and I wait before sending the next packet - so no problem :) .

I'll tidy a bit and combine the two halves.. Did you manage to receive the data from the sensor?

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

I realize how much work that was MArtin. this is something i wanted for years so you imagine how grateful i am!

dint have time enough yesterday to get my circuit done (past midnight) and of coarse searched to long for an easy uart-usb solution i do have somewhere(but where :roll: ) anyway will get there tonight. eager to test stuff now! was setting up a second controller to see the rx tx you made but have to wait for parts. for the one i have left and receive for now i need byte 2 too as decision where to guide the data on my display. have not looked deeper yet in your program. what do i use after .x and .y?

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: C-code

Post by Dutch »

well i have the receiver connected to the J1850 signal and its writing data.. not with your transmitter yet because i don't have parts for that yet.

i tried to put the .x= .m[4] and .y= .m[5] in an calculation but it gives an error. im doing something wrong.

Can you show me how i can have all arrays (0-6) print behind each other so i can compare it with the data i know? this is pretty raw but a start. Then, i want to simplify it to show only array 1 to determine its data origin and make a filter (decision) and then show the data from array 4 and 5 belonging to that array 1.
I know i need to do some calculation to the data from 4 and 5 but first i need to get it on screen to see what im doing. then.. i want to write that to an LCD.
The uart is great because i can send data on the bus too. there are a few funtions i want to do with that too! but first get all good with receiving.

Post Reply