Код: Выделить всё
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
char uart2_rx_buf[128];
uint8_t uart2_rx_bit;
void send_to_uart(uint8_t data)
{
while(!(USART2->SR & USART_SR_TC));
USART2->DR=data;
}
//
void send_str(char * string)
{
uint8_t i=0;
while(string[i])
{
send_to_uart(string[i]);
i++;
}
}
//USART2
void usart_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); //PA3 k TX USART2
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // PA2 k RX USART2
//TX UART
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RX UART
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_StructInit(&USART_InitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
int my_atoi(char a[]) {
int c, sign, offset, n;
if (a[0] == '-') { // Handle negative integers
sign = -1;
}
if (sign == -1) { // Set starting position to convert
offset = 1;
}
else {
offset = 0;
}
n = 0;
for (c = offset; a[c] != '\0'; c++) {
n = n * 10 + a[c] - '0';
}
if (sign == -1) {
n = -n;
}
return n;
}
typedef enum {
TM_DAC1,
TM_DAC2
} TM_DAC_Channel_t;
/**
* Initialize DAC channel and it's pin
*
* - Parameters:
* - TM_DAC_Channel_t DACx:
* - TM_DAC1, TM_DAC2
*/
extern void TM_DAC_Init(TM_DAC_Channel_t DACx);
/**
* Set analog value to ADCx
*
* - Parameters:
* - TM_DAC_Channel_t DACx:
* - TM_DAC1, TM_DAC2
* - uint16_t value
* 12Bit unsigned value for 12bit DAC value
*/
extern void TM_DAC_SetValue(TM_DAC_Channel_t DACx, uint16_t value);
void TM_DAC_Init(TM_DAC_Channel_t DACx) {
GPIO_InitTypeDef GPIO_InitDef;
DAC_InitTypeDef DAC_InitDef;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
if (DACx == TM_DAC1) {
GPIO_InitDef.GPIO_Pin = GPIO_Pin_4;
} else {
GPIO_InitDef.GPIO_Pin = GPIO_Pin_5;
}
GPIO_InitDef.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitDef);
DAC_InitDef.DAC_Trigger = DAC_Trigger_None;
DAC_InitDef.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitDef.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
if (DACx == TM_DAC1) {
DAC_Init(DAC_Channel_1, &DAC_InitDef);
DAC_Cmd(DAC_Channel_1, ENABLE);
} else {
DAC_Init(DAC_Channel_2, &DAC_InitDef);
DAC_Cmd(DAC_Channel_2, ENABLE);
}
}
void TM_DAC_SetValue(TM_DAC_Channel_t DACx, uint16_t value) {
if (value > 4095) {
value = 4095;
}
if (DACx == TM_DAC1) {
DAC_SetChannel1Data(DAC_Align_12b_R, value);
} else {
DAC_SetChannel2Data(DAC_Align_12b_R, value);
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOD, &GPIO_InitStructure);
usart_init();
__enable_irq();
NVIC_EnableIRQ(USART2_IRQn);
NVIC_SetPriority(USART2_IRQn, 0);
USART2->CR1 |= USART_CR1_RXNEIE;
while(1)
{
}
}
void USART2_IRQHandler (void)
{
int device_num = 0;
//char digits[8] = "700";
char uart_data;
if (USART2->SR & USART_SR_RXNE)
{
USART2->DR = USART2->DR;
uart_data=USART2->DR;
device_num = my_atoi(uart2_rx_buf);
// device_num = my_atoi(digits);
uart2_rx_buf[uart2_rx_bit]=USART2->DR;
uart2_rx_bit++;
USART2->DR=device_num;
SystemInit();
//Initialize DAC channel 1, pin PA4
TM_DAC_Init(TM_DAC1);
//Set 12bit analog value of 600/4096 * 3.3V
TM_DAC_SetValue(TM_DAC1, device_num);
}
}