cancel
Showing results for 
Search instead for 
Did you mean: 

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

generic
Associate II
Posted on April 01, 2011 at 19:17

All:

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.

Regards,

-g

/*****************************************************************************

* @file    main.c

* @author  Gunn

* @version V3.1.0

* @date    03/21/2011

* @brief   Test I2C interface to LCD

****************************************************************************** 

*  

*/

/* Includes ------------------------------------------------------------------*/

&sharpinclude ''stm32f10x.h''

&sharpinclude ''STM32vldiscovery.h''

&sharpinclude ''stm32f10x_i2c.h''

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

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

//I2C LCD Defines

&sharpdefine LCDSlave 0x78

&sharpdefine LCDComSend 0x00

&sharpdefine 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

  I2C_StructInit(&I2C_InitStructure);

 

  // Enable I2C1 peripheral (alt example says do this first before init struct)

  I2C_Cmd(I2C1, ENABLE);

 

  */

  //Init Onboard LEDs output

  STM32vldiscovery_LEDInit(LED3);

  STM32vldiscovery_LEDInit(LED4);

  STM32vldiscovery_LEDOff(LED3); // turn LED off

  STM32vldiscovery_LEDOff(LED4); // turn LED off

 

  // LCD_Init();

  while (1){

    STM32vldiscovery_LEDOff(LED4);

   LCD_Show(''Hello World!'');

   Delay(0xAFFFF);

    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,LCDSlave);//LCDSlave=0x78

 I2C_SendData(I2C1,LCDComSend);//LCDComSend = 0x00

 I2C_SendData(I2C1,0x38);

 Delay(0xAFFFF); // I think this is 50ms

 I2C_SendData(I2C1,0x39);

 Delay(0xAFFFF); // I think this is 50ms

 I2C_SendData(I2C1,0x14);

 I2C_SendData(I2C1,0x78);

 I2C_SendData(I2C1,0x5E);

 I2C_SendData(I2C1,0x6D);

 I2C_SendData(I2C1,0x0C);

 I2C_SendData(I2C1,0x01);

 I2C_SendData(I2C1,0x06);

 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_SendData(I2C1,LCDDataSend);//Datasend=0x40

 for(n=0;n<20;n++){

  I2C_SendData(I2C1,*text);

  ++text;

  }

 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_SendData(I2C1,0xC0);

 I2C_GenerateSTOP(I2C1,ENABLE); // Alt example says no need to ever disable

 }

/*****************************************************/

#i2c-stm32vl-discovery-lcd
1 REPLY 1
Posted on April 01, 2011 at 20:41

/* Enable Clocks */

  RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // Enable I2C Clock

Should be

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // Enable I2C Clock

You don't need the ReMap, the I2C1 is the default AF function for PB6/7

You'll need to enable the GPIO B clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Enable GPIOB Clock

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..