By continuing to use this site, you agree to our use of cookies. Find out more
Forum sponsored by:
Forum sponsored by Allendale Jan 24th

Arduinos and Microcontrollers ref: Rotary Table Mew 249

All Topics | Latest Posts

Search for:  in Thread Title in  
Muzzer08/12/2016 13:05:58
avatar
2904 forum posts
448 photos
Posted by Bazyle on 06/12/2016 18:16:19:

Actually most of the controllers with a 'user friendly' IDE, including the Arduino, are not full compliers but producing an intermediate code that is run by an interpreter in the chip - that's why the chips have to be preprogrammed.

I hadn't heard that and can't see any reference to any interpreted element. I understood that Arduino code is a simplified version of C++ that is compiled into machine code and uploaded.

There are loads of references out there and although Wikipedia is hardly the ultimate arbiter, it sates that "after compiling and linking with the GNU toolchain....the Arduino IDE employs the program avrdude to convert the executable code into a text file in hexadecimal coding that is loaded into the Arduino board by a loader program in the board's firmware." Presumably this allows the bootloader to run the code after reset? Can you point us to something that explains what you mean?

SillyOldDuffer08/12/2016 13:37:05
5310 forum posts
1086 photos
Posted by Bazyle on 06/12/2016 18:16:19:

...

Actually most of the controllers with a 'user friendly' IDE, including the Arduino, are not full compliers but producing an intermediate code that is run by an interpreter in the chip - that's why the chips have to be preprogrammed.

...

Thanks to Bazyle for launching me into another useful learning experience!

I didn't believe the suggestion that the Arduino has an interpreter in the chip and tried to confirm that's wrong. Loads of stuff on the web implies that Bazyle is mistaken but I couldn't find anything that confirmed it positively. I ended up doing forensics on one of my own programs.

Normally the Arduino IDE (Integrated Development Environment) doesn't save the compiled file uploaded into the Microcontroller. It can be ordered to save it from the Sketch Menu with 'Export compiled binary'. Doing this produces two hex files; they appear to be identical except that one of them includes bootloader code. These files can be analysed.

A hex file can be disassembled a command line utility called avr-objdump. Although this was downloaded with the IDE it is not installed (at least on linux), so using it involves typing in full pathnames, or copying everything into the same directory.

Then this magic incantation disassembles the binary file, ie converts machine code back into assembly language. My example program is in monitor.ino.standard.hex, and objdump writes the decode into a new text file called hum.s:

./avr-objdump -j .sec1 -m avr -d monitor.ino.standard.hex > hum.s

Inside hum.s is the evidence, and it looks very much like AVR assembly language to me, not interpreter tokens:

avr_disassemble.jpg

That's the case for the defence m'lud. I suspect Bazyle has misremembered how this particular microcontroller works, he's quite right that others interpret.

Normally using an Arduino is much easier than this, but it's useful to know that the IDE comes with a full set of advanced software tools, hidden away though they are.

Dave

After posting, discovered that muzzer got in first!

Edited By SillyOldDuffer on 08/12/2016 13:38:30

Edited By SillyOldDuffer on 08/12/2016 13:41:04

Les Jones 108/12/2016 14:03:50
2106 forum posts
144 photos

It is possible to see the assembler listing of the compiled "C" program. The details are on this web page.

Les

John Rudd08/12/2016 14:15:44
1367 forum posts
58 photos

Oops...deleted in error...I'll try again...

Edited By John Rudd on 08/12/2016 14:21:55

Neil Wyatt08/12/2016 14:28:24
avatar
Moderator
17314 forum posts
690 photos
77 articles

Arduinos use AVR chips inside.

These have a 'Harvard' architecture with separate program and data memory spaces (EEPROM and RAM), although the distinction has become academic as EEPROM is unreliable and awkward to program and newer instructions let you directly read and write to the much more reliable and long-lived (in terms of r/w cycles) flash.

You can write programs for AVR in any language that has a suitable compiler to AVR machine code, and the Arduino standard is a version of C used through the 'Sketch' program.

Most AVR users use GCC (GNU C Compiler) together with WINAVR.

Studio 6 the AVR development environment can supporet multiple languages ( ) although it only has programming support for 18 and 32-bit AVR GCC, AVR assembler and ARM built in (i.e. you need to add a compiler).

For my sins I write most of my code in AVR assembler because I'm a hair-shirted 8-bit obsessive.

Arduinos are really only AVRs with a pre-programmed bootloader on a dedicated pin-out board. they can be programmed as ordinary AVRs OR an ordinary AVR can use the Arduiino bootloader and have greater flexibility of how you use the pins.

You CAN have an interpreter running on AVR, I know because I have written a version of BBC BASIC that runs on an AVR although that link is just about the display driver.

As I've mentioned its well worth looking at Atmel's website and AVRfreaks website if you want to really take these chips to the limit whether or not you use arduino boards.

Neil

Neil Wyatt08/12/2016 14:34:11
avatar
Moderator
17314 forum posts
690 photos
77 articles

Not for the faint-hearted, this is what Studio 6 looks like, a bit more complex than the Arduino sketch tool, but it does make sense in the end...

studio 6.jpg

SillyOldDuffer08/12/2016 14:51:50
5310 forum posts
1086 photos
Posted by Neil Wyatt on 08/12/2016 14:28:24:

...

For my sins I write most of my code in AVR assembler because I'm a hair-shirted 8-bit obsessive.

...

You CAN have an interpreter running on AVR, I know because I have written a version of BBC BASIC that runs on an AVR

...

Neil

Now that combination is impressive! How on earth do you find the time?

The method shown in Les Jones link didn't work for me on linux with version 1.6.8. It's a pity because you get to see the 'C' being converted to Assembly and the machine code as well. As far as I can tell, Arduino 1.6.8 on linux doesn't write any temporary files, or if it does they get deleted. Might be different on Windows, I shall fire it up and have a look later.

Dave

Zebethyal08/12/2016 15:22:59
191 forum posts

The Arduino AVR creates temporary files on all versions (I use the MAC one), but normally deletes them once it has finished with them.

There is a an entry you can add to preferences.txt that will prevent it from deleting all of these temporary files, along with the hex files.

export.delete_target_folder=false

You only need pre-programmed chips (bootloader pre-installed) if you are using the bootloader to help load your program or you want to upload via the USB connection on the board.

If you want to save the space used by the bootloader and/or program the chip directly, or buy blank chips and program them on your breadboard, you can do this via the ICSP connection and a suitable programmer like a USBasp, AVRISP or USBtinyISP, any of which can be bought for less than £3.00, some for less than £1.00.

If you do put a bootloader on, you can then upload programs via the serial pins by resetting the chip at the right point during the compile, such that the bootloader is looking for new code as you are attempting to talk to it from the IDE, if you time it wrong, the bootloader simply loads whatever code is already programmed on the chip (if any).

The first time you program a brand new chip, it may need some different fuse settings as it may be set to the factory default clock speed of 1MHz, which may not be supported by all programmers (some have an additional jumper to allow use at that clock speed).

Edited By Zebethyal on 08/12/2016 15:30:24

SillyOldDuffer08/12/2016 16:08:15
5310 forum posts
1086 photos

Thanks Zebethyal, that preference keeps all my build files, which are stored in /tmp

The only problem now is that the elf doesn't have any debug info in it, so the C source is missing from objdump's output. I'm wondering if gcc flags can be set as a preference. Hmmm.

Cheers,

Dave

Edited By SillyOldDuffer on 08/12/2016 16:08:34

Neil Wyatt08/12/2016 18:35:20
avatar
Moderator
17314 forum posts
690 photos
77 articles

More seriously, I googled 'pic microcontroller rotary table' - still some false positives as PIC means short for picture, 14,000 results. Substituting AVR gave over 45,000 and ardiuino well over 100,000.

the magazine has no bias, the bias is in the numbers of people using different types of micro for workshop projects.

Quite happy to feature some PC projects, but I would worry about 8051, 6502 or even Cray X-1 as minority interests

Neil

Frances IoM08/12/2016 18:54:02
714 forum posts
25 photos
Neil
how about a 4040 project - I still have one (probably never used) + support chips - not 100% sure they have survived the last 35yrs tho as bought as spare for a student project I was supervising and forgotten about for years.
Cyril Bonnett08/12/2016 22:05:42
235 forum posts
1 photos

**LINK**

Might be of interest.

Engine Builder09/12/2016 15:57:30
avatar
222 forum posts

I am very interested in the step index project.

I already had an Arduino uno and thought I would by the LCD shield to convince myself it works before getting the motor and driver. I am not up to speed with the programming.

I have uploaded the sketch and fitted the shield but it's not working quite right, unless this is because the other parts are not installed.

On power up I get the version number displayed and then SELECT MODE and MODE = STEP which is correct.

When I press the SELECT button nothing happens. Pressing LEFT, UP or DOWN scrolls between STEP,TEMP,RUN and JOG. RIGHT does not do anything.

Is it possible the LCD shield is faulty?

I would appreciate any suggestions anyone can offer.

john swift 109/12/2016 16:23:51
avatar
318 forum posts
183 photos

Hi

I don't have an arduino uno and LCD + switch sheild but

pre empting buying them to experiment with I have found conflicting notes about the LCD sheild

it seems that some boards you can only use D10 to switch the display LED backlight on & off

others you can use PWM to contol the brightness

this is what I have so far

pwm brightnes mod lcdkeypad_shield_sch.jpg

John

 

PS

routine to read keys

int read_LCD_button()     // routine to read the LCD's buttons
{
  int key_in;
  key_in = analogRead(0);      // read ADC
  // average values for my board were: 0, 144, 324, 505, 742
  // add approx 100 to those values to set range
  if (key_in > 1000) return NO_KEY;    
  if (key_in < 50)   return RIGHT_KEY;  
  if (key_in < 250)  return UP_KEY;
  if (key_in < 450)  return DOWN_KEY;
  if (key_in < 650)  return LEFT_KEY;
  if (key_in < 850)  return SELECT_KEY;  
}

 

hope you can now check your keys

 

Edited By john swift 1 on 09/12/2016 16:48:53

SillyOldDuffer09/12/2016 17:02:49
5310 forum posts
1086 photos

Hi Emgee,

I've been playing too and have a similar problem. I'm pretty sure the issue is the bit of code that works out which key is being pressed. It's the very last function at the end of the Sketch and it looks like this:

int read_LCD_button() // routine to read the LCD's buttons
{
int key_in;
delay(ADSettleTime); // wait to settle
key_in = analogRead(0); // read ADC once
delay(ADSettleTime); // wait to settle
// average values for my board were: 0, 144, 324, 505, 742
// add approx 100 to those values to set range

if (key_in > 850) return NO_KEY;
if (key_in < 70) return RIGHT_KEY;
if (key_in < 250) return UP_KEY;
if (key_in < 450) return DOWN_KEY;
if (key_in < 650) return LEFT_KEY;
if (key_in < 850) return SELECT_KEY;

}

The way the code works is that pressing a key generates a voltage that's measured by the arduino. The voltages can vary between individual LCD boards. Mine generates voltages that don't quite line up with the values in the sketch. I get:.

  • NO_KEY 1023 (This is OK)
  • RIGHT_KEY 0 (This is OK)
  • UP_KEY 99 (This is OK)
  • DOWN_KEY 255 (This is only just OK)
  • LEFT_KEY 409 (This is WRONG - the sketch thinks the DOWN_KEY has been pressed)
  • SELECT_KEY 638 (This is WRONG - the sketch thinks the LEFT_KEY has been pressed)

Easily fixed. I change these lines in the function to read:

if (key_in > 850) return NO_KEY;
if (key_in < 70) return RIGHT_KEY;
if (key_in < 230) return UP_KEY;
if (key_in < 350) return DOWN_KEY;
if (key_in < 450) return LEFT_KEY;
if (key_in < 850) return SELECT_KEY;

You can use the IDE's monitor to find out what voltages are stored in key_in when the various buttons are pressed. To do this, add the command

Serial.begin(9600);

to the function called setup at the beginning of the sketch.

Then add Serial.println( key_in ) to the read_LCD_button function as shown:

int read_LCD_button() // routine to read the LCD's buttons
{
int key_in;
delay(ADSettleTime); // wait to settle
key_in = analogRead(0); // read ADC once
delay(ADSettleTime); // wait to settle
// average values for my board were: 0, 144, 324, 505, 742
// add approx 100 to those values to set range

Serial.println( key_in ); // for debugging


if (key_in > 850) return NO_KEY;
if (key_in < 70) return RIGHT_KEY;
if (key_in < 230) return UP_KEY;
if (key_in < 350) return DOWN_KEY;
if (key_in < 450) return LEFT_KEY;
if (key_in < 850) return SELECT_KEY;

}

Verify and Upload the sketch, then turn on the monitor with Tools->Serial Monitor. It may be necessary to set it to 9600 Baud. (bottom right of screen)

The monitor should start spewing the number 1023. When you press a button a different number will appear in the stream. This is the value your LCD Board uses to signal that key is pressed. Record the value produced by each key and compare it with the values in the Sketch, changing the sketch as necessary.

After you've got it working it's tidy to remove the Serial commands. The Sketch will still work if you leave them in.

Hope that makes sense!

Dave

 

 

Edited By SillyOldDuffer on 09/12/2016 17:03:14

Journeyman09/12/2016 17:18:58
avatar
715 forum posts
115 photos

Also playing with this, first time with Arduino. More by luck than judgement my key presses seem to work OK and the functions work as per the program. I was testing with the UNO and LCD shield just plugged into the USB port on my PC. I was wondering if the available output from the USB port was on the limit it might affect the key press analogue voltages as it is just a resistor divider chain and if the starting 5v is below spec could have strange consequences down the line. I hasten to add that I have no experience or knowledge in this area! Just trying to learn and keep the little grey cells moving...

John

Engine Builder09/12/2016 17:39:26
avatar
222 forum posts

Sillyoldduffer, your not so silly!

I do not quite understand, as a Arduino virgin, where I should put the line Serial.begin(9600);

I see void setup() { in the sketch.

Anyway I just changed the key_in values to the same as your and it seems to work now.

Thanks for the help.

I would like to check what values my keys are giving though so could you clarify where to add that Serial.begin line please.

Roger Williams 209/12/2016 17:42:35
331 forum posts
1 photos

My first time with the Arduino Uno too, and after getting something like " done uploading ",, I get bugger all on the LCD apart from the light !!. Oops. Any thoughts anyone please, bearing in mind I dont really know what Im doing.

SillyOldDuffer09/12/2016 17:49:52
5310 forum posts
1086 photos
Posted by Engine Builder on 09/12/2016 17:39:26:

Sillyoldduffer, your not so silly!

I do not quite understand, as a Arduino virgin, where I should put the line Serial.begin(9600);

I see void setup() { in the sketch.

Anyway I just changed the key_in values to the same as your and it seems to work now.

Thanks for the help.

I would like to check what values my keys are giving though so could you clarify where to add that Serial.begin line please.

Trust me : I am quite Silly, quite old and a bit of a duffer!

the Serial.begin can go anywhere inside setup(). This is where I put it:

void setup()
{
// Create some custom characters for the lcd display
byte c_CW[8] = {0b01101,0b10011,0b10111,0b10000,0b10000,0b10000,0b10001,0b01110}; //Clockwise
byte c_CCW[8] = {0b10110,0b11001,0b11101,0b00001,0b00001,0b00001,0b10001,0b01110}; // CounterClockWise
#ifdef Fahrenheit
byte c_DegreeF[8] = {0b01000,0b10100,0b01000,0b00111,0b00100,0b00110,0b00100,0b00100}; // degreeF
#endif
#ifdef Celsius
byte c_DegreeF[8] = {0b01000,0b10100,0b01011,0b00101,0b00100,0b00100,0b00101,0b00011}; // degreeC
#endif
lcd.createChar(1,c_CW);
lcd.createChar(2,c_CCW);
lcd.createChar(3,c_DegreeF);

Serial.begin(9600);
// begin program

Glad it's working!

Dave

SillyOldDuffer09/12/2016 17:54:08
5310 forum posts
1086 photos
Posted by Roger Williams 2 on 09/12/2016 17:42:35:

My first time with the Arduino Uno too, and after getting something like " done uploading ",, I get bugger all on the LCD apart from the light !!. Oops. Any thoughts anyone please, bearing in mind I dont really know what Im doing.

This could be another easy one I hope. Top left on the LCD DIsplay is a Blue Box with a small brass screw on stop. This is the multi-turn potentiometer that controls contrast. Try turning it with a small screwdriver (could need to go either way). Be gentle and don't overdo it - I broke one once.

Dave

All Topics | Latest Posts

Please login to post a reply.

Magazine Locator

Want the latest issue of Model Engineer or Model Engineers' Workshop? Use our magazine locator links to find your nearest stockist!

Find Model Engineer & Model Engineers' Workshop

Latest Forum Posts
Support Our Partners
ChesterUK
Eccentric July 5 2018
Warco
Ausee.com.au
cowells
Allendale Electronics
emcomachinetools
Eccentric Engineering
Subscription Offer

Latest "For Sale" Ads
Latest "Wanted" Ads
Get In Touch!

Do you want to contact the Model Engineer and Model Engineers' Workshop team?

You can contact us by phone, mail or email about the magazines including becoming a contributor, submitting reader's letters or making queries about articles. You can also get in touch about this website, advertising or other general issues.

Click THIS LINK for full contact details.

For subscription issues please see THIS LINK.

Digital Back Issues

Social Media online

'Like' us on Facebook
Follow us on Facebook

Follow us on Twitter
 Twitter Logo

Pin us on Pinterest