In: Computer Science
write the code of 4 digit 7-segment display using arduino uno in assembly language by using software AVR STUDIO 5.1?
Arduino Code – for interfacing 4 digit 7 segment display to arduino using shift register
Use following code for the common anode 7 segment display.
#include "Timer.h" //include timer library
Timer t; // create a timer object
long number = 0; //declare the variables
int first_digit = 0;
int second_digit = 0;
int third_digit = 0;
int fourth_digit = 0;
int timer_event = 0;
int CA_1 = 12;
int CA_2 = 11;
int CA_3 = 10;
int CA_4 = 9;
int clk = 6;
int latch = 5;
int data = 4;
int count = 0;
int digits[4] ;
int CAS[4] = {12, 11, 10, 9};
byte numbers[10] {B00000011, B10011111, B00100101, B00001101, B10011001, B01001001, B01000001, B00011111, B00000001, B00001001};
//byte combinations for each number 0-9
void setup() {
Serial.begin(9600); //serial start and pin config
pinMode(CA_1, OUTPUT);
pinMode(CA_2, OUTPUT);
pinMode(CA_3, OUTPUT);
pinMode(CA_4, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(data, OUTPUT);
digitalWrite(CA_1, LOW);
digitalWrite(CA_2, LOW);
digitalWrite(CA_3, LOW);
digitalWrite(CA_4, LOW);
Serial.println("please Enter a number from 0 to 9999");
}
void loop() {
t.update(); //timer update
if (Serial.available()) { // read from serial
t.stop(timer_event); //stop timer if anything to read
cathode_high(); // blank the screen
String s = Serial.readString(); //read the serial value
number = (long)s.toInt(); //convert it to int
if (number > 9999) { //check the number is 0-9999
Serial.println("Please Enter Number Between 0 - 9999");
} else {
break_number(number);
timer_event = t.every(1, display_number); // start timer again
}
}
}
void break_number(long num) { // seperate the input number into 4 single digits
first_digit = num / 1000;
digits[0] = first_digit;
int first_left = num - (first_digit * 1000);
second_digit = first_left / 100;
digits[1] = second_digit;
int second_left = first_left - (second_digit * 100);
third_digit = second_left / 10;
digits[2] = third_digit;
fourth_digit = second_left - (third_digit * 10);
digits[3] = fourth_digit;
}
void display_number() { //scanning
cathode_high(); //black screen
digitalWrite(latch, LOW); //put the shift register to read
shiftOut(data, clk, LSBFIRST, numbers[digits[count]]); //send the data
digitalWrite(CAS[count], HIGH); //turn on the relevent digit
digitalWrite(latch, HIGH); //put the shift register to write mode
count++; //count up the digit
if (count == 4) { // keep the count between 0-3
count = 0;
}
}
void cathode_high() { //turn off all 4 digits
digitalWrite(CA_1, LOW);
digitalWrite(CA_2, LOW);
digitalWrite(CA_3, LOW);
digitalWrite(CA_4, LOW);
}
Use following code for the common cathode 7 segment display.
#include "Timer.h" //include timer library Timer t; // create a timer object long number = 0; //declare the variables int first_digit = 0; int second_digit = 0; int third_digit = 0; int fourth_digit = 0; int timer_event = 0; int CA_1 = 12; int CA_2 = 11; int CA_3 = 10; int CA_4 = 9; int clk = 6; int latch = 5; int data = 4; int count = 0; int digits[4] ; int CAS[4] = {12, 11, 10, 9}; byte numbers[10] {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110}; //byte combinations for each number 0-9 void setup() { Serial.begin(9600); //serial start and pin config pinMode(CA_1, OUTPUT); pinMode(CA_2, OUTPUT); pinMode(CA_3, OUTPUT); pinMode(CA_4, OUTPUT); pinMode(clk, OUTPUT); pinMode(latch, OUTPUT); pinMode(data, OUTPUT); digitalWrite(CA_1, HIGH); digitalWrite(CA_2, HIGH); digitalWrite(CA_3, HIGH); digitalWrite(CA_4, HIGH); Serial.println("please Enter a number from 0 to 9999"); } void loop() { t.update(); //timer update if (Serial.available()) { // read from serial t.stop(timer_event); //stop timer if anything to read cathode_high(); // blank the screen String s = Serial.readString(); //read the serial value number = (long)s.toInt(); //convert it to int if (number > 9999) { //check the number is 0-9999 Serial.println("Please Enter Number Between 0 - 9999"); } else { break_number(number); timer_event = t.every(1, display_number); // start timer again } } } void break_number(long num) { // seperate the input number into 4 single digits first_digit = num / 1000; digits[0] = first_digit; int first_left = num - (first_digit * 1000); second_digit = first_left / 100; digits[1] = second_digit; int second_left = first_left - (second_digit * 100); third_digit = second_left / 10; digits[2] = third_digit; fourth_digit = second_left - (third_digit * 10); digits[3] = fourth_digit; } void display_number() { //scanning cathode_high(); //black screen digitalWrite(latch, LOW); //put the shift register to read shiftOut(data, clk, LSBFIRST, numbers[digits[count]]); //send the data digitalWrite(CAS[count], LOW); //turn on the relevent digit digitalWrite(latch, HIGH); //put the shift register to write mode count++; //count up the digit if (count == 4) { // keep the count between 0-3 count = 0; } } void cathode_high() { //turn off all 4 digits digitalWrite(CA_1, HIGH); digitalWrite(CA_2, HIGH); digitalWrite(CA_3, HIGH); digitalWrite(CA_4, HIGH); }
Important code lines are commented in the code. In this code you can send any number from 0 - 9999 through the serial monitor (refer the image given above). We are using the arduino timer interrupts to switch between digits. The segments should be turn on and off for each number is stored in a byte array. In the loop() – serial values are read and converted to int and then to long data types. Then this long data is broke in to single digits by break_number () method. Multiplexing is done by the timer class every() function and it calls display_number() method once every millisecond. This method use arduino shiftOut () function to send signals to shift register. Note how latch pin is turned LOW before we send data and turned HIGH after sending data. The function called cathode_high() is to turn off the screen.