Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions nucleof072rb/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ void SystemClock_Config(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
const uint8_t BUFFER_SIZE = 3;
const uint8_t TIMEOUT = 100;
const uint16_t PULSE_MIN = 2824;
const uint16_t PULSE_MAX = 5647;

uint8_t tx_buffer[BUFFER_SIZE] = {0x01, 0x80, 0x00};
uint8_t rx_buffer[BUFFER_SIZE] = {};
uint16_t pwm = 0;

uint16_t adc_input = 0; // 10 bits


/* USER CODE END 0 */

Expand Down Expand Up @@ -89,13 +100,31 @@ int main(void)
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET); // Chip Select

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);

HAL_SPI_TransmitReceive(&hspi1, tx_buffer, rx_buffer, BUFFER_SIZE, TIMEOUT);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function call is correct, but it'd be useful to know whether it was successful or not! The function's return value being exposed to the developer would be helpful in debugging as good practice!


HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);


adc_input = ((uint16_t)(rx_buffer[1] & 0x3) << 8 )| rx_buffer[2];
pwm = PULSE_MIN + ((adc_input* (PULSE_MAX - PULSE_MIN)) / 1023);

__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm);


HAL_Delay(10);
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
}
Expand Down
52 changes: 45 additions & 7 deletions nucleof072rb/nucleof072rb.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,37 @@ CAD.formats=
CAD.pinconfig=
CAD.provider=
File.Version=6
GPIO.groupedBy=Group By Peripherals
KeepUserPlacement=false
Mcu.CPN=STM32F072RBT6
Mcu.Family=STM32F0
Mcu.IP0=NVIC
Mcu.IP1=RCC
Mcu.IP2=SYS
Mcu.IP3=USART2
Mcu.IPNb=4
Mcu.IP2=SPI1
Mcu.IP3=SYS
Mcu.IP4=TIM1
Mcu.IP5=USART2
Mcu.IPNb=6
Mcu.Name=STM32F072R(8-B)Tx
Mcu.Package=LQFP64
Mcu.Pin0=PC13
Mcu.Pin1=PC14-OSC32_IN
Mcu.Pin10=VP_SYS_VS_Systick
Mcu.Pin10=PA8
Mcu.Pin11=PA13
Mcu.Pin12=PA14
Mcu.Pin13=PB3
Mcu.Pin14=PB8
Mcu.Pin15=VP_SYS_VS_Systick
Mcu.Pin16=VP_TIM1_VS_ClockSourceINT
Mcu.Pin2=PC15-OSC32_OUT
Mcu.Pin3=PF0-OSC_IN
Mcu.Pin4=PF1-OSC_OUT
Mcu.Pin5=PA2
Mcu.Pin6=PA3
Mcu.Pin7=PA5
Mcu.Pin8=PA13
Mcu.Pin9=PA14
Mcu.PinsNb=11
Mcu.Pin8=PA6
Mcu.Pin9=PA7
Mcu.PinsNb=17
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32F072RBTx
Expand Down Expand Up @@ -69,6 +78,19 @@ PA5.GPIO_PuPd=GPIO_NOPULL
PA5.GPIO_Speed=GPIO_SPEED_FREQ_LOW
PA5.Locked=true
PA5.Signal=GPIO_Output
PA6.Locked=true
PA6.Mode=Full_Duplex_Master
PA6.Signal=SPI1_MISO
PA7.Locked=true
PA7.Mode=Full_Duplex_Master
PA7.Signal=SPI1_MOSI
PA8.Locked=true
PA8.Signal=S_TIM1_CH1
PB3.Locked=true
PB3.Mode=Full_Duplex_Master
PB3.Signal=SPI1_SCK
PB8.Locked=true
PB8.Signal=I2C1_SCL
PC13.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI
PC13.GPIO_Label=B1 [Blue PushButton]
PC13.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
Expand All @@ -88,6 +110,7 @@ PF1-OSC_OUT.Signal=RCC_OSC_OUT
PinOutPanel.RotationAngle=0
ProjectManager.AskForMigrate=true
ProjectManager.BackupPrevious=false
ProjectManager.CompilerLinker=GCC
ProjectManager.CompilerOptimize=6
ProjectManager.ComputerToolchain=false
ProjectManager.CoupleFile=true
Expand Down Expand Up @@ -141,11 +164,26 @@ RCC.USART1Freq_Value=48000000
RCC.USART2Freq_Value=48000000
SH.GPXTI13.0=GPIO_EXTI13
SH.GPXTI13.ConfNb=1
SH.S_TIM1_CH1.0=TIM1_CH1,PWM Generation1 CH1
SH.S_TIM1_CH1.ConfNb=1
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_16
SPI1.CalculateBaudRate=3.0 MBits/s
SPI1.DataSize=SPI_DATASIZE_8BIT
SPI1.Direction=SPI_DIRECTION_2LINES
SPI1.IPParameters=VirtualType,Mode,Direction,BaudRatePrescaler,CalculateBaudRate,DataSize
SPI1.Mode=SPI_MODE_MASTER
SPI1.VirtualType=VM_MASTER
TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
TIM1.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Period
TIM1.Period=56470
TIM1.Prescaler=16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get your prescaler to be lower for greater accuracy!

USART2.IPParameters=VirtualMode,VirtualMode-Asynchronous
USART2.VirtualMode=VM_ASYNC
USART2.VirtualMode-Asynchronous=VM_ASYNC
VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
VP_TIM1_VS_ClockSourceINT.Mode=Internal
VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT
board=NUCLEO-F072RB
boardIOC=true
isbadioc=false