Demo entry 6662790

С

   

Submitted by 123 on Nov 27, 2017 at 16:58
Language: C. Code size: 12.5 kB.

#include <msp430.h>

#define ADCMAXVAL       0x0200      // 0.50 × VCC ~3,4V
#define ADCMINVAL       0x0100      // 0.25 × VCC
#define RESISTANCE      53550       // 51kOm+5%
#define MEASURINGCOUNT  50          // measuring cycles
//-use PORT2 to connect display------------------------------------------------
#define DPORT           P2OUT       // LCD-port
#define A0              BIT0        // P2.0 low - address, high - data
#define WR2             BIT1        // P2.1
#define WR1             BIT2        // P2.2
#define DB3             BIT3        // P2.3
#define DB2             BIT4        // P2.4
#define DB1             BIT5        // P2.5
#define DB0             BIT6        // P2.6
// CLC - address/data
#define CLK             DPORT |= WR1; DPORT &= ~(WR1);
//-----------------------------------------------------------------------------
#define PinCapacitance  BIT1        // P1.1
#define PinCharge       BIT2        // P1.2
#define StartButton     BIT3        // P1.3
//-----------------------------------------------------------------------------
void  setup             (void);
void  initPorts         (void);
void  initADC           (void);
void  initTA            (void);
void  charge            (void);
void  discharge         (void);
float measuring         ();
void  showProgress      (int);
void  showCapacitance   (float);
//-----------------------------------------------------------------------------
char  convert  (char in);
void  setAdr   (char adr);
void  sendSign (char data);
void  dispInit (void);
void  sendRaw  (char adr, char *data, char count);
//-----------------------------------------------------------------------------
volatile unsigned long  taOverflows = 0;
volatile unsigned long  adcCount    = 0;
volatile unsigned int   progress    = 0;
volatile unsigned int   a           = 0;
volatile float          avgValue    = 0;
volatile float          time        = 0;
volatile float          fract       = 0;
volatile unsigned int   pow         = 0;
volatile unsigned int   integer     = 0;
volatile unsigned int   first       = 0;
volatile unsigned int   second      = 0;
volatile unsigned int   Dn          = 0;
volatile char           Dn1, tmp, j;

float    arr[MEASURINGCOUNT];
//-----------------------------------------------------------------------------
char helloMessage[10] = {0xAB, 0x03, 0x8F, 0xCD, 0xCD, 0x00, 0xDD, 0x2F, 0x00, 0x00};
char progressBar[10]  = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
char numericLib[10]   = {0xEE,   // 0
                         0x60,   // 1
                         0x2F,   // 2
                         0x6D,   // 3
                         0xE1,   // 4
                         0xCD,   // 5
                         0xCF,   // 6
                         0x68,   // 7
                         0xEF,   // 8
                         0xED};  // 9
char resultBar[10]    = {0x8E,   // C
                         0x01,   // -
                         0x10,   // .  + Xinteger
                         0x00,   // Xfirst
                         0x00,   // Xsecond
                         0x8F,   // E
                         0x01,   // -
                         0xEE,   // 0
                         0x00,   // pow
                         0x8B};  // F

//-----------------------------------------------------------------------------
int main(void)
{
    WDTCTL = WDTPW + WDTHOLD;
    // DCO calibration to 1Mhz
    if (CALBC1_1MHZ==0xFF)       // If calibration constant erased
    {
        while(1);                // CPU do not load
    }
    DCOCTL = 0;                  // Select lowest DCOx and MODx settings
    BCSCTL1 = CALBC1_1MHZ;       // Set range
    DCOCTL = CALDCO_1MHZ;        // Set DCO step + modulation */
    BCSCTL3 = LFXT1S_2;          // Select ACLK from VLO (no crystal)
    //------------------------------------------------------------------------
    for(;;)
    {
        setup ();
        avgValue = 0;
        volatile unsigned int i = 0;
        for (i = 0; i < MEASURINGCOUNT; i++)
        {
            arr[i] = measuring();
            avgValue += arr[i];
            showProgress(i);
        }
        avgValue /= MEASURINGCOUNT;
        showCapacitance(avgValue);
    }
}
//-----------------------------------------------------------------------------
void initPorts(void)
{
    P1DIR = 0xFF;                               // All P1.x outputs
    P2DIR = 0xFF;                               // All P2.x outputs
    P1OUT = 0x00;                               // All P1.x reset
    P2OUT = 0x00;                               // All P2.x reset
    //----------------------------------------------------------------
    P2SEL = 0x00;                               // Digital i/o rather than crystal
    //----------------------------------------------------------------
    P1DIR &= ~StartButton;                      // P1.3 input
    P1OUT |= StartButton;
    P1IE  |= StartButton;                       // P1.3 interrupt enabled
    P1IES |= StartButton;                       // P1.3 Hi/lo edge
    P1REN |= StartButton;                       // Enable Pull Up on SW2 (P1.3)
    P1IFG &= ~StartButton;                      // P1.3 IFG cleared
}
void initADC(void)
{
    ADC10CTL0 = ADC10SHT_2 + ADC10ON;           // ADC10ON, interrupt disabled
    ADC10CTL1 = INCH_1;                         // input A1
    ADC10AE0  |= PinCapacitance;                // Enable AE

}
//-----------------------------------------------------------------------------
void initTA(void)
{
    TACTL = TASSEL_2 + MC_0 + TAIE;             // SMCLK, contmode, interrupt
}
//-----------------------------------------------------------------------------
int getAdcValue(void)
{
    ADC10CTL0 |= ENC + ADC10SC;                 // Sampling and conversion start
    while (ADC10CTL1 & ADC10BUSY);              // ADC10BUSY?
    adcCount++;
    ADC10CTL0 &= ~ENC;                          // ADC10 disable
    ADC10CTL0 &= ~ADC10SC;                      // Sampling and conversion stop
    return ADC10MEM;
}
//-----------------------------------------------------------------------------
void discharge(void)
{
    P1OUT &= ~PinCharge;                        // Make Charge pin low
    while (getAdcValue() > ADCMINVAL);          // Wait for discharging
}
//-----------------------------------------------------------------------------
void charge(void)
{
    P1OUT |= PinCharge;
    while (getAdcValue() < ADCMAXVAL);  // Wait for charging
}
//-----------------------------------------------------------------------------
float measuring ()
{
    time = 0;
    taOverflows = 0;
    //------------------------------------------------------------------------
    discharge();
    TACTL |= MC_2;                      // Start TA
    charge();
    TACTL &= ~MC_2;                     // Stop TA
    discharge();
    //------------------------------------------------------------------------
    while (taOverflows > 0)
    {
        time += 65535;
        taOverflows--;
    }
    time += TAR;
    time *= 0.000001;
    time = time/RESISTANCE;
    //------------------------------------------------------------------------
    TAR = 0;
    //------------------------------------------------------------------------
    return time;
}
//-----------------------------------------------------------------------------
void setup (void)
{
    initPorts();
    initADC  ();
    initTA   ();
    //------------------------------------------------------------------------
    taOverflows = 0;
    adcCount    = 0;
    progress    = 0;
    //------------------------------------------------------------------------
    volatile unsigned int i = 0;
    for (i = 0; i < 10; ++i)
    {
        progressBar[i] = 0x00;
    }
    for (i = 0; i < MEASURINGCOUNT; i++)
    {
    arr[i] = 0;
    }
    //------------------------------------------------------------------------
    // say press S.2!!!
    dispInit();
    sendRaw(0, helloMessage, 10);
    __bis_SR_register(LPM0_bits + GIE);       // Enter LPM4 w/interrupt
}
//*******************************************************************************************
void showProgress(int mCount)
{
    if (mCount > 0)
    {
        a = ((float)mCount/(float)(MEASURINGCOUNT-1))*10.0;
        if ((int)a >= progress)
        {
            volatile unsigned int i = 0;
            for (i = 0; i < a; i++)
            {
                progressBar[i] = 0x01;
            }
            dispInit();
            sendRaw(0, progressBar, 10);
            progress++;
        }
    }
    else
    {
        dispInit();
        sendRaw(0, progressBar, 10);
        progress++;
    }
}
//*******************************************************************************************
void showCapacitance(float inputData)
{
    fract   = inputData;
    pow     = 0;
    integer = 0;
    first   = 0;
    second  = 0;
    while (integer == 0)
    {
        integer = fract / 0.1;
        fract   = fract / 0.1;
        pow++;
    }
    while (fract > 1)
    {
        fract = fract - 1;
    }
    first = fract / 0.1;
    fract = fract / 0.1;
    while (fract > 1)
    {
        fract = fract - 1;
    }
    second = fract / 0.1;
    resultBar[2] |= numericLib[integer];
    resultBar[3] =  numericLib[first];
    resultBar[4] =  numericLib[second];
    resultBar[8] =  numericLib[pow];
    dispInit();
    sendRaw(0, resultBar, 10);
    __bis_SR_register(LPM0_bits + GIE);       // Enter LPM4 w/interrupt
}
//*******************************************************************************************
char convert(char in)
{
    return ((in & 0x01) * DB0  + (((in & 0x02)>>1) * DB1) +
          (((in & 0x04)>>2) * DB2) + (((in & 0x08)>>3) * DB3));
}
//*******************************************************************************************
void setAdr(char adr)
{
    Dn = convert(adr);
    DPORT |= Dn;                    //set ONEs
    DPORT &= Dn & 0xFF & ~(A0);     //reset ZEROs, address
    CLK;
}
//*******************************************************************************************
void sendSign(char data)   //send to address any value
{
    tmp = data;
    Dn1 = convert(tmp);
    DPORT &= Dn1 & 0xFF;             //reset ZEROs
    DPORT |= Dn1|A0;                 //set ONEs, data
    CLK;
    tmp = (tmp>>4);
    Dn1 = convert(tmp);
    DPORT &= Dn1 & 0xFF;             //reset ZEROs
    DPORT |= Dn1|A0;                 //set ONEs, data
    CLK;
}
//*******************************************************************************************
void sendRaw(char adr, char *data, char count)
{
    tmp = 0;
    j = count;
    volatile char *p;               // = data;
    p=data;
    setAdr(adr); //set address
    while (j--)
    {
        tmp= *p++;
        sendSign(tmp);
    }
}
//*******************************************************************************************
void dispInit(void)                 //unlock and blank display
{
    DPORT &= ~(A0|WR1);             //start condition
    //DB0=Hi to 0xF
    DPORT |= DB0|DB1|DB2|DB3;       // select address 0xF to unlock, and data will be the same
    CLK;                            // write address
    DPORT |= A0;                    // write data
    CLK;                            // unlock done
    // blank display
    DPORT &= ~(A0|DB0|DB1|DB2|DB3); //set addr 0x00;
    CLK;                            //write address
    DPORT |= A0;                    // write data
    volatile unsigned int i = 0;
    for (i=0; i<20; i++) {          // clear display, write 20 half of digits
        CLK;
    }
}
//-TimerA interrupt service routine--------------------------------------------
#pragma vector = TIMER0_A1_VECTOR
__interrupt void Timer_A (void)
{
    switch( TA0IV )
     {
       case  2: break;                          // CCR1 not used
       case  4: break;                          // CCR2 not used
       case 10: taOverflows++;                  // overflow
                break;
     }
}

//-Port 1 interrupt service routine------------------------------
#pragma vector = PORT1_VECTOR
__interrupt void Port_1(void)
{
    P1IFG &= ~StartButton;                      // P1.3 IFG cleared
    __bic_SR_register_on_exit(LPM0_bits);
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).