文章目录
一个简单的sin波形发生器控制频率、幅度一个简单的sin波形发生器
代码:
#include <msp430.h>#define uchar unsigned char#define CPU_F ((double)1000000)//cpu frequency1000000#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))#define CS_SET P2OUT |= BIT6#define CS_CLR P2OUT &= ~BIT6 //P26 CS#define WR_SET P2OUT |= BIT7#define WR_CLR P2OUT &= ~BIT7//P27 WR#define DI P1OUT //DIconst uchar sin_a[52] = {128, 143, 159, 174, 188, 201, 213, 224, 234, 242, 248, 252, 255, 255, 254, 251, 246, 239, 230, 220, 209, 196, 182, 168, 153, 137, 121, 106, 91, 76, 62, 49, 37, 27, 18, 11, 5, 1, 0, 0, 2, 6, 12, 19, 28, 39, 51, 64, 78, 93, 108, 124 };void write_dac(uchar data) //dac写数据函数{CS_CLR;DI = data;WR_CLR;delay_us(1);WR_SET; //latch dataCS_SET;}void sin()//正弦波发生函数{uchar i;for (i = 0; i < 52; i++){write_dac(sin_a[i]);}}void main(void){WDTCTL = WDTPW + WDTHOLD;// Stop WDTP1DIR = 0xff;//0832的数据位P2DIR |= BIT6 + BIT7;P2SEL &= ~(BIT6 + BIT7);for (;;){sin();}}
仿真:
控制频率、幅度
频率依靠定时器可以较为精准控制,但幅度只能加一点减一点。
稍微修改后,按键调节频率和幅度,产生正弦、矩形、三角波:
仿真图和程序源码:/2/28880a10b9990.pg3
如果觉得《【单片机】MSP430单片机 波形发生器 Proteus仿真》对你有帮助,请点赞、收藏,并留下你的观点哦!