mbed_pwm_trinary_counter

mbed demo:
Software PWM on the mbed LEDs. Counting in trinary using off, half bright and full on LEDs.
Smooth scrolling a graphic on a 16x2 LCD display (using ST7066U controller).

Youtube video

Code ported to the Raspberry Pi with a couple of improvements:


9 comments on mbed_pwm_trinary_counter:

06 Jul 2012
06 Jul 2012

Hi Matt,

First you need to make sure the display power, contrast and backlight are set up correctly. You should have something like this

/media/uploads/JohnSchooling/16x2_lcd_start.jpg

before any code is run. Top 16 spaces are just visible and bottom 16 spaces can not be seen. This is because, from switch on, the display is set up as 16x1 (not 16x2).

I am using 5V to power the display (with appropriate resistors for contrast and backlight). The 3v3 digital values from the mbed still drive the digital pins on the display correctly. This has two advantages: 1. I am not taking as much from the mbed regulated supply and 2. I do not need to supply a -ve voltage for the contrast (see the spec for the 3v3 display set up). The only downside is that you may have to be careful if using any of the pins as input from the display (which I am not). They may feed back 5v which could upset the mbed (I am not sure if they are 5v tolerant). Ideally, you should read the busy flag but I just make sure there is enough delay after each write. If you want to use the busy flag (and you are running in 5v mode) you will need to make sure that you limit the voltage on that pin to 3v3.

When you confirm that your display is powered on correctly, we can do the next step. If you do not get the 16x1 spaces then it is probably the contrast that is incorrect (I assume you have the backlight lit properly). Let me know either way.

Edit:
16x2 voltages (in 5v mode):
p01 = 0v
p02 = 5v
p03 = between 1v0 and 1v5
p15 = 2v7 to 3v0 (depending on how bright you want it and how long you want the backlight to last).
p16 = 0v

Regards.
John.

06 Jul 2012

thanks john, went to delete my post because I found the pin outs in your code (doh, why didn't I look!)

I've now got digits but the wrong ones but they arte changing at the right time, so progress :)

must just be crossed up, so thanks, the LEDs changing is really helpful

06 Jul 2012

/media/uploads/lawless/time_is_now.jpg

Only one row and it drops characters but I'm smiling.

06 Jul 2012

Hi Matt,

Awesome!

I think your display (positive) would be better at scrolling graphics. My display (negative) seems to take too long to change the contrast on the pixels so I get more "smudge" on the scrolling smiley face and also when the digits change on the counter.

Dropping characters can happen if lcd_e drops to zero too soon after setting up the data. I never had a problem with this on the mbed but needed a delay of 1 microsecond on the Raspberry Pi due to it being a lot faster than the mbed.

void lcd_write(int c, int rs) {
    int old_lcd_rs = lcd_rs;
    // Should check Busy Flag here.
    lcd_rs  = rs;
    lcd_e   = 1; // E must be on for min 480ns then drop to zero.  Trigger is on falling signal.
    lcd_db0 = c    & 1;
    lcd_db1 = c>>1 & 1;
    lcd_db2 = c>>2 & 1;
    lcd_db3 = c>>3 & 1;
    lcd_db4 = c>>4 & 1;
    lcd_db5 = c>>5 & 1;
    lcd_db6 = c>>6 & 1;
    lcd_db7 = c>>7 & 1;
    // Tdsw Data Setup Width time at least 80ns.  No need for delay on slow processor.
    wait(0.000001);  // Needed the equivalent of this on the Raspberry Pi.
    lcd_e   = 0; // Strobe.
    // Th   Data Hold time at least 10ns.  No need for delay on slow processor.
    wait(0.000037);  // Most instructions take 37us.  May not need this delay if Busy Flag checked at top.
    lcd_rs  = old_lcd_rs;
}

The display starts up in 16x1 mode so you need to tell it you want two lines:

/*
   Instruction              RS RW  7 6 5 4 3 2 1 0  Time (270kHz)
   ...
   Function Set              0  0  0 0 1 D N F x x   .037ms
                             Data interface 8 (4) bits, Number of lines 2 (1), Font 5x11 if 1 line (5x8).
   ...
*/

int main() {

    wait(0.040);          // LCD initialisation takes 40ms.
    lcd_clear();
    lcd_write_inst(0x0C); // 0000 1DCB Display=1 Cursor=0 Blink=0
    lcd_write_inst(0x38); // 001D NFxx Data interface 8 (4) bits, Number of lines 2 (1), Font 5x11 if 1 line (5x8)

    lcd_write_str((uint8_t *)"Trinary", 4, 0);
    lcd_write_str((uint8_t *)"Counter", 4, 1);

Regards.
John.

09 Jul 2012

Thanks for the info. That did cure the dropped character.

I still can't get the second line to display - although when I had the datalines incorrectly wired it was corrupt on both rows !

I spent the evening re-working your code to try and get a better idea of what's required, I hope you approve.

All I did really was turn it into a C++ class rather than C style functions.

http://mbed.org/users/lawless/programs/ST7066U_demo/mcwbeq

11 Jul 2012

Hi Matt,
I ran your code on my board and everything works fine.
If you are only getting the top line then I suspect you are running in 3v3 mode instead of 5v0. Please make sure you have 5v on p02 of the display (see my earlier post with the correct voltages and warnings - you could damage the mbed and/or display if it is not connected correctly). I am taking 5v from the mbed p39 (USB OUT). I suspect you have connected mbed p40 (3v3 regulated out) to the display.
Regards.
John.

31 Jul 2012

hehe I only got notification of your reply today from mbed !

Looks like that's the problem. The meter says 4.7v across the mbed 5v supply.

I put 6v into Vin in the MBED and have two lines !!

Thank you so much. How can I buy you a virtual pint ?

19 Aug 2012

Hi Matt,

You must have a dodgy PC if it is only supplying 4v7 on the USB cable (unless you are somehow drawing too much current - check the voltage with no backlight on the display and also with it disconnected). A bit disappointing that the display does not work with 4v7. I would have expected it to work from 4v5 to 5v5 (in the 5v mode). It would be interesting to know at what minimum voltage the display is guaranteed to work in 5v mode.

Not sure how you can buy me a virtual pint. You could "like" my YouTube video if you want (and/or connect on LinkedIn).

Cheers. John.

Please log in to post comments.