AnsweredAssumed Answered

Problem including a variable

Question asked by delos_reyes.kristian on Apr 8, 2013
Latest reply on Apr 9, 2013 by Andrew Neil
/**
  ******************************************************************************
  * @file    SKY TUBE IndePendent Controller Program - Main.c
  * @author  Bryan and Xtian
  * @version V0.0.1
  * @date    3/10/2013
  * @brief   Main program body
  ******************************************************************************
*/
 
#include "includes.h"
/*#include "stm32_eval_sdio_sd.h"*/
#define DLY_100US  20000000
 
#define BLOCK_SIZE            512 /* Block Size in Bytes */
 
#define NUMBER_OF_BLOCKS      100  /* For Multi Blocks operation (Read/Write) */
#define MULTI_BUFFER_SIZE    (BLOCK_SIZE * NUMBER_OF_BLOCKS)
 
#define SD_OPERATION_ERASE          0
#define SD_OPERATION_BLOCK          1
#define SD_OPERATION_MULTI_BLOCK    2
#define SD_OPERATION_END            3
 
/**
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
 
 
uint8_t Buffer_Block_Tx[BLOCK_SIZE], Buffer_Block_Rx[BLOCK_SIZE];
uint8_t Buffer_MultiBlock_Tx[MULTI_BUFFER_SIZE], Buffer_MultiBlock_Rx[MULTI_BUFFER_SIZE];
volatile TestStatus EraseStatus = FAILED, TransferStatus1 = FAILED, TransferStatus2 = FAILED;
SD_Error Status = SD_OK;
__IO uint32_t SDCardOperation = SD_OPERATION_ERASE;
 
 
void NVIC_Configuration(void);
void SD_EraseTest(void);
void SD_SingleBlockTest(void);
void SD_MultiBlockTest(void);
void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength);
 
 
*/
 
void InitializeInputs(void);
void InitializeOutputs(void);
void InitializeClocks(void);
void SendData(uint8_t line);
void DataClear(void);
void Delay(__IO uint32_t nCount);
void SendDataCmd(uint8_t cmd1, uint8_t cmd2, uint8_t cmd3);
void SendValues (uint8_t line, char data[20]);
 
 
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
uint8_t datas[20];
 
//////////////////////////////////////////////////////////////////////////
/////////PIN ALLOCATION SUMMARY///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
 
/*
Inputs Pins Allocated:
Port D 0-7
1:3-10
Port D 8-15
2:11-18
 
 
Output Pins Allocated
PORT E 8-15
2:11-18
 
PORT E 0-7
1:3-10
 
 
USART ALLOCATED
PB6
Con3->D1
*/
 
///////////////////////////////////////////////////////////////////////////////
/////////////////Variable Declaration//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
 
 
uint8_t hsec1 = 65;
uint8_t hsec10 = 0;
uint8_t fsec1 = 0;
uint8_t fsec10 = 0;
uint8_t fmin1 = 0;
uint8_t fmin10 = 0;
uint8_t dsec1 = 0;
uint8_t dsec10 = 0;
uint8_t dmin1 = 0;
uint8_t dmin10 = 0;
uint8_t dbsec1 = 0;
uint8_t dbsec10 = 0;
uint8_t dbmin1 = 0;
uint8_t dbmin10 = 0;
uint8_t open = 5;
uint8_t turbo = 0;
uint8_t fan2 = 0;
uint8_t defrost = 0;
uint16_t timer = 0;
uint8_t defdown = 0;
uint8_t defup = 0;
 
 
char line0 [20] = "ICE KING ENTERPRISES";
char line1 [20] = "                    ";
char line2 [20] = "      CLEANING      ";
char line3 [20] = "                    ";
 
uint8_t pinOut = 0x0000; //0000 0000 0000 0000
 
int main(void)
{
   
 
   
  InitializeClocks();
  InitializeOutputs();
  InitializeInputs();
  DataClear();
  
 
  GPIOC->BSRRH = GPIO_Pin_13;//HIGH
   Delay(0XFFFFF);
  GPIOC->BSRRL = GPIO_Pin_13;//LOW
   
  GPIOE ->ODR = 0xFF00;
  Delay(0XFFFFF);
   GPIOE ->ODR = 0x0000;
 
  while(open > 0)
  {
    GPIOC->BSRRH = GPIO_Pin_13;//HIGH
    SendValues (0, "ICE KING ENTERPRISES"); //SendValues (Line number, 20 Character values);
    SendValues (1, " version 2.31fjo3-t "); //SendValues (Line number, 20 Character values);
    SendValues (2, "--------------------"); //SendValues (Line number, 20 Character values);
    SendValues (3, "       WELCOME    "); //SendValues (Line number, 20 Character values);
    Delay(0XFFFFFF);
    char* xd = (char*)hsec1;
    SendValues (3, (char)xd + " ");
    Delay(0XFFFFFF);
    open--;
    GPIOC->BSRRL = GPIO_Pin_13;//LOW
    Delay(0XFFFFFF);
  }
   
  while(1) //loopstart
  {
    SendValues (0, "ICE KING ENTERPRISES"); //SendValues (Line number, 20 Character values);
    SendValues (1, "                    "); //SendValues (Line number, 20 Character values);
    SendValues (2, "   PRESS START TO"); //SendValues (Line number, 20 Character values);
    SendValues (3, "      ACTIVATE"); //SendValues (Line number, 20 Character values);
     
    if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3)==1)
    {
 
      while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==0)
      {
        GPIOE ->ODR = 0x0800; //0000 1000 0000 0000
        SendValues (0, "ICE KING ENTERPRISES"); //SendValues (Line number, 20 Character values);
        SendValues (1, "                    "); //SendValues (Line number, 20 Character values);
        SendValues (2, "      CLEANING     "); //SendValues (Line number, 20 Character values);
        SendValues (3, "                   "); //SendValues (Line number, 20 Character values);
        Delay(0XFFFFFF);
        SendValues (3, "                  ");
        Delay(0XFFFFFF);
      }
      GPIOE ->ODR = 0x0000;
    }
     
    if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1)//ndi pwede pin2 gamit ng sd
    {
      if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_12)==1)
      {
        GPIOE ->ODR = 0x5400; //0101 0100 0000 0000 = 84dec 54hex
      }
      if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_12)==0)
      {
        GPIOE ->ODR = 0x1400; //0001 0100 0000 0000 = 20dec 14hex
      }
 
      while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1)
      {
        SendValues (0, "ICE KING ENTERPRISES"); //SendValues (Line number, 20 Character values);
        SendValues (1, "                    "); //SendValues (Line number, 20 Character values);
        SendValues (2, "   MANUAL HARVEST   "); //SendValues (Line number, 20 Character values);
        SendValues (3, "                   "); //SendValues (Line number, 20 Character values);
        Delay(0XFFFFFF);
        SendValues (3, "                  ");
        Delay(0XFFFFFF);
      }
      GPIOE ->ODR = 0x0000;
    }
     
    if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==1)
    {
        timer = 0;
    turbo = 1;
    defrost = 0;
    fan2 = 0;
    hsec1 = 0;
    hsec10 = 3;
    open = 1;
    fsec1 = 0;
    fsec10 = 0;
    fmin1 = 0;
    fmin10 = 0;
    dsec1 = 0;
    dsec10 = dbsec10;
    dmin1 = dbmin1;
    dmin10 = dbmin10;
 
        GPIOE ->ODR = 0x0000;
         
        SendValues (0, "ICE KING ENTERPRISES"); //SendValues (Line number, 20 Character values);
        SendValues (1, "                    "); //SendValues (Line number, 20 Character values);
        SendValues (2, "   PRESS START TO"); //SendValues (Line number, 20 Character values);
        SendValues (3, "      ACTIVATE"); //SendValues (Line number, 20 Character values);
        Delay(0XFFFFFF);
    }
  }
   
   
   
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////FUNCTIONS AND INITS  ////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
 void SendValues (uint8_t line, char data[20])
 {
    DataClear();
    if (line == 0)
    {
      SendDataCmd(17,0,0);
      for (int ct = 0; ct<20; ct++)
      {
        line0[ct] = data[ct];
        datas[ct] = line0[ct];
      }
      SendData(0); //sends data from datas
    }
     
    if (line == 1)
    {
      SendDataCmd(17,0,1);
      for (int ct = 0; ct<20; ct++)
      {
        line1[ct]=data[ct];
        datas[ct] = line1[ct];
      }
      SendData(1); //sends data from datas
    }
     
    if (line == 2)
    {
      SendDataCmd(17,0,2);
      for (int ct = 0; ct<20; ct++)
      {
        line2[ct]=data[ct];
        datas[ct] = line2[ct];
      }
      SendData(2); //sends data from datas
    }
 
    if (line == 3)
    {
      SendDataCmd(17,0,3);
      for (int ct = 0; ct<20; ct++)
      {
        line3[ct]=data[ct];
        datas[ct] = line3[ct];
      }
      SendData(3); //sends data from datas
    }
 }
 void SendData(uint8_t line)
 {
   for (int ctr1 = 0; ctr1<20; ctr1++)
   {
      USART_SendData(USART1, datas[ctr1]);
      Delay(0xFFA);
      SendDataCmd(17,ctr1+1,line);
   }
 }
 void SendDataCmd(uint8_t cmd1, uint8_t cmd2, uint8_t cmd3)
 {
 
    USART_SendData(USART1, cmd1);
    Delay(0xFF01);
    USART_SendData(USART1, cmd2);
    Delay(0xFF01);
    USART_SendData(USART1, cmd3);
    Delay(0xFF01);
 
 }
 void DataClear(void)
 {
   for (int ctr1 = 0; ctr1<20; ctr1++)
   {
      datas[ctr1] = 0;
      line0[ctr1] = 0;
      line1[ctr1] = 0;
      line2[ctr1] = 0;
      line3[ctr1] = 0;
   }
 }
void InitializeInputs(void)
{
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}
 
void InitializeOutputs(void)
 {
  //Initialize output Indicators
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
    
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
  
   //SERIAL PIN TX
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
    
   //SERIAL USART1 INITIALIZATION
   USART_InitStructure.USART_BaudRate = 9600;
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
   USART_InitStructure.USART_StopBits = USART_StopBits_1;
   USART_InitStructure.USART_Parity = USART_Parity_No ;
   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
   USART_InitStructure.USART_Mode = USART_Mode_Tx;
   USART_Init(USART1, &USART_InitStructure);
   USART_Cmd(USART1, ENABLE);
    
   //remaping
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
    
 }
  
 
  
void InitializeClocks(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
   
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
   
   
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); //enable spi2 clock
}
 
void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}
 
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  /* Configure the NVIC Preemption Priority Bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
 
  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
/*
void SD_EraseTest(void)
{
  
  if (Status == SD_OK)
  {
 
    Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
  }
 
  if (Status == SD_OK)
  {
    Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
 
    Status = SD_WaitReadOperation();
 
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }
 
  if (Status == SD_OK)
  {
    EraseStatus = eBuffercmp(Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
  }
   
  if(EraseStatus == PASSED)
  {
    //STM_EVAL_LEDOn(LED1);
  }
  else
  {
    //STM_EVAL_LEDOff(LED1);
    //STM_EVAL_LEDOn(LED4);   
  }
}
 
void SD_SingleBlockTest(void)
{
 
  Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0x320F);
  Buffer_Block_Tx[0] = 10;
  Buffer_Block_Tx[1] = 11;
  Buffer_Block_Tx[2] = 12;
  Buffer_Block_Tx[3] = 13;
  Buffer_Block_Tx[4] = 14;
   
   
 
  if (Status == SD_OK)
  {
 
    Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE);
 
    Status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }
 
  if (Status == SD_OK)
  {
 
    Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE);
 
    Status = SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }
 
 
  if (Status == SD_OK)
  {
    TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE);
  }
   
  if(TransferStatus1 == PASSED)
  {
    //STM_EVAL_LEDOn(LED2);
  }
  else
  {
    //STM_EVAL_LEDOff(LED2);
    //STM_EVAL_LEDOn(LED4);   
  }
}
 
 
void SD_MultiBlockTest(void)
{
 
  Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);
 
  if (Status == SD_OK)
  {
 
    Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
 
    Status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }
 
  if (Status == SD_OK)
  {
    Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
    Status = SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }
 
  if (Status == SD_OK)
  {
    TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
  }
   
  if(TransferStatus2 == PASSED)
  {
    //STM_EVAL_LEDOn(LED3);
  }
  else
  {
   // STM_EVAL_LEDOff(LED3);
    //STM_EVAL_LEDOn(LED4);   
  }
}
 
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
{
  while (BufferLength--)
  {
    if (*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }
 
    pBuffer1++;
    pBuffer2++;
  }
 
  return PASSED;
}
 
void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset)
{
  uint16_t index = 0;
 
  for (index = 0; index < BufferLength; index++)
  {
    pBuffer[index] = index + Offset;
  }
}
TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength)
{
  while (BufferLength--)
  {
    if ((*pBuffer != 0xFF) && (*pBuffer != 0x00))
    {
      return FAILED;
    }
 
    pBuffer++;
  }
 
  return PASSED;
}
*/
#ifdef  USE_FULL_ASSERT
 
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif
 I am trying to send something like this -

  SendValues (3, "                  " + hsec1);

problem:
something weird is happening with the data array. is is now containing many weird values.
instead of   "                  " + hsec1 -< this data appearing in my array

data[0] = 20;
data[1] = 20;
data[xx]=65;

weird characters are showing in my array.

description:
I am using a stm32e407 by olimex and I am communicating to a LCD(Crystalfontz) by serial communication.

Outcomes