The completion and problem solving of the small project of the sensor switch trash can——Based on the STC89C52RC single-chip microcomputer

The completion and problem solving of the small project of the sensor switch trash can——Based on the STC89C52RC single-chip microcomputer

Divided into five steps, as follows:

01 code_steering gear

Insert image description here

Note: The use of the steering gear cannot be rotated by hand, which may cause damage to the steering gear (I broke one because of this, maybe the steering gear used is too low)

After assembly, the code can run normally, but the steering gear "twitches" when the cover is opened:

`voi`d openDusbin()`
`{
    
    `
    `char n;`
    `jd = 3; //90度 1.5ms高电平`
`//舵机开盖`
        `cnt = 0;`
        `beep = 0;`
        `for(n=0; n<2; n++)`
            `Delay150ms();`
        `beep = 1;`
        `Delay2000ms();`
    }`

"twitch" when cnt goes back to 0

Solution:

Need to make a flag to judge, make a variable to save the last angle degree **char jd_bak; **Close cover also add jd_back=jd;

`void openDusbin()`
`{
    
    `
    `char n;`
    `jd = 3; //90度 1.5ms高电平`
`//舵机开盖`
    `if(jd_bak != jd) {
    
    //如果上一次角度不等于本次角度则执行,否则不执行`
        `cnt = 0;`
        `beep = 0;`
        `for(n=0; n<2; n++)`
            `Delay150ms();`
        `beep = 1;`
        `Delay2000ms();`
    `}`
    `jd_bak = jd;`
`}`

02 Code_Distance switch cover-ultrasonic distance measurement, add button to open the cover, vibration sensor

Ultrasonic ranging
Vibration Sensor Module

`sbit SW1 = P2^1;`
`sbit Trig = P1^5;`
`sbit Echo = P1^6;`
`sbit vibrate = P3^2;`
`void main()`
`{
    
    `
	`double dis;`
	`Time0Init();`
    `Time1Init();`
	`EX0_Init();`
	`//舵机的初始位置`
	`initSG90_0();`
    `while(1) {
    
    `
		`//超声波测距`
        `dis = get_distance();`
        `if(dis < 10 || SW1 ==0 || mark_vibrate ==1) {
    
    //如果小于10cm,或者SW1按键被按下`
			`//开盖,灯状态,D5亮`
          `openStatusLight();`
			`//舵机关盖`
			`openDusbin();`
			`mark_vibrate = 0;`
        `} else {
    
    `
			`//灌溉,灯状态,D5灭`
            `closeStatusLight();`
			`closeDusbin();`
        `}`
    `}`
`}`

The vibration sensor is not sensitive: the software delay used is equivalent to the CPU counting. Using the query method, the level change may be too rapid and unstable, and it cannot be detected when the CPU is counted upside down. Optimization of the vibration sensor: add external interrupt 0, once a vibration occurs, the external interrupt will immediately set the flag, and the CPU will check again.

`void EX0_Init()`
`{
    
    `
	`//打开外部中断`
	`EX0 = 1;`
	`//低电平触发`
	`IT0 = 0;`
`}`

Interrupt handler

`char mark_vibrate = 0;`

`void Ex0_Handler() interrupt 0`
`{
    
    `
	`mark_vibrate = 1;`
`}`

05 code_distance switch cover - add buzzer to open the cover

buzzer module

`sbit beep = P2^0;`
 `if`(jd_bak != jd) {
    
    `
        `cnt = 0;`
        `beep = 0;`
        `for(n=0; n<2; n++)`
            `Delay150ms();`
        `beep = 1;`
        `Delay2000ms();`
    }`



        `for(n=0; n<2; n++)`
            `Delay150ms();`
        `beep = 1;`
        `Delay2000ms();`
    }`

Guess you like

Origin blog.csdn.net/weixin_54882070/article/details/129419626