MicroprocessorBasedSystems Term-II Lec6 Embedded C Programming Interrupts
MicroprocessorBasedSystems Term-II Lec6 Embedded C Programming Interrupts
‘Embedded C programming-
Interrupts’
▪ Clock
▪ You'll need to generate or provide a clock signal to
the ADC0808. This might be done externally or by
toggling a GPIO pin in the 8051 in a timer
interrupt.
▪ Address
▪ The address lines need to be set before the ALE
pulse. This will select which channel you are
reading from.
▪ ALE (Address Latch Enable)
▪ Set ALE high and then low to latch in the address.
▪ Start
▪ After a brief delay (following the ALE pulse), set
START high and then low to initiate conversion.
▪ EOC (End of Conversion)
▪ Wait for EOC to go low, which indicates the
conversion is complete. Better to wait till it goes
high again.
▪ Output Enable
▪ After EOC goes low, set OE high to enable the
output of the data. The data will be valid for
reading as indicated in the timing diagram.
▪ Data Outputs
▪ Read the data outputs from the ADC0808.
© Dr. Ahmed El-Awamry 15.03.2024 13
Code Example
#include <reg51.h>
// Control Pins
sbit ALE = P3^3;
sbit START = P3^4;
sbit EOC = P3^5;
sbit OE = P3^6;
sbit CLOCK = P3^7; // Timer 0 will toggle this clock pin (500kHz)
#define ADC_DATA P1
// Start Timer 0
TR0 = 1;
// Disable output
OE = 0;
return adc_value;
}
© Dr. Ahmed El-Awamry 15.03.2024 16
Code Example
void main() {
ADC_Init(); // Initialize ADC and
Timer 0 for clock generation
while(1) {
unsigned char value =
ADC_Read();
// Process the value or display it
}
}
// Sine wave lookup table (for example: 32 entries of 8-bit sine wave
values)
unsigned char code sine_table[] = {
128, 176, 218, 245, 255, 245, 218, 176,
128, 79, 37, 10, 0, 10, 37, 79,
//... repeat pattern or fill in the rest of the sine values
};
// Timer 0 ISR
void Timer0_ISR(void) interrupt 1 {
// Write the next value from the sine table to the DAC
DAC_PORT = sine_table[sine_index];
void Timer0_Init() {
// Timer 0 in mode 2 (auto-reload mode)
TMOD = (TMOD & 0xF0) | 0x02; // Set T0 to mode 2
// Calculate and set the reload value for Timer 0 to get the required interrupt rate
// Example values given for a 12 MHz clock to produce a 256 kHz update rate
TH0 = 0xA4; // Timer high byte (reload value)
TL0 = 0xA4; // Timer low byte (initial value)
void DAC_Init() {
// Initialize the DAC port as output
DAC_PORT = 0x00; // Set all DAC pins to low
}
void main() {
DAC_Init();
Timer0_Init();
while(1) {
// Main loop can be used to perform other tasks or put the MCU
in low-power mode
// DAC update is handled by the Timer 0 interrupt
}
}
© Dr. Ahmed El-Awamry 15.03.2024 21
Appendix