2398 views|3 replies

1

Posts

0

Resources
The OP
 

Microcontroller programming problems [Copy link]

How to add a function in the following program when programming with stm12A5c60s2 chip, convert the collected voltage into digital signal, set the signal range to drive the motor forward and reverse #include "STC12C5A60S2.h" #include "stdio.h" #include "intrins.h" #include "math.h" #define uint unsigned int #define uchar unsigned char sbit LED1 =P1^4; //Definition of IO port of the lamp sbit LED2 =P1^5; sbit LED3 =P1^6; sbit LED4 =P1^7; sbit Relay1=P2^6; //Definition of two relays, control forward and reverse sbit Relay2=P2^5; sbit SMOG=P3^7; //Smoke sensor collection uint ADC_Get(uchar Channel) //Collect photosensitive voltage value { ADC_CONTR=0x88|Channel; _nop_(); _nop_(); _nop_(); _nop_(); while(!(ADC_CONTR&0x10)); //Wait for the conversion to complete ADC_CONTR&=0xe7; //Turn off AD conversion, ADC_FLAG is cleared by software return(ADC_RES*4+ADC_RESL); //Return the 10-bit data (hexadecimal) completed by AD conversion }float ADC_Value(uchar Channel) { float AD_Value; uint i; for(i=0;i<10;i++) //Sampling 10 times and taking the average value AD_Value+=ADC_Get(Channel); //Convert 100 times and get the average value (to improve accuracy) AD_Value/=10; // AD_Value=(AD_Value*5)/1024;//ADRJ=0, take the 10-bit conversion result ADC_RES+ADC_RESL=1024*Vin/Vcc return AD_Value; } void delay(unsigned int a) { unsigned int i; while (--a!=0) for(i=600;i>0;i--); } void USART_Init() TMOD=0x20; TH1=0xfd; TL1=0xfd; SCON=0x50; TR1=1; } void ADC_Init() { P1ASF=0x01; AUXR1=0x00; ADC_RES=0; ADC_RESL=0 ; ADC_CONTR=0x0c; delay(2); } int light ; int ATOK ; void main() { USART_Init(); ADC_Init(); while(1) { light= ADC_Value(3); ATOK= ADC_Value(2); if(light>=300&&light<500) { LED1=0; LED2=LED3=LED4=1; } else if(light>=500&&light <700) { LED1=LED2=0; LED3=LED4=1; } else if(light>=700&&light<900) { LED1=LED2=LED3=0; LED4=1; } else if(light>=900) { LED1=LED2=LED3=LED4=0; }else LED1=LED2=LED3=LED4=1; if(SMOG==0) { Relay1=1;Relay2=0; } else if(AT OK>=300&&ATOK<900) { Relay1=1;Relay2=0; } else Relay1=0;Relay2=1; } TI=1; printf("%04d\r\n",light); TI=0; delay(500); }


This post is from 51mcu

Latest reply

stc There are routines for ADC, just modify it, but I don't know what your motor is, stepper or DC, driver or module... I don't know what the signal range is.   Details Published on 2019-6-17 17:05
 

4005

Posts

0

Resources
2
 
What's the problem?
This post is from 51mcu
 
 
 

291

Posts

0

Resources
3
 

The original poster would like to ask: When programming with the stm12A5c60s2 chip, how can I add a function to the following program to convert the collected voltage into a digital signal and set the signal interval to drive the motor forward and reverse.

Writing in assembly looks awkward

This post is from 51mcu

Comments

STC ADC has routines, just modify it, the motor does not know what your situation is, stepper or DC, driver or module... The signal range is even more unknown  Details Published on 2019-6-17 17:05
 
 
 

4005

Posts

0

Resources
4
 
generalcircuits Published on 2019-6-17 14:09 The OP wants to ask: When programming with the stm12A5c60s2 chip, how to add a function to the following program to convert the collected voltage into a digital signal and set...

stc

There are routines for ADC, just modify it, but I don't know what your motor is, stepper or DC, driver or module...

I don't know what the signal range is.

This post is from 51mcu
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list