Microcontrollers
1. Digital keypad with switch coding
Main.h
#ifndef MAIN_H
#define MAIN_H
#define LED_ARRAY1 PORTD
#define LED_ARRAY1_DDR TRISD
#define OFF 0x00
#endif /* MAIN_H */
Main.c
#include <xc.h>
#include "main.h"
#include "digital_keypad.h"
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
static void init_config(void) {
LED_ARRAY1 = OFF;
LED_ARRAY1_DDR = 0x00;
init_digital_keypad();
}
void main(void) {
unsigned char key;
init_config();
while (1) {
key = read_digital_keypad(STATE);
if (key != ALL_RELEASED)
{
LED_ARRAY1 = key;
}
}
return;
}
Digital_keypad.h
#ifndef DIGITAL_KEYPAD_H
#define DIGITAL_KEYPAD_H
#define LEVEL_DETECTION 0
#define LEVEL 0
#define STATE_DETECTION 1
#define STATE 1
#define KEYPAD_PORT PORTB
#define KEYPAD_PORT_DDR TRISB
#define INPUT_LINES 0x3F
#define SW1 0x3E
#define SW2 0x3D
#define SW3 0x3B
#define SW4 0x37
#define SW5 0x2F
#define SW6 0x1F
#define ALL_RELEASED INPUT_LINES
unsigned char read_digital_keypad(unsigned char mode);
void init_digital_keypad(void);
#endif /* DIGITAL_KEYPAD_H */
Digital_keypad.c
#include <xc.h>
#include "digital_keypad.h"
void init_digital_keypad(void)
{
/* Set Keypad Port as input */
KEYPAD_PORT_DDR = KEYPAD_PORT_DDR | INPUT_LINES; //set 6 ports as input
}
unsigned char read_digital_keypad(unsigned char mode)
{
static unsigned char once = 1;
if (mode == LEVEL_DETECTION)// if the switch is pressed only once
{
return KEYPAD_PORT & INPUT_LINES;
}
else
{
if (((KEYPAD_PORT & INPUT_LINES) != ALL_RELEASED) && once)
{
once = 0;
return KEYPAD_PORT & INPUT_LINES;
}
else if ((KEYPAD_PORT & INPUT_LINES) == ALL_RELEASED)
{
once = 1;
}
}
return ALL_RELEASED;
}
Level detection refers to the process of identifying whether a signal is at a particular voltage
level. In digital circuits or switches, this can involve detecting whether a signal is high or low
(i.e., whether the voltage level is above or below a certain threshold).
Edge detection refers to detecting changes in a signal, typically from one state to another. In
digital systems, this would involve detecting transitions in the signal, such as from a "low"
state to a "high" state (rising edge) or from "high" to "low" state (falling edge).
State detection refers to identifying or detecting the current state or value of a signal at a
particular moment in time. In digital systems, this typically means checking whether the
signal is at a logical high (1) or logical low (0), or checking the state of a system or register
(e.g., whether a switch is "on" or "off").
Seven segment Display
1. Common Cathode (CC) Seven-Segment Display:
• In this type, all the cathodes (negative terminals) of the individual LEDs in the display
are connected together and then to ground (GND).
• To light up a segment, a positive voltage is applied to the corresponding anode
(positive terminal) of that segment.
• When the common cathode is grounded, applying a high voltage (usually 5V) to the
appropriate segment will turn it on.
2. Common Anode (CA) Seven-Segment Display:
• In this type, all the anodes (positive terminals) of the individual LEDs are connected
together and then to a positive voltage (usually 5V).
• To light up a segment, a low voltage (ground) is applied to the corresponding cathode
(negative terminal) of that segment.
• When the common anode is connected to a positive voltage, applying a low voltage
to a segment will turn it on.
Blocking Delay:
Non-Blocking delay
We are using common cathode configuration
EEPROM(Electrically erasable read only memory)
EEPROM stands for Electrically Erasable Programmable Read-Only Memory. It is a type of
non-volatile memory used in electronics to store data that must be retained even when the
power is turned off. Here are some key points about EEPROM:
Key Characteristics:
1. Non-volatile: EEPROM retains data even after the device is powered off, unlike
volatile memory like RAM (Random Access Memory), which loses data when power
is lost.
2. Electrically Erasable and Programmable: Unlike older ROM (Read-Only Memory)
chips that could only be programmed during manufacturing, EEPROM can be erased
and reprogrammed multiple times using electrical signals.
3. Byte-level access: EEPROM allows data to be written or erased on a byte-by-byte
basis, rather than in large blocks, making it suitable for storing small amounts of data
that change frequently, such as configuration settings or calibration data.
1. External EEPROM
• Description: External EEPROM refers to EEPROM chips that are physically separate
from the main microcontroller or processor. These chips are connected to the
microcontroller (or another device) via a communication interface like I2C, SPI,
or Parallel.
2. Internal EEPROM
• Description: Internal EEPROM refers to EEPROM memory that is built directly into
the microcontroller or processor itself. It is part of the microcontroller's architecture
and is used for small, non-volatile data storage needs.
Second code causes bad response time and event missing
In embedded systems, polling is a technique where the system repeatedly checks (or
"polls") the status of an event or input at regular intervals to detect any changes or
responses. This approach is used to monitor hardware or software conditions, such as
checking the state of a sensor, a button press, or a communication interface.
Disadvantages:
1. Inefficient Use of CPU Resources
2. Increased Power Consumption
3. Latency and Delayed Response
4. Unpredictable Behaviour
6. Missed Events
Interrupt
An interrupt is a mechanism in a computer or embedded system that temporarily halts the
current execution of a program to handle an event or condition that requires immediate
attention. When an interrupt occurs, the system saves its state, executes a specific function
called an Interrupt Service Routine (ISR) to address the event, and then resumes the normal
program execution once the ISR is completed. Interrupts allow a system to respond quickly
to external or internal events without the need for continuous polling.
An internal interrupt is an interrupt generated by events that occur inside the
microprocessor or microcontroller itself, typically caused by the CPU’s internal operations or
conditions. These interrupts do not require external hardware signals; instead, they are the
result of internal faults, errors, or specific processor states. They are often used for handling
exceptions or abnormal conditions that arise during the execution of a program.
An external interrupt is an interrupt generated by external hardware devices or
peripherals that require the processor's immediate attention. These interrupts are typically
triggered by events such as input from sensors, user interaction (e.g., button presses), or
communication devices (e.g., UART or SPI data received). External interrupts allow the
system to respond to real-world events asynchronously, without the need for constant
polling.
An interrupt handler (also known as an Interrupt Service Routine or ISR) is a special
function or piece of code that is executed in response to an interrupt in an embedded
system. When an interrupt occurs, the processor halts its current execution flow and
transfers control to the interrupt handler to process the event that triggered the interrupt.
Once the interrupt handler completes its task, the processor resumes normal execution
from where it was interrupted.
1 An Interrupt Vector Table (IVT) is a data structure used in microcontrollers and embedded
systems that holds the memory addresses (or pointers) of all the interrupt service routines
(ISRs) in the system. Each entry in the interrupt vector table corresponds to a specific
interrupt, either hardware or software, and points to the ISR that should be executed when
that interrupt occurs.
2. An Interrupt Service Routine (ISR), also known as an interrupt handler, is a special function
or block of code in a system that is executed when an interrupt occurs. The ISR is
responsible for handling the specific event or condition that triggered the interrupt, such as
a hardware event (e.g., timer overflow, button press) or a software exception (e.g., division
by zero error).
External interrupt key
RB0 is used as interrupt key
Code:
Program will enter sleep mode and wake when it gets interrupt for power management.
Timers
Counter
Properties of timer
Count can’t be in points. So we need to change the value of resolution.
Code
Pre- scaling
1. Prescaling:
Prescaling is the process of dividing the input clock frequency before it is fed into the timer.
It is used to reduce the frequency of the timer to make it more manageable for various
applications. Without prescaling, the timer would run at the full input clock speed, which
might be too fast for certain tasks. By applying a prescaler, the timer’s clock speed can be
slowed down to a desired rate.
• How it works: A prescaler divides the input clock by a certain factor, which results in
a lower timer frequency. The prescaler is typically a factor like 1, 8, 64, 256, etc.,
depending on the timer configuration.
• Example: If the timer is driven by a clock with a frequency of 16 MHz, and a prescaler
of 8 is used, the timer will operate at 16 MHz / 8 = 2 MHz. This reduces the rate at
which the timer counts, allowing for more manageable time intervals for the
application.
2. Post-scaling:
Post-scaling is less common but still used in some timers. It involves dividing the timer count
after it has been incremented by the timer's clock. This can be useful when you want to
increase the period of the timer before it triggers an event or interrupt, by dividing the
number of counts the timer accumulates.
• How it works: After the timer has counted to a certain value, the post-scaler divides
the timer's count. This means that for every N counts of the timer, the post-scaler
ensures the timer triggers only after every M counts. Essentially, it affects how often
a certain timer overflow or event occurs.
• Example: If the timer is set to overflow after 256 counts, and a post-scaler is set to
divide by 4, the timer would trigger an event every 1024 counts, instead of every 256
counts.
>>> created a program to toggle led using timer0
8-bit timer: Timer2 is an 8-bit timer, which means it can count from 0 to 255.
CLCD
Matrix keypad
When a key is pressed, it connects one row to one column. This creates a closed circuit that
can be detected by the microcontroller.
Rows: 4 lines (R1, R2, R3, R4) connected to the rows of the matrix.
Columns: 4 lines (C1, C2, C3) connected to the columns of the matrix
Clock I/O
Analog inputs