Sunteți pe pagina 1din 12

#include<msp430F2122.

h> void init_adc() {

ADC10CTL0=ADC10ON | SREF_0 | ADC10SHT_2; ADC10CTL1=INCH_4 | SHS_0 | ADC10DIV_0 | ADC10SSEL_0 | CONSEQ_0 ; ADC10AE0=BIT4; ADC10CTL0|=ENC; }

void start_conversion() {

ADC10CTL0|=ADC10SC;

int is_converting() {

return ADC10CTL1 & ADC10BUSY;

void main() {

int i=0;

init_adc();

P1DIR=0x41;

P1OUT=0x00;

while(1) {

start_conversion();

while( is_converting() );

if(ADC10MEM >=0 && ADC10MEM <= 255.75)

P1OUT=0x00;

else if (ADC10MEM>=256.75 && ADC10MEM <=511.5)

P1OUT=0x01;

else if(ADC10MEM >=512.5 && ADC10MEM <=767.25)

P1OUT=0x40;

else

P1OUT=0x41;

for(i=0;i<32000;i++);

}
//******************************************************************** ********** // MSP430F21x2 Demo - ADC10, Sample A7, 1.5V, TA1 Trig, Ultra-Low Pwr // // Description: A7 is sampled 1024/second (32xACLK) with reference to 1.5V. // All activity is interrupt driven with proper usage of MSP430 lowpower // modes, ADC10 and Vref demonstrated. Timer_A with both TA1/TA0 used in // upmode to drive ADC10 conversion (continuous mode can also be used). // Inside of TA0_ISR software will enable ADC10 and internal reference and // allow > 30us delay for Vref to stabilize prior to sample start. Sample // start is automatically triggered by TA1 every 32 ACLK cycles. ADC10_ISR // will disable ADC10 and Vref and compare ADC10 conversion code. Internal // oscillator times sample (16x) and conversion (13x). If A7 > 0.2Vcc, // P1.0 is set, else reset. Normal Mode is LPM3.

// //* An external watch crystal on XIN XOUT is required for ACLK *// // // +-----(0.9766us)---------\\------------------>+ // TA0_ISR TA1 ADC10_ISR TA0_ISR TA1 // -----+------------+------------+-----------\\------+------------+----> // Enable ADC Trigger ADC Disable ADC // and Vref Compare // +-( >30us--->+ // // // MSP430F21x2 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // >---|P3.7/A7 P1.0 |--> LED // // A. Dannenberg // Texas Instruments Inc. // December 2007 // Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.41A //******************************************************************** ********** #include "msp430x21x2.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR = 0xFF; P1OUT = 0; P2DIR = 0xFF; P2OUT = 0; P3DIR = 0xFF; P3OUT = 0; ADC10CTL1 = INCH_7 + SHS_1; sample start ADC10AE0 = 0x80; select TACCTL0 = CCIE; TACCR0 = 32-1; TACCTL1 = OUTMOD_3; TACCR1 = 2; TACTL = TASSEL_1 + MC_1; __bis_SR_register(LPM3_bits + GIE); interrupts } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { ADC10CTL0 &= ~ENC; ADC10CTL0 = 0; completely if (ADC10MEM < 0x88) P1OUT &= ~0x01; else P1OUT |= 0x01;

// // // // // // // //

Stop WDT All P1.x outputs All P1.x reset All P2.x outputs All P2.x reset All P3.x outputs All P3.x reset P3.7, TA1 trigger

// P3.7 ADC10 option // // // // // // Enable interrupt PWM Period TACCR1 set/reset TACCR1 PWM Duty Cycle ACLK, up mode Enter LPM3, enable

// ADC10 disabled // ADC10, Vref disabled // ADC10MEM = A7 > 0.2V? // Clear P1.0 LED off // Set P1.0 LED on

} // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A(void) { ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE; ADC10CTL0 |= ENC; // ADC10 enable set }

1. //***************************************************************************** * 2. // MSP430F22x4 Demo - ADC10, Sample A10 Temp, Set P1.0 if Temp ++ ~2C 3. // 4. // Description: Set ADC10 and the integrated temperature sensor to detect 5. // temperature gradients. The temperature sensor output voltage is sampled 6. // ~ every 120ms and compared with the defined delta values using an ISR. 7. // (ADC10OSC/4)/64 determines sample time which needs to be greater than 8. // 30us for temperature sensor. 9. // ADC10 is operated in repeat-single channel mode with the sample and 10. // convert trigger sourced from Timer_A CCR1. The ADC10IFG at the end 11. // of each converstion will trigger an ISR. 12. // ACLK = n/a, MCLK = SMCLK = default DCO ~1.2MHz, ADC10CLK = ADC10OSC 13. // 14. // MSP430F22x4 15. // ----------------16. // /|\| XIN|17. // | | | 18. // --|RST XOUT|19. // | | 20. // |A10 (Temp) P1.0|-->LED 21. // 22. // A. Dannenberg 23. // Texas Instruments Inc. 24. // April 2006 25. // Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.41A 26. //***************************************************************************** * 27. #include "msp430x22x4.h" 28. 29. static unsigned int FirstADCVal; // holds 1st ADC result 30. 31. #define ADCDeltaOn 3 // ~ 2 Deg C delta for LED on 32. 33. void main(void) 34. { 35. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog 36. ADC10CTL1 = ADC10DIV_3 + INCH_10 + SHS_1 + CONSEQ_2; // TA trig., rpt, A10 37. ADC10CTL0 = SREF_1 + ADC10SHT_3 + REF2_5V + ADC10IE + REFON + ADC10ON; 38. TACCR0 = 30; // Delay to allow Ref to settle 39. TACCTL0 |= CCIE; // Compare-mode interrupt 40. TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode 41. __bis_SR_register(CPUOFF + GIE); // LPM0, TA0_ISR will force exit 42. TACCTL0 &= ~CCIE; // Disable timer Interrupt 43. ADC10CTL0 |= ENC; 44. TACCTL1 = OUTMOD_4; // Toggle on EQU1 (TAR = 0) 45. TACTL = TASSEL_2 + MC_2; // SMCLK, cont-mode 46. while (!(ADC10IFG & ADC10CTL0)); // First conversion? 47. FirstADCVal = ADC10MEM; // Read out 1st ADC value 48. P1OUT = 0x00; // Clear P1

49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67.

P1DIR = 0x01; // P1.0 as output __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt } #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { if (ADC10MEM >= FirstADCVal + ADCDeltaOn) P1OUT |= 0x01; // LED on else P1OUT &= ~0x01; // LED off } #pragma vector=TIMERA0_VECTOR __interrupt void TA0_ISR(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return }

#include "msp430x22x4.h" /* Main program: entry point */ void delay (void) { int i; i = 30000; do i--; while (i != 0); } int main (void) { int gothit = -1; int i; WDTCTL = WDTPW + WDTHOLD; /* turn off watchdog */ P1DIR P1REN P2DIR P2OUT P2REN P1IE P1IFG P1IES |= |= |= |= |= |= |= |= 0x03; 0x04; 0x08; 0x08; 0x16; 0x04; 0x04; 0x04; // set red and green led for output // connect resistor to switch

// SW Delay

// Enable switch as interrupt // Set interrupt flag to 1 for switch // Set intterupt select high to low edge

//ADC stuff ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE; TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode.

__bis_SR_register(CPUOFF + GIE); will force exit TACCTL0 &= ~CCIE; Interrupt ADC10AE0 |= 0x01; select P2OUT ^= 0x08;

// LPM0, TA0_ISR // Disable timer // P2.0 ADC option

__enable_interrupt(); // enable all interrupts // while (1) { // //P2OUT ^= 0x08; using exclusive-OR // // if (P2IN & 0x16) { // P1OUT ^= 0x02; // i = 10000; // do i--; // while (i != 0); // } // } while(1) { ADC10CTL0 |= ENC + ADC10SC; conversion start __bis_SR_register(CPUOFF + GIE); will force exit if (ADC10MEM < 0x3FC /*0x3E8*/) { = A0 > 1.65V? P1OUT &= ~0x01; off P1OUT |= 0x02; gothit++; for (i = 0; i < gothit; i++) { P1OUT |= 0x01; P1.0 LED on delay(); P1OUT &= ~0x01; delay(); } P1OUT &= ~0x02; delay(); } // } else { // //P1OUT &= ~0x02; // } } } // Port 2 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { P2OUT ^= 0x08; //Toggle laser state P1IFG &= ~0x04; // Toggle interrupt edge P1IES ^= 0x04; } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) // Toggle P1.0

// SW Delay

// Sampling and // LPM0, ADC10_ISR // ADC10MEM // Clear P1.0 LED

// Set

{ __bic_SR_register_on_exit(CPUOFF); 0(SR) } #pragma vector=TIMERA0_VECTOR __interrupt void TA0_ISR(void) { TACTL = 0; LPM0_EXIT; // Clear CPUOFF bit from

// Exit LPM0 on return

Recuperar escrever na memoria

#include "msp430g2231.h"..... #define info_seg_B 0x1080 unsigned int storedVlvOffPosition; unsigned int storedVlvOnPosition; // Declare Flash Subroutines void read_segment(int address); void erase_segment(int address); void write_segment(int address, int valveOff, int valveOn);

void main(void) { WDTCTL = WDTPW + WDTHOLD; ..... // Call the flash functions.... read_segment(info_seg_B); // Read the previously stored valve positions from flash

erase_segment(info_seg_B); prepare write new values

// Erase the segment to

write_segment(info_seg_B, vlvOffPosition, vlvOnPosition); // Write the new valve positions to flash } // End of main()

//********************************************************* **************** // Read stored valve positions from the Information flash segment B //********************************************************* **************** void read_segment(int address) { int *Flash_ptr; // Flash pointer Flash_ptr = (int *)address; // Initialize flash pointer storedVlvOffPosition = *Flash_ptr; Flash_ptr = Flash_ptr + 2; // Increment the flash pointer to next integer storedVlvOnPosition = *Flash_ptr; } //********************************************************* **************** // Erase Information Flash segment B //********************************************************* **************** void erase_segment(int address) { int *Flash_ptr; // Flash pointer

Flash_ptr = (int *)address; // Initialize Flash pointer to 0x1080 FCTL1 = FWKEY + ERASE; // Set Erase bit FCTL3 = FWKEY; // Clear Lock bit *Flash_ptr = 0; // Dummy write to erase Flash segment FCTL3 = FWKEY + LOCK; // Set LOCK bit }

//********************************************************* *************** // Write valve on/off positions to the Information flash Segment B //********************************************************* ***************

void write_segment(int address, int valveOff, int valveOn) { int *Flash_ptr; // Flash pointer Flash_ptr = (int *)address; // Initialize Flash pointer to 0x1080 FCTL3 = FWKEY; // Clear Lock bit FCTL1 = FWKEY + WRT; // Set WRT bit for write operation *Flash_ptr = valveOff; // Write value to flash Flash_ptr = Flash_ptr + 2; // Increment the flash pointer to next even address *Flash_ptr = valveOn; // Write value to flash FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY + LOCK; // Set LOCK bit }

S-ar putea să vă placă și