The Godfather talking
You think I am funny guy huh?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 19, 2024, 09:30:15 09:30


Login with username, password and session length


Pages: [1]
Print
Author Topic: STM32 ADC+2x16 LCD Keil Code  (Read 10239 times)
0 Members and 1 Guest are viewing this topic.
demure
Newbie
*
Offline Offline

Posts: 19

Thank You
-Given: 5
-Receive: 19


« on: March 25, 2013, 02:57:55 14:57 »

Code:
#include "system_stm32f4xx.c"
 #include <stdio.h>
#include "main.h"

#define DATA_PORT GPIOC->ODR  // PC6,PC7,PC8,PC9
#define CMD_PORT GPIOB->ODR

#define DATA 6
#define DATA_CLR 0xFFFFFC3F

#define LCD_E  0x100         // PB8
#define LCD_RS 0x200         // PB9

#define CMD 0
#define TXT 1

#define        LINE1    0x80
#define        LINE2    0xC0
#define ADC_CDR_ADDRESS    ((uint32_t)0x40012308)
__IO uint32_t ADC1ConvertedValue;
__IO uint32_t ADC1ConvertedVoltage = 0;
char adc_data[16];

/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef  ADC_InitStructure;
ADC_InitTypeDef       ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef       DMA_InitStructure;


static __IO uint32_t TimingDelay;
unsigned int i=0;



//--------------------------------------------------------------------------------//
void WaitLCDBusy(void){
    Delay(2);
}

//--------------------------------------------------------------------------------//
void LCD_KONTROL(unsigned char veri,unsigned char type){
    DATA_PORT &= DATA_CLR;    //LCD_PORT &= 0xF0;
    DATA_PORT |= (veri<<DATA); //LCD_PORT |= (veri & 0x0F);

    if(type == CMD){
        CMD_PORT &= ~(LCD_RS);
    } else {
        CMD_PORT |= LCD_RS;
    }

    CMD_PORT |= LCD_E;    //LCD_E = 1;
    Delay(1);
    CMD_PORT &= ~(LCD_E); //LCD_E = 0;
}

//--------------------------------------------------------------------------------//
void LCD_DATA(unsigned char data,unsigned char type){

    WaitLCDBusy();

    if(type == CMD){
        CMD_PORT &= ~(LCD_RS);
    } else {
        CMD_PORT |= LCD_RS;
    }

    LCD_KONTROL(data>>4,type);
    LCD_KONTROL(data,type);
}

//--------------------------------------------------------------------------------//
void LCD_STR(const char *text){
    while(*text){
        LCD_DATA(*text++,1);
    }
}


//--------------------------------------------------------------------------------//
void LCD_LINE(char line){
    switch(line){
        case 0:
        case 1:
            LCD_DATA(LINE1,0);
            break;
        case 2:
            LCD_DATA(LINE2,0);
            break;
    }
}

void lcd_init()
{

    CMD_PORT &= ~(LCD_E);  //LCD_E = 0;
    CMD_PORT &= ~(LCD_RS); //LCD_RS = 0;

    Delay(30);                //delay for LCD to initialise.
    LCD_KONTROL(0x30,0);          //Required for initialisation
    Delay(5);                 //required delay
    LCD_KONTROL(0x30,0);          //Required for initialisation
    Delay(1);                 //required delay
    LCD_DATA(0x02,0);         //set to 4 bit interface, 1 line and 5*7 font
    LCD_DATA(0x28,0);         //set to 4 bit interface, 2 line and 5*10 font
    LCD_DATA(0x0c,0);         //set to 4 bit interface, 2 line and 5*7 font
    LCD_DATA(0x01,0);         //clear display
    LCD_DATA(0x06,0);         //move cursor right after write
}

void STM32F4Discovery_init(void)
{
      
  /* Enable peripheral clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2 |RCC_AHB1Periph_GPIOA |  RCC_AHB1Periph_GPIOB, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 |RCC_APB2Periph_ADC3, ENABLE);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 3;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;        
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);

  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA2_Stream0, ENABLE);

/******************************************************************************/
/*  ADCs configuration: triple interleaved with 5cycles delay to reach 6Msps  */
/******************************************************************************/

  /* ADC Common configuration *************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_Interl;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;  
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 regular channel 12 configuration ************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
  
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);

 
/* ADC2 regular channel 12 configuration ************************************/
  ADC_Init(ADC2, &ADC_InitStructure);
  /* ADC2 regular channel12 configuration */
  ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);

  /* ADC3 regular channel 12 configuration ************************************/
  ADC_Init(ADC3, &ADC_InitStructure);
  
  /* ADC3 regular channel12 configuration *************************************/
  ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);

  /* Enable DMA request after last transfer (multi-ADC mode) ******************/
  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);

  /* Enable ADC1 **************************************************************/
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC2 **************************************************************/
  ADC_Cmd(ADC2, ENABLE);

  /* Enable ADC3 **************************************************************/
  ADC_Cmd(ADC3, ENABLE);
    
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConv(ADC1);
  
  /* Initialize Leds mounted on STM32F4-Discovery board */
ADC1ConvertedValue=ADC_GetConversionValue(ADC1);

}



int main(void)
{

  if (SysTick_Config(SystemCoreClock / 1000))
  {
    while (1);
  }
  
  STM32F4Discovery_init();
  lcd_init();
  LCD_DATA(0x01,0);
  LCD_LINE(1);
  LCD_STR((const char*)"Hello");
  LCD_LINE(2);
  LCD_STR((const char*)"MARCH 2013");  
  Delay(2000);

  while (1)
  {

ADC_SoftwareStartConv(ADC1);
ADC1ConvertedValue=ADC_GetConversionValue(ADC1);
ADC1ConvertedVoltage = ADC1ConvertedValue *3300/0xFFF;


    LCD_LINE(2);
    sprintf(adc_data,"  ADC=  %4d",ADC1ConvertedVoltage);
    LCD_STR((const char*)adc_data);
  




  }
}



void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}


void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}


« Last Edit: March 25, 2013, 03:49:46 15:49 by demure » Logged
Mano
Inactive

Offline Offline

Posts: 1

Thank You
-Given: 1
-Receive: 0


« Reply #1 on: December 28, 2015, 10:15:14 22:15 »

Some explanation for beginner, please. Thank you.   Huh
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC