Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Monochron <-> Arduino?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Monochron <-> Arduino?

by stephanie on Wed Dec 29, 2010 4:40 pm

Is it possible to 'convert' the Monochron so that it can be programmed like an Arduino? I have some mods in mind that I would like to do to it, but the avr-gcc environment is giving me a lot of grief trying to get it set up... whereas the Arduino environment seems to Just Work for me. (fwiw I am on a recent iMac with OSX 10.6.5)

The arduino environment is also attractive as it has been a bit easier for me to learn. I gather it is a simplified version of the avr-gcc... I like simple :)

Thanks & cheers!
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by adafruit on Sun Jan 02, 2011 1:56 pm

thats a very good question, we are thinking about 'porting' the code to arduino-ese which in and of itself isnt -terribly- hard but it will take a while. however, the code is kinda similar already, check out some of the CHRONs we have, like the 7chron which is quite simple!

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Monochron <-> Arduino?

by stephanie on Mon Jan 03, 2011 12:57 am

That would be very cool.

I've been looking at the Arduino ks0108 library but I'm not sure yet where to start in adapting it to the Monochron.
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by stephanie on Wed Jan 05, 2011 9:41 pm

I've started trying to get the monochron to work through the Arduino IDE -- not sure if I should continue on in this thread or head over to the Arduino area to discuss things there.

It might be 'overkill' but I went into the boards.txt and defined a new board for the Monochron, since it doesn't quite fit any of the other board definitions. (8MHz internal clock, Atmega328, etc.)

Then I went into the pins_arduino.c file and modified it to add 'digital pins' 20 and 21, to enable access to PortB6 and PortB7.

Finally, I went into the ks0108 library and created a new header for the monochron since it is wired up slightly differently from the standard Arduino wiring for the glcd. Here's where I'm running into trouble.

I've verified that I can set pin 21 (portb7, LCD_RS) as an output and set it high and low. However, the GLCD.init function fails at the first WriteCommand, and at this point I'm not sure how to debug that. I've double-checked the wiring, i.e. comparing the Monochron's schematic against the header file I created, and it looks like I'm targetting all the correct pins.

If anyone is interested, I can post / upload the changes I've made to the Arduino library etc., maybe someone else can catch whatever I've missed that's making it fail.
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by meteor on Thu Feb 10, 2011 9:22 pm

Stephanie,

Have you made any further progress on the arduino conversion? I'd like to help.
meteor
 
Posts: 5
Joined: Sat Feb 05, 2011 11:56 am

Re: Monochron <-> Arduino?

by stephanie on Thu Feb 10, 2011 11:19 pm

Sadly I gave up on it. :(

I just wasn't able to get the ks0108 to function using the arduino ide & library. I did modify the ks0108 library in that I added a .h configuration that matched the monochron setup, and I modified the arduino files to function on the monochron hardware (with access to pins B6 and B7, using internal osc. instead of a crystal).

I could upload and run sketches on the monochron and was able to get a 'blink' demo sketch to run on both the B6 and B7 pins. However, no matter what I attempted, it failed during the ks0108 initialization. (This was running the example sketch - I never even got so far as working on a monochron-duino clock sketch.)

The library uses its own 'fastwrite' function which directly write to the atmega's port registers, rather than using the arduino 'digitalWrite' function. I did try modifying this function to include the portb 6 and 7 pins, and as near as I can tell, my maths should work, but it doesn't. This is where the init was failing.
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by meteor on Fri Feb 11, 2011 11:03 am

Could you give me access to the files you used? Not promising anything, but I might be able to sort it out.
meteor
 
Posts: 5
Joined: Sat Feb 05, 2011 11:56 am

Re: Monochron <-> Arduino?

by wbp on Mon Jun 13, 2011 11:29 pm

meteor, stephanie, any further on this?

I've got an Arduino with a 61x128 KS0108 display attached, working fine with the GLCD library. I just got a Monochron and I really like the idea of using the Arduino IDE to program it. Nothing against avrdude, which I have working for the ice tube clock, it's just that for my purposes I don't need any pong games or anything fancy, and the Arduino IDE is easier to use.

wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Monochron <-> Arduino?

by stephanie on Tue Jun 14, 2011 10:07 pm

I completely lost track of this thread, sorry folks!

There were three files that I changed, two 'successfully' and one not so much. The first file is in the Arduino IDE files, which is "boards.txt". To that file I added the following board section:
Code: Select all | TOGGLE FULL SIZE
##############################################################

monochron.name=Monochron Platform - 328p using internal osc.

monochron.upload.protocol=stk500
monochron.upload.maximum_size=30720
monochron.upload.speed=57600

monochron.bootloader.low_fuses=0xe2
monochron.bootloader.high_fuses=0xdd
monochron.bootloader.extended_fuses=0x05
monochron.bootloader.path=atmega
monochron.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
monochron.bootloader.unlock_bits=0x3f
monochron.bootloader.lock_bits=0x0f

monochron.build.mcu=atmega328p
monochron.build.f_cpu=8000000L
monochron.build.core=arduino

Note, while I was experimenting, I had an ATMega328p in the Monochron. I don't remember if it comes with that or a 168 but I have some extra 328s, and I know that I was using a 328 when I was working on this.

The second file, also in the Arduino IDE, is "pins_arduino.c" which I modified to include references to the pins PortB6 and PortB7 which I designated as pins 20 and 21. The modifications were made as follows:
Code: Select all | TOGGLE FULL SIZE
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
   PD, /* 0 */
   PD,
   PD,
   PD,
   PD,
   PD,
   PD,
   PD,
   PB, /* 8 */
   PB,
   PB,
   PB,
   PB,
   PB,
   PC, /* 14 */
   PC,
   PC,
   PC,
   PC,
   PC,
   PB, /* 20 -- added this line to include the xtal lines */
   PB, /* 21 -- added this line to include the xtal lines */
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
   _BV(0), /* 0, port D */
   _BV(1),
   _BV(2),
   _BV(3),
   _BV(4),
   _BV(5),
   _BV(6),
   _BV(7),
   _BV(0), /* 8, port B */
   _BV(1),
   _BV(2),
   _BV(3),
   _BV(4),
   _BV(5),
   _BV(0), /* 14, port C */
   _BV(1),
   _BV(2),
   _BV(3),
   _BV(4),
   _BV(5),
   _BV(6), /* 20, port B xtal -- added this line to include the xtal lines */
   _BV(7), /* 21, port B xtal -- added this line to include the xtal lines */
};


The third file is where it didn't work and that was the GLCD library. Specifically where I couldn't get it to work was the 'fastwrite' functions. This was a macro defined in one of the headers. I tried to modify it to be able to reference pins 20 and 21 but it just refused to work.

With the changes listed above I was able to confirm the Arduino IDE was able to load sketches, and I could DigitalWrite to pins 20 and 21 and it would work correctly.

Last thing - all my work was on the 'current' version of the GLCD library. I've since learned that there is a new version that's in RC2 which is much more stable and much more capable. It's entirely possible that the newer library would have solved my problems, but I haven't had the time or inclination to have another go at it.

Cheers!
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by wbp on Wed Jun 15, 2011 12:50 am

Stephanie,

Thanks for posting that!

One observation so far - the makefiles for the various monochron builds use 0xd7 for the high fuses, but you have 0xdd. Any idea why?

Thanks!
William

wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Monochron <-> Arduino?

by stephanie on Wed Jun 15, 2011 8:26 am

I wasn't entirely certain about the fuse settings, but I used an online AVR fuse calculator and I compared the fuse settings to other entries in boards.txt for other Arduinos that were using the internal 8MHz osc. and between those two things, that's what I came up with.

Sorry I can't remember more details then that - eg. I can't remember what the actual difference was. As I said, I did confirm that the result was a functioning arduino-compatible board.

Also - I found my old ks0108 config file I had made when attempting this (yay for Mac Time Machine!) In the ks0108 library I had added a new file, ks0108_Monochron.h as follows:
Code: Select all | TOGGLE FULL SIZE
#ifndef   KS0108_CONFIG_H
#define KS0108_CONFIG_H

#define CSEL1            14
#define CSEL2            2
#define R_W               13
#define D_I               21      // aka RS
#define EN               12

//#define LCD_CMD_PORT      PORTC      // Command Output Register for pins 14-19

#define dataPins8to11
#define LCD_DATA_NIBBLES
#define LCD_DATA_LOW_NBL   B
#define LCD_DATA_HIGH_NBL  D

// the fastwrite macros won't work for D_I on pin 21! They assume the standard pins 0-19. Frak.
//#define fastWriteHigh(_pin_) ( _pin_ < 8 ?  PORTD |= 1 << (_pin_ & 0x07) : ( _pin_ < 14 ?  PORTB |= 1 << ((_pin_ -8) & 0x07) : PORTC |= 1 << ((_pin_ -14) & 0x07)  ) )
//#define fastWriteLow(_pin_) ( _pin_ < 8 ?   PORTD &= ~(1 << (_pin_  & 0x07)) : ( _pin_ < 14 ?  PORTB &= ~(1 << ((_pin_ -8) & 0x07) )  :  PORTC &= ~(1 << ((_pin_ -14) & 0x07) )  ) )

// this *might* work, it is saying if the pin # is over 19 then go to portb and subtract 14 from pin number, which should get us portb pins 6 and 7 for arduino pins 20 and 21
#define fastWriteHigh(_pin_) ( _pin_ < 8 ?  PORTD |= 1 << (_pin_ & 0x07) : ( _pin_ < 14 ?  PORTB |= 1 << ((_pin_ -8) & 0x07) : ( _pin_ < 20 ? PORTC |= 1 << ((_pin_ -14) & 0x07) : PORTB |= 1 << ((_pin_ -14) & 0x07) ) ) )
#define fastWriteLow(_pin_) ( _pin_ < 8 ?   PORTD &= ~(1 << (_pin_  & 0x07)) : ( _pin_ < 14 ?  PORTB &= ~(1 << ((_pin_ -8) & 0x07) )  : ( _pin) < 20 ? PORTC &= ~(1 << ((_pin_ -14) & 0x07) : PORTB &= ~(1 << ((_pin_ -14) & 0x07) ) ) ) )
#define MONOCHRON_TEST 1
#endif
The 'fastwritehigh' and 'fastwritelow' macros are where it seemed to fail for me. I'm just not good with bitshifting and port writing with the &= and |= stuff.

Then I modified the file "ks0108.h" to include an ifdef to grab the Monochron configs. This relied in putting a #define MONOCHRON at the top of the sketch.
Code: Select all | TOGGLE FULL SIZE
// Chip specific includes
#if defined(__AVR_ATmega1280__)
#include "ks0108_Mega.h"  // include this for the Arduino Mega other ATmega1280 boards
#elif defined (__AVR_ATmega644__)  // TODO - check this define
#include "ks0108_Sanguino.h"   // include this for Sanguino or ATmega644 boards
#elif defined (MONOCHRON)
#include "ks0108_Monochron.h"   // modified configs for the monochron clock
#else
#include "ks0108_Arduino.h"  // include this for the Arduino or other ATmega168 boards
#endif


Like I said earlier, this is based on the current GLCD library which is about 2 years old. The newer one is still in RC testing but seems much better. I haven't tried adapting the newer library to the Monochron.

Cheers!
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by wbp on Wed Jun 15, 2011 3:38 pm

Stephanie,

Thank you so much for taking the time to find all of this and post it. Thanks to your efforts, and a bit of my own, I got it working!!!

I modified "boards.txt" and "pins_arduino.c" per your examples, except I used "d7" for the high fuses because that is what is in all the makefiles for the Monochron.

Code: Select all | TOGGLE FULL SIZE
monochron.name=MonoChron Clock w/ ATmega328 using internal osc

monochron.upload.protocol=stk500
monochron.upload.maximum_size=30720
monochron.upload.speed=57600

monochron.bootloader.low_fuses=0xE2
monochron.bootloader.high_fuses=0xD7
monochron.bootloader.extended_fuses=0x05
monochron.bootloader.path=atmega
monochron.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
monochron.bootloader.unlock_bits=0x3F
monochron.bootloader.lock_bits=0x0F

monochron.build.mcu=atmega328p
monochron.build.f_cpu=8000000L
monochron.build.core=arduino


Then I made a copy of the glcd library which I named "glcdMC" (simpler for me than adding a #define - but that's cleaner so I'll probably do that as well). FYI - I'm using this version of glcd: "glcd-20110423.zip" so it may be newer than what you have.

In glcd file "ks0108_Arduino.h" I changed the pin definitions:
Code: Select all | TOGGLE FULL SIZE
define glcdCSEL1        14   // LCD_CS0
#define glcdCSEL2        2    // LCD_CS1

and
Code: Select all | TOGGLE FULL SIZE
#define glcdRW           13   // SCK
#define glcdDI           21   // LCD_RS
#define glcdEN           12   // MISO


In file "arduino_io.h" I changed the 2 macros:
Code: Select all | TOGGLE FULL SIZE
   // Standard Arduino Pins (modified for MonoChron)
#define digitalPinToPortReg(P) \
    ( ((P) >= 0 && (P) <= 7) ? &PORTD : (((P) >= 8 && (P) <= 13) ? &PORTB : ( ((P) >= 20 && (P) <= 21) ? &PORTB : &PORTC ) ) )
#define digitalPinToBit(P) \
    ( ((P) >= 0 && (P) <= 7) ? (P) : (((P) >= 8 && (P) <= 13) ? (P) - 8 : (P) - 14) )


I've got a very simple sketch that displays the time & date. My plan is to add an SHT11 for temperature and humidity, and a GPS module, and a menu to set the clock. It's very very cool that I can do this in the Arduino IDE now!

William

wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Monochron <-> Arduino?

by wbp on Wed Jun 15, 2011 3:40 pm

correction to my last post (can't edit posts here?) - In file "arduino_io.h" only the first macro is changed. The second one works fine as is.

wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Monochron <-> Arduino?

by stephanie on Wed Jun 15, 2011 9:07 pm

That's great work!

I believe the glcd library you mentioned is the one I have now, that's the latest RC, not the one I was trying back in February.

The difference in the fuses that I can see are the 0xdd is set to 'preserve the contents of eeprom through chip erase' whereas 0xd7 is set to 'select boot size 0'... everything else is the same.

Cheers!
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am
Location: Canada

Re: Monochron <-> Arduino?

by edc1591 on Wed Jul 25, 2012 10:42 pm

William,

Could you please post the Arduino project you made for the monochron if you still have it?

Thanks
edc1591
 
Posts: 40
Joined: Sat Jul 14, 2012 11:12 am
Location: New York, NY

Please be positive and constructive with your questions and comments.