Sure, here is an explanation of each line of code:
#include "STM32F10x.h"
This line includes the header file for the STM32F10x series microcontrollers.
//#include <stm32f103x6.h>
This line is commented out, meaning it is not being used in this program.
#define SampleNumber 100
This line declares and defines a constant named SampleNumber with a value of 100.
// INIT functions ------------------------
void GPIOInit(void);
void gpioIntInit(void);
void spi1Init(void);
void timer2Init(void);
void GenaralSramSetting(void);
These lines are function prototypes for the initialization functions for various components of the microcontroller.
// TRANSMITE DATA functions ---------------
uint16_t RamReader(void);
void RamWriter(uint16_t d);
void DAC(uint16_t d);
uint16_t ADC (void);
These lines are function prototypes for functions related to transmitting and receiving data from various components of the microcontroller.
//GLOBAL data -----------------------------
uint16_t Counter=0;
uint16_t address=0x0000;
uint8_t flag1=1;
These lines are declarations of global variables.
//Main function -------------------------------
int main(){
GPIOInit();
gpioIntInit();
spi1Init();
timer2Init();
GenaralSramSetting();
while(1){
}
}
This is the main function of the program. It initializes various components of the microcontroller and then enters an infinite loop.
//GPIO AND GENERAL SETTING-------------------------------------------------
void GPIOInit(void){
RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN ;
RCC->APB2RSTR |= RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST;
RCC->APB2RSTR &= ~(RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST);
GPIOA->CRL = 0xa4a42224 ; //a5, a7 : spi1, a1, a2, a3 : output (NCS)
GPIOA->ODR |= 0x00e ;
GPIOB->CRL = 0x22224444 ; //b1: push button
GPIOB->CRH = 0x22222222 ;
}
This function initializes the GPIO pins of the microcontroller.
//---------------------------------------------------------------
void gpioIntInit(void){
RCC->APB2ENR |= 0x00000001 ;
AFIO->EXTICR[0] =0x0010; //interrupt from port B pin 1
EXTI->IMR = (1<<1); //PB1 unmasked
EXTI->FTSR = (1<<1); //falling edge interrupt
EXTI->PR = (1<<1); //clear pending interrupts
NVIC_EnableIRQ(EXTI1_IRQn);//This line enables the interrupt request for EXTI1_IRQn.
//EXTI1 because interrupt is received through pin 1 of portB
}
This function initializes the external interrupt that is generated by the push button connected to PB1.
//------------------------------------------------------------------
void timer2Init(void){
RCC->APB1RSTR |= RCC_APB1RSTR_TIM2RST;
RCC->APB1RSTR &= ~(RCC_APB1RSTR_TIM2RST);
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = (40-1); //prescaler
TIM2->ARR = (200-1); //auto-reload
TIM2->DIER = 0x0001; // update interrupt enable
TIM2->CR1 = 0x0001; //enable timer
NVIC_EnableIRQ(TIM2_IRQn);
}
This function initializes and enables Timer 2 of the microcontroller with a prescaler of 40 and an auto-reload value of 200.
//---------------------------------------------------------------------
uint16_t ADC(void){
uint16_t a;
GPIOA->ODR &= ~(0x0002);
SPI1->DR = 0x06;
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR = 0x40;
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
a=((SPI1->DR & 0x000f)<<8);
SPI1->DR = 0x00;
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
a |= (SPI1->DR & 0x00ff);
GPIOA->ODR |= (0x0002);
return(a);
}
This function reads an analog value from the ADC (analog to digital converter) of the microcontroller, using SPI communication.
//----------------------------------------------------------------------
void DAC(uint16_t d){
uint16_t s;
s = (1<<12) | (d&0x0fff);
GPIOA->ODR &= ~(0x0008);
SPI1->DR = ((s>>8)&0xff);
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR =(s&0xff);
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR;
GPIOA->ODR |= (0x0008);
}
This function writes an analog value to the DAC (digital to analog converter) of the microcontroller, using SPI communication.
//---------------------------------------------------------------------
uint16_t RamReader(void){
uint16_t r;
GPIOB->ODR &= ~(0x0010);
SPI1->DR = 0x03; //READ instruction
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR;
SPI1->DR = ((address>>8) & 0xff); //Address(first 8 bits)
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR;
SPI1->DR = (address & 0xff); //Address(last 8 bits)
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR;
SPI1->DR = 0x00;
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
r = (SPI1->DR)<<4;
SPI1->DR = 0x00;
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
r |= (SPI1->DR)>>4;
GPIOB->ODR |= (0x0010);
address = address + 2;
if(address>=2*SampleNumber){
address = 0;
}
return(r);
}
This function reads data from an external SRAM (static RAM) connected to the microcontroller, using SPI communication.
//-------------------------------------------------------------------------------
void RamWriter(uint16_t d){
GPIOB->ODR &= ~(0x0010);
SPI1->DR = 0x02; //WRITE instruction
while((SPI1->SR & SPI_SR_BSY) == SPI_SR_BSY);
SPI1->DR;
SPI1->DR = ((address>>