Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------
- // Example demo C code for ECE 3362 Micrcontrollers class
- // Provides simple example of using both Timers with Interrupts in C code
- //
- // Function: Sits in an IDLE LOOP and the LED's are controlled
- // using the two timers independently. Timing of the two LED's is
- // independent and can be controlled by changing the timer parameters, or
- // by changing the MAX_COUNT values for each of the LED's
- //
- // PB interrupt on Port 1 is set up, but only used to increment global
- // variable Count. Could be used to control mode of operation - that is
- // left as an exercise for the student
- //
- //
- //
- // Target: TI LaunchPad development board with MSP430G2553 device
- //
- // Date: Nov 13, 2015
- // Last Revision: 1.0
- // Written by: Dr. Michael Helm, ECE dept, Texas Tech University
- // Adapted from: TI example code, non-specific
- // Assembler/IDE: IAR Embedded Workbench 5.5
- //
- // HW I/O assignments:
- // P1.0 LED1 (Active HIGH)RED
- // P1.1 not used
- // P1.2 not used
- // P1.3 PushButton (Active LOW) (internal Pullup Enabled)
- // P1.4 not used
- // P1.5 not used
- // P1.6 LED2 (Active HIGH)GREEN
- // P1.7 not used
- //
- // P2.0 not used
- // P2.1 not used
- // P2.2 not used
- // P2.3 not used
- // P2.4 not used
- // P2.5 not used
- // P2.6 not used
- // P2.7 not used
- #include <msp430g2553.h>
- //----------------------------------------------------------------------
- // CONSTANT DEFINITIONS
- #define MAX_RED_COUNT 2 // max number of Timer0 interrupts
- #define MAX_GRN_COUNT 10 // max number of Timer1 interrupts
- #define MAX_TA0_COUNT 5000 // max count for Timer0
- #define MAX_TA1_COUNT 8000 // max count for Timer1
- #define LED1RED (0x01u) //(%00000001) //Port pin position P1.0
- #define LED2GRN (0x40u)//(%01000000) //Port pin position P1.6
- #define PUSHBUTTON (0x08u) //(%00001000) //Port pin position P1.3
- //----------------------------------------------------------------------
- // GLOBAL VARIABLE definition
- int Count = 0;
- int CountRED = 0;
- int CountGRN = 0;
- int CurrentDigitPos = 0;
- int CurrentDigitValue = 0;
- int DisplayValue = 0;
- int DispCopy = 0;
- int SegPatternTable[14] = {0xC0u, 0xF9u, 0xA4u, 0xB0u, 0x99u, 0x92u, 0xF8u, 0x80u, 0x98u, 0x86u, 0xC6u, 0xBFu, 0xFFu};
- //----------------------------------------------------------------------
- // FUNCTIONs
- // delay_1 - delays by an amount proportional to the argument passed in
- // returns- nothing
- //----------------------------------------------------------------------
- void delay_1(int DelayTime)
- {
- for(int i = 0;i < DelayTime;i++);
- }; // end delay_1 function
- //----------------------------------------------------------------------
- //WriteNextDigitToDisplay
- // accomplishes - Writes next digit to the expansion bd display
- // uses: R15, global variable CurrentDigitPos, CurrentDigitValue, DisplayValue, DispCopy
- //----------------------------------------------------------------------
- void WriteNextDigitToDisplay(void)
- {
- P2OUT = 31; //set digport to turn LED's off to fix ghosting
- if (CurrentDigitPos == 0)
- {
- DispCopy = DisplayValue;
- DispCopy &= 0x000Fu;
- P1OUT = SegPatternTable[DispCopy];
- P2OUT = 0x1u;
- }
- if (CurrentDigitPos == 1)
- {
- DispCopy = DisplayValue;
- DispCopy &= 0x00F0u;
- DispCopy >>= 4;
- P1OUT = SegPatternTable[DispCopy];
- P2OUT = 0x02u;
- }
- if (CurrentDigitPos == 2)
- {
- DispCopy = DisplayValue;
- DispCopy &= 0x0F00u;
- DispCopy >>= 8;
- P1OUT = SegPatternTable[DispCopy];
- P2OUT = 0x04u;
- }
- if (CurrentDigitPos == 3)
- {
- DispCopy = DisplayValue;
- DispCopy &= 0xF000u;
- DispCopy >>= 12;
- P1OUT = SegPatternTable[DispCopy];
- P2OUT = 0x08u;
- }
- CurrentDigitPos++; // set up for next digit position for next time
- if(CurrentDigitPos==4)
- CurrentDigitPos=0;
- }; // end WriteNextDigitToDisplay
- //HowtoCALL
- //WriteNextDigitToDisplay(); // write the next digit
- // none used here
- // MAIN PROGRAM
- void main( void )
- {
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- P1DIR = 0xFFu; // both LEDs
- P2DIR = 0x1Fu;
- P2OUT = 0xE0;
- P2SEL &= 0xC0;
- P2REN |= 0xC0; // resistor enable for PB position
- P1OUT = 0XFF;
- P2OUT = 0XFF;
- P2IE |= 0xC0; // PB interrupt enabled
- P2IES |= 0xC0; // PB Hi/lo edge
- P2IFG = 0; // clear ALL the Int Flags on Port 1
- // end if
- // end while
- // Set up Timers
- // TimerA0
- TA0CCR0 = MAX_TA0_COUNT; // load a count "up to"value into timer
- TA0CTL = TASSEL_2+ID_3 + MC_1; // SMCLK, input div = 8, upmode
- TA0CCTL0 = CCIE; // interrupt enabled for Timer0
- // TimerA1
- TA1CCR0 = MAX_TA1_COUNT; // load a count "up to"value into timer
- TA1CTL = TASSEL_2+ID_3 + MC_1; // SMCLK, input div = 8, upmode
- TA1CCTL0 = CCIE; // interrupt enabled for Timer1
- Count = 0;
- CountRED = 0; // initialize CountRED to zero
- CountGRN = 0; // initialize CountGRN to zero
- _BIS_SR(GIE); // enable general interrupts
- while(1){ // forever idle loop - Timer interrupts control the action
- WriteNextDigitToDisplay();
- }
- } // end of MAIN
- //ISR's
- //Timer0_A0 ISR
- #pragma vector=TIMER0_A0_VECTOR // this line tells the C compiler to put
- // the start address of the following ISR
- // into the Interupt Vector table
- __interrupt void Timer_A0_ISR (void) // required syntax for first line of ISR
- { WriteNextDigitToDisplay();
- }
- //ISR's
- //Timer0_A1 ISR
- #pragma vector=TIMER1_A0_VECTOR // this line tells the C compiler to put
- // the start address of the following ISR
- // into the Interupt Vector table
- __interrupt void Timer_A1_ISR (void) // required syntax for first line of ISR
- {
- }
- // Port 1 interrupt service routine
- #pragma vector=PORT1_VECTOR
- __interrupt void Port_2(void)
- {
- // Keep track of number of times PB is pressed
- P1IFG = 0; // clear ALL the Int Flag bits on Port 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement