experts.
I am new to STM32.
So I downloaded some sample code from github.
https://github.com/yohanes-erwin/stm32f103-keil
And I download the simple led blinking code of it to the STM32F103C6 developing board but it doesn't work at all.
The original program embedded on the chip had been erased successfully ( originally the led was always on and LCD screen shows some text, but now after I download the code, the led is off and screen off. So I think the downloading is successful.) but the code does not work.
When I download the original code to the chip, it works again. So I think the chip isn't broken.
I think it's because of compatibility of sample code and my chip. The sample code was written for STM32F103C8. But it was a very simple code. What is the reason?
Here is the code.
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
void delay(unsigned int nCount);
GPIO_InitTypeDef GPIO_InitStruct;
int main (void)
{
// Enable clock for GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Configure PA4 as push-pull output
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
while (1)
{
/* Toggle LED on PA0 */
// Reset bit will turn on LED (because the logic is interved)
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
delay(1000);
// Set bit will turn off LED (because the logic is interved)
GPIO_SetBits(GPIOA, GPIO_Pin_4);
delay(1000);
}
}
// Delay function
void delay(unsigned int nCount)
{
unsigned int i, j;
for (i = 0; i < nCount; i++)
for (j = 0; j < 0x2AFF; j++);
}
Related
I use a simple code in stm32 for SD card.
The code is written in "int main(void)" section. I am trying to configure a SD card so that as soon as the STM32F750vbt6 turn on, a file is created in the SD card and a text is written in the file.
when I turn the STM32F750vbt6 on, this does not happen and nothing is written in the SD card but when the code reaches to the "while" section (I have written an LED blink code inside the while section) and I reset the micro controller using RST pin of the STM32F750vbt6, the device works properly and a text is written in the sd card.
How can I fix this? Why is there a need for resetting the device for the code to work?
this is my code :
const char wtext[] = "hi world";
int main(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
MPU_Config();
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
if(retSD == 0)
{
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
{
if(f_open(&MyFile, "file.txt", FA_CREATE_ALWAYS | FA_WRITE) ==FR_OK)
{
f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten);
f_close(&MyFile);
}
}
}
FATFS_UnLinkDriver(SDPath);
while (1)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
HAL_Delay(2000);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
HAL_Delay(2000);
}
}
The problem was that the device did not work from the ports. The data and CMD ports must be connected as pull-up
So, I am trying to toggle a LED based on an interrupt from a button.
Ideally the when the button is pressed the LED should toggle i.e. switch on if its off and vice versa. But when I execute this code it toggles and returns to its original state.
Expected Result:
LED OFF » Button pressed » LED ON
Practical Result:
LED OFF » Button pressed » LED ON » LED OFF
I have added a delay for debouncing so bouncing is out of the picture. Also the GPIO's ODR is set in the ISR when the button is pressed so how is it getting cleared while exiting the ISR?
I would really appreciate your help! Thank you.
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/cm3/nvic.h>
#define LEDPIN (GPIO13)
static void exti_setup(void)
{
/* Enable GPIOA and AFIO clock. */
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_AFIO);
/* Enable EXTI0 interrupt. */
nvic_enable_irq(NVIC_EXTI15_10_IRQ);
/* Set GPIO12 (in GPIO port B) to input */
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT, GPIO12);
/* Configure the EXTI subsystem. */
exti_select_source(EXTI12,GPIOB);
exti_set_trigger(EXTI12, EXTI_TRIGGER_BOTH);
exti_enable_request(EXTI12);
}
static void gpio_setup(void)
{
/* Enable clock for GPIO port C */
rcc_periph_clock_enable(RCC_GPIOC);
/* Set LEDPIN (in GPIO port C) as opendrain output */
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, LEDPIN);
}
void delay(){
int i;
for (i = 0; i < 1000000; i++)
{
__asm__("nop");
}
}
void handler(){
delay();
gpio_toggle(GPIOC, GPIO13);
}
int main(void)
{
gpio_setup();
exti_setup();
while (1) {
__asm__("nop");
}
return 0;
}
void exti15_10_isr()
{
exti_reset_request(EXTI12);
handler();
}
Not open drain but push-pull
Butttons should not use EXTI as it makes debouncing more complicated, often floods the uC with interrupts, Use timer interrupt instead to read the key and debounce.
As #dev_eng rightly pointed out the issue was the interrupt being configured as both RISING/FALLING edge.
Configuring it with single EDGE that is either RISING or FALLING solved my problem.
I've been reading a lot online to figure what is not working in my code, but it seems like the ADC conversion in my code never starts... Not matter what I do, the EOC Flag always remain to 0 and I am currently out of ideas. Making the ADC work should be simple, but for some reason, I am completly unable to make it work.
I'd be very grateful if someone could show me where my mistakes seems to be. I am using the STM32L100CR-Discovery.
/* Standard includes. */
#include "stm32l1xx.h"
#include "stm32l1xx_rcc.h"
#include "stm32l1xx_gpio.h"
#include "stm32l1xx_exti.h"
#include "stm32l1xx_syscfg.h"
#include "stm32l1xx_spi.h"
#include "stm32l1xx_adc.h"
#include "stdio.h"
#include "misc.h"
void ADC_Initialization (void)
{
ADC_DeInit( ADC1);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_NbrOfConversion = 1;
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ScanConvMode = DISABLE;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_CommonInitTypeDef ADC_CommonInitStruct;
ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div1;
ADC_CommonInit(&ADC_CommonInitStruct);
ADC_Cmd(ADC1, ENABLE);
ADC_BankSelection(ADC1, ADC_Bank_A);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_4Cycles);
ADC_TempSensorVrefintCmd (ENABLE);
}
int Read_ADC(void)
{
ADC_SoftwareStartConv(ADC1);
int statut = ADC_GetSoftwareStartConvStatus(ADC1);
printf("%d", statut);
ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
int valeur = ADC_GetConversionValue(ADC1);
return valeur;
}
int main(void)
{
ADC_Initialization();
int ADC_Valeur = 0;
while(1)
{
ADC_Valeur = Read_ADC();
printf("%d", ADC_Valeur);
}
}
Thanks in advance!
In the end, I did manage to find my problem! If anyone ever happens to search for the solution here it is: First, I did mess up this line :
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
It should read :
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
But it is also necessary on the STM32L100C to activate the HSI oscillator clock, which is not done in the library. Adding this line somewhere in the initialization should work:
RCC -> CR |= (0x1U << (0U));
Im trying to configure clocks on STM32F4 Discovery for precise time measurement. I have this configuration:
int main(void)
{
NVIC_InitTypeDef nvici;
GPIO_InitTypeDef gpioi;
TIM_TimeBaseInitTypeDef timtbi;
SystemInit();
RCC_HSEConfig(RCC_HSE_ON);
RCC_PLLConfig(RCC_PLLCFGR_PLLSRC_HSE, 8, 320, 8, 8);
RCC_PLLCmd(ENABLE);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCK);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
nvici.NVIC_IRQChannel = TIM2_IRQn;
nvici.NVIC_IRQChannelPreemptionPriority = 0;
nvici.NVIC_IRQChannelSubPriority = 1;
nvici.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvici);
gpioi.GPIO_Pin = GPIO_Pin_15;
gpioi.GPIO_Mode = GPIO_Mode_OUT;
gpioi.GPIO_OType = GPIO_OType_PP;
gpioi.GPIO_Speed = GPIO_Speed_100MHz;
gpioi.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD,&gpioi);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
timtbi.TIM_Period = 20000000;
timtbi.TIM_Prescaler = 0;
timtbi.TIM_ClockDivision = 0;
timtbi.TIM_CounterMode = TIM_CounterMode_Up;
timtbi.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timtbi);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
GPIO_SetBits(GPIOD,GPIO_Pin_15);
while(1)
{
}
}
void TIM2_IRQHandler()
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
GPIO_ToggleBits(GPIOD,GPIO_Pin_15);
}
with this i should have TIM2 sourced with 20MHz clock, but it appears to have diffrent frequency (about 10-30% diffrent). This problem appears for all other PLL configurations i tried, but when i use HSE as SYSCLK directly it works just fine. Am i doing something wrong, or is it PLL that isn't reliable?
Can't say with 100% certainty whether that's the problem, but after enabling the HSE using RCC_HSEConfig(), you should call RCC_WaitForHSEStartUp() since it takes a while for the HSE to start oscillating, and check the return code to make sure the call was successful and the HSE actually initialized.
Also, if you're using the system_stm32f4xx.c file that comes with the Standard Peripheral Library, you can scrap your PLL initialization code and just use the code that's called by SystemInit(). There are a few #defines that control the PLL configurations, near the beginning of the file (#define PLL_M, #define PLL_N and so on; their purpose should be self-evident). I always initialize my clocks using the code there, and they're always precise to within the crystal's accuracy. Note that this code assumes a 25 MHz oscillator by using PLL_M equal to 25, so you should set it to 8 for use with the STM32F4DISCOVERY board -- exactly as you've already done in your code. I'm not suggesting this because I have any prejudices against your code, but the code there has been tested far and wide, and in my experience it can be trusted.
I'm using Eclipse and CDT to work with the mspgcc compiler, it compiles fine, but the code view highlights all my special function registers as unresolved.
I've made a C project where the compiler is "msp430-gcc -mmcu=msp430x2012", and that's set to look for includes in /usr/msp430/include/. I've set the linker to "msp430-gcc -mmcu=msp430x2012", and that's set to look fo libraries in /usr/msp430/lib/. I've set the assembler to "msp430-as". I've told eclipse it's making an elf and I've disabled automatic includes discovery to not find the i686 libraries on my linux box (stupid eclipse!).
Here's the code:
#include <msp430.h>
#include <signal.h> //for interrupts
#define RED 1
#define GREEN 64
#define S2VAL 8
void init(void);
int main(void) {
init(); //Setup Device
P1OUT = GREEN; //start with a green LED
_BIS_SR(LPM4_bits); //Go into Low power mode 4, main stops here
return(1); //never reached, surpresses compiler warning
}
interrupt (PORT1_VECTOR) S1ServiceRoutine(void) {
//we wake the MCU here
if (RED & P1IN) {
P1OUT = GREEN;
} else {
P1OUT = RED;
}
P1IFG = 0; //clear the interrupt flag or we immidiately go again
//we resume LPM4 here thanks to the RETI instruction
}
void init(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
/*Halt the watchdog timer
P1DIR = ~S2VAL; //Set LED pins as outputs and S2 as input
P1IES = S2VAL; //interrupt on High to Low
P1IE = S2VAL; //enable interrupt for S1 only
WRITE_SR(GIE); //enable maskable interrupts
}
All the variables defines in the mspgcc includes such as P1OUT and WDTCTL show up in the problems box as "not resolved", but remember it builds just fine. I've even tried explicitly including the header file for my chip (normally msp430-gcc does this via msp430.h and the -mmcu option).
I resolved this issue by explicitly including the msp430g2553.h file
#include <msp430g2553.h>
I resolved the issue by following the instructions here