STM32 ADC+2x16 LCD Keil Code.
STM32 ADC+2x16 LCD Keil Code.
c"
#include <stdio.h>
#include "main.h"
#define DATA 6
#define DATA_CLR 0xFFFFFC3F
#define CMD 0
#define TXT 1
/* Private variables
---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
//----------------------------------------------------------------------------
----//
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;
}
//----------------------------------------------------------------------------
----//
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()
{
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_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);
/*****************************************************************************
*/
/* ADCs configuration: triple interleaved with 5cycles delay to reach 6Msps
*/
/*****************************************************************************
*/
/* ADC2 regular channel 12 configuration ************************************/
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC2 regular channel12 configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
int main(void)
{
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);
}
}
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}