Skip to content

Instantly share code, notes, and snippets.

@abdul-rehman-2050
Last active March 7, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdul-rehman-2050/a77ee65811bb7fd2f1173e5e565afc5a to your computer and use it in GitHub Desktop.
Save abdul-rehman-2050/a77ee65811bb7fd2f1173e5e565afc5a to your computer and use it in GitHub Desktop.
STM32F103RB Nucleo board STD Peripheral library examples
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
GPIO_InitTypeDef GPIO_InitStructure;
void delay(__IO unsigned long mval){
int i=0;
for(mval;mval>0;mval--){
for(i=0;i<5000;i++);
}
}
void mot1_fwd(){
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
delay(100);
}
void mot2_fwd(){
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOA,GPIO_Pin_9);
delay(100);
}
void mot2_stop(){
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOA,GPIO_Pin_9);
delay(100);
}
void mot1_stop(){
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
delay(100);
}
void stopAll(){
GPIO_ResetBits(GPIOA,GPIO_Pin_8 | GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_9);
delay(10);
}
void revAll(){
GPIO_ResetBits(GPIOA,GPIO_Pin_8 | GPIO_Pin_6 );
GPIO_SetBits(GPIOA,GPIO_Pin_9 | GPIO_Pin_7);
delay(100);
}
void fwdAll(){
GPIO_ResetBits(GPIOA,GPIO_Pin_9 | GPIO_Pin_7 );
GPIO_SetBits(GPIOA,GPIO_Pin_8 | GPIO_Pin_6);
delay(100);
}
int mot_flag=0;
int main(){
/* GPIOA Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PA5 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7| GPIO_Pin_8 |GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*On Board User Button@ PC13 as Input*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*IR Sensors attached to A0,A1,A2,A3 PINS*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
stopAll();
while(1){
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==Bit_RESET){
GPIOA->ODR ^= GPIO_Pin_5;
delay(2000);
if(mot_flag==1){
mot_flag=2;
stopAll();
}else if(mot_flag==2){
mot_flag=3;
revAll();
}else if(mot_flag==3){
mot_flag=0;
stopAll();
}else {
mot_flag=1;
fwdAll();
}
}
}
}
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
GPIO_InitTypeDef GPIO_InitStructure;
void delay(__IO unsigned long mval){
int i=0;
for(mval;mval>0;mval--){
for(i=0;i<5000;i++);
}
}
int main(){
/* GPIOA Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PA5 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*On Board User Button@ PC13 as Input*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
while(1){
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==Bit_RESET){
GPIOA->ODR ^= GPIO_Pin_5;
delay(2000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment