0% found this document useful (0 votes)
65 views3 pages

Uart PWM

The document defines functions for configuring GPIOs, timers, and USART communication on an STM32 microcontroller. It initializes GPIO pin D14 for output, configures timer 4 for PWM on pins D12 and D13, and configures USART2 to communicate at 9600 baud on pins A2 and A3. The main function calls the initialization functions and enters an infinite loop. It defines interrupt and callback functions to receive USART data, modify the PWM duty cycles based on the received character, and send the character back over USART.

Uploaded by

Sinh Dũng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views3 pages

Uart PWM

The document defines functions for configuring GPIOs, timers, and USART communication on an STM32 microcontroller. It initializes GPIO pin D14 for output, configures timer 4 for PWM on pins D12 and D13, and configures USART2 to communicate at 9600 baud on pins A2 and A3. The main function calls the initialization functions and enters an infinite loop. It defines interrupt and callback functions to receive USART data, modify the PWM duty cycles based on the received character, and send the character back over USART.

Uploaded by

Sinh Dũng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "stm32f4xx.

h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 255

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

char RXBuffer[BUFFERSIZE];
void GPIO_Configuration(void);
void TIM_PWM_Configuration(void);

void USART_Configuration(void);
void USART_Send_String(volatile char *String);
void USART2_IRQHandler(void);

void SendUSART(char* data);


char* GetUSART(void);

void delay(uint32_t time);

int main()
{
GPIO_Configuration();
TIM_PWM_Configuration();
USART_Configuration();
GPIO_SetBits(GPIOD,GPIO_Pin_14);
while(1)
{
}
}

void GPIO_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void TIM_PWM_Configuration(void)
{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;


GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);


GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);

TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_Period = 8400-1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 0;

TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}

void USART_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2,&USART_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
USART_Cmd(USART2,ENABLE);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);


NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

void USART2_IRQHandler(void)
{
char* data=GetUSART();
delay(100);
SendUSART(data);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}

void SendUSART(char* data)


{
for (int i=0; *(data+i) != NULL; i++)
{
USART_SendData(USART2, *(data+i));
if((*(data+i))=='1'){
TIM4->CCR1 = 80*8399/100;
TIM4->CCR2 = 10*8399/100;
}
else if((*(data+i))=='2'){
TIM4->CCR1 = 10*8399/100;
TIM4->CCR2 = 80*8399/100;
}
*(data+i) = NULL;
while (USART_GetFlagStatus(USART2, USART_SR_TXE)==RESET);
}
}
char* GetUSART(void)
{
uint16_t i = 0;
memset(RXBuffer, NULL, sizeof(RXBuffer));
for(i=0; i<BUFFERSIZE; i++){
while (USART_GetFlagStatus(USART2, USART_SR_RXNE)==RESET);
RXBuffer[i] = USART_ReceiveData(USART2);
if (RXBuffer[i] == 10 || RXBuffer[i]==13) break;
}
return RXBuffer;
}

void delay(uint32_t time)


{
while(time)
time--;
}

/*(khi chay code thi xoa khuc nay nha :))


Note:
PA2: TX
PA3: RX
PD12: PWM1
PD13: PWM2
PD14: Den bao hoat dong
*Khi gui co ki tu '1': quay ,'2' dao chieu quay
*/

You might also like