Simulation of alarm mode control for Proteus sensor + gas concentration detection

catalog

1 understanding of experimental significance

Based on the first two experiments, we have successfully implemented:

  • Sampling and conversion of sensor data
  • Fit sample value
  • The fitting data is displayed on the HDG12864F-1 display screen

It seems that the data that should be obtained has been obtained. What else can we do?

Yes, we can also use the data to do something, such as how to control and solve when the gas concentration is out of range.

So that leads to the experiment, how to control the solution?

In fact, we have seen a lot in our daily life, when the gas concentration exceeds a certain range:

  • call the police
    • Turn on the alarm light
    • Buzzer gives out alarm sound
  • Dredge
    • Turn on fan ventilation
    • ......
  • ......

As mentioned above, we have adopted the first three small ways to control the solution.

In addition, we also need to optimize the LCD display, which is to display the rotating fan on the screen.

2 main experimental devices

  1. CPU processor

    • AT89C52
  2. LCD display

    • HDG12864F-1
  3. AD converter

    • ADC0834
  4. sensor

    • Temperature and humidity: SHT10
    • Light sensor: TORCH_LDR
    • Gas concentration sensor: LDR
    • Carbon monoxide concentration sensor: LDR
    • Air pressure sensor: MPX4115
  5. Control related

    • Warning lamp: RGBLED-CC
    • Buzzer: SOUNDER
    • Ventilation control: FAN

3 experimental reference circuit

  1. Not running

  2. Runtime

  3. explain

    • There is a buzzer sound
    • The letter after the channel in the first line of the display will change according to the channel currently being sampled and converted
    • All data is only displayed after conversion, but not stored. To store the display, just open a few global variables to save

4. Problems in the experiment

4.1 fans for turning

To realize this function, we only need to know two key points:

  • HDG12864F-1 liquid crystal display if you do not overwrite the written value of one pixel, the last written value will be maintained
  • Any dynamic effect is essentially a "fast" play of static pictures

So it's easy to do this:

  • Find several static fan diagrams with different rotation angles (the best size is the same, so it is easy to write and completely cover)
  • Mold these static fan diagrams
  • Call HDG12864F-1 draw picture function to write a static graph
  • Let the processor do other things, such as executing a few other statements, or directly delay waiting
  • Call HDG12864F-1 to draw picture function and write another static graph
  • ......

So, why is there "let the processor do other things, such as executing a few other statements, or directly delay waiting"?

In fact, this is a control analog fan rotation speed. You think if you quickly switch between two pictures, your eyes have no visual residue, it's gone. It's estimated that your eyes will be blinded Therefore, it is very important to choose an appropriate switching time interval

Second, there are at least two static pictures, which should be well understood. If there is one picture, there is no change. It's the same thing in exchange

4.2 control part

First, let's take a look at the schematic diagram of the control part:

Next, let's talk about each part separately.

  1. Alarm lamp

    To make this RGBLED-CC work, first of all, give a low level to the K terminal.

    RGB end, input 1 is on, 0 is not on.

    Then, in this experiment, we only need to use red and green colors, so we directly ground the B terminal, and then bind the R and G terminals to the processor pins respectively.

    During operation, it is judged that if the danger value is reached or the safety value is restored, the processor will reset the corresponding pin value.

  2. Buzzer

    It is mainly SOUNDER element, one end is applied with voltage, the other end needs to give pulse signal.

    This pulse signal is very important. At the beginning, our dog didn't set the input pulse signal (the default frequency is very small), so we can't hear it

    Later, double-click the input SW1(A), adjust the following parameters, and then you will hear. Refer to the figure below for my adjustment:

    Of course, how to make it play music??

    Here are two experiences that give me a further understanding of this:

    • During the summer internship in sophomore school, when I was working on the electronic clock, I wrote it with verilog. If I want to let the buzzer out, it's not only that kind of hard to hear scream all the time, but also play music or something. I need to give different frequencies

    • In the third year of college, we used the chip built in the notebook to make a clock based on x86 (written in the assembly language). Among them, what about alarm function and ringing tone? What do I do if I want to change to music?

      • Find music score and corresponding note frequency, relative delay
      • Write "table" for frequency and delay respectively, and delay output in turn

      Here, expand again. What if you want to listen to the fast version of music? If you want to listen to the down tone, what about the up tone version?

      • Frequency: control tone
      • Delay: control speed

    Then, again, how to control the sound switch?

    DSWITCH element is used here, which can be regarded as a three state gate:

    • BP end set to 1, receive input of SW1(A)
    • BP end set to 0, high resistance state, no sound
  3. Shelter control

    This module is mainly implemented by FAN + relay PCJ-112D3MH. I didn't find its DATASHEET on the IC network, so I followed the teacher's principle and made a personal understanding.

    First of all, how to control its switch?

    • We can see that there is a NPN type triode connected on the left side of it, which is mainly used for

      • FAN set to 1, turn on the electric FAN
      • FAN is set to 0, turn on the electric FAN

      As for the specific functions of this type of triode, what's the difference with PNP type triode?

      I searched the Internet for a question and answer, which can be referred to: Detailed explanation and difference of NPN and PNP triode

    Maybe After you write the code as above, we find that Why did I turn off the program? The fan of the simulation is still running?!

    Is the time I input not enough?! FAN=0,FAN=0,FAN=0,FAN=0,…… nm (can't crack rough) or not

    In fact, the actual stop of this fan is determined by the number in the display box below it.

    • When the fan is on, the number will increase;
  • When the fan is off, the number will drop;
    • When it's 0.00, it stops.

Of course, not stopping immediately is also in line with the situation in real life. I think it's mainly because of the problem of relay. This understanding is used to control strong electricity with weak electricity (if you are interested, you can learn by yourself)

Of course, the specific parameters of the fan can be modified by double clicking the component.

5 experiment reference code

The reference code is given below, and the control standard here is:

  • CH4 concentration < 20, normal - Green - fan off - buzzer off
  • CH4 concentration > = 20 & < 40, warning - yellow - fan on - buzzer off
  • CH4 concentration > = 40, danger - red - fan off - buzzer on
#include <reg52.h> 
#include <intrins.h>
#include<math.h>
#define NOP    _nop_()
#define uint unsigned int
#define uchar unsigned char
#define ACK 1
#define noACK 0
#define DISPLAY_ LEFT_ TO_ Right 1 / / calculates the column position from the left side. After each byte is written, the column number will automatically move to the right one
#define DISPLAY_ RIGHT_ TO_ Left 0 / / the column position is calculated from the number on the right. After each byte is written, the column number will automatically move to the left one

//SHT10 instruction set
//Write status register
#define STATUS_REG_W 0x06
//Read status register
#define STATUS_REG_R 0x07
//temperature measurement 
#define MEASURE_TEMP 0x03
//Humidity measurement
#define MEASURE_HUMI 0x05
//Soft reset
#define RESET 0x1E
//Enumerate selected temperature / humidity measurements
enum {TEMP,HUMI};
//ADC0834
sbit CS=P1^1;//ADC0834 chip selection signal
sbit CLK=P1^0;//ADC0834 clock signal
//sbit SARS0834=P1^2; / / conversion status output, low level means conversion is completed
sbit DO=P1^5;//ADC0834 data interface
sbit DI=P1^4;//ADC0834 channel selection
//HDG12864F-1
sbit cs1 = P2^4;//-cs, chip selection, low level effective
sbit rst = P2^3;//-rst, reset, active low level
sbit a0 = P2^2;//Write command, write data control bit. 1=Display data; 0=Control data;
sbit scl = P2^1;//Shift clock input
sbit si = P2^0;//Serial data input
//SHT10
sbit SCK=P1^2;
sbit DATA=P1^3;
//Control related
sbit LEDR=P1^6;
sbit LEDG=P1^7;
sbit FAN=P2^5;
sbit BP=P2^6;
//ADC
unsigned int temp,humi;
unsigned char ad_res = 0;
unsigned char ad_res1 = 0;
unsigned char ad_res2 = 0;
unsigned char ad_res3 = 0;
unsigned char ad_res4 = 0;
double dat=0.0;
//ADC0834 channel switching
unsigned int code channel0834[8]={0,0,1,0,0,1,1,1};
//Fan
uchar code pic_data1[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0xFC,0xFE,0xFE,0xFE,
0xFE,0xFE,0xFE,0xFC,0xF8,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xC0,0xF0,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFC,0xFC,0xBC,0xE8,0xF7,0xFF,0xFF,
0xFF,0xFF,0xFF,0xEF,0xEF,0xE3,0xF0,0xF0,0xF8,0xFC,0xFC,0xFC,0xF8,0xF0,0xC0,0x00,
0x00,0x01,0x07,0x0F,0x1F,0x1F,0x1F,0x0F,0x07,0x07,0xE3,0xFB,0xFB,0xFF,0xFF,0xFF,
0xFF,0xFF,0xF7,0x8F,0x0E,0x1F,0x1F,0x3F,0x3F,0x3F,0x3F,0x1F,0x0F,0x07,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x1F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x1F,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
uchar code pic_data2[]=
{0x00,0x80,0xE0,0xF0,0xF8,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFC,0xF8,0xF0,0xE0,
0x00,0x00,0x00,0x80,0xF8,0xFC,0xFC,0xFE,0xFE,0xFC,0xFC,0xF8,0xF8,0xF0,0xC0,0x00,
0x00,0x03,0x0F,0x1F,0x1F,0x1F,0x1F,0x1F,0x3F,0x3F,0x7F,0xDF,0xEF,0xF7,0xFF,0xFF,
0xFF,0xF8,0xFC,0xFF,0xEF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x3F,0x1F,0x06,
0x00,0xE0,0xF8,0xFC,0xFC,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xF7,0xEF,0xDF,0x3F,0x3F,
0xFF,0xFF,0xDF,0xFF,0xFF,0xFB,0xFD,0xF9,0xF9,0xF0,0xF0,0xF0,0xF0,0xF0,0xC0,0x00,
0x00,0x03,0x0F,0x1F,0x3F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x3F,0x1F,0x00,0x00,0x00,
0x03,0x0F,0x1F,0x7F,0x7F,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x3F,0x1F,0x0F,0x07,0x00,
};

//Character library
uchar code BMP[][6]=
{
//Two dimensional hexadecimal array corresponding to character display
{0x00,0x00,0x00,0x00,0x00,0x00}, //   0-bit display blank
{0x00,0x00,0x00,0x00,0x00,0x00}, //   1
{0x00,0x00,0x00,0x00,0x00,0x00}, //   2
{0x00,0x00,0x00,0x00,0x00,0x00}, //   3
{0x00,0x00,0x00,0x00,0x00,0x00}, //   4
{0x00,0x00,0x00,0x00,0x00,0x00}, //   5
{0x00,0x00,0x00,0x00,0x00,0x00}, //   6
{0x00,0x00,0x00,0x00,0x00,0x00}, //   7
{0x00,0x00,0x00,0x00,0x00,0x00}, //   8
{0x00,0x00,0x00,0x00,0x00,0x00}, //   9
{0x00,0x00,0x00,0x00,0x00,0x00}, //   10
{0x00,0x00,0x00,0x00,0x00,0x00}, //   11
{0x00,0x00,0x00,0x00,0x00,0x00}, //   12
{0x00,0x00,0x00,0x00,0x00,0x00}, //   13
{0x00,0x00,0x00,0x00,0x00,0x00}, //   14
{0x00,0x00,0x00,0x00,0x00,0x00}, //   15
{0x00,0x00,0x00,0x00,0x00,0x00}, //   16
{0x00,0x00,0x00,0x00,0x00,0x00}, //   17
{0x00,0x00,0x00,0x00,0x00,0x00}, //   18
{0x00,0x00,0x00,0x00,0x00,0x00}, //   19
{0x00,0x00,0x00,0x00,0x00,0x00}, //   20
{0x00,0x00,0x00,0x00,0x00,0x00}, //   21
{0x00,0x00,0x00,0x00,0x00,0x00}, //   22
{0x00,0x00,0x00,0x00,0x00,0x00}, //   23
{0x00,0x00,0x00,0x00,0x00,0x00}, //   24
{0x00,0x00,0x00,0x00,0x00,0x00}, //   25
{0x00,0x00,0x00,0x00,0x00,0x00}, //   26
{0x00,0x00,0x00,0x00,0x00,0x00}, //   27
{0x00,0x00,0x00,0x00,0x00,0x00}, //   28
{0x00,0x00,0x00,0x00,0x00,0x00}, //   29
{0x00,0x00,0x00,0x00,0x00,0x00}, //   30
{0x00,0x00,0x00,0x00,0x00,0x00}, //   31
{0x00,0x00,0x00,0x00,0x00,0x00}, // sp 32
{0x00,0x00,0x2f,0x00,0x00,0x00},   // ! 33
{0x00,0x07,0x00,0x07,0x00,0x00},   // " 34 
{0x14,0x7f,0x14,0x7f,0x14,0x00},   // # 35
{0x24,0x2a,0x7f,0x2a,0x12,0x00},   // $ 36
{0xc4,0xc8,0x10,0x26,0x46,0x00},   // % 37
{0x36,0x49,0x55,0x22,0x50,0x00},   // & 38 
{0x00,0x05,0x03,0x00,0x00,0x00},   // ' 39
{0x00,0x1c,0x22,0x41,0x00,0x00},   // ( 40
{0x00,0x41,0x22,0x1c,0x00,0x00},   // ) 41
{0x14,0x08,0x3E,0x08,0x14,0x00},   // * 42
{0x08,0x08,0x3E,0x08,0x08,0x00},   // + 43
{0x00,0x00,0x50,0x30,0x00,0x00},   // , 44
{0x10,0x10,0x10,0x10,0x10,0x00},   // - 45
{0x00,0x60,0x60,0x00,0x00,0x00},   // . 46
{0x20,0x10,0x08,0x04,0x02,0x00},   // / 47
{0x3E,0x51,0x49,0x45,0x3E,0x00},   // 0 48
{0x00,0x42,0x7F,0x40,0x00,0x00},   // 1 49
{0x42,0x61,0x51,0x49,0x46,0x00},   // 2 50
{0x21,0x41,0x45,0x4B,0x31,0x00},   // 3 51
{0x18,0x14,0x12,0x7F,0x10,0x00},   // 4 52
{0x27,0x45,0x45,0x45,0x39,0x00},   // 5 53
{0x3C,0x4A,0x49,0x49,0x30,0x00},   // 6 54
{0x01,0x71,0x09,0x05,0x03,0x00},   // 7 55
{0x36,0x49,0x49,0x49,0x36,0x00},   // 8 56
{0x06,0x49,0x49,0x29,0x1E,0x00},   // 9 57
{0x00,0x36,0x36,0x00,0x00,0x00},   // : 58
{0x00,0x56,0x36,0x00,0x00,0x00},   // ; 59
{0x08,0x14,0x22,0x41,0x00,0x00},   // < 60
{0x14,0x14,0x14,0x14,0x14,0x00},   // = 61
{0x00,0x41,0x22,0x14,0x08,0x00},   // > 62
{0x02,0x01,0x51,0x09,0x06,0x00},   // ? 63
{0x32,0x49,0x59,0x51,0x3E,0x00},   // @ 64
{0x7E,0x11,0x11,0x11,0x7E,0x00},   // A 65
{0x7F,0x49,0x49,0x49,0x36,0x00},   // B 66
{0x3E,0x41,0x41,0x41,0x22,0x00},   // C 67
{0x7F,0x41,0x41,0x22,0x1C,0x00},   // D 68
{0x7F,0x49,0x49,0x49,0x41,0x00},   // E 69
{0x7F,0x09,0x09,0x09,0x01,0x00},   // F 70
{0x3E,0x41,0x49,0x49,0x7A,0x00},   // G 71
{0x7F,0x08,0x08,0x08,0x7F,0x00},   // H 72
{0x00,0x41,0x7F,0x41,0x00,0x00},   // I 73
{0x20,0x40,0x41,0x3F,0x01,0x00},   // J 74
{0x7F,0x08,0x14,0x22,0x41,0x00},   // K 75
{0x7F,0x40,0x40,0x40,0x40,0x00},   // L 76
{0x7F,0x02,0x0C,0x02,0x7F,0x00},   // M 77
{0x7F,0x04,0x08,0x10,0x7F,0x00},   // N 78
{0x3E,0x41,0x41,0x41,0x3E,0x00},   // O 79
{0x7F,0x09,0x09,0x09,0x06,0x00},   // P 80
{0x3E,0x41,0x51,0x21,0x5E,0x00},   // Q 81
{0x7F,0x09,0x19,0x29,0x46,0x00},   // R 82
{0x46,0x49,0x49,0x49,0x31,0x00},   // S 83
{0x01,0x01,0x7F,0x01,0x01,0x00},   // T 84
{0x3F,0x40,0x40,0x40,0x3F,0x00},   // U 85
{0x1F,0x20,0x40,0x20,0x1F,0x00},   // V 86
{0x3F,0x40,0x38,0x40,0x3F,0x00},   // W 87
{0x63,0x14,0x08,0x14,0x63,0x00},   // X 88
{0x07,0x08,0x70,0x08,0x07,0x00},   // Y 89
{0x61,0x51,0x49,0x45,0x43,0x00},   // Z 90
{0x00,0x7F,0x41,0x41,0x00,0x00},   // [ 91
{0x55,0x2A,0x55,0x2A,0x55,0x00},   //55 92
{0x00,0x41,0x41,0x7F,0x00,0x00},   // ] 93
{0x04,0x02,0x01,0x02,0x04,0x00},   // ^ 94
{0x40,0x40,0x40,0x40,0x40,0x00},   // _ 95
{0x00,0x01,0x02,0x04,0x00,0x00},   // ' 96
{0x20,0x54,0x54,0x54,0x78,0x00},   // a 97
{0x7F,0x48,0x44,0x44,0x38,0x00},   // b 98
{0x38,0x44,0x44,0x44,0x20,0x00},   // c 99
{0x38,0x44,0x44,0x48,0x7F,0x00},   // d 100
{0x38,0x54,0x54,0x54,0x18,0x00},   // e 101
{0x08,0x7E,0x09,0x01,0x02,0x00},   // f 102 
{0x0C,0x52,0x52,0x52,0x3E,0x00},   // g 103 
{0x7F,0x08,0x04,0x04,0x78,0x00},   // h 104
{0x00,0x44,0x7D,0x40,0x00,0x00},   // i 105
{0x20,0x40,0x44,0x3D,0x00,0x00},   // j 106
{0x7F,0x10,0x28,0x44,0x00,0x00},   // k 107
{0x00,0x41,0x7F,0x40,0x00,0x00},   // l 108
{0x7C,0x04,0x18,0x04,0x78,0x00},   // m 109
{0x7C,0x08,0x04,0x04,0x78,0x00},   // n 110
{0x38,0x44,0x44,0x44,0x38,0x00},   // o 111
{0x7C,0x14,0x14,0x14,0x08,0x00},   // p 112
{0x08,0x14,0x14,0x18,0x7C,0x00},   // q 113
{0x7C,0x08,0x04,0x04,0x08,0x00},   // r 114
{0x48,0x54,0x54,0x54,0x20,0x00},   // s 115
{0x04,0x3F,0x44,0x40,0x20,0x00},   // t 116
{0x3C,0x40,0x40,0x20,0x7C,0x00},   // u 117
{0x1C,0x20,0x40,0x20,0x1C,0x00},   // v 118
{0x3C,0x40,0x30,0x40,0x3C,0x00},   // w 119
{0x44,0x28,0x10,0x28,0x44,0x00},   // x 120
{0x0C,0x50,0x50,0x50,0x3C,0x00},   // y 121
{0x44,0x64,0x54,0x4C,0x44,0x00},   // z 122
{0xD5,0x01,0x80,0x01,0x80,0xAB},  // <50        123
{0xFF,0x81,0x81,0x81,0x81,0xFF},  //50<= <100   124
{0xFF,0x81,0xBD,0xBD,0x81,0xFF},  //100<= <150  125
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},  //>=150       126
{0x00,0x00,0x00,0x00,0x00,0x00}   // sp         127
};

unsigned char code myChannel[7]={'C','h','a','n','n','e','l'};
unsigned char code myTemp[4]={'T','e','m','p'};
unsigned char code myHumi[4]={'H','u','m','i'};
unsigned char code myCO[2]={'C','O'};
unsigned char code myCH4[3]={'C','H','4'};
unsigned char code myLight[5]={'L','i','g','h','t'};
unsigned char code myAir[7]={'A','i','r','-','p','r','e'};
unsigned char code **msg[7]={myChannel,myTemp,myHumi,myCO,myCH4,myLight,myAir};

//Delay Functions 
void Delayms(uint x )
{
	uint t; 
	while(x--) 
		for (t= 0; t<120; t++);
}

//HDG12864F-1
//Write command
void wrt_cmd(unsigned char command)
{
    unsigned char i = 8;//8 bits
    cs1 = 0;//Chip selection, low level effective
    a0 = 0;//0=Control data, command set to 0
    while(i--)
	{
		scl = 0;
		si = (bit) (command & 0x80);//Write high first
		scl = 1;//Rising edge
		command <<= 1;//Move left one bit
    }
    scl = 0;
}
//Write data
void wrt_dt(unsigned char data_)
{
    unsigned char i = 8;//8 bits
    cs1 = 0;//Chip selection, low level effective
    a0 = 1;//1=Display data, write data to 1
    while(i--)
	{
		scl = 0;
		si = (bit) (data_ & 0x80);//Write high first
		scl = 1;//Rising edge
		data_ <<= 1;//Move left one bit 0
    }
    scl = 0;
}
//Set column position, where parameters address:0~127
void HDG12864F1_SetColumnAddress(unsigned char address)
{	//Writing columns should be divided into two steps: first write the high four digits, then write the low four digits
	//Manual Column Address Set
	wrt_cmd(0x10 + (address >> 4 & 0x0f));//Shift right in C is arithmetic shift right. You must remove the upper 4 bits of & 0x0f to get the correct result
	wrt_cmd(address & 0x0f);
}
//Set the longitudinal position, where the parameter pa geAddress:0 ~8
void HDG12864F1_SetPageAddress(unsigned char pageAddress)
{	//Manual Page Address Set
    wrt_cmd(0xb0 + pageAddress);//1011 + page
}
//Write data
void HDG12864F1_WriteData(unsigned char data_)
{
    wrt_dt(data_);
}
//Set write direction: right to left is positive
void HDG12864F1_Direction(unsigned char direction)
{	//Manual ADC Select
    wrt_cmd(0xa0+direction);
}
//Scroll up the content of the written screen, and the top part will appear from the bottom of the screen
void HDG12864F1_SetStartLine(unsigned char line)
{	//Parameter line value range 0 ~ 63
    wrt_cmd(0x40 + line);
}

//Write English characters, number up and down 1 8 * 6 dot matrix
void HDG12864F1_WriteEnglishChar(unsigned char *pEChar, unsigned char column, unsigned char page)
{
    unsigned char i;
    HDG12864F1_Direction(DISPLAY_LEFT_TO_RIGHT);//DISPLAY_LEFT_TO_RIGHT is a macro with a value of 1 that represents writing from the left side of the screen
    HDG12864F1_SetColumnAddress(column);//Set the horizontal coordinate to start writing
    HDG12864F1_SetPageAddress(page);//Set the vertical Page to start writing
    for(i=0; i<6; i++)
		HDG12864F1_WriteData(*(pEChar + i));//Continuous English Writing
}
//Picture 32 * 32
void HDG12864F1_DrawPic(unsigned char *pChar, unsigned char column, unsigned char page)
{
	unsigned char i,j;
    HDG12864F1_Direction(DISPLAY_LEFT_TO_RIGHT);//DISPLAY_LEFT_TO_RIGHT is a macro with a value of 1, which represents writing from the left side of the screen
    for(i=0;i<4;i++)
	{
		HDG12864F1_SetColumnAddress(column);//Set the horizontal coordinate to start writing
    	HDG12864F1_SetPageAddress(page+i);//Set the vertical Page to start writing
		for(j=0;j<32;j++)
		{
			HDG12864F1_WriteData(*(pChar + i*32+j));//Continuous English Writing
		}
	}
}

//ADC0834
unsigned char AD0834_conv(unsigned int n)
{
	unsigned char i,com;
	CS=1;
	CS=0;	_nop_();	_nop_();//CS set low, start conversion
	DI=1; 	_nop_();	_nop_();//Start, prepare the first pulse of output data
	CLK=1;	_nop_();	_nop_();
	CLK=0;	_nop_();	_nop_();
	//Select channel, second pulse
	DI=1;
	CLK=1;	_nop_();	_nop_();	
	CLK=0;	_nop_();	_nop_();

	DI=channel0834[n*2];
	CLK=1;	_nop_();	_nop_();
	CLK=0;	_nop_();	_nop_();

	DI=channel0834[n*2+1];
	CLK=1;	_nop_();	_nop_();
	CLK=0;	_nop_();	_nop_();

	DI=1;
	CLK=1;	_nop_();	_nop_();
	CLK=0;	_nop_();	_nop_();
	
	//Start to collect conversion data	
	for(i=8;i>0;i--)
	{	
		com<<=1;//Move left, take the highest position first
		if(DO)com=com|0x01;//Collect current data
		CLK=1;
		CLK=0;
		_nop_();
	    _nop_();	
	}	
	CS=1;//Close selection, disable
	return com;
}

//LDR sensor data fitting
unsigned char ChangeDataLDR(unsigned char res)
{
	unsigned char com;
	com=0.00000000221308*res*res*res*res*res
		-0.0000009287723*res*res*res*res 
		+ 0.0001465584*res*res*res 
		- 0.008997464*res*res 
		+ 0.2657836*res - 0.5918848;
	return com;
}

//MPX4115 sensor data fitting
unsigned char ChangeDataMPX(unsigned char res)
{
	unsigned char com;
	//Fit yourself
	com=0.436*res+9.053;
	return com;
}

//SHT10
//Write Bytes
char s_write_byte(uchar value)
{
	uchar i,error=0;
	//Take out the corresponding bit string transmission of instructions respectively
	//From high
	for(i=0x80;i>0;i>>=1)
	{
		if(i&value) DATA=1;
		else DATA=0;
		SCK=1;
		//Keep SCK high
		_nop_();_nop_();_nop_();
		SCK=0;
	}
	DATA=1;
	SCK=1;
	error=DATA;//ACK
	_nop_();_nop_();_nop_();
	SCK=0;
	DATA=1;
	return  error;		
}

//Read bytes
char s_read_byte(uchar ack)
{
	uchar i,val=0;
	DATA=1;
	//Read a byte of data
	for(i=0x80;i>0;i>>=1)
	{
		SCK=1;
		if(DATA) val=(val|i);
		SCK=0;
	}
	if(ack==1)DATA=0;//Confirm each byte by pulling down DATA to low level
	else DATA=1;  //If it is a check (ack==0), keep the ACK high level to end the communication after reading
	_nop_();_nop_();_nop_();  //pulswith approx. 3 us
	SCK=1;  //clk #9 for ack
	_nop_();_nop_();_nop_();  //pulswith approx. 3 us
	SCK=0;
	_nop_();_nop_();_nop_();  //pulswith approx. 3 us
	DATA=1;  //Release data line
	return val;
}

//Start transfer
void s_transstart(void)
{
	DATA=1; SCK=0;
	_nop_();
	SCK=1;
	_nop_();
	DATA=0;
	_nop_();
	SCK=0;
	_nop_();_nop_();_nop_();
	SCK=1;
	_nop_();
	DATA=1;
	_nop_();
	SCK=0;
}

//Connection reset
void s_connectionreset(void)
{
//Communication interrupt requires communication reset
//DATA remains high and triggers SCK clock 9 times or more
	uchar i;
	DATA=1;SCK=0;
	for(i=0;i<9;i++)
	{
		SCK=1;
		SCK=0;
	}
//Send a transmission start sequence
	s_transstart();
//Reset the serial port while the content of the status register remains
}

//Temperature and humidity measurement
char s_measure(uchar *p_value,uchar *p_checksum,uchar mode)
{
	unsigned error=0;
	unsigned int i;

	s_transstart();
	switch(mode)
	{
		case TEMP:
			error+=s_write_byte(MEASURE_TEMP); break;
		case HUMI:
			error+=s_write_byte(MEASURE_HUMI); break;
		default: break;
	}
	//After the measurement, the Sensor will pull down the DATA line
	//The waiting time for the end of measurement is different according to the measurement of different digits
	for(i=0;i<65535;i++) if(DATA==0) break;//2^16
	if(DATA) error+=1;//Description not over
	//Transmit 2 bytes of measurement data and 1 byte of CRC parity
	*(p_value)=s_read_byte(ACK);
	*(p_value+1)=s_read_byte(ACK);
	*p_checksum=s_read_byte(noACK);
	return error;
}

//Temperature and humidity value scale transformation and temperature compensation
void calc_sth10(float *p_humidity,float *p_temperature)
{
// input :  humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output:  humi [%RH]
	const float C1=-2.0468;	// for 12 Bit
	const float C2=+0.0367;	    // for 12 Bit
	const float C3=-0.0000015955;	// for 12 Bit
	const float T1=+0.01;		// for 12 Bit @ 5V
	const float T2=+0.00008;	// for 12 Bit @ 5V
	float rh=*p_humidity;// rh:  Humidity [Ticks] 12 Bit
	float t=*p_temperature;	// t:  Temperature [Ticks] 14 Bit
	float rh_lin;// rh_lin:  Humidity linear
	float rh_ture;// rh_true: Temperature compensated humidity
	float t_C;// t_C  :  Temperature [C]
	t_C=t*0.01-40;//Temperature conversion [C]
	rh_lin=C3*rh*rh+C2*rh+C1;//Nonlinear compensation of relative humidity: [% RH]
	rh_ture=(t_C-25)*(T1+T2*rh)+rh_lin;//Temperature compensation of humidity signal: [% RH]
	//Out of range processing
	if(rh_ture>100) rh_ture=100;
	if(rh_ture<0.1) rh_ture=0.1;
	//Transfer the results back
	*p_temperature=t_C;//[C]
	*p_humidity=rh_ture;//[%RH]
}


typedef union
{
 unsigned int i;
 float f;
}value;

void main(void)
{
   int i=0;
   int j=0;
	int mycol=0;
   value humi_val,temp_val;
   uchar error;
	uchar check_sum;//Checksums
	int flag;//Judge temperature symbol
	BP=0;
	FAN=0;
   while(1)
	{
	mycol=0;
	for(i=0;i<7;i++)
		HDG12864F1_WriteEnglishChar(BMP[myChannel[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['A'],8*8,mycol);
	mycol=mycol+1;
	//Temperature and humidity
 	  error=0;
	  error+=s_measure((uchar*)&humi_val.i,&check_sum,HUMI);
	  error+=s_measure((uchar*)&temp_val.i,&check_sum,TEMP);
	  
	  if(error!=0)//The communication is interrupted and the measurement is not completed
	  	s_connectionreset();
	  else//It has been measured
	  {
	  	humi_val.f=(float)humi_val.i;
		temp_val.f=(float)temp_val.i;
		//Temperature and humidity compensation
		calc_sth10(&humi_val.f,&temp_val.f);
		//Judging symbol
		if(temp_val.f<0)
		{
			flag='-';
			temp_val.f=-temp_val.f+2;
		}
		else
			flag='+';
		//Can display the next decimal place
		temp=temp_val.f*10;
	    humi=humi_val.f*10;			 
	  }
	//temperature
	for(i=0;i<4;i++)
		HDG12864F1_WriteEnglishChar(BMP[myTemp[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],4*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[flag],5*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[temp/1000+'0'],6*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[temp%1000/100+'0'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[temp%100/10+'0'],8*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['C'],9*8,mycol);
	mycol=mycol+1;
	//humidity
	for(i=0;i<4;i++)
		HDG12864F1_WriteEnglishChar(BMP[myHumi[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],4*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[humi/1000+'0'],5*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[humi%1000/100+'0'],6*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[humi%100/10+'0'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['%'],8*8,mycol);
	mycol=mycol+1;

	ad_res=AD0834_conv(0);
	ad_res1=ad_res;
	for(i=0;i<5;i++)
		HDG12864F1_WriteEnglishChar(BMP[myLight[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],5*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res1%1000/100+'0'],6*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res1%100/10+'0'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res1%10+'0'],8*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['L'],9*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['x'],10*8,mycol);
	mycol=mycol+1;

	ad_res=AD0834_conv(1);
	ad_res2=ChangeDataLDR(ad_res);
	for(i=0;i<2;i++)
		HDG12864F1_WriteEnglishChar(BMP[myCO[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],2*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res2%1000/100+'0'],3*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res2%100/10+'0'],4*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res2%10+'0'],5*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['p'],6*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['p'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['m'],8*8,mycol);
	mycol=mycol+1;

	ad_res=AD0834_conv(2);
	ad_res3=ChangeDataLDR(ad_res);
	//Control RGB-LED and BP and FAN
	if(ad_res3<20)//Normal - Green - fan off - buzzer off
	{
		LEDR=0;
		LEDG=1;
		BP=0;
		FAN=0;
	}
	else if(ad_res3>=20 & ad_res3<40)//Warning - yellow - fan on - buzzer off
	{
		LEDR=1;
		LEDG=1;
		BP=0;
		FAN=1;
	}
	else//Danger - red - fan off - buzzer on
	{
		LEDR=1;
		LEDG=0;
		BP=1;
		FAN=0;
	}
	for(i=0;i<3;i++)
		HDG12864F1_WriteEnglishChar(BMP[myCH4[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],3*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res3%1000/100+'0'],4*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res3%100/10+'0'],5*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res3%10+'0'],6*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['p'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['p'],8*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['m'],9*8,mycol);
	mycol=mycol+1;

	ad_res=AD0834_conv(3);
	ad_res4=ChangeDataMPX(ad_res);
	for(i=0;i<7;i++)
		HDG12864F1_WriteEnglishChar(BMP[myAir[i]],i*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[':'],7*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res4%1000/100+'0'],8*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res4%100/10+'0'],9*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP[ad_res4%10+'0'],10*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['k'],11*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['P'],12*8,mycol);
	HDG12864F1_WriteEnglishChar(BMP['a'],13*8,mycol);
	//Rotating fan
	HDG12864F1_DrawPic(&pic_data1,90,0);
	Delayms(800);//Delay display
	HDG12864F1_DrawPic(&pic_data2,90,0);
	Delayms(700);//Delay display
    }
}

Among them, the procedures in HDG12864F-1 are as follows: Operating HDG12864F-1 LCD with Proteus simulation

Tags: Verilog Assembly Language network

Posted on Thu, 25 Jun 2020 21:57:40 -0400 by DJ_CARO