AnsweredAssumed Answered

STM32VL-Discovery and NewHaven LCD - request for help with I2C initialization code...

Question asked by gunn on Apr 1, 2011
Latest reply on Apr 1, 2011 by Clive One
I'm trying to get my STM32Discovery board to talk to a NewHaven LCD display/controller C0220BIZ-FS(RGB)-FBW-3VM via I2C and have encountered some problems. I managed to piece together the following code from various examples found online but I don't know if I have the initialization strings right. Put simply, I don't know what I'm doing and the code doesn't work. I double checked my wiring with the NewHaven support folks and I believe I have that built correctly. Howver, without a logic analyzer, I'm not sure if the board is even outputting I2C.

Q: Would one of you folks mind taking a look over this program to see if I've gotten the I2C interface initialized correctly or not? It would be nice to have a "known" good I2C example that I could readily adapt to my needs.

Thanks in advance for your replies.

* @file    main.c
* @author  Gunn
* @version V3.1.0
* @date    03/21/2011
* @brief   Test I2C interface to LCD

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "STM32vldiscovery.h"
#include "stm32f10x_i2c.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

//I2C LCD Defines
#define LCDSlave 0x78
#define LCDComSend 0x00
#define LCDDataSend 0x40

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructureOutput; // Structure for Output Definitions
GPIO_InitTypeDef GPIO_InitStructureI2C; // Structure for I2C Definitions

I2C_InitTypeDef I2C_InitStructure; // Structure for I2C

/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount);
void LCD_Init(void);
void LCD_Show(unsigned char *text);
void LCD_NextLine(void);
/* Private functions ---------------------------------------------------------*/

int main(void) {
  /* Enable Clocks */
  RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // Enable I2C Clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // Enable GPIOC Clock

  /* Init Outputs */ 
  GPIO_InitStructureOutput.GPIO_Pin = LED3_PIN | LED4_PIN;
  GPIO_InitStructureOutput.GPIO_Mode = GPIO_Mode_Out_PP; // Push Pull aka Correct Mode to output LED on wire
  GPIO_InitStructureOutput.GPIO_Speed = GPIO_Speed_50MHz; // Correct Speed to output LED on output wire
  GPIO_Init(GPIOC, &GPIO_InitStructureOutput); // Send Struct

  // I2C CONFIGURATION --------------------------------------------------------/
  // Configure all GPIOB in open drain AF mode (for different I2C levels)
  GPIO_InitStructureI2C.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructureI2C.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructureI2C.GPIO_Mode = GPIO_Mode_AF_OD; 
  // Initialise the GPIOB Peripheral
  GPIO_Init(GPIOB, &GPIO_InitStructureI2C);
  // Configure the GPIO ports as alternate functions (PB6,7) -- already initialized above?
  // GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);

  // I2C CONFIGURATION ---------------------------------------------------------/

  // Declare an I2C_InitTypeDef structure, with I2C_InitStructure variable
  // Fill the variable with allowed values of member structure
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;  //only effects fast mode
  I2C_InitStructure.I2C_OwnAddress1 = 0x03A2; // Alt example 0xA0
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = 100000; // Alt example 400000      
  // Initialise the I2C1 peripheral
  // Enable I2C1 peripheral (alt example says do this first before init struct)
  I2C_Cmd(I2C1, ENABLE);
  //Init Onboard LEDs output
  STM32vldiscovery_LEDOff(LED3); // turn LED off
  STM32vldiscovery_LEDOff(LED4); // turn LED off
  // LCD_Init();
  while (1){
   LCD_Show("Hello World!");
    STM32vldiscovery_LEDOn(LED4); // Blink LED to tell me its in the loop


  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
void Delay(__IO uint32_t nCount) {
  for(; nCount != 0; nCount--);

/****************************************************  n
* Initialization For ST7036i *

void LCD_Init(void) {
 I2C_GenerateSTART(I2C1,ENABLE); // Alt example says no need to ever disable
 I2C_SendData(I2C1,LCDComSend);//LCDComSend = 0x00
 Delay(0xAFFFF); // I think this is 50ms
 Delay(0xAFFFF); // I think this is 50ms
 Delay(0xAFFFF); // I think this is 50ms
 I2C_GenerateSTOP(I2C1,ENABLE); // Alt example says no need to ever disable

* Send string of ASCII data to LCD *
void LCD_Show(unsigned char *text) {
 int n;
 //int d=0x00;
 I2C_GenerateSTART(I2C1,ENABLE); // Alt example says no need to ever disable
 I2C_SendData(I2C1,LCDSlave); //LCDSlave=0x78
 I2C_GenerateSTOP(I2C1,ENABLE); // Alt example says no need to ever disable
* Send a NextLine to LCD *
void LCD_NextLine(void) {
 I2C_GenerateSTART(I2C1,ENABLE); // Alt example says no need to ever disable
 I2C_SendData(I2C1,LCDSlave); //LCDSlave=0x78
 I2C_SendData(I2C1,LCDComSend);//LCDComSend = 0x00
 I2C_GenerateSTOP(I2C1,ENABLE); // Alt example says no need to ever disable