venerdì 16 marzo 2012

Elettronica - MSP430 LaunchPad - 2° Parte - Codice C

Codice parziale realizzato con l'aiuto di GRACE
//*****************************************************************************
// Giuseppe Marchì
// rev.1
// Si devono apportare modifiche alla documentazione e alla logica delle zone
//                   MSP430
//                            -----------------
//                     /|\|                                   |
//                     --|RST                           |-
//                         |                                 |
//  ZONA1      ->|P1.3                          |
//  ZONA2       ->|P1.5                   P1.1|-->TXD
//  ZONA3   ->    |P1.7                   P1.2|<--RXD
//                         |                                  |
//TENS_BATT ->|P1.4                   P1.0|-->LED1 (SIRENA)
//                          |                          P3.3|-->LED2 (OPERATIVO
//  CHIAVE  ->    |P3.0                   P2.7|-->BATTERIA_SCARICA
//                          |_________________|
//
// RIEPILOGO
// p2.2 (era P1.0) p3.5 LED1 SIRENA
// P1.1 TXD
// P1.2 RXD
// P1.3 ZONA 1
// P1.4 TENS_BATT
// P1.5 ZONA 2
// P3.3 (era 1.6) LED2 OPERATIVO
// P1.7 ZONA3
// p3.0 (era P2.6) CHIAVE
// P3.2 batteria scarica
//   Texas Instruments Inc.
//*****************************************************************************

/*
 * ======== Standard MSP430 includes ========
 */
#include <msp430.h>

/*
 * ======== Grace related includes ========
 */
#include <ti/mcu/msp430/csl/CSL.h>

/*
 * ======== application definitions ========
 * Bit period for 9600/4 = 2400 Baud SW UART, assuming
 *    SMCLK = 1MHz,
 *    Timer_A input divider = 8 (=> timer freq = 125 KHz)
 */
#define BITIME  (13 * 4)                // 125 KHz /(13*4) = 2404 bits/sec

unsigned char bitCnt;                   // number of bits to transmit
unsigned int  txByte;                   // transmit buffer with start/stop bits


unsigned int AdcData[3] = {0};              // Store results A2,A1,A0
unsigned int const_volt = 0;                // Determine ConstVoltage/ConstCurrent
                                            // stage
unsigned int timedelay = 0;                 // Variable to Start Watchdog timer delay
unsigned int timercounts = 0;               // Track time delay
unsigned int delayflag = 0;


void putstr(char *str);                 // transmit string
void transmit(unsigned char ch);        // transmit a character


/*
 *  ==== platform-specific definitions ========
 */
#define LEDOUT  P1OUT           // GPIO port with LEDs
#define GREEN   BIT6            // green LED location for the LaunchPad
#define RED     BIT0            // red LED location
#define TELECOMANDO     BIT0            // red LED location

/*
 * ======== globals ========
 */
volatile int batt;              // raw ADC10 value
volatile int degF;              // temperature in degrees F
volatile int degC;              // temperature in degrees C
volatile int initialDegF;       // initial measurement in degrees F
unsigned int allarme;
unsigned int chiave;
unsigned int chiave_abilatata;
unsigned int timerCount = 0;
unsigned int contatore =0;
/*
 *  ======== main ========
 */
int main(void)
{
    CSL_init();                         // Activate Grace-generated config
    ADC10CTL0 |= ENC;                   // Enable conversion
 //   __bis_SR_register(LPM0_bits);       // Wait in LPM0 for temp to be sampled


     P1IES &= ~ BIT3;   // low -> high is selected with IES.x = 1.
     P1IFG &= ~BIT3;  // To prevent an immediate interrupt, clear the flag for
                    // P1.3 before enabling the interrupt.
     P1IE &= ~ BIT3;    // disEnable interrupts for P1.3
    __enable_interrupt();             // Set global interrupt enable
   
    // Stop Timer_A
     TA1CTL &= ~(MC1 + MC0); // Clear MCx bits to stop timer
   
    allarme =0;
    //buttonOn = 0;                     // Initialize buttonOn state and LEDs
    //P1OUT &= ~BIT0;                   // Turn off LED (P1.0)
    WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
     P3OUT |= BIT3;                   // Turn off LED (P3.3) non operativo
     P2OUT |= BIT2;                   // Turn off LED (P3.3) Sirena non operativo
     P3OUT |= BIT2;                   // Turn off LED (P3.3) Batteria carica
    
  //   TACCTL0 &= ~CCIE;
  //   TACCR0 = 0xFFFF; //Timer A0 interrupt enable off
    
  //   TACCTL1 &= ~CCIE;
  //  TACCR1 = 0xFFFF; //Timer A1 interrupt enable off
    
  
   
  //   TACCTL3 &= ~CCIFG; //Clear Timer A3 flag
   
    chiave = 0;
    chiave_abilatata=1;
     if ((P3IN & BIT0) != 1) { chiave =1; }
    while (1) {
        ADC10CTL0 |= ENC;               // Enable next conversion
     //   __bis_SR_register(LPM0_bits);   // Wait in LPM0 for temp to be sampled
        __no_operation();               // this nop is only to assist debugging
  
     if (contatore==33)
            {contatore =0;
            // Stop Timer_A
            TA1CTL &= ~(MC1 + MC0); // Clear MCx bits to stop timer   
            P3OUT |= BIT5; //sirena rele spegne
            }
   
   
    if (((P3IN & BIT0) != 1) && (chiave==0)) // CHIAVE
    
    {
    
     allarme =0;
     delayflag = 1;

      IFG1 &= ~WDTIFG;                  // Clear any pre-existing WDT interrupt
      WDTCTL = (WDTCTL & 7) + WDTCNTCL  // Reset watchdog with same divider,
        + WDTPW + WDTTMSEL;           // write password, and timer mode

      IE1 |= WDTIE;                             // Enable WDT interrupt
      
      __bis_SR_register(LPM0_bits); // Enter LPM0
      chiave =1;
   
delayflag = 1;
    }
  
   
   
    //}  fibe del ciclo chiave attiva
  
  
   if (((P3IN & BIT0) != 0) && (chiave==1)) // CHIAV
    {
     
     P3OUT |= BIT3;                   // Turn off LED (P3.3) non operativo
     P2OUT |= BIT2;  //  Spegne la sirena
     allarme = 0;
     // Stop Timer_A
     TA1CTL &= ~(MC1 + MC0); // Clear MCx bits to stop timer
    // TACCTL3 &= ~CCIE;
   //  TACCR3 = 0xFFFF; //Timer A3 interrupt enable off
    
   //  TACCTL3 &= ~CCIFG; //Clear Timer A3 flag resetta il conteggio
    
    
     IE1 &= ~WDTIE;                    // Disable this (WDT) interrupt
  //   timercounts = 0;
     P1IFG &= ~BIT3;    // Clear the port 1.3 interrupt flag
     P1IFG &= ~BIT3;                   // Ensure button interrupt flag is clear
     P1IE &= ~BIT3;                     // disable button/zone interrupt
    chiave =0;
     chiave_abilatata=1;
    }
    }
  }


void watchdogIsr(void)
{
  if(timercounts > 20) //720
  {
     TACTL = 0;                             // before stop charging
     P1SEL = 0;
     P1OUT = 0;
     ADC10CTL0 = 0;
     timercounts = 0;
     timedelay = 0;
     delayflag = 0;
     IE1 &= ~WDTIE;                    // Disable this (WDT) interrupt
     P1IFG &= ~BIT3;                   // Ensure button interrupt flag is clear
     P1IE |= BIT3;                     // Re-enable button/zone interrupt
     P3OUT &= ~BIT3;                   // Turn on LED (P3.3 lec operativo)}
  //    __enable_interrupt();             // Set global interrupt enable
 // p2.2 (era P1.0) p3.5 LED1 SIRENA
    // P2OUT &= ~BIT2;                   // Turn off (P2.2) sirena led
    // P3OUT &= ~BIT5;                   // Turn off (P3.5) sirena rele

  }
  else
 {
  timercounts++;
  //P1OUT ^= 0x01;
//  P1OUT |= BIT0;;                   // Turn on LED (P1.0)
 // p2.2 (era P1.0) p3.5 LED1 SIRENA
 // P2OUT |= BIT2;                   // Turn off (P2.2) sirena led
  //  P3OUT |= BIT5;                   // Turn on (P3.5) sirena rele
  //P1IE &= ~BIT3;    // Disable this (GPIO P1.3) interrupt

     P3OUT ^= BIT3;    //toggle  LED (P3.3) non operativo   using exclusive-OR          )
 }
}

void a3_isr(void)
{
   
   // P3OUT ^=  BIT5; //accende rele sirena rele
    contatore++;
    timerCount = (timerCount + 1) % 4;
if(timerCount ==0)  P3OUT ^=  BIT5;                   // Toggle (P3.5) sirena rele

}
void pioIsr(void)
  {
   //switch(P1IFG&40) {  // bit3 e bit5
       
     //   case BIT3:
      if(P1IFG & BIT3){
            P1IFG &= ~BIT3;    // Clear the port 1.3 interrupt flag
            allarme =1 ;
          
            P2OUT &= ~ BIT2;                   // Turn on LED (P3.3) Sirena non operativo
            // Start Timer_A
            TA1CTL |= MC_1; // Start timer in up mode
           P1IFG = 0;
            //BCSCTL1 = bcs_vals[i];
            //DCOCTL = dco_vals[i];
            //if (++i == 3)
              //  i = 0;
        //    return;
        }

     //   case BIT5:
      if(P1IFG & BIT5){
            P1IFG &= ~BIT5;    // Clear the port 1.3 interrupt flag
            allarme =1 ;
          
            P2OUT &= ~ BIT2;                   // Turn on LED (P3.3) Sirena non operativo
            // Start Timer_A
            TA1CTL |= MC_1; // Start timer in up mode
           P1IFG = 0;
            //BCSCTL1 = bcs_vals[i];
            //DCOCTL = dco_vals[i];
            //if (++i == 3)
              //  i = 0;
        //    return;
        }          
           
//        default:
//            allarme =0;
//            P2OUT |= BIT2;
         
//            P1IFG = 0;    // probably unnecessary, but if another flag occurs
                          // in P1, this will clear it.  No error handling is
                          // provided this way, though.
  //          return;
    //}
   } // P1_ISR

/*
 *  ======== putstr ========
 */
void putstr(char *str)
{
    char *cp;
    for (cp = str; *cp != '\0'; cp++) {
        transmit(cp[0]);
    }
}

/*
 *  ======== transmit ========
 *  Transmit specified character
 */
void transmit(unsigned char ch)
{
    bitCnt = 0xA;                       // Load Bit counter, 8data + ST/SP
    txByte = (unsigned int)ch | 0x100;  // Add mark stop bit to txByte
    txByte = txByte << 1;               // Add space start bit

    CCR0 = TAR + BITIME;                // Some time till first bit
    CCTL0 = OUTMOD0 + CCIE;             // TXD = mark = idle
    while (CCTL0 & CCIE);               // Wait for ISR to complete TX
}

/*
 *  ======== timer_A_ISR ========
 *  Timer A0 interrupt service routine
 */
void timer_A_ISR(void)
{
    CCR0 += BITIME;                     // Schedule next interrupt
    if (bitCnt == 0) {
        CCTL0 &= ~CCIE;                 // All bits TXed, disable interrupt
    }
    else {
        if (txByte & 0x01) {
            CCTL0 &= ~OUTMOD2;          // TX Mark
        }
        else {
            CCTL0 |= OUTMOD2;           // TX Space
        }
        txByte = txByte >> 1;
        bitCnt--;
    }
}


/*
 *  ======== adcIsr ========
 *  ADC10 conversion ISR
 *
 *  This ISR moves the conversion result into global variable. Doing so
 *  will also clear the associated interrupt flag ADC10IFG.
 *
 *  This interrupt handler routine gets assigned in the ADC10 Grace view.
 */
void adcIsr(void)
{
    batt = ADC10MEM;                    // read ADC10 sampled temperature
}

/*

  if (AdcData[0] < 355)                     // Batt_Temperature too high?
  {
    TACTL = 0;
    P1SEL = 0;
    P1OUT = 0x01;
    ADC10CTL0 = 0;
    timercounts = 0;
    timedelay = 0;
    delayflag = 0;
    WDTCTL = WDTPW + WDTHOLD;              // Stop WDT
  } */

Nessun commento:

Posta un commento