Firstly, I'n fairly new Keil, and am "upgrading from 8-bit PIC's"
I'm trying to read some data from an ADC.
Not working so I wrote a simple program that reads the input pin status.
To my surprise it doesn't seem to pay any attention to reading to pin status.
I have included the STM32F10xR.lib in the project. Compiles OK, and runs as in I can get it to toggle a couple of output LED's. Just nothing when I read a pin.
Compiler Keil UV4 HArdware Olimnexio (STM32F103RBT6)
#include <stm32f10x_lib.h> GPIO_InitTypeDef GPIO_InitStructure; u8 gpio_val; int main(void) { int i; /* Enable GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); /* Enable GPIOD clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD,&GPIO_InitStructure); //Make sure the LED's are working GPIOA->BSRR= 1<<1;//Set pin A.1 to 1 GPIOA->BSRR= 1<<5;//Set pin A.5 to 1 for(i=0;i<0x40000;i++) { } //loop forever for(;;) { for(i=0;i<0x40000;i++) { } //read the pin status and set LED's accordingly if(!GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)) { GPIOA->BRR= 1<<1;//Set pin A.1 to 0 GPIOA->BSRR= 1<<5;//Set pin A.5 to 1 } else { GPIOA->BSRR= 1<<1;//Set pin A.1 to 1 GPIOA->BRR= 1<<5;//Set pin A.5 to 0 } } }
Reuse of data structure:
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; <== here you request your LED pins to be output GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; *** *** where did the structure get cleared? will you not make GPIOD.4 an output too? *** GPIO_Init(GPIOD,&GPIO_InitStructure);
Are you able to show me how it should be done. I took this from an example from the Keillor website, so assumed it would be correct....I'm not sure I know what you mean by clearing the structure?
I don't use that library so I don't know the specifics of how it works.
But if you take a structure and assign three fields before initializing the first port.
You then reuse the same structure and assign only two fields before initializing the second port.
Isn't it likely then that the third field - that you didn't make any assign to - will still say GPIO_Mode_Out_PP, making the pin you expected to use as an input become an output?
You based your code on an example? But didn't you locate the reference for the library functionality? Especially after your program didn't work? Does the library reference claim that every call to GPIO_Init() will reset all fields of that structure to a neutral state? Because your code is written as if it did.
Never ever use code you find on the net without understanding every single line of it. And that would include locating the documentation for library functions called, unless you already know the specifics of the used library functions (like strcpy(), etc).
ST's examples for the STM32 Standard Peripheral Library? Or the Discovery boards?
problem sorted.
As perhaps hinted at I was miss-reading the hardware schematic - so what was labeled D0 was in fact portA bit3 not portD bit0!!