导语
最近在用MSP430做i项目,用到了ADC模块(模数转换器)。
在这里分享一下用CCS开发环境使用MSP430F5529中的ADC来读取灰度传感器输出的模拟量,然后读取电压并将其显示在OLED上。
效果展示
MSP430F5529有4个12位ADC通道,分别为P6.0~P6.3。ADC计算公式:
ADC转化结果为0-4095,最大值为2的12次方。
此外MSP430的ADC有4个转换模式:单通道单次转化,多通道单次转化,单通道多次转化和多通道多次转化。在这里用的是多通道多次转化,毕竟要在OLED上显示多个通道的ADC值,这些值是随传感器变化的。
主函数代码
/******************接线********************/
//
// MSP430F5529
// -----------------
// /|\| |
// | | |
// --|RST |
// | |
// Vin0 -->|P6.0/CB0/A0 P3.6|-->SDA
// Vin1 -->|P6.1/CB1/A1 P3.5|-->SCL
// Vin2 -->|P6.2/CB2/A2 |
// Vin3 -->|P6.3/CB3/A3 |
// | |
/**************************************/
#include <msp430.h>
#include "oled.h"
#include "type.h"
#include "bmp.h"
#define Num_of_Results 8
volatile unsigned int A0results[Num_of_Results];
volatile unsigned int A1results[Num_of_Results];
volatile unsigned int A2results[Num_of_Results];
volatile unsigned int A3results[Num_of_Results];
int main(void)
{
OLED_Init(); //OLED初始化
WDTCTL = WDTPW+WDTHOLD; // 停止关门狗
P6SEL = 0x0F; // 使能 A/D
ADC12CTL0 = ADC12ON+ADC12MSC+ADC12SHT0_8; // 打开ADC12,延长采样时间以避免结果溢出。
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_3; // 使用采样定时器,多通道重复顺序转化。
ADC12MCTL0 = ADC12INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 = ADC12INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 = ADC12INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = ADC12INCH_3+ADC12EOS; // ref+=AVcc, channel = A3, end seq.
ADC12IE = 0x08; // Enable ADC12IFG.3
ADC12CTL0 |= ADC12ENC; // 启用转换
ADC12CTL0 |= ADC12SC; // 启用转换 -软件触发
OLED_Clear(); //OLED清屏
while(1)
{
OLED_ShowString(30,1,"ADC",20);
OLED_ShowCHinese(60,0,0);//采
OLED_ShowCHinese(80,0,1);//集
OLED_ShowString(20,2,"ADC0=",20);
OLED_ShowNum(60,2,ADC12MEM0,4,20);
OLED_ShowString(20,3,"ADC1=",20);
OLED_ShowNum(60,3,ADC12MEM1,4,20);
OLED_ShowString(20,4,"ADC2=",20);
OLED_ShowNum(60,4,ADC12MEM2,4,20);
OLED_ShowString(20,5,"ADC3=",20);
OLED_ShowNum(60,5,ADC12MEM3,4,20);
// __delay_cycles(1000000); //延时1s
// OLED_DrawBMP(0,0,127,7,BMP);//显示图片
}
__bis_SR_register(LPM0_bits + GIE);
__no_operation();
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC12_VECTOR))) ADC12ISR (void)
#else
#error Compiler not supported!
#endif
{
static unsigned int index = 0;
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: break; // Vector 6: ADC12IFG0
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: // Vector 12: ADC12IFG3
A0results[index] = ADC12MEM0; // Move A0 results, IFG is cleared
A1results[index] = ADC12MEM1; // Move A1 results, IFG is cleared
A2results[index] = ADC12MEM2; // Move A2 results, IFG is cleared
A3results[index] = ADC12MEM3; // Move A3 results, IFG is cleared
index++; // Increment results index, modulo; Set Breakpoint1 here
if (index == 8)
{
(index = 0);
}
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
CSDN CCS工程下载
也可以百度网盘 下载 提取码:8888。
感谢支持。
B站视频
MSP430 ADC采样-OLED显示