#ifndef __SPRAY_H_ #define __SPRAY_H_ #include <sstream> // #define BUTTON BIT4; // #define SPRAY_PORT BIT6;// P1.6 #define LED_GREEN BIT1; // P2.1 #define LED_RED BIT2; // P2.2 #define LIGHT_SENSOR INCH_5; // A5 #define DARK_VALUE 650; // " ." #define LOWBATT_VALUE 520; // . // unsigned int SleepPeriod = 900; // . 15 . (900) unsigned int SpayDelay = 5; // 5. unsigned int DarkValue = 650; // " ." unsigned int LowBattValue = 520; // . unsigned int BattInterval = 5; // 5. // unsigned int ADCValue; // unsigned int SprayCount; // unsigned int TimerCount = 0; // unsigned int BattTimer = 0; // unsigned int RXByte; // UART // bool ADCDone = false; // bool LightOn = false; // ( ) bool SleepMode = false; // ( 15 ) bool IsCounting = false; // , // bool LowBatt = false; // bool BlinkOn = false; // bool Tick = false; // bool IsADCOn = false; // bool IsADCLight = true; // (true - // , false - ) bool ForceSpay = false; // bool UARTReceived = false; // UART // void UARTSend(string str); // UART void LightSensorOn(); // void VCCSensorOn(); // unsigned int ReadCountFromFlash(); // void WriteCountToFlash(unsigned int); // void Spray(); // #endif
#include <msp430g2553.h> #include "spray.h" int main( void ) { // WDT WDTCTL = WDTPW + WDTHOLD; // //------------------------- // - P1DIR &= ~BUTTON; // P1REN |= BUTTON; // P1OUT &= ~BUTTON; // - GND P1IES |= BUTTON; // P1IFG &= ~BUTTON; // P1IE |= BUTTON; // P1DIR |= SPRAY_PORT; // P1OUT &= ~SPRAY_PORT; // // P2DIR |= LED_GREEN P2DIR |= LED_RED; // ( ) P2OUT |= LED_GREEN; P2OUT &= ~LED_RED; // . //------------------------- // DCOCTL = CALDCO_1MHZ; // ~ 1 BCSCTL1 = CALBC1_1MHZ; //------------------------- // TACTL = TASSEL_1 + MC_1; // ACLK (), // CCR0 CCR0 = 0x8000; // .. 32768, // CCTL0 = CCIS0 + OUTMOD0 + CCIE; // , // //------------------------- // UART. FLASH P1SEL = BIT1 + BIT2; // P1SEL2 = BIT1 + BIT2; // UART UCA0CTL1 |= UCSSEL_2; // - SMCLK ~ 1MHz UCA0BR0 = 0x68; // 9600 UCA0BR1 = 0; // 1000000 / 9600 = 104 = 0x68 UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; // IE2 = UCA0RXIE; // UART //------------------------- // Flash FCTL2 = FWKEY + FSSEL1 + FN1; // // . 65535 - // SprayCount = ReadCountFromFlash(); if(SprayCount == 65535) { SprayCount = 0; // WriteCountToFlash(SprayCount); // 0 Flash } //------------------------- __delay_cycles(2000); // //------------------------- __bis_SR_register(GIE); // P2OUT &= ~LED_GREEN; // . . UARTSend("Ready\r\n"); // while(true) { if(Tick) // . { Tick = false; // BattTimer++; if(BattTimer >= BattInterval) // { BattTimer = 0; // VCCSensorOn(); // } if(!SleepMode) // { LightSensorOn(); // if(IsCounting) // { if(TimerCount >= SpayDelay) // { Spray(); // } } } else { // if(TimerCount >= SleepPeriod) { TimerCount = 0; // SleepMode = false; // } } } if(ADCDone) // { ADCDone = false; // if(IsADCLight) // { if(ADCValue < DarkValue) // . { LightOn = true; // " " IsCounting = false; // ( , ) TimerCount = 0; // } else // { if(LightOn) // { LightOn = false; // IsCounting = true; // TimerCount = 0; } } } else // { if(ADCValue < LowBattValue) { LowBatt = true; // } else { LowBatt = false; // - } } } if(ForceSpay) // . . { ForceSpay = false; Spray(); } if(UARTReceived) // UART. . { UARTReceived = false; ostringstream s; s << SprayCount << "\r\n"; UARTSend(s.str()); } } } // UART void UARTSend(string str) { int len = str.length(); for(int i=0;i<len;i++) { while(!(IFG2&UCA0TXIFG)); UCA0TXBUF = str[i]; } } // void LightSensorOn() { if(!IsADCOn) { IsADCOn = true; IsADCLight = true; ADC10CTL0 &= ~ENC; ADC10CTL0 = ADC10SHT_3 + ADC10ON + ADC10IE; ADC10CTL1 = ADC10SSEL_2 + LIGHT_SENSOR; ADC10CTL0 |= ENC + ADC10SC; } } // void VCCSensorOn() { if(!IsADCOn) { IsADCOn = true; IsADCLight = false; ADC10CTL0 &= ~ENC; ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + REF2_5V + ADC10IE; ADC10CTL1 = ADC10SSEL_2 + INCH_11; __delay_cycles(128); ADC10CTL0 |= ENC + ADC10SC; } } // unsigned int ReadCountFromFlash() { __bic_SR_register(GIE); // int *Flash_ptr; Flash_ptr = (int *) 0x1040; // *Flash_ptr = 0; // unsigned int value = *Flash_ptr; // __bis_SR_register(GIE); // return value; // } // void WriteCountToFlash(unsigned int value) { __bic_SR_register(GIE); // int *Flash_ptr; Flash_ptr = (int *) 0x1040; // FCTL1 = FWKEY + ERASE; // FCTL3 = FWKEY; *Flash_ptr = 0; // FCTL1 = FWKEY + WRT; *Flash_ptr++ = value; // Flash FCTL1 = FWKEY; FCTL3 = FWKEY + LOCK; // __bis_SR_register(GIE); // } // void Spray() { __bic_SR_register(GIE); // P1OUT |= SPRAY_PORT; // __delay_cycles(500000); // ~ 0.5 ( 1) P1OUT &= ~SPRAY_PORT; // // LightOn=false; SleepMode=true; IsCounting=false; TimerCount=0; SprayCount++; WriteCountToFlash(SprayCount); __bis_SR_register(GIE); // } //--------------------------- // // #pragma vector=ADC10_VECTOR __interrupt void ADC_Handler() { ADCValue = ADC10MEM; // ADCDone = true; // IsADCOn = false; } // #pragma vector=PORT1_VECTOR __interrupt void Button_Handler() { P1IE &= ~BUTTON; // P1IFG &= ~BUTTON; // ForceSpay = true; // // __delay_cycles(300000); P1IE |= BUTTON; // } // 1 . #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_Handler() { // if(!BlinkOn) // { // // - , if(LowBatt) { P2OUT |= LED_RED; } else { P2OUT |= LED_GREEN; } BlinkOn = true; } else // { P2OUT &= ~LED_RED; P2OUT &= ~LED_GREEN; BlinkOn = false; } TimerCount++; // Tick=true; // } // UART #pragma vector=USCIAB0RX_VECTOR __interrupt void UART_Handler() { RXByte = UCA0RXBUF; UARTReceived = true; }
Source: https://habr.com/ru/post/158965/
All Articles