文件名:DAC_10k
功能:使用msp430f169内部DAC模块通过DMA模块的传输
产生10kHz的正玄信号,本程序在一个周期内取点
为100个
*****************************************************************/
#include
static int Sin_tab[100] = {
1638,1740,1843,1944,2045,2143,2240,2335,2426,2515,2600,2681,2758,2831,
2899,2962,3020,3072,3119,3160,3195,3224,3246,3262,3272,3272,3263,3247,
3224,3196,3161,3120,3074,3021,2964,2901,2833,2760,2683,2602,2517,2429,
2337,2243,2146,2047,1947,1845,1743,1640,1537,1435,1333,1233,1134,1037,
943,851,762,677,596,519,446,378,314,256,204,157,116,81,52,29,13,3,0,2,
12,28,50,78,113,154,200,252,310,373,440,513,590,671,756,756,844,936,
1030,1127,1225,1326,1427,1529};
void INIT_XT2();
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // 停看门狗
INIT_XT2();
P5SEL=0XFF;
ADC12CTL0 = REFON; // 参考电压为内部2.5v
DMA0SA = (int) Sin_tab; // 源地址寄存器
DMA0DA = DAC12_0DAT_; // 目的地址寄存器
DMA0SZ = 100; // 传输基本单元的个数
DMACTL0 = DMA0TSEL_5; // DAC12_0CTL的DAC12IFG标志
DMA0CTL = DMADT_4 + DMASRCINCR_3 + DMAEN; // DMADT_4:重复的块传输方式 Rpt, inc src, word-word
DAC12_0CTL = DAC12LSEL_2 + DAC12IR + DAC12AMP_5 + DAC12IFG + DAC12ENC; // Config
// **force first interrupt**
CCTL1 = OUTMOD_3; // Set/reset
CCR1 = 1; // PWM Duty Cycle
CCR0 = 8-1; // ~1kHz Clock period
TACTL = TASSEL_2 + MC_1; // SMCLK, up-mode
_BIS_SR(LPM0_bits); // Enter LPM0
}
void INIT_XT2()
{
unsigned char i;
BCSCTL1&=~XT2OFF;//打开XT2振荡器
BCSCTL2|=SELM_2+SELS;//SELM_2:MCLK的时钟源选XT2 SELS:选择SMCLK的时钟源为XT2
do
{
IFG1&=~OFIFG;//清除震荡错误标志
for(i=0;i<100;i++);
_NOP();
}
while((IFG1&OFIFG)!=0);
IFG1&=~OFIFG;
}
======================================================================================
/******************************************************************************
* 文件: main.c
* 说明: MSP430F5438开发板 ADC12 转换实验
* 设置断点观察 voltage 中的电压值
* 改变可变电阻器 R25 观察 voltage 中的电压值的变化
* 编译: IAR Embedded Workbench IDE for MSP430 v5.10
******************************************************************************/
#include
#include "GPIO.h"
#include "BoardConfig.h"
#include "Clock.h"
/******************************************************************************
*宏定义区
******************************************************************************/
#define Num_of_Results 8 //数据存储的最大值
/******************************************************************************
*函数声名区
******************************************************************************/
void ADC_init(void);
float ADC_opera(void);
/******************************************************************************
*变量定义区
******************************************************************************/
volatile unsigned int results[Num_of_Results]; //存储 ADC12 采样转换结果
static unsigned char index = 0;
float voltage; // 电压值运算结果存储
/*******************************系统主函数************************************/
int main( void )
{
WDTCTL=WDTPW+WDTHOLD;
BoardConfig(0xEF); // 板上资源配置
Init_CLK(); // 初始化系统时钟
WDTCTL = WDT_ADLY_1_9;
SFRIE1 |= WDTIE; // WDT 中断使能
ADC_init(); // ADC12转换初始化
_EINT(); // 开全局中断
while(1)
{
if(index==Num_of_Results-1)
voltage=ADC_opera();
_NOP();
LPM1;
}
}
/******************************************************************************
* 函数名: void ADC_init(void)
* 功 能: ADC12初始化
* 参 数: 无
* 返回值: 无
******************************************************************************/
void ADC_init(void)
{
P6SEL |= 0x01; // 打开通道1
ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC;
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_2;
ADC12IE = 0x01;
ADC12CTL0 |= ADC12ENC;
ADC12CTL0 |= ADC12SC;
}
/******************************************************************************
* 函数名: void ADC_opera(void)
* 功 能: 将ADC采样进来的数据转换成十进制数
* 参 数: 无
* 返回值: 无
******************************************************************************/
float ADC_opera(void)
{
unsigned long temp=0;
float voltage;
for(unsigned char i=0;i
temp +=results[i];
}
voltage=((temp/Num_of_Results)*3.3/4095);
return voltage;
}
/******************************************************************************
* 函数名: __interrupt void ADC12ISR (void)
* 功 能: ADC12 中断服务函数
* 参 数: 无
* 返回值: 无
******************************************************************************/
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 6: // ADC12IFG0
results[index] = ADC12MEM0; //
index++; //
if (index == 8)
{
index = 0;
}
default: break;
}
LPM1_EXIT;
}
-----------------------------------------------------------------------------------------------------------------
这张图片在我学习过程中帮了不少忙,程序看不懂了,可以来看看这些,或许会有些收获,本人是学集成电路设计的,觉得很有帮助。
下面贴些自己的测试程序,别小看这几行程序啊,可是我测试N次的出来的
#include "msp430x14x.h"
void InitADC12();
unsigned char wait;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
InitADC12();
return 0;
}
void InitADC12()
{
//_DINT();
ADC12CTL0 &= ~ENC; //只有在ENC复位的情况下才能对ADC12CTL0、1进行设置!!!这点很重要哦!!!
ADC12CTL0 = MSC + REFON + REF2_5V + SHT0_15 + SHT1_15;
/*MSC:多次采样转换位,只对序列采样或者多次多次转换有效。
0:采样定时器需要SHI信号的上升沿触发
1:首次需要采样定时器SHI信号触发,之后的转换在上次转换之后随即开始。*/
ADC12CTL1 |= SHP + CONSEQ_0;
/*SHP: 1:采样信号源自采样定时器 0:采样信号源自采样输入信号
CONSEQ: 0: 单通道单次转换 1:序列通道单词转换 2:单通道多次转换 3:序列通道多次转换
CSTARTADD_x(0~15):转换起始位置 */
ADC12MCTL0 = EOS + SREF_0 + INCH_7;
/* EOS:序列结束位
SREF: 参考电压选择
INCH:模拟输入通道选择 */
ADC12IE |= 0x0001;//开相应模拟通道中断,转换完毕后引发中断
_EINT();
//ADC12CTL0 |= ADC12ON + ENC; //修改!!!// 这样写是不对的
ADC12CTL0 |= ADC12ON;
ADC12CTL0 |= ENC;
wait = 0; //此处可定义变量,指示转换是否完成
ADC12CTL0 |= ENC+ADC12SC; //转换开始 若希望在其他时间开始转换 可将次句放在别处逼比必[page]
while(wait==0) ;
}
#pragma vector=ADC_VECTOR
__interrupt void ADC()
{int result[7];
wait=1;
result[0] = ADC12MEM0;
/*unsigned char q0;
int *pmem=ADC12MEM;
//转换结束的标志位置位
for(q0=0;q0<16;q0++)
{
AdMem[q0]= *pmem;
pmem++;
}*/
}
下面再来简要的介绍一下这个模块不易理解的地方
1、MSP430可以使用内部、外部的参考电压,内部1.5或2.5,外部0~3.3,可以通过寄存器设置,采样电压的输入范围最大为0~3.3即AVss~AVcc,不能检测负电压,如果需要检测负电压,可以使用运放,电压偏置成为正电压然后检测
2、使用外部AVss~AVcc作为参考电压不够稳定,但是精度高,使用内部电压较稳定。
3、程序中一定要打开全局中断_EINT();
ADC12
************************************************** ********** /
#定义__ MSP430_HAS_ADC12__ / *定义表明模块是* /
#定义ADC12CTL0_(0x01A0u)/ * ADC12控制0 * /
DEFW(ADC12CTL0,ADC12CTL0_)
#定义ADC12CTL1_(0x01A2u)/ * ADC12控制1 * /
DEFW(ADC12CTL1,ADC12CTL1_)
#定义ADC12IFG_(0x01A4u)/ * ADC12中断标志* /
DEFW(ADC12IFG,ADC12IFG_)
#定义ADC12IE_(0x01A6u)/ * ADC12中断使能* /
DEFW(ADC12IE,ADC12IE_)
#定义ADC12IV_(0x01A8u)/ * ADC12中断向量字* /
DEFW(ADC12IV,ADC12IV_)
#定义ADC12MEM_(0x0140u)/ * ADC12转换内存* /
的#ifndef __ IAR_SYSTEMS_ICC__
#定义ADC12MEM(ADC12MEM_)/ * ADC12转换存储器(汇编)* /
其他#
#定义ADC12MEM((INT *)ADC12MEM_)/ * ADC12转换存储器(C)* /
#ENDIF
#定义ADC12MEM0_(0x0140u)/ * ADC12转换内存0 * /
DEFW(ADC12MEM0,ADC12MEM0_)
#定义ADC12MEM1_(0x0142u)/ * ADC12转换内存1 * /
DEFW(ADC12MEM1,ADC12MEM1_)
#定义ADC12MEM2_(0x0144u)/ * ADC12转换内存2 * /
DEFW(ADC12MEM2,ADC12MEM2_)
#定义ADC12MEM3_(0x0146u)/ * ADC12转换记忆体3 * /
DEFW(ADC12MEM3,ADC12MEM3_)
#定义ADC12MEM4_(0x0148u)/ * ADC12转换内存4 * /
DEFW(ADC12MEM4,ADC12MEM4_)
#定义ADC12MEM5_(0x014Au)/ * ADC12转换记忆5 * /
DEFW(ADC12MEM5,ADC12MEM5_)
#定义ADC12MEM6_(0x014Cu)/ * ADC12转换存储器6 * /
DEFW(ADC12MEM6,ADC12MEM6_)
#定义ADC12MEM7_(0x014Eu)/ * ADC12转换记忆7 * /
DEFW(ADC12MEM7,ADC12MEM7_)
#定义ADC12MEM8_(0x0150u)/ * ADC12转换内存8 * /
DEFW(ADC12MEM8,ADC12MEM8_)
#定义ADC12MEM9_(0x0152u)/ * ADC12转换存储器9 * /
DEFW(ADC12MEM9,ADC12MEM9_)
#定义ADC12MEM10_(0x0154u)/ * ADC12转换记忆体10 * /
DEFW(ADC12MEM10,ADC12MEM10_)
#定义ADC12MEM11_(0x0156u)/ * ADC12转换记忆体11 * /
DEFW(ADC12MEM11,ADC12MEM11_)
#定义ADC12MEM12_(0x0158u)/ * ADC12转换记忆体12 * /
DEFW(ADC12MEM12,ADC12MEM12_)
#定义ADC12MEM13_(0x015Au)/ * ADC12转换记忆体13 * /
DEFW(ADC12MEM13,ADC12MEM13_)
#定义ADC12MEM14_(0x015Cu)/ * ADC12转换记忆体14 * /
DEFW(ADC12MEM14,ADC12MEM14_)
#定义ADC12MEM15_(0x015Eu)/ * ADC12转换记忆体15 * /
DEFW(ADC12MEM15,ADC12MEM15_)
#定义ADC12MCTL_(0x0080u)/ * ADC12内存控制* /
的#ifndef __ IAR_SYSTEMS_ICC__
#定义ADC12MCTL(ADC12MCTL_)/ * ADC12存储器控制(用于汇编)* /
其他#
#定义ADC12MCTL((char *)的ADC12MCTL_)/ * ADC12存储器控制(对于C)* /
#ENDIF
#定义ADC12MCTL0_(0x0080u)/ * ADC12内存控制0 * /
DEFC(ADC12MCTL0,ADC12MCTL0_)
#定义ADC12MCTL1_(0x0081u)/ * ADC12内存控制1 * /
DEFC(ADC12MCTL1,ADC12MCTL1_)
#定义ADC12MCTL2_(0x0082u)/ * ADC12内存控制2 * /
DEFC(ADC12MCTL2,ADC12MCTL2_)
#定义ADC12MCTL3_(0x0083u)/ * ADC12内存控制3 * /
DEFC(ADC12MCTL3,ADC12MCTL3_)
#定义ADC12MCTL4_(0x0084u)/ * ADC12内存控制4 * /
DEFC(ADC12MCTL4,ADC12MCTL4_)
#定义ADC12MCTL5_(0x0085u)/ * ADC12内存控制5 * /
DEFC(ADC12MCTL5,ADC12MCTL5_)
#定义ADC12MCTL6_(0x0086u)/ * ADC12内存控制6 * /
DEFC(ADC12MCTL6,ADC12MCTL6_)
#定义ADC12MCTL7_(0x0087u)/ * ADC12内存控制7 * /
DEFC(ADC12MCTL7,ADC12MCTL7_)
#定义ADC12MCTL8_(0x0088u)/ * ADC12内存控制8 * /
DEFC(ADC12MCTL8,ADC12MCTL8_)
#定义ADC12MCTL9_(0x0089u)/ * ADC12内存控制9 * /
DEFC(ADC12MCTL9,ADC12MCTL9_)
#定义ADC12MCTL10_(0x008Au)/ * ADC12内存控制10 * /
DEFC(ADC12MCTL10,ADC12MCTL10_)
#定义ADC12MCTL11_(0x008Bu)/ * ADC12内存控制11 * /
DEFC(ADC12MCTL11,ADC12MCTL11_)
#定义ADC12MCTL12_(0x008Cu)/ * ADC12内存控制12 * /
DEFC(ADC12MCTL12,ADC12MCTL12_)
#定义ADC12MCTL13_(0x008Du)/ * ADC12内存控制13 * /
DEFC(ADC12MCTL13,ADC12MCTL13_)
#定义ADC12MCTL14_(0x008Eu)/ * ADC12内存控制14 * /
DEFC(ADC12MCTL14,ADC12MCTL14_)
#定义ADC12MCTL15_(0x008Fu)/ * ADC12内存控制15 * /
DEFC(ADC12MCTL15,ADC12MCTL15_)
/ * ADC12CTL0 * /
#定义ADC12SC(0×001)/ * ADC12开始转换* /
#定义ENC(0x002)/ * ADC12启用转换* /
#定义ADC12TOVIE(量0x004)/ * ADC12定时器溢出中断使能* /
#定义ADC12OVIE(量0x008)/ * ADC12溢出中断使能* /
#定义ADC12ON(量0x010)/ * ADC12开/使能* /
#定义REFON对*(量0x020)/ * ADC12参考/
#定义REF2_5V(量0x040)/ * ADC12参考0:1.5 V / 1:2.5 V * /
#定义MSC(量0x080)/ * ADC12多SampleConversion * /
#定义SHT00(0x0100u)/ * ADC12采样保持0选择0 * /
#定义SHT01(0x0200u)/ * ADC12采样保持0选择1 * /
#定义SHT02(0x0400u)/ * ADC12采样保持0选择2 * /
#定义SHT03(0x0800u)/ * ADC12采样保持0选择3 * /
#定义SHT10(0x1000u)/ * ADC12采样保持0选择0 * /
#定义SHT11(0x2000u)/ * ADC12采样保持1选择1 * /
#定义SHT12(0x4000u)/ * ADC12采样保持2选择2 * /
#定义SHT13(0x8000u)/ * ADC12采样保持3选择3 * /
#定义MSH(量0x080)
#定义SHT0_0(0 * 0x100u)/ * ADC12采样保持0选择位:0 * /
#定义SHT0_1(1 * 0x100u)/ * ADC12采样保持0选择位:1 * /
#定义SHT0_2(2 * 0x100u)/ * ADC12采样保持0选择位:2 * /
#定义SHT0_3(3 * 0x100u)/ * ADC12采样保持0选择位:3 * /
#定义SHT0_4(4 * 0x100u)/ * ADC12采样保持0选择位:4 * /
#定义SHT0_5(5 * 0x100u)/ * ADC12采样保持0选择位:5 * /
#定义SHT0_6(6 * 0x100u)/ * ADC12采样保持0选择位:6 * /
#定义SHT0_7(7 * 0x100u)/ * ADC12采样保持0选择位:7 * /
#定义SHT0_8(8 * 0x100u)/ * ADC12采样保持0选择位:8 * /
#定义SHT0_9(9 * 0x100u)/ * ADC12采样保持0选择位:9 * /
#定义SHT0_10(10 * 0x100u)/ * ADC12采样保持0选择位:10 * /
#定义SHT0_11(11 * 0x100u)/ * ADC12采样保持0选择位:11 * /
#定义SHT0_12(12 * 0x100u)/ * ADC12采样保持0选择位:12 * /
#定义SHT0_13(13 * 0x100u)/ * ADC12采样保持0选择
上一篇:松翰单片机8p2501的功耗问题
下一篇:用链表实现的屏幕飘雪程序
推荐阅读最新更新时间:2024-03-16 14:02