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

📄 exercise-11.c

📁 TI MSP430针对cap sense 按键的源码, 可实现单击或滑动键, 如IPOD上的人机接口.
💻 C
字号:
/* Exercise 10

    A simple ohm-meter demonstration.
*/
#include <stdint.h>
#include <msp430x42x0.h>

//defines
#define PB_2_0 (1 << 0)                                      // Push Button on P2.0
#define PB_2_1 (1 << 1)                                      // Push Button on P2.1

#define seg_a       0x01
#define seg_b       0x02
#define seg_c       0x10
#define seg_d       0x04
#define seg_e       0x80
#define seg_f       0x20
#define seg_g       0x08
#define seg_h       0x40

#define CHAR_0   (seg_a | seg_b | seg_c | seg_d | seg_e | seg_f)
#define CHAR_1   (seg_b | seg_c)
#define CHAR_2   (seg_a | seg_b | seg_d | seg_e | seg_g)
#define CHAR_3   (seg_a | seg_b | seg_c | seg_d | seg_g)
#define CHAR_4   (seg_b | seg_c | seg_f | seg_g)
#define CHAR_5   (seg_a | seg_c | seg_d | seg_f | seg_g)
#define CHAR_6   (seg_a | seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_7   (seg_a | seg_b | seg_c)
#define CHAR_8   (seg_a | seg_b | seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_9   (seg_a | seg_b | seg_c | seg_d | seg_f | seg_g)
#define CHAR_A   (seg_a | seg_b | seg_c | seg_e | seg_f | seg_g)
#define CHAR_B   (seg_c | seg_d | seg_e | seg_f | seg_g)
#define CHAR_C   (seg_a | seg_d | seg_e | seg_f)
#define CHAR_D   (seg_b | seg_c | seg_d | seg_e | seg_g)
#define CHAR_E   (seg_a | seg_d | seg_e | seg_f | seg_g)
#define CHAR_F   (seg_a | seg_e | seg_f | seg_g)
#define CHAR_SPACE  0

const unsigned char hex_table[] =
{
    CHAR_0,
    CHAR_1,
    CHAR_2,
    CHAR_3,
    CHAR_4,
    CHAR_5,
    CHAR_6,
    CHAR_7,
    CHAR_8,
    CHAR_9,
    CHAR_A,
    CHAR_B,
    CHAR_C,
    CHAR_D,
    CHAR_E,
    CHAR_F
};

#define _4052_A  BIT5
#define _4052_B  BIT4

long sample_accum;
long long_Rx;
int sample_count;
int V_R;
int V_Rx;

int read_channel(unsigned int count, unsigned char channel);
int itobcd(int i);                                           // 16-bit hex to bcd conversion

int itobcd(int i)                                            // Convert hex word to BCD.
{
    int bcd = 0;
    char j = 0;

    while (i > 9)
    {
        bcd |= ((i % 10) << j);
        i /= 10;
        j += 4;
    }
    return (bcd | (i << j));                                 // Return converted value
}

void LCDchar(int ch, int pos)
{
    /* Put a segment pattern at a specified position on the LCD display */
    LCDMEM[7 - pos] = ch;
}

#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer_interrupt(void)
{
    /* Do nothing for now */
}

void init_basic_timer(void)
{
    /* Basic timer setup */
    /* Set ticker to 32768/(256*256) */
    BTCTL = BT_fLCD_DIV64 | BT_fCLK2_DIV128 | BT_fCLK2_ACLK_DIV256;
    /* Enable the 1 second counter interrupt */
    IE2 |= BTIE;
}

void init_fll(void)
{
    int i;

    FLL_CTL0 |= XCAP18PF;                                     // Set load capacitance for xtal
    while (LFOF & FLL_CTL0)
        /*wait*/;                                             // wait for watch crystal to stabilize
    SCFQCTL = 63;                                             // 32 x 32768 x 2 = 2.097152MHz
    for (i = 0;  i < 10000;  i++)
        /* wait */;
}

#pragma vector=SD16_VECTOR
__interrupt void sd16_interrupt(void)
{
}

void init_adc(void)
{
    /* Initialize and enable the SD16 */
    SD16CTL = SD16DIV_2 | SD16SSEL_1 | SD16REFON | SD16VMIDON;
    SD16IV = 0;
    SD16INCTL0 = SD16GAIN_1 | SD16INCH_0;
    SD16AE = SD16AE2 | SD16AE3 | SD16AE4 | SD16AE6 | SD16AE7;
    P6SEL |= (BIT3 | BIT2 | BIT1 | BIT0);
    SD16CCTL0 = SD16BUF_1 | SD16OSR_128 | SD16UNI;
}

void init_lcd(void)
{
    int j;

    for (j = 0;  j < 20;  j++)
        LCDMEM[j] = 0;

    /* Turn on the COM0-COM3 and R03-R33 pins */
    P5SEL |= (BIT7 | BIT6 | BIT5 | BIT4 | BIT3 | BIT2 | BIT1 | BIT0);
    P5DIR |= (BIT4 | BIT3 | BIT2);

    LCDACTL = LCDFREQ_128 | LCD4MUX | LCDSON | LCDON;
    LCDAPCTL0 = LCDS0 | LCDS4 | LCDS8 | LCDS12;
    LCDAPCTL1 = 0;
    LCDAVCTL0 = LCDCPEN;
    LCDAVCTL1 = 12 << 1;
}

int read_channel(unsigned int count, unsigned char channel)
{
    unsigned int i;
    unsigned long l;

    SD16INCTL0 = SD16GAIN_1 | channel;
    l = 0;
    for(i = 0;  i < count;  i++)
    {
        SD16CCTL0 |= SD16SC;
        while (!(SD16CCTL0 & SD16IFG))
            /* wait */;

        l += SD16MEM0;
    }

    return((int) (l/count));
}

void main(void)
{
    unsigned int i;
    unsigned int j;

    WDTCTL = WDTPW | WDTHOLD;
    init_fll();
    P6DIR = 0xFF;                         // All P6.x outputs
    P6OUT = _4052_A + _4052_B;            // All P6.x reset

    init_basic_timer();
    init_lcd();
    init_adc();

    DAC12_0CTL = DAC12OPS | DAC12CALON | DAC12IR | DAC12AMP_2 | DAC12SREF_2 | DAC12ENC;            // DAC0 enable
    DAC12_0DAT = 0x099A;

    DAC12_0DAT = 512;
    P6OUT = _4052_B;                      // All P6.x reset

    for (;;)
    {
        V_R = (unsigned int) read_channel(500, SD16INCH_2);    // known resistor
        V_Rx = (unsigned int) read_channel(500, SD16INCH_4);   // unknown resistor

        long_Rx = ((long) V_Rx) * 1000;
        long_Rx /= (long) V_R;
        j = (unsigned int) long_Rx;

        for (i = 0;  i < 5;  i++)
        {
            LCDMEM[i] = hex_table[j%10];                      // remainder = character in table to display
            j /= 10;                                          // shifts right so next character can be displayed
        }
        //LCDMEM[1] |= seg_h;                                 // Decimal point
                                                              // 去掉小数点,使单位为K欧。(lierda 修改)
    }
}

⌨️ 快捷键说明

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