The microcontroller source program is as follows:
#include #include #include #include #define uchar unsigned char #define uint unsigned int #define ulong unsigned long #define BUSY 0x80 //Constant definition #define DATAPORT P0 sbit Alarm_led_red = P1^6; //Red light alarm when the pressure gauge exceeds the maximum value sbit Alarm_led_green=P1^7; //Street light alarm when pressure gauge is lower than minimum value sbit LCM_RS=P2^0; sbit LCM_RW=P2^1; sbit LCM_EN=P2^2; sbit k1=P2^3; sbit k2=P2^4; sbit k3=P2^5; sbit k4=P2^6; sbit ADCS =P3^5; //ADC0832 chip seclect sbit ADCLK =P3^6; //ADC0832 clock signal sbit ADDI =P3^7; //ADC0832 k in sbit ADDO =P3^7; //ADC0832 k out uchar ad_data; //sampling value storage unsigned char ad_alarm; //alarm value storage unit unsigned char press_bai=0; //display value hundreds unsigned char press_shi=0; //display value ten digit unsigned char press_ge=0; //display value units unsigned char press_dot=0; //display value tenth place uchar code str0[]={"Press: . kpa. "}; uchar code str1[]={"ZKB: 00 % "}; uchar ZKB[2]; uchar HighRH = 0; uchar HighRL = 0; uchar LowRH = 0; uchar LowRL = 0; unsigned int a; uchar rate; sbit PWMOUT = P1^0; uchar step, rec_data,flag; uchar Adc0832(unsigned char channel); void delay(uint); void lcd_wait(void); // detect busy subroutine void delay_LCM(uint); //delay subroutine void initLCM(void); //initialization subroutine void WriteCommandLCM(uchar WCLCM,uchar BusyC); //Write command to LCM subroutine void WriteDataLCM(uchar WDLCM); //Write data to LCM subroutine void DisplayOneChar(uchar X,uchar Y,uchar DData); //Display a character subfunction at the specified coordinates void DisplayListChar(uchar X,uchar Y,uchar code *DData); //Sub-function to display a string of characters at the specified coordinates void display(void); //system display subfunction void ConfigPWM(uchar rate); void alarm(void); void data_pro(void); void ConfigPWM(uchar dc); void key_setPWM(void); uchar duty_cycle(uchar ad_data); void display_zkb(void); void zkb_pro(void); void chuankou_init(); /**********main function************/ void main(void) { delay(50); initLCM( ); chuankou_init(); WriteCommandLCM(0x01,1); //Clear screen display DisplayListChar(0,0,str0); DisplayListChar(0,1,str1); delay(10); PWMOUT=1; EA = 1; ConfigPWM(rate); while(1) { ad_data = Adc0832(0); // Initialize the sampling value storage unit to 0 alarm(); data_pro(); zkb_pro(); display(); display_zkb(); a=duty_cycle(ad_data); ConfigPWM(a); key_setPWM(); if(a!=rate) { ConfigPWM(rate); } if(RI==1)//Serial port { RI=0; rec_data=SBUF; step=rec_data*2; SBUF=rec_data; } } } void TIME_INIT() { TMOD|=0X02; IT0=1; IT1=1; TR1=1; EX1=1; EX0=1; ET0=1; TR0=1; EA=1; } void chuankou_init() { SCON|=0X50; TMOD|=0X20; PCON|=0X80; TH1=0XFA; TL1=0XFA; TR1=1; SM0=0; SM1=1; REN=1; EA=1; ES=1; } /********Delay K*1ms,12.000mhz**********/ void delay(uint x) { uchar i; while(x--) { for(i = 0;i < 120;i++) {;} } } /**********Write data to ICM subfunction************/ void WriteCommandLCM(uchar WCLCM,uchar BusyC) { if(BusyC) lcd_wait(); //Detect busy signal DATAPORT=WCLCM; LCM_RS=0; LCM_RW=0; LCM_EN=1; _nop_(); _nop_(); _nop_(); LCM_EN=0; } /**********????LCM???************/ void WriteDataLCM(uchar WDLCM) { lcd_wait() ; DATAPORT=WDLCM; LCM_RS=1; //Select data register LCM_RW=0; //write mode LCM_EN=1; _nop_(); _nop_(); _nop_(); LCM_EN=0; } /***********lcm internal waiting procedure*************/ void lcd_wait(void) { DATAPORT=0xff; //If the MCU outputs a low level before reading the LCD, and the LCD reads a high level, there will be a conflict, and the Proteus simulation will display a logical yellow LCM_EN=1; LCM_RS=0; LCM_RW=1; _nop_(); _nop_(); _nop_(); while(DATAPORT&BUSY) { LCM_EN=0; _nop_(); _nop_(); LCM_EN=1; _nop_(); _nop_(); } LCM_EN=0; } /**********LCM??????***********/ void initLCM( ) { DATAPORT=0; delay(15); WriteCommandLCM(0x38,0); //Three-time display mode, do not detect busy signal delay(5); WriteCommandLCM(0x38,0); delay(5); WriteCommandLCM(0x38,0); delay(5); WriteCommandLCM(0x38,1); //8bit data transmission, two-line display, 5*7 font, busy signal detection WriteCommandLCM(0x08,1); //Turn off the display and detect the busy signal WriteCommandLCM(0x01,1); //Clear the screen and detect busy signal WriteCommandLCM(0x06,1); //Display cursor right move setting WriteCommandLCM(0x0c,1); //Display screen is turned on, cursor is not displayed, not blinking, busy signal is detected } /****??????????????****/ void DisplayOneChar(uchar X,uchar Y,uchar DData) { Y&=1; X&=15; if(Y)X|=0x40; //If Y is 1 (display the second line) address decoding + 0x40 X|=0x80; //Instruction code is address code + 0x80 WriteCommandLCM(X,0); WriteDataLCM(DData); } /*******?????????????*****/ void DisplayListChar(uchar X,uchar Y,uchar code *DData) { uchar ListLength=0; Y&=0x01; X&=0x0f; while(X<16) { DisplayOneChar(X,Y,DData[ListLength]); ListLength++; X++; } } /*****************???????********************/ void display(void) { WriteCommandLCM(0x0c,1); //Display screen is turned on, cursor is not displayed, not blinking, busy signal is detected DisplayListChar(0,0,str0); DisplayListChar(0,1,str1); DisplayOneChar(7,0,press_bai+0x30); DisplayOneChar(8,0,press_shi+0x30); DisplayOneChar(9,0,press_ge +0x30); DisplayOneChar(10,0,056); DisplayOneChar(11,0,press_dot+0x30); } void display_zkb(void) { WriteCommandLCM(0x0c,1); DisplayListChar(0,1,str1); DisplayOneChar(7,1,ZKB[0]+0x30); DisplayOneChar(8,1,ZKB[1]+0x30); delay(1000); //Stable display } /************?ADC0832??************/ uchar Adc0832 (unsigned char channel) //AD conversion and return the result { uchar i=0; uchar j; uint dat=0; uchar ndat=0; if(channel==0)channel=2; if(channel==1)channel=3; ADDI=1; _nop_(); _nop_(); ADCS=0; //Pull CS end low _nop_(); _nop_(); ADCLK=1; //Pull up the CLK terminal _nop_(); _nop_(); ADCLK=0; //Pull down the CLK end to form a falling edge 1 _nop_(); _nop_(); ADCLK=1; //Pull up the CLK terminal ADDI=channel&0x1; _nop_(); _nop_(); ADCLK=0; //Pull down the CLK end to form a falling edge 2 _nop_(); _nop_(); ADCLK=1; //Pull up the CLK terminal ADDI=(channel>>1)&0x1; _nop_(); _nop_(); ADCLK=0; //Pull down the CLK end to form a falling edge 3 ADDI=1; //End of control command _nop_(); _nop_(); dat=0; for(i=0;i<8;i++) { dat|=ADDO; //Receive data ADCLK=1; _nop_(); _nop_(); ADCLK=0; //Form a clock pulse _nop_(); _nop_(); dat<<=1; if(i==7)dat|=ADDO; } for(i=0;i<8;i++) { j=0; j=j|ADDO; //Receive data ADCLK=1; _nop_(); _nop_(); ADCLK=0; //Form a clock pulse _nop_(); _nop_(); j=j<<7; ndat=ndat|j; if(i<7)ndat>>=1; } ADCS=1; //Pull CS terminal low ADCLK=0; //Pull down the CLK terminal ADDO=1; //Pull up the data terminal and return to the initial state dat<<=8; dat|=ndat; return(dat); } void zkb_pro(void) { unsigned int temp; temp=100*ad_data/256; if(temp>0 && temp<100) { ZKB[0]=temp/10; ZKB[1]=temp%10; } } void data_pro(void) { unsigned int temp; float press; if(14 int vary=ad_data; //y=(115-15)/(243-13)*X+15kpa //press=24+ad_data/2.67; press=((10.0/28.1)*vary)+9.3; //The compensation value during the test is 9.3 temp=(int)(press*10); //Magnify 10 times for later calculation press_bai=temp/1000; press_shi=(temp%1000)/100; press_ge=((temp%1000)%100)/10; press_dot=((temp%1000)%100)%10; } } /*****************?????*******************/ void alarm(void) { if(ad_data>=243) { Alarm_led_red=0; a=10000; while(a--); { } Alarm_led_red=1; a=10000; while(a--); { } } else { Alarm_led_red=1; } if(ad_data<=15) { Alarm_led_green=0; a=10000; while(a--); { } Alarm_led_green=1; a=10000; while(a--); { } } else { Alarm_led_green=1; }
Previous article:12864 LCD menu interface small system
Next article:Detect 0~20MA current sensor alarm control program
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microcontroller selection
- Made a tool to use serial port IAP to realize batch online upgrade program
- Things that electronic hardware engineers need to know
- EEWORLD University ---- Automotive/Industrial Millimeter Wave Radar Sensors
- When designing an isolation system using a linear optocoupler, the isolation strength is required to reach 1800 volts. What should be considered during the design?
- First day of work in 2021
- TL335x-EVM development board processor, FLASH, RAM, FRAM
- DSP2812 CMD detailed configuration example
- 【ESP32-Korvo Review】 01 Unboxing Experience
- PCB circuit board heat dissipation tips