Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootcamp: Annika Logarta #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

annikalogarta
Copy link

No description provided.

htim1.Instance = TIM1;
htim1.Init.Prescaler = 14;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 64000;
Copy link

Choose a reason for hiding this comment

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

Correct PWM frequency!

Core/Src/main.c Outdated
uint16_t ADC_Value;
float PWM_DutyCycle; //
uint8_t PWM_DutyCycle_Count;
const uint16_t PERIOD = 0xfa00; // 64000, our period, in hexadecimal
Copy link

Choose a reason for hiding this comment

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

Honestly, I think it makes more sense to use decimal here. Sometimes I ask people to use hex to represent the data because those data are highly associated with bytes, using hexadecimal can increase the readability of the text. Here, I don't think using hex is offering any readability advantage comparing to others...

float PWM_DutyCycle; //
uint8_t PWM_DutyCycle_Count;
const uint16_t PERIOD = 0xfa00; // 64000, our period, in hexadecimal

/* USER CODE END 2 */
Copy link

Choose a reason for hiding this comment

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

You are missing a hal function call here that can ask the microcontroller to start the PWM wave as you configured in the ioc file. Unfortunately, if you don't call that function, no wave will be produced from that pin. I recommend searching about stm32 pwm and I will see the function I am talking about.

Core/Src/main.c Outdated
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // Pulling CS line high
Copy link

Choose a reason for hiding this comment

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

The CS pin is wrong here.


// Below is combining the necessary 10 bits of data we need into 1.
ADC_Value = RxData[1] << 8;
ADC_Value |= RxData[2];
Copy link

Choose a reason for hiding this comment

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

The bit-wise manipulations here are correct. But according to the datasheet, unexpected bits can occupy the first 6 bits in the RxData[1]', so I would recommend masking the ADC_Value` with a 10-bit mask to make sure only 10-bits are used from the rx_data.

// Convert to a counter
// ADC Value 0 means PWM is 5%, ADC value 1023 means PWM is 10%
PWM_DutyCycle = ADC_Value/(0x3ff);
PWM_DutyCycle_Count = (PERIOD*0.05*(1+PWM_DutyCycle));
Copy link

Choose a reason for hiding this comment

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

The math here is correct!

Copy link

@HardyYu HardyYu left a comment

Choose a reason for hiding this comment

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

Well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants