Embedded System Design - Displays
Embedded System Design - Displays
presentation
BITS Pilani Faculty Name
Faculty Department
Pilani Campus
BITS Pilani
Pilani Campus
0x5b
080x
Draw the circuit schematic diagram for interfacing a three digit 7-segment
LED with an 8-bit microcontroller. Explain the function of the circuit. Show
how the decimal number 876 can be displayed continuously with a short
program.
void main()
{
TRISC = 0x00; // Define PORTC for output operation
TRISB = 0x00; //Define PORTB for output operation
while(1)
{
PORTC = 0x01; //Make RC0 logic high – Binary value 0000 0001
PORTB = 0x7F; //Pattern for Digit 8 in Seg1
Delay_ms(10);
PORTC = 0x02;//Make RC1 logic high - Binary 0000 0010
PORTB = 0x07; //Pattern for Digit 7 in Seg2
Delay_ms(10);
PORTC = 0x04;//Make RC2 high - Binary 0000 0100
PORTB = 0x7D; //Pattern for Digit 6 in Seg3
Delay_ms(10);
}
}
• In order to display a number in the first digit, transistor T1 needs to be turned ON by giving a logic
high from port pin RC0 and the data for the segment needs to be written in PORTB.
• To display the second digit in Seg2, T1 needs to be turned OFF by giving a logic low at RC0 first
and then a logic high at RC1 is given to turn ON T2 connecting the COM of display to GND. Now
PORTB is written with the data byte for display in Seg2.
• Finally the third segment is written by turning OFF T1 and T2 and turning T3 ON by making RC2
logic high and the corresponding data byte written in PORTB.
• Each segment is therefore turned ON in sequence using PORTC bits RC0, RC1 and RC2.
PORTB is then sequentially written with corresponding data bytes that will display the numbers in
each of the segments.
• To write 876 the following pattern needs to be written in PORTB.
Note: Assuming a common cathode type, each segment is lit up when a logic high voltage is given to
the anode terminal using a microcontroller port pin.
Hardware Connections
Register Select RS-> RC3
Enable EN-> RC0
Read/Write is connected to GND
LCD data lines-> PORTD(RD0 to RD7)
Contrast VEE – Connected to a resistor divider to get 0.3V
VCC +5V
GND
LED+ Connected with series resistance to 5V (Backlight)
LED- connected to GND
void LCD_Data(char i)
{
PORTC|=0x08; //RS=1
PORTD=i; //Assign the value to PORTD to display
PORTC|=0x01; // RS=1,R/W=0,EN=1
PORTC&=~0x01; // RS=1,R/W=0,EN=0
Delay(100);
}
Example: Writing ASCII character such as LCD_Data(0x31)
to display the number 1.
Wait Acquisition
20µsec delay
Time
Read ADCON0 No
GO/DONE
=0?
Yes
ADRESH:ADRESL
Read Result (BANK 0:BANK 1)
End
Steps for Conversion
1. Configure the A/D module:
• Configure analog pins / voltage reference /and
digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D conversion clock (ADCON0)
• Turn on A/D module (ADCON0)
2. Wait the required acquisition time.
3. Start conversion:
• Set GO/DONE bit (ADCON0)
4. Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
5. Read A/D Result register pair(ADRESH:ADRESL).
ADC Result Registers
Program
void init(void);
void LCD_command(unsigned char);
void LCD_data(unsigned char);
void LCDoutput(unsigned int);
void delay(unsigned int);
unsigned char k[10];
unsigned int voltage,hivalue,lovalue,value;
void main()
{
init();
while(1)
{
ADCON0=0x81; //configure the A/D control
registers
ADCON0|=0X04; //start ADC conversion
while(ADCON0&0X04); //wait for conversion to
complete
lovalue=ADRESL; //read the low 8 bit value
hivalue=ADRESH; //read the upper 8 bit value
value=((unsigned int)hivalue<<8)+(unsigned int)lovalue;
//load the 10 bit result into value
voltage=value*48/1023; //scale the value to the range
0.0-48.0
LCD_command(0x80);
LCDoutput(voltage); //LCD display function is
called
delay(1000);
}
}
void init(void)
{
TRISA=0X01; //port-Ra0 is made as input
TRISB=0X00;
TRISC=0X00;
ADCON0=0X81; //configure the A/D control
registers
ADCON1=0X8E;
LCD_command(0x38); //Initialize the 2lines and 5*7matrix LCD
LCD_command(0x38);
LCD_command(0x38);
LCD_command(0x38);
LCD_command(0x38);
LCD_command(0x06); //Increment cursor(shift cursor to
right)
LCD_command(0x0c); //Display on,cursor off
LCD_command(0x01); //Clear display screen
LCD_command(0x80);
}
void LCDoutput(unsigned int i)
{
unsigned char n,s,j=1;
unsigned int m;
m=i; //assign formal argument to other
variable
while(m!=0) //check that value is zero or not
{
s=m-(m/10)*10; //Store the remainder of m/10 in s
k[j]=s; //move s to an array
j++; //increment the array address
m=m/10; //Divide the value by 10,store the quotient in `m`
}
k[j]=`\0`; //assign the NULL value at the end of the array
j=j-1; //decrement the array address
if(k[2]>0)
{
n=0x30+k[2]; //convert to ascii value
LCD_data(n); //pass that value to display function
}
else LCD_data(0x20); //print a space if a zero value
n=0x30+k[1]; //convert to ascii value
LCD_data(n); //pass that value to display function
n=0x56; //print v for volts
LCD_data(n);
}
void LCD_command(unsigned char i)
{
//LCD connections
//RSRB6
//ENRB7
PORTB&=~0x40; //RS=0
PORTC=i;
PORTB|=0x80; //RS=0,R/W=0,EN=1
PORTB&=~0x80; //RS=0,R/W=0,EN=0
delay(10);
}
void LCD_data(char i)
{
PORTB|=0X40; //RS=1
PORTC=i; //assign the value to
portc to display
PORTB|=0x80; //RS=1,R/W=0,EN=1
PORTB&=~0x80; //RS=1,R/W=0,EN=0
delay(10);
}
void delay(unsigned int delaycount)
{
while(--delaycount);
}