⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_glcd.c

📁 TI公司的CCS一些常用的函数库
💻 C
字号:
/////////////////////////////////////////////////////////////////////////
////                           EX_GLCD.C                             ////
////                                                                 ////
//// This example program demonstrates the use of a graphic LCD.     ////
//// A reading is taken by the analog to digital converter and       ////
//// displayed on the LCD. A bar shows the current reading relative  ////
//// to the minimum and maximum values. If the reading is greater    ////
//// than 4 volts, a warning message is displayed. A clock timer     ////
//// demonstrates the use of the circle and line functions and shows ////
//// that the program is active.                                     ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services         ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////

#if defined(__PCM__)
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#include <glcd.c>
#include <math.h>

void displayVoltage(int data) {
   char voltValue[9];                                // Stores the converted number
   sprintf(voltValue, "%f", (float)data * .01960784);// Converts number to text
   voltValue[4] = '\0';                              // Limit shown digits to 3
   glcd_rect(45, 18, 69, 25, YES, OFF);              // Clear the old number
   glcd_text57(45, 18, voltValue, 1, ON);            // Write the new number
}

void main() {
   int   value1 = 0, value2 = 0, Warn = 0;
   char  voltText[] = "Volts", warning[] = "Warning";
   float theta = 0;

   setup_adc_ports(RA0_ANALOG);                 // Set the ADC to read A0
   setup_adc(ADC_CLOCK_INTERNAL);               // Set the ADC clock
   set_adc_channel(0);                          // Set ADC to use channel 0
   glcd_init(ON);                               // Must initialize the LCD
   glcd_rect(1, 5, 126, 15, NO, ON);            // Draw the rectangle around the bar
   glcd_text57(70, 18, voltText, 1, ON);        // Write "Volts" on the LCD
   glcd_circle(30, 47, 10, NO, ON);             // Draw the clock circle

   do{value1 = read_adc();                      // Read a value from the ADC
      displayVoltage(value1);                   // Display the reading
      value1 = (value1 > 250) ? 250 : value1;   // Keep the value under 251

      if(value1 > 200 && !Warn) {               // Check if reading is above 200
         glcd_rect(45, 38, 124, 55, YES, ON);   // Draw a filled black rectangle
         glcd_text57(47, 40, warning, 2, OFF);  // Write "Warning" on the LCD
         Warn = 1; }
      else if(value1 <=200){
         glcd_rect(45, 37, 125, 55, YES, OFF);  // Draw a filled white rectangle
         Warn = 0; }
      
      // The following 3 lines make the clock hand spin around
      glcd_line(30, 47, 30+(int)(8*sin(theta)+.5), 47-(int)(8*cos(theta)+.5), OFF);
      theta = (theta > 5.9) ? 0 : (theta += .3);
      glcd_line(30, 47, 30+(int)(8*sin(theta)+.5), 47-(int)(8*cos(theta)+.5), ON);

      glcd_rect(value1/2, 6, value2/2, 14, YES, OFF); // Clears the old bar
      glcd_rect(1, 6, value1/2, 14, YES, ON);         // Draws a new bar
      value2 = value1;                                // Set old value to new
      delay_ms(150);                                  // Delay for a bit
   } while (TRUE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -