看你调了好几天,没调出来,也不是很忍心,看你也是老用户了,发你个寄存器版的程序吧,切记将系统时钟改为 24MHz !!!
#include "stm32f10x.h"
void DAC_Initial(unsigned char channel) //channel = 1,2
{
RCC->APB1ENR |= 1 << 29; //RCC DAC
RCC->APB2ENR |= 1 << 2; //RCC PA
if(channel == 1)
{
GPIOA->CRL &= 0xfff0ffff;
//default:DAC channel1 DMA mode disabled,Unmask bit0 of LFSR/ Triangle Amplitude equal to 1,
//00: wave generation disabled,DAC channel1 output buffer enabled,
DAC->CR |= 7 << 3; //Software trigger
DAC->CR |= 1 << 2; //DAC channel1 trigger enable
DAC->CR |= 1 << 0; //DAC channel1 enable
}
if(channel == 2)
{
GPIOA->CRL &= 0xff0fffff;
//default:DAC channel2 DMA mode disabled,Unmask bit0 of LFSR/ Triangle Amplitude equal to 1,
//00: wave generation disabled,DAC channel2 output buffer enabled,
DAC->CR |= 7 << 19; //Software trigger
DAC->CR |= 1 << 18; //DAC channel2 trigger enable
DAC->CR |= 1 << 16; //DAC channel2 enable
}
}
void DAC_Software_Trigger(unsigned char channel) //channel = 1,2
{
//This bit is reset by hardware (one APB1 clock cycle later) once the DAC_DHRx registervalue is loaded to the DAC_DORx register.
if(channel == 1)
DAC->SWTRIGR |= 1 << 0;
if(channel == 2)
DAC->SWTRIGR |= 1 << 1;
}
void DAC_Output(unsigned char channel,unsigned char lenth,unsigned short data) //channel = 1,2;lenth = 8,12(bits);右对齐;data为输出数据
{
if(channel == 1)
{
if(lenth == 8)
{
DAC->DHR8R1 = data;
}
if(lenth == 12)
{
DAC->DHR12R1 = data;
}
}
if(channel == 2)
{
if(lenth == 8)
{
DAC->DHR8R2 = data;
}
if(lenth == 12)
{
DAC->DHR12R2 = data;
}
}
DAC_Software_Trigger(channel);
}
void Soft_Delay(uint32_t t)
{
while(t --);
}
int main(void)
{
uint8_t i = 0;
DAC_Initial(1);
DAC_Initial(2);
DAC_Output(1,12,64);
DAC_Output(2,12,500);
while(1)
{
for(i = 0;i < 200;i ++)
{
DAC_Output(2,12,300 + i);
Soft_Delay(2000000);
}
}
while(1);
}
|