cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L, ADC, USART, INTERRUPT, PC COMMUNICATION

rutinitaspenting
Associate II
Posted on August 31, 2014 at 20:10

The original post was too long to process during our migration. Please click on the attachment to read the original post.
4 REPLIES 4
Posted on August 31, 2014 at 20:27

Yes, that's all rather a mess, with several concepts mixed up inappropriately. Don't use interrupts unless you're going to actually service them. If you're going to start each ADC acquisition do use continuous mode, or reconfigure the channel. The USART pins both need to be in AF mode, this is an L1 part not an F1.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 31, 2014 at 20:55

Some of the mess cleaned up...

#include ''stm32l1xx.h''
#include <
stdio.h
>
void RCC_Configuration(void)
{
// Realistically SystemInit() and CMSIS should have set this all up already
RCC_DeInit();
/* Enable the HSI */
RCC_HSICmd(ENABLE);
/* Wait until HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
}
char USART_getchar(void) // fungsi menerima data//
{
char ch;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART1);
return(ch);
}
void USART_putchar(uint8_t ch)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
void USART_puts(char *ch)
{
while(*ch)
USART_putchar(*ch++);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART parameters */
USART_InitStructure.USART_BaudRate = 115200;
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_Rx | USART_Mode_Tx;
/* Configuring and enabling USART1 */
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
// Why?
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//==Configure ADC pins (PC.0) as analog inputs==
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) and Rx (PA10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //Tx
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); //Rx
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
//==Configure ADC1 - Channel 10==
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 1 Channel
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // On Demand
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1; //We are using one channel
ADC_Init(ADC1, &ADC_InitStructure); //Initialise ADC1
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_384Cycles); // PC0 ADC_IN10
ADC_Cmd(ADC1, ENABLE); //Enable ADC1
/* Wait until the ADC1 is ready */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);
}
uint8_t readADC1(void)
{
// Start the conversion
ADC_SoftwareStartConv(ADC1);
// Wait until conversion completion
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
// Get the 8-bit conversion value
return((uint8_t)ADC_GetConversionValue(ADC1));
}
/*
* Main program start
*/
int samples[200]; // ADC DIGITAL OUTPUT SAMPLE
int main(void)
{
char word[16]; // ADC DIGITAL OUTPUT FOR TRANSMITING PURPOSE
int m;
char k; // CHAR FOR TO TELL STM32
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
USART_Configuration();
while(1)
{
k = USART_getchar(); //receive char '2' from PC to start
if (k == '2')
{
k = USART_getchar(); // receive char 's' from PC to start take sample ADC
if (k=='s')
{
// ''2s'' command
for(m=0;m<200;m++)
samples[m] = readADC1(); // ****** Taking Samples ******* //
/*
Send 200 Samples using Protocol ''f'' at the begining of the line
And Ending with ''
'' indicating New Line.
*/
USART_putchar('f'); //send char to tell PC that start to convert The 'String' to 'Double'
for(m=0; m<200; m++)
{
sprintf(word, ''%d
'', samples[m]); //send the ADC output data
USART_puts(word);
}
}
}
}
}//end of main

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rutinitaspenting
Associate II
Posted on August 31, 2014 at 21:23

wow .. Thank you Mr. Clive...:) 

sorry for the previous mistake, I have put the PIN configuration pins B6 and B7 only blinking LED output. 

I will make a PC oscilloscope for my final project. 

I want to make some features such as triggering from the internal analog comparator. the output of the comparator will provide 

''interrupt'' the ADC to stop working or ''frezee''. and of course, the ADC requires an interrupt. is that rigth? 

And back to the issue USART interrupt, 

I will put some protocols on pc for triggering features, such as changing the triggering threshold which will affect the input (+) for analog comparator. So, on the main loop MCU will receive more than one protocol from a PC, rigth? USART is still no need to use interrupts?

rutinitaspenting
Associate II
Posted on August 31, 2014 at 21:32

For triggering feature I meant was, is like the one on this site... http://www.instructables.com/id/Girino-Fast-Arduino-Oscilloscope/