⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fet140_adc12_06.c

📁 msp430的C语言示例 来自TI官方
💻 C
字号:
//******************************************************************************
//  MSP-FET430P140 Demo - ADC12, 重复序列转换
//
//  Description: This example shows how to perform a repeated sequence of
//  conversions using "repeat sequence-of-channels" mode.  AVcc is used for the
//  reference and repeated sequence of conversions is performed on Channels A0,
//  A1, A2, and A3. Each conversion result is stored in ADC12MEM0, ADC12MEM1,
//  ADC12MEM2, and ADC12MEM3 respectively. After each sequence, the 4 conversion
//  results are moved to A0results[], A1results[], A2results[], and A3results[].
//  Test by applying voltages to channels A0 - A3. Open a watch window in 
//  debugger and view the results. Set a breakpoint in the last line to see the 
//  array values change sequentially.
//
//  Note that a sequence has no restrictions on which channels are converted.
//  For example, a valid sequence could be A0, A3, A2, A4, A2, A1, A0, and A7.
//  See the MSP430x1xx User's Guide for instructions on using the ADC12.
//
//
//                 MSP430F149
//            -----------------
//           |                 |
//   Vin0 -->|P6.0/A0          |
//   Vin1 -->|P6.1/A1          |
//   Vin2 -->|P6.2/A2          |
//   Vin3 -->|P6.3/A3          |
//           |                 |
//
//
//  M. Mitchell
//  Texas Instruments Inc.
//  Feb 2005
//  Built with IAR Embedded Workbench Version: 3.21A
//******************************************************************************
//ADC12内核时钟源为ADC12OSC
#include  <msp430x14x.h>

#define   Num_of_Results   8			//定义常数
//定义全局变量数组,第个数据为16位.
static unsigned int A0results[Num_of_Results];  // A0结果数组
static unsigned int A1results[Num_of_Results];  // A1结果数组
static unsigned int A2results[Num_of_Results];  // A2结果数组
static unsigned int A3results[Num_of_Results];  // A3结果数组

void main(void)
{
  WDTCTL = WDTPW+WDTHOLD;                   // 停止看门狗
  P6SEL = 0x0F;                             // 打开A0-A3 A/D通道输入
  ADC12CTL0 = ADC12ON+MSC+SHT0_8;           // 开ADC12模块+采样信号由SHI仅首次触发
					    //+采集定时器分频系数n=64,
  // extend sampling time to avoid overflow of results
 
  ADC12CTL1 = SHP+CONSEQ_3;                 // 使用采样定时器输出作采集/转换信号SAMPCON
					    // 重复序列采样模式

  ADC12MCTL0 = INCH_0;                      // 参考电压ref+=AVcc, 输入通道选择为 A0
  ADC12MCTL1 = INCH_1;                      // 参考电压ref+=AVcc, 输入通道选择为 A1
  ADC12MCTL2 = INCH_2;                      // 参考电压ref+=AVcc, 输入通道选择为 A2
  ADC12MCTL3 = INCH_3+EOS;                  // 参考电压ref+=AVcc, 输入通道选择为 A3,+由此通道产生序列结束控制位
  ADC12IE = 0x08;                           // A3通道开中断ADC12IFG.3
  ADC12CTL0 |= ENC;                         // 允许转换
  ADC12CTL0 |= ADC12SC;                     // 启动转换
  _BIS_SR(LPM0_bits + GIE);                 // 进入LPM0模式, 开总中断允许
}

#pragma vector=ADC_VECTOR		    //ADC中断服务程序
__interrupt void ADC12ISR (void)
{
  static unsigned int index = 0;	    //中断服务程序中的静态变量

  A0results[index] = ADC12MEM0;             // 移动A0转换结果往全局数组,此操作的同时清除ADC12FIG.0
  A1results[index] = ADC12MEM1;             // 移动A1转换结果往全局数组,此操作的同时清除ADC12FIG.1
  A2results[index] = ADC12MEM2;             // 移动A2转换结果往全局数组,此操作的同时清除ADC12FIG.2
  A3results[index] = ADC12MEM3;             // 移动A3转换结果往全局数组,此操作的同时清除ADC12FIG.3
  index = (index+1)%Num_of_Results;         // 增加结果的索引,取index变量的模(余数)
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -