Microcontroller Lecture
Microcontroller Lecture
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1
General microcontroller
1. Microcontroller definition
A microcontroller is a small, low-cost, and self-contained computer on a chip that can be used as an
embedded system.
A microcontroller is a single chip and it is denoted with μC or uC.
An alternate name of the microcontroller is the embedded controller.
Note:
Embedded system:
An embedded system is a microprocessor-based computer hardware system with software that is
designed to perform a dedicated function, either as an independent system or as a part of a
large system. At the core is an integrated circuit designed to carry out computation for real-time
operations.
A combination of computer hardware and software where software is embedded into hardware is
called an embedded system.
3. Classification
The microcontrollers are characterized regarding bus-width, instruction set, and memory
structure.
For the same family, there may be different forms with different sources/manufacturer.
At present, there are different microcontrollers types that are existing in the market like 4-bit, 8-bit, 64-
bit & 128-bit.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3
In an 8-bit microcontroller, the point when the internal bus is 8-bit then the ALU performs
the arithmetic and logic operations. The examples of 8-bit microcontrollers are Intel
8031/8051, PIC1x, and Motorola MC68HC11 families.
A longer timer’s most extreme worth can likely prove to be useful in certain applications
and circuits. It can automatically operate on two 16 bit numbers. Some examples of the
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4
16-bit microcontrollers are 16-bit MCUs are extended 8051XA, PIC2x, Intel 8096, and
Motorola MC68HC12 families.
The 32-bit microcontroller uses the 32-bit instructions to perform the arithmetic and logic
operations. These are used in automatically controlled devices including implantable
medical devices, engine control systems, office machines, appliances, and other types of
embedded systems. Some examples are Intel/Atmel 251 family, PIC3x.
RISC: The RISC stands for Reduced Instruction set Computer, this type of instruction sets
reduces the design of microprocessor for industry standards. It allows each instruction to
operate on any register or use any addressing mode and simultaneous access of program
and data.
Loop Begin
From the above example, RISC systems shorten execution time by reducing the clock
cycles per instruction, and CISC systems shorten execution time by reducing the number
of instructions per program. The RISC gives a better execution than the CISC.
Microcontrollers Types
There are different microcontroller types like 8051, PIC, AVR, ARM,
AVR Microcontrollers
AVR microcontroller is developed by Alf-Egil Bogen and Vegard Wollan from Atmel
Corporation. The AVR microcontrollers are modified Harvard RISC architecture with
separate memories for data and program and the speed of AVR is high when compared to
8051 and PIC. The AVR stands for Alf-Egil Bogen and Vegard Wollan’s RISC processor.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6
i) TinyAVR – Less memory, small size, suitable only for simpler applications.
ii) MegaAVR – These are the most popular ones having a good amount of memory (up
to 256 KB), the higher number of inbuilt peripherals, and suitable for moderate to
complex applications.
iii) XmegaAVR – Used commercially for complex applications, which require large
program memory and high speed.
Features of AVR Microcontroller
GND: Ground.
Port B: Port B is an 8-bit bi-directional I/O port. The Port B pins are tri-stated when a reset
condition becomes active or one, even if the clock is not running.
[ Tri-state : Can be isolated from bus]
Port C: Port C is a 7-bit bi-directional I/O port with internal pull-up resistors.
PC6/RESET
Port D: It is an 8-bit bi-directional I/O port with internal pull-up resistors. The output buffers
of Port D consist of symmetrical drive characteristics.
MSP Microcontroller
A microcontroller like MSP430 is a 16-bit microcontroller. The term MSP is the acronym of
“Mixed Signal Processor”. This microcontroller family is taken from Texas Instruments and
designed for low cost as well as low power dissipation systems. This controller includes a
16-bit data bus, addressing modes-7 with reduced instructions set, which permits a
denser, shorter, programming code used for quick performance.
This Microcontroller is one kind of integrated circuit, used to execute the programs to
control other machines or devices. It is one kind of a micro-device, used to control other
machines. The features of this microcontroller are normally obtainable with other kinds of
the microcontroller.
• Complete SoC like ADC, LCD, I/O ports, RAM, ROM, UART, watchdog timer, basic
timer, etc. [SoC : System of chip]
• It uses one external crystal and an FLL (frequency-locked loop) oscillator mainly derives
all inner CLKs
• Power utilization is low like 4.2 nW only for each instruction
• Stable generator for the most frequently used constants like –1, 0, 1, 2, 4, 8
• Typical high speed is 300 ns for each instruction like 3.3 MHz CLK
• Addressing modes are 11 where the seven addressing modes are used for source
operands & four addressing modes are used for destination operand.
• RISC architecture with 27 core instructions
Real-time capacity is full, stable, and nominal system CLK frequency is obtainable after 6-
clocks only once the MSP430 is restored from low-power mode. For the main crystal, no
waiting to start stabilize & oscillation.
The core instructions were combined using special features to make the program easy
within the MSP430 microcontroller using assembler otherwise in C to provide outstanding
functionality as well as flexibility. For instance, even by using a low instruction count, the
microcontroller is capable of following approximately the whole instruction set.
Hitachi Microcontroller
Hitachi microcontroller belongs to the H8 family. A name like H8 is used within a large 8-
bit, 16-bit & 32-bit family of microcontrollers. These microcontrollers were developed
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10
through Renesas Technology. This technology was founded in Hitachi semiconductors, in
the year 1990.
Motorola Microcontroller
Motorola microcontroller is an extremely incorporated microcontroller, used for data
handling process with high-performance. The unit of this microcontroller uses a SIM
(System Integration Module), TPU (Time Processing Unit) & QSM (Queued Serial
Module).
4. Architecture
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 11
I/O Ports
Basically, these ports are used to interface with different appliances like LEDs, LCDs,
printers, etc.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
12
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. Function(ADC/Timer/Interupt/PWM/USB/serial communication)
Microcontroller performs special functions described below.
Timers
A microcontroller includes timers otherwise counters.
These are used to manage all the operations of timing and counting in a
microcontroller.
The main function of the counter is to count outside pulses whereas the operations
which are performed through timers are clock functions, pulse generations,
modulations, measuring frequency, making oscillations, etc.
Interpret Control
This controller is employed to give delayed control to a running program &
interpretation is either internal otherwise external.
ADC (Analog to Digital Converter)
ADC is the acronym of analog to digital converter.
The main function of ADC is to change the signals from analog to digital. For ADC, the
required input signals are analog and the production of a digital signal is used in
different digital applications like measurement devices
DAC (Digital to Analog Converter)
The acronym of DAC is digital to analog converter, used to perform reverse functions
to ADC.
Generally, this device is used to manage analog devices such as DC motors, etc.
Serial Ports
Serial ports are used to provide serial interfaces between microcontroller as well as a
variety of other peripherals which support serial port communication.
USB Port
USB (Universal Serial Bus)
It is the most common connection used to connect a microcontroller to different
devices such as digital cameras, printers, scanners and external hard drives etc.
USB TRANSFER SPEEDS
USB 1.0 Supports data transfer rates of:
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 13
5 Mega bits per second Mbps (low speed), used for Human Input Devices e.g
•
keyboard, mouse, joysticks etc
• 12 Mbps (high speed) used for printers, scanners etc
USB 2.0 known as hi-speed USB and capable of supporting a transfer rate up to
480Mbps.
USB 3.0 Also known as super-speed USB and supports transfer rates up to
5.0 gigabits per second (Gbps).
6. Purpose/Application
There are many applications of microcontrollers; they are used in home automation,
touch screen, automobiles, medical devices, and defense.
Automobiles
Mobile Phones
Computer Systems
Security Alarms
Appliances
Current meter
Cameras
Micro Oven
Measurement Instruments
Controlling Devices
Light Sensing
Safety devices
Controlling devices
Fire detection
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 15
Temperature sensing
Mobile Phones
Auto Mobiles
Washing Machines
Cameras
Security Alarms
7. Process of using(Programming/Burning)
Microcontroller programming means coding of a microcontroller for different purposes
in a special software. There are lots of software in which we can write different codes
for microcontroller and IC Chips..
Intel hexadecimal object file format, Intel hex format or Intellec Hex is a file format
that conveys binary information in ASCII text form. It is commonly used for
programming microcontrollers, EPROMs, and other types of programmable logic
devices.
8. Advantages
The advantages of microcontrollers types include the following.
Dependable
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 16
Reusable
Energy-efficient
Cost-effective
Reusable
It requires less time to operate
These are flexible & very small
Because of their high integration, its size & cost of the system can be decreased.
Interfacing of the microcontroller is easy with additional ROM, RAM & I/O ports.
Many tasks can be performed, so the human effect can be reduced.
It is simple to use, troubleshooting & maintaining the system is simple.
It works like a microcomputer without any digital parts
9. Disadvantages
The disadvantages of the microcontrollers types include the following.
Programming Complexity
Electrostatic Sensitivity
Specific microcontroller(PIC18f4550):
1. Pin configuration
PIC18F4550 Compiler
Their official Compiler is available online at Microchip website and can be used free
of cost.
MikroC Pro for PIC Compiler which you can download from their official site and it has
a demo version but you have to buy the full version.
All the Grounded lines are shown in black color and Power Lines with +5V are shown
with Red Lines.
We also have Crystal Oscillator for frequency here connected to Pin # 13 & Pin # 14.
Now, let's have a look at PIC18F4550 Ports:
Port A: It has 6 Pins in total starting from Pin # 2 to Pin # 7.
Port B: It has 8 Pins in total starting from Pin # 33 to Pin # 40.
Port C: It has 8 Pins in total. It's pins are not aligned together. ...
Port D: It has 8 Pins in total.
Port E: It has 3 Pins in total starting from Pin # 8 to Pin
2. Architecture
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 20
3. Function(ADC/Timer/Interupt/PWM/USB/serial communication)
PIC18F4550 USB Communication
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 21
Here's the main reason of PIC18F4550's popularity, it supports USB Communication.
Here's the simplest circuit diagram for PIC18F4550 USB Communication,
So, we can do the PIC18F4550 USB Communication by using below two Pins:
Pin # 23: (D-)
Pin # 24: (D+)
The performance of the ADC is measured using the resolution and speed as
parameters. Resolution is nothing but the number of levels or intervals which can be
divided from a certain analog input range which is expressed in bits. The resolution of
an n-bit ADC is 1/(2^n). For example, an ADC with 10-bit resolution as one in PIC
18F4550 can have 2^10 (1024) intervals for the range of analog input signal.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 23
PIC18F4550 has 13 channels which mean 13 analog input signals can be converted
simultaneously using the module. 8 choosable clock inputs are available for conversion
and the module can be configured in auto triggering mode.
Other terminology usually appears while using ADC is the reference voltage. It
specifies the minimum and maximum voltage range of analog input. PIC18F4550 has
two reference voltage inputs, Vref- and Vref+. Vref- is the minimum input voltage of
analog input and Vref+ is the maximum voltage. That means if Vref- is applied to the
pin for conversion, the digital value after conversion will be 0 and for the Vref+, this will
be the maximum value (1023 for 10-bit resolution). The PIC18F4550 pins for Vref+ is
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 24
RA3 and Vref- is RA2. In normal cases, we set Vref- as 0 and Vref+ as 5V. The digital
value is calculated based on the following equation.
• 2 Tosc
• 4 Tosc
• 8 Tosc
• 16 Tosc
• 32 Tosc
• 64 Tosc
• Internal RC Oscillator
Tad must be greater than minimum Tad specified and must be as short as possible.
From PIC18F4550 datasheet, it is 0.7 µs (page 400, table 28-29). So for a 20MHz
PIC18F4550,
Fosc = 20 MHz
Tosc = 1/20 MHz = 50ns
If we select 16 Tosc
16 Tosc = 16x 50ns = 0.8 us
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 25
which is more than 0.7 us(minimum requirement)
xxxxxxxxxxxxxxxxxxx
PIC18F4550 ADC
Introduction
• When we interface sensors to the microcontroller, the output of the sensor many of the times is analog
in nature. But microcontroller processes digital signals.
• Hence we use ADC in between sensors and microcontrollers. It converts an analog signal into digital
and gives it to the microcontroller.
• There are a large number of applications of ADC like in a biometric application, Environment
monitoring, Gas leakage detection, etc.
ADRESH (High byte) and ADRESL (Low byte) Registers are used in combination to store the
converted data i.e. digital data. But the data is only 10-bit wide, so the remaining six bits are not
used.
When ADON=1
0= A/D is idle
1= Vref –
0= Vss
1= Vref +
0= Vdd
As the ADC pins in PIC18F4550 are multiplexed with many other functions. So these bits are
used to de-multiplex them and use them as an analog input pin.
xxxxxxxxxxx
The microcontroller PIC18F4550 has 13 ADC (Analog to Digital Converter) channels.
The resolution of the PIC18F4550 ADC is 10-bit, which means the analog value after conversion is stored as a
10-bit number that varies from 0 to 1023 (0x3FF).
The analog reference voltage is software selectable to either the device’s positive and negative supply
voltage(VDD and VSS) or the voltage level on the RA3/AN3/VREF+ and RA2/AN2/VREF-/CVREF pins.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 29
The A/D converter has a unique feature of being able to operate while the device is in Sleep mode.
To operate in Sleep, the A/D conversion clock must be derived from the A/D’s internal RC oscillator.
Timer
PIC18F4550 Timer
Introduction
Timers in microcontrollers are used for introducing delay, counting events, generating
waveforms, and also for PWM generation.
1. Provide delay using a software (through code using loops). But, a delay provided in this
way forces the microcontroller to put all its resources for the processing of the loops, and
thus blocks the code execution.
2. Providing delay using the timer in the microcontroller. In this approach, the delay is
provided by loading a count in a timer. So when a timer interrupt is generated current
execution will move to the ISR to serve the task. But when there is no timer interrupt it will
perform another task. Therefore, we can say it is a kind of non-blocking function.
• Timer0 : 16-bit
• Timer1 : 16-bit
• Timer2 : 8-bit
• Timer3 : 16-bit
Out of the four timers in PIC18F4550, we will be discussing here Timer1. The working of the rest
of the timers is the same. To generate a time delay using Timer1, we have to calculate a count of
the desired time.
Fosc = 8 MHz
Then, to find the period which will be taken by the timer to increment the count,
Therefore, after each 0.5 us time elapses, the timer value will get incremented by 1.
Before we load this count in the timer, we first need to subtract it from 65536. The reason we
need to subtract it is that the timer is 16-bit. Therefore,
Converting this count into hex we get 0xF830. Load this hex value in the Timer1 i.e. TMR1
register. TMR1 is a 16-bit register that is used to load the count for Timer1.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 31
Timer1 Interrupt
The TMR1 register gets incremented from this loaded value to the highest value i.e. 0xFFFF. After
an overflow occurs in the register, it resets to 0 and the TMR1IF overflow interrupt flag gets set.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 32
TMR1H and TMR1L registers can also be individually used to load the count in Timer1. These are
both 8-bit registers.
To use Timer1 in PIC18F4550, Control and Status registers are used. Let us understand more
about these registers of Timer1 in PIC18F4550.
0 = Enable read/write of Timer1 in two 8-bit registers which are TMR1H and TMR1L
0 = Device clock is derived from another source; it may be an internal oscillator or external
crystal oscillator.
These bits divide the clock by a value before it is applied to the Timer1 as a clock.
11 = 1 : 8 pre-scale value
10 = 1 : 4 pre-scale value
01 = 1 : 2 pre-scale value
00 = 1 : 1 pre-scale value
When TMR1CS = 0
When TMR1CS = 1
This bit is generally used for counting events. To use Timer1 as counter, set TMR1CS = 1.
1 = Start Timer1.
0 = Stop Timer1.
After Timer1 initializes, it continuously monitors the TMR1IF interrupt flag which gets set when
the Timer1 overflows. TMR1IF is located in the PIR1register as shown below.
1. Sample projects
(Circuit,programming,Simulation)
Project 1:
Question:
Build a microcontroller project which converts analogue voltage into
equivalent binary/decimal number and display in LCD display unit.
----------------
This project converts analogue voltage into binary/decimal number.
Converted number is shown in LCD Display.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
A potentiometer is used to insert variable input voltage(0V-5V).
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
#device ADC=10
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>
unsigned int16 i;
void main(){
setup_oscillator(OSC_8MHZ);
lcd_init(); // Initialize LCD module
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 35
setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc
setup_adc_ports(AN0); // Configure RA0 (AN0) as analog
set_adc_channel(0); // Select channel 0 input
delay_ms(100); // Wait 100ms
lcd_gotoxy(3, 1); // Go to column 3 row 1
lcd_putc("ADC reading:");
while(TRUE){
i = read_adc();
lcd_gotoxy(7, 2); // Go to column 7 row 2
printf(lcd_putc,"%4Lu",i); // Write i with 4 numbers max
delay_ms(10); // Wait 10ms
}
}
------------------------------ xxxxxxxxxxxxx------------------------------------------
-------------------xxxxxxxxxxxxxxxx-------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 36
Project 2:
Question:
Build a microcontroller project which converts analogue voltage into
equivalent binary/decimal number and display in LCD display unit.
Reference voltages should be used to have higher precision.
--------------------
This project converts analogue voltage into binary/decimal number.
Converted number is shown in LCD Display.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
A potentiometer is used to insert variable input voltage(0V-5V).
2 reference pins are used to limit the conversion voltage and variable precision.
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
#device ADC=10
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>
unsigned int16 i;
void main(){
setup_oscillator(OSC_8MHZ);
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 37
lcd_init(); // Initialize LCD module
setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc
Project 3:
Question:
Build a microcontroller project where an LED blinks at a regular
interval.
--------------------
An LED blinks at a regular interval.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
void main()
{
setup_wdt(WDT_ON);
setup_wdt(WDT_1S);
while(TRUE)
{
output_toggle(PIN_B0); // Toggle output pin RB0
delay_ms(500);
restart_wdt();
}
}
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 39
-------------------xxxxxxxxxxxxxxxx----------------------------
Project 4:
Question:
Build a microcontroller project showing bit wise data reading and
writing.
--------------------
Press switches and LEDs are use.
When a switch is pressed, corresponding LED is lit.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 40
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
short azbit1, azbit2 ;
void main()
{
set_tris_b(0x00); // set output mode
output_b(0x00); //All pins of PORTE LOW (VSS)
set_tris_d(0xFF); // set Input mode
setup_wdt(WDT_ON);
setup_wdt(WDT_1S);
while(TRUE)
{
azbit1 = input(PIN_D0);
azbit2 = input(PIN_D1);
if( azbit1==1)
{
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
if( azbit2==1)
{
output_high(PIN_B1);
}
else
{
output_low(PIN_B1);
}
--------------xxxxxxxxxxxxxxx----------------------
-----------------------------xxxxxxxxxxxxxxxxxx---------------------------
Project 5:
Question:
Build a microcontroller based project showing byte wise data
reading and writing.
--------------------
Band-switches and Band-LEDs are use.
Port D is read and written in port D
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 42
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <18F4550.h>
void main()
{
setup_wdt(WDT_ON);
setup_wdt(WDT_1S);
while(TRUE)
{
i=input_d();
delay_ms(10);
output_b(i); //All pins of PORT b
-----------------xxxxxxxxxxxxx------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 44
Project 6:
Question:
Build a microcontroller based project do develop a sine wave
inverter. Use internal interrupt and PWM.
--------------------
Internal interrupts are used. Timer2 is used to generate PWM. Sine function is used to
generate sinusoidal pulse width modulation.
PIC18F4550 microcontroller is used.
Program is written in CCS C compiler.
Circuit is drawn and simulation is run in Proteus 8.
Program:
// PIC18F4550 ADC example with CCS C compiler
#include <code6.h>
#fuses MCLR
#FUSES WDT // Watch Dog Timer
#FUSES HS //Highspeed Osc > 4mhz
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=20000000)
long int init_time=0 ; // present time, max time, timer initial number
short mode;
int8 i_az=0;
i_az = -1;
mode = ~mode; // Toggle mode variable
if(mode == 1)
{
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 45
output_high(PIN_B6); //
output_low(PIN_B5); //
}
else
{
output_low(PIN_B6); //
output_high(PIN_B5); //
}
}
set_pwm2_duty(sin_table1[i_az]);
if(i_az >= 101) i_az = 100;
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
init_time=64286; // 34.286, 65536 -1250
set_timer0(init_time);
enable_interrupts(INT_RTCC);
}
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 46
sin_table1[0]=0;
sin_table1[1]=0;
sin_table1[97]=0;
sin_table1[98]=0;
sin_table1[99]=0;
sin_table1[100]=0;
sin_table1[101]=0;
sin_table1[102]=0;
setup_ccp2(CCP_PWM);
enable_interrupts(GLOBAL);
set_tris_e(0xF0);
set_tris_b(0x0F);
output_e(0x00); //All pins of PORTE LOW (VSS)
setup_wdt(WDT_ON);
setup_wdt(WDT_1S);
} // End main
Program:
// PIC18F4550 example with CCS C compiler
#include <18F4550.h>
#fuses NOMCLR
#FUSES WDT // Watch Dog Timer
#FUSES HS //Highspeed Osc > 4mhz
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=20000000)
void main()
{
setup_oscillator(OSC_8MHZ); // Set the internal oscillator to 8MHz
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts(GLOBAL);
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 50
while(TRUE)
{
output_low(PIN_B3); // output low pin RB3
delay_ms(500);
restart_wdt();
output_high(PIN_B3); // output low pin RB3
delay_ms(800);
output_low(PIN_B4);
restart_wdt();
}
}
Circuit and simulation
---------------------------xxxxxxxxxxxxxxxx-----------------------------
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
51
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
########################################################
Introduction to Microcontroller
Collected and Arranged by Prof Dr Md Aziz ul Huq, EEE, IUBAT university, Dhaka, Bangladesh
[email protected], [email protected], Cell: +8801713018164
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 52
Square wave inverter