/* Program name: lcd1602,4-bit data bus LCD clock, stc12c5a60s2 10-bit ADC function program writing time: October 4, 2015 hardware support: LCD1602 LCD screen Stc12c5a60s2 external 12MHZ crystal oscillator Wiring definition: db7 --> p1^7 DB6 --> P1^6 DB5 --> P1^5 DB4 --> P1^5 RS --> P3^2; RW --> P3^3; E --> P3^4; ADC --> P1^0; potentiometer voltage for 0--5v */#include < Stc12c5a60s2. h> //Single chip head text#include <intrins.h> //51 basic operations (including _NOP_ empty functions) typedef unsigned char uint8; // unsigned 8-bit integer variable //# define lcm2402_db0_db7 p1 // defines the LCM2402 data bus sbit lcm2402_rs = P3 ^ 2; // define LCM2402 's RS control line sbit lcm2402_rw = p3 ^ 3; // defines the RW control line for LCM2402 SBIT&NBSP;LCM2402_E&NBSP;&NBSP;&NBSP;&NBSP;=&NBSP;P3 ^ 4; // define LCM2402 's e control line sbit lcm2402_busy = p1 ^ 7; // define LCM2402 (associated with LCM2402_DB0_DB7) Data unsigned char time_dd,time_mo, time_yy,time_ww,time_hh,time_mm,time_ss;//set day, month, year, week, hours, minutes, seconds, and temperature storage area data bit day_bit = 0;// Number of days increase flag bit (for start of date carry) data unsigned char dis_bit = 0; //switch display of various information data unsigned char cou = 0; // Soft counter, add to 1s // definition LCM2402 instruction set for 10ms time Base signal // (see Technical Manual for details) #define CMD_clear 0x01 // Clear Screen #define cmd_back 0x02 // ddram back to 0-bit #define cmd_dec1 0x04 // ac (hands) minus 1 after reading, write left #define cmd_add1 0x06 // after reading AC (hands) plus 1, Write to the right #define cmd_dis_gb1 0x0f // OPEN Cursor _ open cursor blinking #define cmd_dis_gb2 0x0e // on display _ Open cursor _ off cursor blink #define cmd_dis_gb3 0x0c // Open Display _ off cursor-off cursor blink #define cmd_off_dis 0x08 // off Show _ off cursor-off cursor blink #define cmd_set82 0x38 // 8 bit bus _2 line display #define cmd_set81 0x30 // 8 bit bus _1 line display (top row) #define cmd_set42 0x28 // 4 bit bus _2 line display #define Cmd_set41 0x20 // 4 Bit Bus _ 1 line display (top row) #define lin_1 0x80 // 4-bit bus _1 line display (top row) #define lin_2 0xc0 // 4-bit bus _1 line display (top row) #define ct 2 // Write command, write data to e continuous milliseconds void delay_ms (unsigned int a) { Unsigned int i; while ( --a != 0) { for (i = 0; i < 600; i++) }}// lcm2402, if the LCM2402 is busy, the function waits until the non-busy state //void lcm2402_testbusy ( void) { lcm2402_db0_db7 = 0xff; //device read State lcm2402_rs = 0; LCM2402_RW = 1; LCM2402_E = 1; while (lcm2402_busy); //wait for LCM not busy LCM2402_E = 0; //}// Write command program //// write commands to LCM2402 This function requires 1 instruction set entry parameters //void lcm2402_writecmd (Uint8 lcm2402_command) { lcm2402_testbusy (); lcm2402_rs = 0; &nbsP lcm2402_rw = 0; lcm2402_db0_db7 = (LCM2402_COMMAND/16) <<4;//0x45 0x40 lcm2402_e = 1; delay_ms (CT); LCM2402_E = 0; LCM2402_DB0_DB7 = (LCM2402_COMMAND%16) <<4;//0x45 0x50 lcm2402_e = 1; delay_ms (CT); lcm2402_e = 0;} Write data to LCM2402 //void lcm2402_writedata (uint8 lcm2402_data) { Lcm2402_testbusy (); lcm2402_rs = 1; lcm2402_rw = 0; LCM2402_DB0_DB7 = (LCM2402_DATA/16) <<4; lcm2402_e = 1; delay_ms (CT); lcm2402_e = 0; LCM2402_DB0_DB7 = (lcm2402_data%16) <<4; lcm2402_e = 1; delay_ms (CT); lcm2402_e = 0;} // sends a string to the LCM with a length of 48 characters within void print (UINT8&NBSP;A,UINT8&NBSP;*STR) { lcm2402_writecmd (a | 0x80) while (*str != ') { lcm2402_writedata (*str++); } *str = 0;} Print single character program // void print2 (uint8 a,uint8 t) { lcm2402_writecmd (a | 0x80); lcm2402_writedata (t);} Define small Kanji write 16 literal characters, which can be extracted directly from the Cgram code after writing. uint8 code xword[]={ 0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, //℃, Code 0x00 0x00,0x00,0x00,0x00,0xff,0x00,0x00 , 0x00, //, Code 0x01 0x00,0x00,0x00, 0x0e,0x00,0xff,0x00,0x00, //Two, Code 0x02 0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00, //III, Code &NBSP;0X03&NBSP;&NBSP;&NBSP;&NBSP;0X00,0X00,0XFF,0XF5 , 0xfb,0xf1,0xff,0x00, //four, code 0x04 0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00, //Five, Code 0x05 0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00, //Six, Code 0x06 0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00, //Day, Code 0x07};void cgramwrite (void) { // loading cgram // uint8 i; lcm2402_writecmd (0x06); // cgram address Auto plus 1 lcm2402_writecmd (0x40); // cgram address is set at 00 for (i=0;i< 64;i++) { lcm2402_writedata (xword[i]);// write data by array }}// lcm2402 Initialize //(user customizable, add * program line must be retained but modifiable) void lcm2402_init (void) { lcm2402_ Writecmd (CMD_SET42); //* display mode setting: Displays 2 rows, each character is 5*7 pixels lcm2402_writecmd (cmd_set42); * display Mode settings: Display 2 rows, each character is 5*7 pixels lcm2402_writecmd (cmd_clear); // display clear screen lcm2402_writecmd (cmd_back); //* data pointer points to 1th line 1th character position lcm2402_writecmd (CMD_ ADD1); // display cursor movement settings: text does not move, the cursor moves right lcm2402_writecmd (CMD_DIS_GB3); // display on and cursor settings: Display on, cursor on, flashing open cgramwrite (); // write custom characters to Cgram}bit Isleapyear (void) { //calculates if year is year unsigned int a; a = 2000+time_yy;//Plus 2000 represents the full year if ((a%4==0 && a%100!=0) | | (a%400==0)) { //Formula return 1;//is the annual return 1 }else{ return 0;// Not the run-year return 0 }} void month_day (void) { unsigned char mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31}; time_dd++;// Day plus 1 time_ww++;//week value plus 1 if (time_ww > 7) { time_ww = 1;//period value limit } if (Time_dd > mon_day[time_mo-1]) {//Check whether the day is greater than the maximum value of the current month if (Isleapyear () &&time_mo==2) { Calculate whether this month is run year of February time_dd = 29;//if is run year is February, then days for 29 }else{ time_dd = 1; //otherwise equals 1 time_mo++;//month plus 1 if (time_mo > 12) { TIME_ mo = 1; //if month is greater than 12 month equals 1 time_yy++;//year plus 1 (unlimited add) } } }} void init ( void) { //power-on initialization TMOD = 0x11; // Timer/Counter 0, 1 working in mode 1 th0 = 0x3c; // presets Generate 50ms time-based signals TL0 = 0xb0; EA = 1; // Total Interruption ET0 = 1; // Timer/Counter 0 allow interrupts TR0 = 1; // Open/Close Timer/Counter 0 time_dd = 4; //time in the first use value, The value of the day is automatically recorded in the EEPROM time_mo = 10; //initial time: May 18, 2009 Monday, 20:13 40 seconds TIME_YY = 15; TIME_WW = 1; time_hh = 0; time_mm = 35; time_ss = 0;} Show items Time section inFirst row full line display time Void realtime_display (void) { print (0x80, "" "); print2 (0x82,time_yy/10+0x30); print2 (0x83,time_yy%10+0x30); print (0x84, "/"); // Show Year print2 (0x85,time_mo/10+0x30); Print2 (0x86,time_mo%10+0x30); print (0x87, "/"); // Display month print2 (0x88,time_dd/ 10+0X30); print2 (0x89,time_dd%10+0x30); print (0x8b, " ["); // display [ print2 (0X8C,TIME_WW%10); //Week print (0x8d, "]"); // show] print2 (0x40,time_hh/10+0x30);//hour print2 (0x41,time_hh%10+0x30); print (0x42, ":"); // display Cgram First font ":" print2 ( 0X43,TIME_MM/10+0X30);//min print2 (0x44,time_mm%10+0x30); print (0x45, "."); // display Cgram First font "." print2 (0x46,time_ss/10+0x30);//Sec print2 (0x47, TIME_SS%10+0X30);} /*** function Name: 10-bit A/D conversion initialization function with:read_init (?); Ginseng Number: Input port (0000 0xxx where xxx is the set input port number, available in decimal 0~7, 0 means p1.0,7 represents P1.7) return value: No knot Result: Turn on the ADC function and set the input port for ADC Note: The corresponding IO interface needs to be set to a high impedance input mode (e.g.:p1m1 = 0x01;) when using the ADC function ***/void read_ init (Unsigned char cha) { unsigned char ad_fin=0; //Storage A/D conversion flag CHA &= 0x07; //Select one of the 8 interfaces of the ADC (0000 0111 0 High 5-bit) &NBSP;ADC_CONTR&NBSP;=&NBSP;0X40;&NBSP;//ADC conversion speed (0xx0 0000 which xx control speed, please set according to data sheet) _nop_ (); adc_contr |= cha; // Select a/d current channel _nop_ (); adc_contr |= 0x80; //start A/D power delay_ms (1); //to stabilize the input voltage (1ms)}/*** function name: 10 bit A/D conversion function using:? = adc_read () number: No return value: 10-bit ADC data high (from 0 to 1023 (decimal)) Knots fruit: read out A/D conversion value for the specified ADC interface and return the numeric preparation Note: Suitable for STC12C5A60S2 Series microcontroller (must use STC12C5A60S2.h header file) ***/unsigned int adc_read (void) { // int 2 bytes, 16-bit unsigned char  ad_fin=0; //Storage A/D conversion flag ADC_CONTR |= 0x08; //start A/D conversion (0000 1000 order adcs = 1) _nop_ (); _nop_ (); _nop_ (); _nop_ (); while (ad_fin ==0) { //wait for A/D conversion to end AD_FIN = (adc_contr & 0x10); //0001 0000 test A/D conversion end no } adc_contr &= 0xe7; //1111 0111 clear Adc_flag bit, turn off A/D conversion, return (ADC_RES*4+ADC_RESL);//Returns a/D conversion result (10-bit ADC data high 8 bits in ADC_ Res, low 2 bits in ADC_RESL)//Internal register Adc_res returns the value, when reversed to int, left two bits, empty two bits out to low two bits adc_resl}void main (void) {unsigned Int m; // m must be a 16-bit data type! init ();//Initialize &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; lcm2402_init (); /lcm2402 initialization P1M1 = 0x01;// High impedance will be sensed, which one will be used to open which Read_init (0); // Select p1^0 pin while (1) { realtime_ Display (); m = adc_read ();//m must be a 16-bit data type! Print2 (0x4a,m/1000+0x30);p Rint2 (0x4B, (m%1000)/100+0x30);p Rint2 (0x4C, (m%100)/10+0x30);p Rint2 (0x4d,m%10+0x30); if (day_bit == 1) { //Check if the days are updated, the Gregorian calendar is calculated month_ Day ();//Calculate Gregorian date day_bit = 0;//date change flag position 0 } }}void tiem0 (void) after calculation is completed &NBSP;INTERRUPT&NBSP;1{&NBSP;&NBSP;&NBSP;//&NBSP;T/C0 Interrupt Service Program (generates 50MS time base signal) cou++; // Soft counter plus 1 if (cou > 19) { // count value up to (1s) cou = 0; // Soft counter Clear 0 TIME_SS++; // seconds counter plus 1 (Carry 10ms*100=1s) if (TIME_SS > 59) { // Second count to 60 time_ss = 0; // sec counter Clear 0 TIME_MM++; // counter plus 1 (carry 60s=1m) if (time_mm > 59) { // Count to 60 TIME_MM = 0; // Sub-counter Clear 0 TIME_HH++; // counter plus 1 (carry 60m=1h) if (time_hh > 23) { // Time Count to 23 TIME_HH = 0; // Time counter Zeroing day_bit = 1; //Day add-on sign } } } } TH0 = 0x3c; // Reset timing constant tl0 = 0xb0; }
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/74/0C/wKioL1YRIB2zgJzlAAKuRZMP6c4875.jpg "title=" Img_ 20151004_204356.jpg "alt=" Wkiol1yrib2zgjzlaakurzmp6c4875.jpg "/>
lcd1602,4-bit data bus LCD clock, stc12c5a60s2 10-bit ADC function program