#include <stm32f10x.h>
void delay(void) { uint32_t i; for (i = 0; i < 100000; i++); }
int main(void) { uint8_t data = 0; uint8_t dir = 0; RCC->APB1ENR = RCC_APB1ENR_DACEN; RCC->APB2ENR = RCC_APB2ENR_IOPAEN; // PA4默认就是模拟输出模式(Analog), 所以无需配置GPIOA->CRL // PA4接电压表的正极, GND接电压表的负极 DAC->CR |= DAC_CR_EN1; while (1) { if (dir == 0 && data == 0xff) { dir = 1; data--; } else if (dir == 1 && data == 0x00) { dir = 0; data++; } else { if (dir == 0) data++; else data--; } DAC->DHR8R1 = data; delay(); } }
|