0% found this document useful (0 votes)
56 views6 pages

M4 Micro OneNote

The document discusses generating square and triangular waves using a microcontroller and DAC. It explains how to generate a square wave by toggling a port pin high and low with delays. Code is provided to generate a 1 kHz square wave using an 8051 microcontroller. The document also discusses generating a triangular wave by sending sampling points from a pre-generated array to a DAC. Finally, it provides an overview of using an ADC to build a water level controller that monitors water level with a sensor and controls a pump or valve.

Uploaded by

Aryan Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

M4 Micro OneNote

The document discusses generating square and triangular waves using a microcontroller and DAC. It explains how to generate a square wave by toggling a port pin high and low with delays. Code is provided to generate a 1 kHz square wave using an 8051 microcontroller. The document also discusses generating a triangular wave by sending sampling points from a pre-generated array to a DAC. Finally, it provides an overview of using an ADC to build a water level controller that monitors water level with a sensor and controls a pump or valve.

Uploaded by

Aryan Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

M-4 Applications of Microcontroller

29 April 2023 01:00

Square wave generation using port pins of 8051


Steps to generate a square wave using 8051
1. Set the output of any port on the 8051 to logic high.
2. Wait for some time.
3. Set the output of the same port to logic low.
4. Again wait for the same amount of time as you did earlier.
5. Loop around the same.
Subsequently, for obtaining the desired frequency on the square wave, we have to manipulate with
the delay. We know that the machine cycle frequency is 1/12 of the crystal oscillator frequency. So,
with the crystal oscillator’s frequency as 11.0592 MHz the machine cycle frequency is 921.6 Khz. To
sum up, that's equivalent to 1.085 μsecond.
To generate a square wave of 1 Khz., in other words, a wave of time period 1 millisecond, we have
to use the delay in such a way that it causes a delay of 1 millisecond. We will have to loop around
doing nothing for about 461 machine cycles to generate a square wave of 50% duty cycle. That is to
say, an on time and an off time of 0.5 millisecond.
Now, since we know the logic behind the generation of square wave, we can work on the hardware.
The Hardware
When you are working on big projects, starting on with the hardware is the best way to do it.
So, let's build the hardware. The components that we are using are as follows
1. The AT89C51 μcontroller.
2. DAC0808 IC
3. Resistors.
4. Capacitors.
5. An op amp – the DAC0808 output is known current. The op amp is to convert it to known
voltage.
6. CRO to visualize the wave.
The Placements and Connections
Firstly, place the μcontroller and the DAC0808 IC from the libraries in Proteus workspace. Connect
the reset pin to the power source (Vcc). We will be connecting the DAC0808’s data pins to the port 1
of the μcontroller. Make sure you are connecting the MSB bit to the A1 pin (pin number 5) on
DAC0808 and the LSB pin to the A8 pin (pin 12 physically). To avoid any confusions, follow the
schematic below.

The code to generate a square wave using 8051


Let's start off with the line. Since we need the wave to start from logic 0, we pass on the data 00h to

Microcontroller Theory and Design Page 1


Let's start off with the line. Since we need the wave to start from logic 0, we pass on the data 00h to
port 1.
org 0000h
mov a, #00h
mov p1, a
Next, we need to add some delay so that we get a constant high DC voltage – logic high. We do it by
defining a function or sub-routine called as delay.
acall delay
delay: mov r1, #0ffh
loop2: mov r0, #0ah
loop: djnz r0, loop
djnz r1, loop2
ret
Then we need to send logic high to the DAC connected to port 1, and then call the delay sub-routine
again.
mov a, #0ffh
mov p1, a
acall delay
Now that we have created the first wave. We need to loop the program so that it keeps on running
as long as the μcontroller is on. So we loop it by using the SJMP command.
sjmp startOver
and also add a lebel at the beginning of the code. So, replace
mov a, #00h
with
startOver:mov a, #00h
or any other label name you used.
Compilation
• Further, we need to compile the code written. For instance you should prefer compiling in Keil
μVision.
• Make sure your option to generate hex file is enabled.
• Subsequently, open your Proteus File and double Click the μcontroller, you should see a pop-up
window asking you to select the debug file.
• You should select the hex file, it is usually placed in the Objects folder in your Keil project folder.
• Debug the file via Digital oscilloscope.
Your square wave is generated
https://fazals.ddns.net/generate-a-square-wave-using-8051/

Square & Triangular Waveform generation using DAC


Output triangular wave through DAC
First, clear the sine wave program in the previous while cycle.

To output triangular wave, the DAC also needs to output continuous curve, so a large number of
sampling points are required, but not as many as sine wave.

Here, I use python program to generate 128 sampling points, that is, a triangular wave period is
divided into 128 points.
The Python program for generating triangular wave sampling points is as follows:

POINT_NUM = 64
step=4096/POINT_NUM
a=[]
r=[]
for i in range(POINT_NUM):
a.append(int(i*step))
for i in range(POINT_NUM):
a.append(4096-int(i*step))

Microcontroller Theory and Design Page 2


print(a)

print(len(a))
At User Code Begin 0, create a new TriData array to store the generated sampling point data.

uint32_t TriData[]={0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 640,
704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472,
1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176, 2240,
2304, 2368, 2432, 2496, 2560, 2624, 2688, 2752, 2816, 2880, 2944, 3008,
3072, 3136, 3200, 3264, 3328, 3392, 3456, 3520, 3584, 3648, 3712, 3776,
3840, 3904, 3968, 4032, 4090,4090, 4032, 3968, 3904, 3840, 3776, 3712,
3648, 3584, 3520, 3456, 3392, 3328, 3264, 3200, 3136, 3072, 3008, 2944,
2880, 2816, 2752, 2688, 2624, 2560, 2496, 2432, 2368, 2304, 2240, 2176,
2112, 2048, 1984, 1920, 1856, 1792, 1728, 1664, 1600, 1536, 1472, 1408,
1344, 1280, 1216, 1152, 1088, 1024, 960, 896, 832, 768, 704, 640, 576,
512, 448, 384, 320, 256, 192, 128,64,0};

The code in the while function is as follows:


while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

for(int x=0;x<sizeof(TriData)/sizeof(TriData[0]);x++){
HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_
12B_R,TriData[x]);
}
}
The results are as follows:

ADC - Analog to Digital Converter


ADC stands for Analog to digital conversion and it is used to convert analog values from real world
into digital values like 1’s and 0’s.
Analog values - These are the ones that we see in our day to day life like temperature, speed,
brightness etc.
An ADC can only convert analog voltage values into digital values.
So which ever parameter we wish to measure, it should be converted into voltage first, this
conversion can be done with the help of sensors.
For example to convert temperature values into voltage we can use a Thermistor similarly to convert

Microcontroller Theory and Design Page 3


For example to convert temperature values into voltage we can use a Thermistor similarly to convert
brightness to voltage we can use a LDR. Once it is converted to voltage we can read it with the help
of ADC’s.
Reference Voltage
During an ADC conversion the value of unknown voltage is found by comparing it with a known
voltage, this is known voltage is called as Reference voltage.
Resolution bits and channels
For instance an Arduino UNO’s ATmega328 has a 8-channel 10-bit ADC. Not every pin on a
microcontroller can read Analog voltage, the term 8-channel means that there are 8 pins on this
ATmega328 microcontroller which can read Analog voltage and each pin can read the voltage with a
resolution of 10-bit.
ADC range is from 0V to 5V
10 bit ADC
2^10 = 1024
1024 is the resolution for a 10-bit ADC
(ADC Resolution / Operating Voltage) = (ADC Digital Value / Actual Voltage Value)
ADC has an internal capacitor which will get charged by the analog voltage that is to measured. Then
we measure the voltage value by discharging the capacitor over a period of time.

Water Level Controller using ADC


A water level controller using an ADC (Analog-to-Digital Converter) can be implemented using a
microcontroller or a programmable logic controller (PLC). The ADC is used to convert the analog
water level readings from a sensor into digital values that can be processed by the controller.
Here's a general outline of how you can build a water level controller using an ADC:
Components Needed:
• Water level sensor: This can be a float switch, pressure sensor, or any other type of sensor that
provides analog output based on the water level.
• Microcontroller or PLC: Choose a suitable microcontroller or PLC with ADC capabilities.
• Relay module: To control the water pump or valve,
• Power supply and other necessary components.
Sensor Connection:
Connect the analog output of the water level sensor to one of the ADC input pins of the
microcontroller or PLC. Ensure the power supply for the sensor is connected properly.
ADC Configuration:
Set up the ADC on the microcontroller or PLC to read analog values from the connected pin. Refer to
the documentation or programming guide of your specific controller to configure the ADC module
accordingly.
Threshold Levels:
Determine the desired water level thresholds for different states, such as low level, medium level,
and high level. These thresholds will be used to trigger the pump or valve to maintain the water level
within the desired range.
Programming Logic:
Write the programming logic to read the ADC values and compare them with the threshold levels.
Based on the comparison, control the relay module to turn the water pump or valve on or off.
Here's a basic example pseudocode for the programming logic:
loop:
read ADC value
if ADC value < low threshold:
turn on pump/valve
else if ADC value > high threshold:
turn off pump/valve
else:
maintain current state
delay for a certain period
This logic continuously monitors the water level by reading the ADC values and controls the pump or
valve accordingly.

Microcontroller Theory and Design Page 4


valve accordingly.
Testing and Refinement:
Upload the code to the microcontroller or PLC and test the system. Make any necessary adjustments
to the threshold levels or programming logic to ensure the desired water level control is achieved.

Temperature Controller using ADC


To build a temperature controller using an ADC (Analog-to-Digital Converter), you will need a
temperature sensor that provides an analog output, a microcontroller or PLC with ADC capabilities,
and a relay module to control the heating or cooling device. Here's a general outline of the process:

1. Components Needed:
- Temperature sensor: You can use a thermistor or any other analog temperature sensor.
- Microcontroller or PLC: Choose a suitable microcontroller or PLC with ADC capabilities.
- Relay module: To control the heating or cooling device (e.g., a heater or a fan).
- Power supply and other necessary components.
2. Sensor Connection:
Connect the analog output of the temperature sensor to one of the ADC input pins of the
microcontroller or PLC. Ensure the power supply for the sensor is connected properly.
3. ADC Configuration:
Set up the ADC on the microcontroller or PLC to read analog values from the connected pin. Refer
to the documentation or programming guide of your specific controller to configure the ADC module
accordingly.
4. Setpoint and Hysteresis:
Determine the desired temperature setpoint for your system. Additionally, decide on the
hysteresis value, which is the temperature difference allowed before triggering the heating or
cooling device. For example, if the setpoint is 25°C and the hysteresis is 2°C, the device will turn on
when the temperature falls below 23°C and turn off when it exceeds 27°C.
5. Programming Logic:
Write the programming logic to read the ADC values, convert them to temperature values using
appropriate calibration, and compare them with the setpoint and hysteresis values. Based on the
comparison, control the relay module to turn the heating or cooling device on or off.
Here's a basic example pseudocode for the programming logic:
loop:
read ADC value
convert ADC value to temperature
if temperature < setpoint - hysteresis:
turn on heating/cooling device
else if temperature > setpoint + hysteresis:
turn off heating/cooling device
else:
maintain current state
delay for a certain period
This logic continuously reads the ADC values, converts them to temperature values, and controls the
heating or cooling device based on the setpoint and hysteresis.
6. Testing and Refinement:
Upload the code to the microcontroller or PLC and test the temperature controller. Make any
necessary adjustments to the setpoint, hysteresis, or programming logic to ensure accurate
temperature control.

Microcontroller Theory and Design Page 5


temperature control.

Microcontroller Theory and Design Page 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy