Question

In: Computer Science

write the code of 4 digit 7-segment display using arduino uno in assembly language by using...

write the code of 4 digit 7-segment display using arduino uno in assembly language by using software AVR STUDIO 5.1?

Solutions

Expert Solution

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.


Related Solutions

The following are to be completed using MIPS Assembly code. A.   Using syscall #4, display a...
The following are to be completed using MIPS Assembly code. A.   Using syscall #4, display a prompt for the user to enter a number. Then using syscall #5, get a decimal number from the user and save it to data memory (not into a register). After you have stored the number, display it back to the user as a 32-bit binary value. B.   Prompt the user to enter a decimal number and save it to data memory. Using addition and/or...
I need to develop a VHDL code for a FPGA basys 3, 4 digit 7 segment...
I need to develop a VHDL code for a FPGA basys 3, 4 digit 7 segment display. So when binary 0, 1 ,and 2 are inputted the display says bad. When binary 3,4,5,6, the display says good.
I need to develop a VHDL code for a FPGA basys 3, 4 digit 7 segment...
I need to develop a VHDL code for a FPGA basys 3, 4 digit 7 segment display. So when binary 0, 1 ,and 2 are inputted the display says bad. When binary 3,4,5,6, the display says good.
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code...
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code that transmits a single character and lights the red LED upon receiving that character. The board will "talk" to itself. The green LED should turn on whenever a message is sent and the LCD will display the message being received.
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me #include <stdlib.h> #include <DHT.h> #define DHTPIN 5         // DHT data pin connected to Arduino pin 5 #define DHTTYPE DHT11     // DHT11 (DHT Sensor Type ) DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "WiFi Name"     // "WiFi Name" #define PASS "WiFi Password"       // "Password" #define IP "184.106.153.149"// thingspeak.com ip String msg = "GET /update?key=Your API...
Write a verilog code for digital clock and display it’s seven segment using fpga?
Write a verilog code for digital clock and display it’s seven segment using fpga?
Need to code this is assembly language. Using the windows32 framework, write a procedure to read...
Need to code this is assembly language. Using the windows32 framework, write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the output...
Using x86 assembly language, create a flowchart and write an example of code that will sort...
Using x86 assembly language, create a flowchart and write an example of code that will sort 2 arrays of unsigned doubleword integers in ascending order and output the largest element in each array. Any sorting procedure can be used, but this procedure must be called twice for each array. The first time it is called, the first array should be sorted and the second time it is called, the second array must be sorted. As well as outputting which is...
We need to code an LCD with a temperature and humidity sensor for the arduino UNO...
We need to code an LCD with a temperature and humidity sensor for the arduino UNO board. The LCD should simply display the temperature and humidity reading from the DHT11 sensor. We must do this without the use of arduino or lcd libraries, and only use raw c code. For example we need to use PORTD |= (1<<5); to set the pin to 5. The LCD is connected to pins A0 and A1, and it is using I2C. The temperature...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot : raw data, delay data (int) Purpose: Delay Block (**Using Class**) Input : u(t) Output : o(t)=u(t-h) sample time=0.02 Delay (h) = 0.4
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT