Sie sind auf Seite 1von 5

/* Temperature acquisition program based on Polling_105.c Use the 10msecs Output Compare interrupt to do the sampling. C.A.Maynard v1.

06 20110131 */ #include "clic2_225.h" #include "LCD2_302.h" #include "temperature.h" #include "extras.h" #define databus16 // #define databus8 // Global variables enum bool switchesFlag, keypadFlag, temperatureFlag; uc_8 switchesValue, keypadValue; ui_16 temperatureValue; // Local Prototypes void ECT0_init(void); // Interrupt prototype declaration #pragma vector=ECT0 __interrupt void ECT0_InterruptRoutine( void ); // Keypad routines in clic2_225.h and c // Useful Magic constants #define tenmsecs (20000) #define degree (0xDF) #define DegC (0x43) // Strings uc_8 Gooday[] = "Gooday"; void main (void) { uc_8 segID = 0, ascii; uc_8 LCDdisplayCount = 0; enum bool temp; // System initialisation // **************************************************************** // The following code MUST be at the beginning of the program // Configure the microcontroller for correct expanded mode operation #ifdef databus16 MODE=0xE0; /* Expanded wide */ #endif #ifdef databus8

MODE = 0xA0; /* Expanded narrow */ #endif PEAR=0x04; /*Switch ON Clock and R/W signal-address 0AH*/ MISC=0x0F; /*Disable first half of Flash EEPROM-address 13H*/ // ***************************************************************** // As a precaution turn off the Peltier heater system and the DAS interrupt system DDRS=0x0FF; //Port S as an output PTS=0x00; //Switch Off Temperature unit // ***************************************************************** // Initialise the timer subsystem for the timer functions TSCR2 = 0x00; // Timer clock is 2MHz TSCR1 = 0x90; // Enable timer and fast clear of interrupt flag! // ***************************************************************** // Initialise PORTP to set bits 76 as output and a value of 0. DDRP = 0xC0; PTP = 00; // All other timer channel operations must not interfere with each other!!! // Peripheral initialisation ECT0_init(); // Do timer ones first! temperatureInit(); switchesInit(); LEDsInit(); keypadInit(); sevenSegInit(); LCDinit(); // Set up the LCD display LCDhome(); // Initialise the time waiting system to start polling after 10msecs TC0 = TCNT + tenmsecs; __enable_interrupt(); LCDprintstring(Gooday); // main loop (Background task) uc_8 state=0;//Each key press represents a particular state ui_16 keyTemperature=0;//Records the keypad entered Temperature uc_8 *keyTempDisplay; // Records ascii values for the keypad entered Temperature while (true) { // Get the switch input and display if( switchesFlag) { temp = LEDsPut(switchesValue); // the return value is not used in this code. switchesFlag = false; } else {} // Get the keypad input and display if(keypadFlag ) { //This condition becomes true for each individual key press

switch(state){ case 0 : { //First digit of the temperature. If temperature ranges from 01-09 user may have to input 0 in this state. if(keypadValue < 0x0A){ // No hexa-decimal inputs are allowed in the state 0. keyTemperature = keypadValue*10; // First state indicates base 10 decimal values. state++;//Successful input on state 1 changes state to next. } break; }//case 0 case 1 : {//Second digit of the temperature. if(keypadValue < 0x0A){ //No hexa-decimal inputs are allowed in the state 0. keyTemperature = keyTemperature + keypadValue; // Second state indicates base 1 decimal values. state++; // Successful input on state 2 changes state to next. } break; }//case 1 case 2 : { // Third state to analyze the keypad input values. if(keypadValue == 0x0E) // If user presses "E" on the keypad. { state=0; // On the next keypad input, user may enter a new temperature. LCDhome(); // Results on this temperature will display from the begining of the line 1. if(keyTemperature > 0x31 || keypadValue < 0x01){ // Detect an invalid temperature. LCDprintstring("Invalid Range"); // Display error messages. Error message should not exceed 16 characters. LCDdisplaycount=13; // Record position of the cursor after printing the message. } else{ // Detect a valid temperature. keyTempDisplay=StrfromUns(keyTemperature); // Convert temperature in to an LCD displayable string. LCDprintstring(keyTempDisplay); //Display keypad entered temperature. LCDdisplaycount=2; //Record position for the next character (degree and DegC symbols). LCDcmd((uc_8)(LCDDDAddr + LCDdisplayCount));

//Issu command to the LCD to set the cursor on proper location of display. LCDchrout(degree);// Display the symbol for degree. See Magic constants. LCDchrout(DegC);// Display the character C LCDdisplaycount=4; // Record position of the cursor } } else if(keypadValue == 0x0D){ // If user presses "D" on teh keypad. keyTemperature=keyTemperature/10; // Delete last digit of the temperature. state=1; // allow user to re-enter last digit. } else if(keypadValue == 0x0C){ // If user presses "C" keyTemperature =0; // Clear recorded temperature. state=0; // Allow user to re-enter temperature. } else{} break; }//case 2 }//switch }//keypadFlag else{}// Preserver Misra-C rule 60 (All if, else if constructs should contain a final else clause) // Anything else to be done if(temperatureFlag){ LCDline2();// Move to the second line of the LCD display uc_8 *displayTemp = StrfromUnsFixedPoint(temperatureValue,2); // Display an integer representing the temperatureValue. LCDprintstring(displayTemp); //displayTemp points to temperature value formatted as a string with two decimal places. //Scaling and type conversion is done in extras.c. LCDcmd((uc_8)(LCDDDAddr + 0x45));//Issue command to the LCD to set the cursor on proper location of the display. LCDchrout(degree);// Display the symbol for degree. See Magic constants. LCDchrout(DegC);// Display the character C temperatureFlag = false; } } // end while (true)

} // end main // Output Compare Channel 0 Initialisation void ECT0_init(void) { // All other timer channel operations must not interfere with each other!!! TIOS |= 0x01; // Output Compare 0 TFLG1 |= 0x01; // Clear channel 0 compare flag; TCTL2 |= 0x01; // Toggle Output on compare (for external viewing oly) TIE |= 0x01; // Enable channel 0 interrupt } __interrupt void ECT0_InterruptRoutine( void ) { // Foreground task get switches input every tenmsecs static uc_8 TempTime = 0; switchesFlag = switchesGet(&switchesValue); TempTime++; // TempTime increases every 10 ms. if(TempTime==10){ // TempTime = 10 indicates a total of 100 ms. TempTime=0; // Reset TempTime to facilitate calculation of next 100ms. temperatureFlag=temperatureGet(&temperatureValue): // Retreive temperature every 100 ms. } TC0 += tenmsecs; // Updates compare }

Das könnte Ihnen auch gefallen