First LCD basic demo on STM32F100C4

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

First LCD basic demo on STM32F100C4

#1 Post by Mince-n-Tatties » 31 May 2012 18:25

Hi All,

i have a small board (commercial product) which uses the STM32F100C4, it has LCD a couple of buttons, 2 uarts and a couple of LEDs. The actual product is coded in uVision but it has all the basics for simple testing so i thought lets try the mikroC ARM compiler out on it.

The LCD is in 4bit mode with R/W (this is simply defined and driven low to fit with mE LCD library), the main thing to take from this quick example is that PORTB PIN4 is a JTAG pin by default and so there are a couple of things that need to be done to use this pin as a gpio. Alternate function switch and alternate function clock needs to be turned on.

taking the LCD example supplied with the compiler and making a couple of minor changes...

Code: Select all

/*
 * Project name:
     Lcd_Custom_Test (Simple demonstration of the LCD Library functions)
 * Copyright:
     (c) mikroElektronika, 2009.
 * Revision History:
     20091030:
       - Initial release;
 * Description:
     This is a simple demonstration of LCD library functions. LCD is first
     initialized (PORB, 4-bit data interface, default pin settings), then some
     text is written at the first and the second row.
 * Test configuration:
     MCU:             STM32F100C4
     Oscillator:      HSI noPLL, 8.000MHz
     Ext. Modules:    ac:LCD
                       on portB
     SW:              mikroC PRO for ARM

 * NOTES:

     Using PORTB to drive LCD, MCU PIN PB4 is a JTAG pin be default so
     Alternate function needs to be enabled and APB2PeriphAFIO clock needs
     to be enabled.
*/

// LCD module connections
sbit LCD_RS at GPIOB_ODR.B2;
sbit LCD_EN at GPIOB_ODR.B0;
sbit LCD_D4 at GPIOB_ODR.B4;  // alternate pin function
sbit LCD_D5 at GPIOB_ODR.B5;
sbit LCD_D6 at GPIOB_ODR.B6;
sbit LCD_D7 at GPIOB_ODR.B7;

sbit LCDRW at GPIOB_ODR.B1;
// End LCD module connections

/*************************************************************************************************
* Init MCU function
**************************************************************************************************/
void InitMCU(){
  
  // setup LCD RW pin as output and drive low
  LCDRW = 0;
  GPIO_Config(&GPIOB_BASE, _GPIO_PINMASK_1, _GPIO_CFG_DIGITAL_OUTPUT);
}

char txt1[] = "mikroElektronika";
char txt2[] = "STM32F100C4";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(750);                     // You can change the moving speed here
}

void main(){
  
  InitMCU();
 
  AFIOEN_bit = 1;    //APB2 AFIO clock
  SWJ_CFG0_bit = 1;  // PB4 alternate function enable //AFIO_MAPR.B24 = 1;
  
  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
 

  Lcd_out(1,1,"Hello");

  Delay_ms(2000);
  
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Out(1,6,txt3);                 // Write text in first row

  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,4,txt2);                 // Write text in second row

  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop
    for(i=0; i<7; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<7; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
  }
}
Best Regards

Mince

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: First LCD basic demo on STM32F100C4

#2 Post by filip » 01 Jun 2012 09:19

Hi,

Thank you for sharing this code, I'm glad that you have managed to easily adapt the code and make it work. :)

Regards,
Filip.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: First LCD basic demo on STM32F100C4

#3 Post by Mince-n-Tatties » 01 Jun 2012 09:25

Hi Filip

MikroC ARM has completely changed the game. Anyone starting out in ARM has now had all the pain taken out for them.
Best Regards

Mince

Post Reply

Return to “mikroC PRO for ARM General”