Task: Read external 0 ~ 5 V voltage change, output to LCD display
1. Create the following schematic
2. GenerateCode
//************************************** * ************ <Br/> // main. c <br/> // latest revise: 2010-009-20 <br/> // by: Alex shoal <br/> // Email: alex.shoal@gmail.com <br/> # include <reg51.h> <br/> # include <stdio. h> <br/> # include "Global. H "<br/> # include" adc0808.h "<br/> # include" ks0066.h "// for LCD 1602 <br/> main () {<br/> // float res = 0; <br/> int res = 0; <br/> float Vol = 0.0; <br/> char PR [8]; <br/> LCD _init (); <br /> While (1) <br/>{< br/> res = read_data (); <br/> Vol = 5.0 * res/255; <br/> sprintf (PR, "%. 2f ", vol); </P> <p> // sprintf (PR," % d ", Res); <br/> // LCD _clear (); <br/> LCD _home (); <br/> print_string ("read:"); <br/> print_string (PR ); <br/> print_string ("v ");} <br/>}< br/> //***************************** * ************************ <br/> // adc0808.h <br/> // # include "Global. H "<br/> sbit start = P2 ^ 3; <br/> sbit ale = P2 ^ 4; <br/> sb It EOC = P2 ^ 5; <br/> sbit OE = P2 ^ 6; <br/> void conv_start (void); <br/> void conv_check (void ); <br/> int read_data (void ); <br/> // ------------------------------------------ <br/> // start ADC convert <br/> // convert <br/> void conv_start () {<br/> ale = 1; <br/> Start = 1; <br/> delay_ms (1); // latency when the crystal oscillator is high <br/> ale = 0; <br/> Start = 0; <br/> // delay_ms (5); // delay added when the crystal oscillator is high <br/>}< br/> int R Ead_data () {<br/> int I = 0; <br/> // delay_ms (5); <br/> // while (! EOC); // If EOC = 0, means busy <br/> OE = 1; <br/> conv_start (); <br/> while (! EOC); </P> <p> // delay_ms (5); <br/> I = P3; <br/> OE = 0; <br/> return I; <br/>}< br/> //***************************** *************************] <br/> // filename: glocal. h <br/> // latest revise: 2010-07-01 <br/> // by: Alex shoal <br/> // Email: alex.shoal@gmail.com <br/> # define uchar unsigned char <br/> # define uint unsigned int <br/> void delay_ms (INT t) {<br/> Int J; <br/> for (; t! = 0; t --) <br/> for (j = 0; j <255; j ++ ); </P> <p >}< br/> //************************** **************************** <br/> // filename: ks0066.h <br/> // latest revise: 2010-07-01 <br/> // by: Alex shoal <br/> // Email: alex.shoal@gmail.com <br/> // # include "Global. H "<br/> # define data_port P1 <br/> // define <br/> // pin definition <br/> // ------------------------------------------ <br/> sbit rs = P2 ^ 0; // data (L)/command (h) Select <br/> sbit lcdrw = P2 ^ 1; // write, low level valid <br/> sbit en = P2 ^ 2; // enables, low level valid <br/> // ========================================== ============< br/> // function declaration <br/> // ----------------------------------------------------------- <br/> void delay_ LCD (char ); <br/> void write_data (char); <br/> void write_com (char); <br/> void LCD _clear (void); <br/> void LCD _home (void ); <br/> void LCD _init (); <br/> void print_string (char *); <br/> //--------- --------------------------------- <Br/> // write data functions <br/> // compile <br/> void write_data (char dat) {<br/> rs = 1; // data <br/> lcdrw = 0; // write <br/> data_port = dat; // send data to the P port <br/> delay_ms (5 ); // Add latency when the crystal oscillator is high <br/> en = 1; <br/> delay_ms (5 ); // Add a delay when the crystal oscillator is high <br/> en = 0; // disable enabling <br/>}< br/> // ------------------------------------------------ <br/> // write command functions <br/> //---------------------------------- ---------- <Br/> void write_com (char com) {<br/> rs = 0; // command <br/> lcdrw = 0; // write <br/> data_port = com; <br/> delay_ms (5); // Add latency when the crystal oscillator is high <br/> en = 1; <br/> delay_ms (5); // added latency when the crystal oscillator is high <br/> en = 0; <br/>}< br/> void LCD _clear (void) {<br/> write_com (0x01); <br/>}< br/> void LCD _home (void) {<br/> write_com (0x02 ); <br/>}< br/> // -------------------------------------------- <br/> // 1602 initialize the function <br/> //--------------------------- ----------------- <Br/> void LCD _init () {<br/> lcdrw = 0; <br/> rs = 0; <br/> write_com (0x36 ); // set text home address <br/> // write_com (0x0f); // display switch, display on, cursor on, blink on. <br/> write_com (0x0c); // display on, cursor off, blink off <br/> write_com (0x38 ); <br/> write_com (0x06); // input set. <br/> write_com (0x01); // screen clear <br/> write_com (0x80); // ddram ad set to 00 H, <br/>}< br/> void print _ String (char * Str) {<br/> int I; <br/> for (I = 0; STR [I]! = 0; I ++) <br/>{</P> <p> write_data (STR [I]); <br/>}< br/> //************************ ********************************
3. Run
4. Note.
On the out1 and out8 of adc0808, the former is actually a high position, and the latter is a low position. Therefore, the connection to the P port should be reversed. Otherwise, the data displayed will be messy and it seems that there are no rules.
At the beginning, I thought it wasProgramThere is a problem in it, which wastes a lot of time.
In fact, datasheet also has instructions, but it is not easy to attract attention. See below.
LSB (least significant bit), meaning the lowest valid bit;MSB (most significant bit), meaning the highest valid bit