Attempting to read an ADC0831 using C
Blake Koch
Posts: 39
I would like to read an ADC 0831 chip using C Simple IED. Im having trouble finding the code need to pulse the CLK pin as well as the Code needed to return and store the data from DO pin.
Can anyone help ?
Thanks
Blake
Can anyone help ?
Thanks
Blake
Comments
OBEX link: http://obex.parallax.com/object/300
dgately
Thanks
Blake
Hmm... The first advice I would give is to start with some simple C programs before attempting to deal with a device like an ADC. Have you attempted Parallax's Propeller C learning system yet? (http://learn.parallax.com/propellerc) The Parallax staff has put a lot of work into developing very simple tutorials that will get you up and programming in C for the propeller. Learning C syntax while trying to access a device that is not yet supported by a library is going to make your task difficult. So, spend a day or two just learning C before you attempt to access the ADC. Just my advise (of course, my advise and $4.50 will buy you a latte at Starbucks and not much more:blank:)
For C syntax learning, the best guide is "The C Programming Language" text by Brian Kernighan & Denis Ritchie. You can find this text as a .PDF on the internet or buy a paper copy from Amazon. (http://www.cs.ucsb.edu/~buoni/cs16/The_C_Programming_Language.pdf). Although there are many books on C programming, this is "the text" on C that millions of programmers cut their teeth on.
dgately
/*
Measure Volts.c
Make voltmeter-style measurements with the Propeller Activity Board.
*/
#include "simpletools.h" // Include simpletools
#include "adcDCpropab.h" // Include adcDCpropab
int main() // main function
{
adc_init(21, 20, 19, 18); // CS=21, SCL=20, DO=19, DI=18
float v2, v3; // Voltage variables
Can this line code be modified to fit the ADC 831? adc_int(21,20,19,18) What pulse duration is at pin 20, Can it be adjusted. and since the 831 does not have a DI pin 18 can that segment of code be eliminated or modified.
If it can't be modified it would seem to me that all I need to do is write a simple code 1 to select chip (easy) 2 send a pulse equal to the 831 range 420us, (this code I don't know yet) 3 retrieve the data from the do pin and put it in a variable.(this code I don't know yet). Sound like it should be simple.http://forums.parallax.com/images/icons/icon6.png
Thanks for any help guys.
Blake
Left all of Johnny Mac's comments as-is as this is just a test. He may want to port this and update, but thought it would be a good exercise in porting spin to C, for the moment.
Can you test this?
dgately
This could make a good CogC test, for a couple of useful indicator metrics :
If the ADC read and scale target one COG via CogC, (means main printf is in another ?),
M1 :
How much of a COG does this use, and how does that use split between int32_t mathops and user code.
What is speed of int32_t scale ?
M2:
if the int32_t changes to int64_t, how does that use split between int64_t mathops and user code.
What is speed of int64_t scale ?
M3: Repeat M2 for Real Numbers.
You can force it to do things to find it's limits as an exercise if you like; however, even our smallest floating point print function (about 4KB) would not fit in CogC.
Best to leave the business logic in CMM and drivers in cogs.
There is an fpucog.c file that we can add to projects that speed up default floating point by about 10x. At the moment it is in simpletools, but i wouldn't count on it staying there by default because it uses a COG.
That was why I excluded printf, I expected that to have a high cost.
The intention is to place the sensor driver into a COG, but it makes most sense to also include the sensor fitting/scale maths as well as that makes for better modularity and support.
It is still not clear what subsets can fit into a cog, via CogC, and with what overhead. (ie how much do they remove from user code space)
What about Float Mul and Div, and int32 and/or int64 mul/div/mod - do those compile into CogC, or are they always calls outside the COG.
The only library code as I recall that was ever written for CogC is an integer printf function and some support items (a version of which is in the repository)..
Float MUL/DIV, etc... are either in the floating point emulation library, or the fpucog.c which is in-line ASM COG code included automatically when added to a project.
That's a useful link, I can see 32 bit Mu & Div, are there plans for 64 bit versions ?
The classic sensor scale eqn is A*B/C, (as above) and it can be important to at least operate 64b on the interim result.
So a lib that allowed this to be the most compact (reside within the COG ) would be great
Scaled_int32 = Scale_int32_via_int64(A,B,C); and the function does A*B -> 64b/C -> 32b
on the topic of compact, in divide usually DIV and MOD come for free, so a function form of
DivideAbyB = Div_Mod_int32(A,B,CommonRem);
returns A/B and also places remainder in CommonRem, a library or global variable.
The _UDIVSI function already returns both quotient and remainder, you just have to declare it to call it directly (for some reason the compiler isn't merging the calls to divide and modulo, although we've told it that both call the same function). For example you can do: The "__attribute__((native))" says that the function is in COG memory and should be called with the standard COG call/ret sequence instead of the default C calling convention (which is different because C functions can be recursive).
Did you find the code?
Have you considered using basic C++ rather than C, since then you have access to the same basic OO principles that Spin provides?
@Blake
I took a couple seconds to review the datasheet for the ADC 0831. It mentioned "TI Microwire" for the serial interface and I believe that basically means SPI right? If that's the case, you can start with PropWare's PropWare::SPI class if you're willing to use a bit of C++ in your project. It will save you the trouble having to bit-bang the serial protocol yourself by providing easy, high-level send and receive functions.
Check out the API here which lists out all of the functions and, if it sounds good, i'll provide you a zip with all of the files you need to import into SimpleIDE.
BTW... my commentary is not an indictment of your libraries. Though I may never use them myself, I appreciate that you're providing code for Propeller users -- something I try to do via my Spin projects.
You could always use spin2cpp for the translation from Spin back to C or C++.
If you prefer to hand-write the C code, you can embed it in special comments in your Spin driver and then have spin2cpp extract the C code (translating the PASM to a C binary blob, and leaving the Spin alone). That's one of the major use cases I imagined for spin2cpp; using the same driver source for Spin and C/C++.
struct and union supported?
typedef supported?
I learned C in the last 6month and it's not bad once you get a hang of it.
IAR compiles really tight code when you use High Optimized setting, that rivals any assembly coder.
PropGcc is a version of gcc, which means it has all the standard C & C++ features. You are only limited by several of C's standard libraries being large (for the P1's storage capabilities) and so the PropGcc developers have added a few slimmed-down replacements for things like printf and its cousins...
Basically, you should have little issue in developing typical C or C++ code on the Propeller. It's when you want to code Propeller/microcontroller-specific type actions that you need to think outside the standard C box. SimpleIDE & PropWare as 2 examples, provide many libraries that are specific to supporting the Propeller.
Just my $.02 US,
dgately