当前位置:
文档之家› STM32_DAC 实验(正弦波_方波_锯齿波_发生器)
STM32_DAC 实验(正弦波_方波_锯齿波_发生器)
1//sine
正弦波
2//rectangular 矩形波
3//hackle 锯齿波
UINT8 flag = 0; UINT8 func = SINE_WAVE;
/*---------------------------------------------------------------------------MAIN function *----------------------------------------------------------------------------*/
Set_IO_direction(PORT_B,SW2,INPUT); Set_IO_direction(PORT_B,SW3,INPUT); Set_IO_direction(PORT_E,SW4,INPUT); Set_IO_direction(PORT_E,SW5,INPUT);
Turn_OFF_LED1(); Turn_OFF_LED2(); Turn_OFF_LED3(); Turn_OFF_LED4(); }
#endif break;
default: break; } //------------------------------------------------------------} // end while } // end main
/*----------------------------------------------------------------------------
GPIOD->CRH |= (Value << (GPIO * 4 - 8 * 4));//Set used bit } }
if(PORT == PORT_E)
{
if(GPIO < 8)
{
GPIOE->CRL &= ~(0x0f << (GPIO * 4));
//Clr used bit
GPIOE->CRL |= (Value << (GPIO * 4));
// STM32F10x Library Definitions // STM32 register and bit Definitions
// STM32 Initialization
// 输出端口 : PA4 #define SINE_WAVE #define RECT_WAVE #define HACKLE_WAVE
}
else if(!Get_SW4())
{
func = RECT_WAVE;
}
else if(!Get_SW3())
{
func = HACKLE_WAVE;
}
switch(func) { case SINE_WAVE:
//------------------------------------------------------------#ifdef SINE_WAVE
//Set used bit
}
else
{
GPIOE->CRH &= ~(0x0f << (GPIO * 4 - 8 * 4));//Clr used bit
GPIOE->CRH |= (Value << (GPIO * 4 - 8 * 4));//Set used bit
}
}
}
/*---------------------------------------------------------------------------SendChar Write character to Serial Port. *----------------------------------------------------------------------------*/
int GetKey (void) {
while (!(USART2->SR & USART_FLAG_RXNE));
return ((int)(USART2->DR & 0x1FF)); }
void LED_Init(void) {
Set_IO_direction(PORT_E,LED1,OUTPUT); Set_IO_direction(PORT_E,LED2,OUTPUT); Set_IO_direction(PORT_E,LED3,OUTPUT); Set_IO_direction(PORT_E,LED4,OUTPUT);
//设置 DAC 控制参数,请参考 STM32_Ref_Manul.pdf 第 260 页,里面有对该寄存器中每一 位的介绍
*(pDAC_BASE + 0x00) = (0x01 << 0)| (0x00 << 2) | (0x04 << 3) | (0x03 << 6) | (0x0b << 8);
*----------------------------------------------------------------------------*/
#include <stdio.h> #include <stm32f10x_lib.h> #include "STM32_Reg.h" #include "STM32_Init.h" #include "common.h" #include "sine_wave_1024.h"
//Set used bit
}
else
{
GPIOC->CRH &= ~(0x0f << (GPIO * 4 - 8 * 4));//Clr used bit
GPIOC->CRH |= (Value << (GPIO * 4 - 8 * 4));//Set used bit
}
}
if(PORT == PORT_D)
{
if(GPIO < 8)
{
GPIOD->CRL &= ~(0x0f << (GPIO * 4));
//Clr used bit
GPIOD->CRL |= (Value << (GPIO * 4));
//Set used bቤተ መጻሕፍቲ ባይዱt
}
else
{
GPIOD->CRH &= ~(0x0f << (GPIO * 4 - 8 * 4));//Clr used bit
/*---------------------------------------------------------------------------insert a delay time. *----------------------------------------------------------------------------*/
* 设置方向
*----------------------------------------------------------------------------*/
void Set_IO_direction(UINT8 PORT, UINT8 GPIO, UINT8 Value)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
void delay(unsigned int nCount) {
for(; nCount != 0; nCount--); }
} }
if(PORT == PORT_B)
{
if(GPIO < 8)
{
GPIOB->CRL &= ~(0x0f << (GPIO * 4));
//Clr used bit
GPIOB->CRL |= (Value << (GPIO * 4));
//Set used bit
}
else
{
GPIOB->CRH &= ~(0x0f << (GPIO * 4 - 8 * 4));//Clr used bit
if(PORT == PORT_A)
{
if(GPIO < 8)
{
GPIOA->CRL &= ~(0x0f << (GPIO * 4));
//Clr used bit
GPIOA->CRL |= (Value << (GPIO * 4));
//Set used bit
}
else
{ GPIOA->CRH &= ~(0x0f << (GPIO * 4 - 8 * 4));//Clr used bit GPIOA->CRH |= (Value << (GPIO * 4 - 8 * 4));//Set used bit
*(pDAC_BASE + 0x04) = 0x01;
printf(" --------------- Program start --------------- \r\n");
while (TRUE)
// Loop forever
{
if(!Get_SW5())
{
func = SINE_WAVE;
int SendChar (int ch) {
while (!(USART2->SR & USART_FLAG_TXE)); USART2->DR = (ch & 0x1FF);