找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4249|回复: 0
收起左侧

基于MSP430F6638的ADC实验(拨动电位器数码管显示对应电压值)

[复制链接]
ID:234748 发表于 2017-9-21 21:29 | 显示全部楼层 |阅读模式
#include <msp430f6638.h>
#define LCD_d 0x01
#define LCD_e 0x02
#define LCD_g 0x04
#define LCD_f 0x08
#define LCD_dp 0x10
#define LCD_c 0x20
#define LCD_b 0x40
#define LCD_a 0x80
const char LCD_NUM[] = {
    LCD_a + LCD_b + LCD_c + LCD_d + LCD_e + LCD_f,       //0
    LCD_b + LCD_c,                               //1
    LCD_a + LCD_b + LCD_d + LCD_e + LCD_g,             //2
    LCD_a + LCD_b + LCD_c + LCD_d + LCD_g,             //3
    LCD_b + LCD_c + LCD_f + LCD_g,                   //4
    LCD_a + LCD_c + LCD_d + LCD_f + LCD_g,             //5
    LCD_a + LCD_c + LCD_d + LCD_e + LCD_f + LCD_g,       //6
    LCD_a + LCD_b + LCD_c,                         //7
    LCD_a + LCD_b + LCD_c + LCD_d + LCD_e + LCD_f + LCD_g, //8
    LCD_a + LCD_b + LCD_c + LCD_d + LCD_f + LCD_g        //9
};
void Init_TS3A5017DR()
{
    // Configure TS3A5017DR IN1 and IN2
    P1DIR |= BIT6 + BIT7; // P1.7 : IN1; P1.6 : IN2  set as output
    P1OUT &= ~BIT7; // IN1 = 0
    P1OUT |= BIT6; // IN2 = 1
}
void Init_LCD()
{
    LCDBCTL0 = LCDDIV0 + LCDPRE0 + LCDMX1 + LCDSSEL + LCDMX1 + LCD4MUX;
    LCDBPCTL0 = LCDS0 + LCDS1 + LCDS2 + LCDS3 + LCDS4 + LCDS5 + LCDS6 + LCDS7 + LCDS8 + LCDS9 + LCDS10 + LCDS11;
    P5SEL = 0xFC; // use LCD driver
}
void BackLightEnable()
{
    P8DIR |= BIT0;
    P8OUT |= BIT0;
}
void LCD_Set(const char is_launched)
{
    if (is_launched == 1)
    {
        LCDBCTL0 |= LCDON;
    }
    else if (is_launched == 0)
    {
        LCDBCTL0 &= ~LCDON;
    }
}
void LCD_Clear()
{
    unsigned char index;
    for (index = 0; index < 12; ++index)
    {
        LCDMEM[index] = 0;
    }
}
int his[8];
int temp;
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;            //关闭看门狗
    P3DIR |= BIT4+BIT5+BIT2; //设置MSP430端口和TM1638相连的引脚
    ADC12CTL0 |= ADC12ON + ADC12SHT0_2 + ADC12REFON; //打开ADC,设置采样时间,打开内部参考电压发生器
    ADC12CTL1 |= ADC12SHP; //选择SAMPON来自采样定时器,单通道单次采样模式(默认)
    ADC12MCTL0 = ADC12SREF_0 + ADC12INCH_6;
    ADC12IE = BIT0; //开中断使能
    ADC12CTL0 |= ADC12ENC; //ADC使能
    Init_TS3A5017DR();
    Init_LCD();
    LCD_Set(1);
    LCD_Clear();
    BackLightEnable();
    while(1)
    {
        ADC12CTL0 |= ADC12SC;   //启动转换
        _BIS_SR(CPUOFF + GIE);    //CPU休眠,开启总中断,等待转换完成
        volatile int i;
        for (i = 7; i > 0; --i)
            his[i] = his[i - 1];
        his[0] = temp;
        double sum = 0;
        for (i = 0; i < 8; ++i)
            sum += his[i];
        sum = sum / 8 * 15000 / 4095;
        unsigned int result = sum;
        LCDMEM[4] = LCD_NUM[result % 10];
        LCDMEM[3] = LCD_NUM[result / 10 % 10];
        LCDMEM[2] = LCD_NUM[result / 100 % 10];
        LCDMEM[1] = LCD_NUM[result / 1000 % 10];
        LCDMEM[0] = LCD_NUM[result / 10000 % 10];
    }
}
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
    temp = ADC12MEM0;
    _BIC_SR_IRQ(CPUOFF);   //退出低功耗模式,相当于LPM0_EXIT;
}

//TM1638资料见上一条帖子
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表