![]() |
Forum Index : Microcontroller and PC projects : AIN function?
Author | Message | ||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
Below is a small snippet of code for using the AIN. When I ran this code I noticed that the value that I was capturing via the COM port is completely different than the value that was being shown with the local print command. Can someone explain what is going on? Thanks [code] setpin 2,AIN open "COM2:19200" as #2 do print #2, pin(2) pause 1000 print pin(2) "Volts" [/code] |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
Can you post the output as well? We need to get "completely different" into focus ![]() |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
The completely different can have many causes, in this case it is hardware related. You are using two different values to print. [code] setpin 2,AIN open "COM2:19200" as #2 do value = pin(2) print #2, value pause 1000 print value "Volts" [/code] I'll guarante it will print the same value. :) Microblocks. Build with logic. |
||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
This would be: print #2, pin(2) --> 52.00 print pin(2) "Volts" --> 3.00514Volts I am trying to capture the voltage of a battery that is putting out ~8.0VDC as measured by a voltmeter. I do have a voltage divider setup. The thing that gets me is the very different numbers that I am seeing at the moment. If I were seeing something like 3.00 via the COM port, then I would think it was a round up, but 3.00514 vs 52.00. |
||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
I'll guarante it will print the same value. :) I tried that scenario already, did not make a difference. Still got the same results. |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
That would be impossible as the snippet I posted prints exactly the same value. In your snippet there are two pin(2) reads so you get two samples which could be different. Storing the single sample in a value variable first and then print it would show similar values. but, if it does show a difference then there is a bug in the firmware! Microblocks. Build with logic. |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
I agree with MB... if the variable is printing different values in the two sections and isn't being altered, it's time to re-flash the microcontroller. |
||||
panky![]() Guru ![]() Joined: 02/10/2012 Location: AustraliaPosts: 1114 |
dasyar, How are you decoding the COM1 output? Is it via teraterm or some other terminal program? Another way to check would be to loop COM1 Tx and Rd then read COM1 Rd with a line input program to see what you are actually sending? panty. Edit: As a further check, change your PRINT #2, pin(2) statement to PRINT #2,"Value of Pin 2 = ",pin(2) Does the text come out correctly? p. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
panky, Good thinking! It might be a mismacth in baudrate, stopbits or parity settings that give a totally different output on the serial device. Still outputting 52.00 looks pretty normal, you would expect at least some unexpected characters. There might be a small chance for a bug because a PIN() read returns a float when it is in analog input mode, but a integer when in normal input mode. It might be processed wrong with the print# statement. Maybe the float is processed as an integer. First we need to know more results. Microblocks. Build with logic. |
||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
I am trying to establish a COM link between the MM and another chip, which happens to be a Parallax Propeller chip. I am using C as the programming language on the propeller chip which has a standard COM program for creating the COM link. The baud rate, stop bits, … , etc matches what is on the MM. When I use: print("%.2f volts \n", gmmcvolts); --> 52.00 volts when I use: print("%d volts \n", gmmcvolts); --> 1256234 volts when I use: print("%c volts \n", gmmcvolts); --> 3 volts, at least this has a "could be" 3 number. I am not sure what the pin() value that is being sent out. It is different than what the local print value is. |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
OK, so most likely a formatting issue on the C side of things. I think Panky's idea of putting in a loop back so you can see what is leaving the com port (still on the MM side) - just as a confirmation you haven't got some odd stuff happening inside the print# statement. If all OK, it needs a C guru (which as you might have guessed does not include me) |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
I think you are handling a string as a number. What is send over the comport is just text. So you would need to printf("%s volts\n", gmmcvolts); And of course gmmcvolts should also be a string. Microblocks. Build with logic. |
||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
I connected a USB-TTL cable to COM2 on the MM, and I was getting garbage chars, using Tera Term. Then I switched the USB-TTL cable to COM1, and I was getting the same thing I was seeing on the local print. Not sure what is going on with COM2. I connected the other chip to COM1, on the MM, and was not able to get anything to display on the other chip. So, it looks like if you are using Tera Term with the MM, it will probably work. But using something else, a good chance it will not work. I still cannot figure out what value form the pin() is sending, is text, or float or what. This is giving me a head ache, have to go lay down. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Some users have had problems with talking to picaxe chips. The solution in that case was add a small delay between characters. The simplest way to do that is set the micromite to 2 stop bits. Jim VK7JH MMedit |
||||
panky![]() Guru ![]() Joined: 02/10/2012 Location: AustraliaPosts: 1114 |
dasyar, The print command sends data 1 character (byte) at a time to represent what value you are trying to print. This is true regardless of whether you are printing to the screen or a file or a com port. The character (byte) is encoded in the ASCII form. Eg. the number 1 is sent as binary 00110001 or hex 31, the letter V would be sent as binary 01010110 or hex 56. In both cases, it is a single byte. The PRINT command neither knows or care what the actual data being sent is -that is if you wanted to print an integer such as 1234 then PRINT would send 4 characters (bytes) as hex 31 hex 32 hex 33 hex 34. The same data format is sent to the screen as is sent out to a file or COM port. In your case where you are reading an analog in pin, MMBasic returns a floating point number from the pin() statement thus if the value returned bt PIN(2) was 3.045 then the statement PRINT #2,PIN(2) would send the followinh string of hex characters out the COM port hex 33 hex 2E hex 30 hex 34 hex 35 hex 0D hex 0A Note the last 2 characters sent are a carriage return and a line feed which will always be sent unless you append a ; to the end of your PRINT statement. Hope this helps and I apologise if I have misunderstood your level of knowledge on serial comms. panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
dasyar Regular Member ![]() Joined: 13/04/2015 Location: United StatesPosts: 58 |
@panky Thanks for the explanation, which verifies what I thought was supposed to be occurring. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |