* We have declared some functions in "timer.c" and corresponding function * prototypes in "timer.h": * void timer_init(void); // Initialise TCB0 to produce interrupts every 1ms * * Your task for Ex 9.3 is to complete the ISR declared in "timer.c" to display * simultaneously the first digit of your student number on the LHS of the 7-segment * display and the second digit of your student number on the RHS of this display (only * after timer_init() is called). * * TIP: Every time a new interrupt occurs you should swap which digit you are displaying. * * TIP: You will want to declare a global or static variable so you know which digit to * display on each iteration. * * TIP: You might like to refer to the following truth table for 7-segment digit mapping. * (remember to check which output Q0..Q6 maps to which segment on the QUTy) * * ABCDEFG * 0: 0000001 * 1: 1001111
c Copy code
#include "timer.h"
// Global variable to keep track of which digit to display
static uint8_t display_digit = 0;
// Interrupt Service Routine for Timer 0 overflow
ISR(TCB0_OVF_vect)
{
// Toggle which digit to display
display_digit = !display_digit;
// Display the first digit on the left side of 7-segment display
if (display_digit == 0) {
PORTD |= (1 << PD7); // set PD7 (QUT0) HIGH
PORTC = 0b00000010; // digit 1 (QUT1 HIGH, QUT2-QUT6 LOW)
}
// Display the second digit on the right side of 7-segment display
else {
PORTD |= (1 << PD6); // set PD6 (QUT3) HIGH
PORTC = 0b10011111; // digit 9 (QUT1, QUT6 LOW, QUT2-QUT5 HIGH)
}
}
void timer_init(void)
{
TCB0.CTRLA = TC_CLKSEL_DIV2_gc; // Set Timer clock to 8MHz/2 = 4MHz
TCB0.CTRLB = TCB_CNTMODE_INT_gc; // Set Timer mode to Normal with interrupt
TCB0.INTCTRL = TCB_OVFINTLVL_LO_gc;// Set Timer overflow interrupt to low level
TCB0.PER = 4000; // Set period of Timer to 1ms
TCB0.CTRLA |= TC_CLKSEL_ENABLE_gc; // Enable the timer
}
👩💻 attiny1626 quty board
c Copy code
#include "timer.h"
// Global variable to keep track of which digit to display
static uint8_t display_digit = 0;
// Interrupt Service Routine for Timer 0 overflow
ISR(TCB0_OVF_vect)
{
// Toggle which digit to display
display_digit = !display_digit;
// Display the first digit on the left side of 7-segment display
if (display_digit == 0) {
PORTB |= (1 << PB4); // set PB4 (QUT0) HIGH
PORTA = 0b00000100; // digit 1 (QUT1 HIGH, QUT2-QUT6 LOW)
}
// Display the second digit on the right side of 7-segment display
else {
PORTB |= (1 << PB5); // set PB5 (QUT3) HIGH
PORTA = 0b10011111; // digit 9 (QUT1, QUT6 LOW, QUT2-QUT5 HIGH)
}
}
void timer_init(void)
{
TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // Set Timer clock to 8MHz/2 = 4MHz
TCB0.CTRLB = TCB_CNTMODE_INT_gc; // Set Timer mode to Normal with interrupt
TCB0.INTCTRL = TCB_OVFINTLVL_LO_gc;// Set Timer overflow interrupt to low level
TCB0.PER = 4000; // Set period of Timer to 1ms
TCB0.CTRLA |= TCB_ENABLE_bm; // Enable the timer
}
Keep in mind that the code above assumes that the 7-segment display is connected to the following ATtiny1626 QUTY board pins:
If your connections are different, make the necessary changes to the code to reflect your particular setup.
property infinity
Infinity
is a property in JavaScript that represents the mathematical infinity value. It is a globally defined variable that represents a numeric value greater than any other number. Here's an example:
javascript Copy code
console.log(Infinity); // Output: Infinity
console.log(10/0); // Output: Infinity
console.log(-10/0); // Output: -Infinity
👩💻 - infinity
Negative Infinity -Infinity
is the opposite of Infinity
. It is a globally defined variable in JavaScript representing a numeric value less than any number. Here's an example:
javascript Copy code
console.log(-Infinity); // Output: -Infinity
console.log(-10/0); // Output: -Infinity
console.log(10/-0); // Output: -Infinity
👩💻 nan
In JavaScript, NaN
stands for 'Not a Number' and it is a value representing an undefined or unrepresentable value resulting from an arithmetic operation. It's typically the result of dividing a number by zero or performing other mathematical computations that don't produce a meaningful result. Here's an example:
javascript Copy code
console.log(0 / 0); // Output: NaN
console.log(Math.sqrt(-1)); // Output: NaN
console.log(parseInt("hello")); // Output: NaN
It's important to note that NaN
is not equal to any value, including itself, so you cannot use standard operators like ==
and ===
to compare it to other values. To check if a value is NaN
, you can use the isNaN()
function.
👩💻 isFinite()
isFinite()
is a built-in function in JavaScript that determines whether a given value is a finite number. It returns true
if the value is a finite number, false
if the value is NaN
, Infinity
, or -Infinity
. Here's an example:
javascript Copy code
console.log(isFinite(3)); // Output: true
console.log(isFinite(Infinity)); // Output: false
console.log(isFinite(-Infinity)); // Output: false
console.log(isFinite(NaN)); // Output: false
console.log(isFinite("hello")); // Output: false
isFinite()
is often used to validate user input to ensure that a value is a valid number.
👩💻 isNaN()
isNaN()
is a built-in function in JavaScript that determines whether a value is NaN
or not. It returns true
if the value is NaN
, false
if the value is a number or can be converted to one. Here's an example:
javascript Copy code
console.log(isNaN(NaN)); // Output: true
console.log(isNaN(10)); // Output: false
console.log(isNaN('Hello')); // Output: true
console.log(isNaN('10')); // Output: false
It's important to note that isNaN()
will attempt to convert non-numeric values into numeric values before checking if it is NaN
. For example, isNaN('Hello')
will return true
because 'Hello'
cannot be converted into a numeric value. However, isNaN('10')
will return false
because '10'
can be converted into a numeric value of 10
. To avoid this confusion, you can use the dedicated function Number.isNaN()
introduced in ECMAScript 2015 (ES6). It only returns true
if the value is NaN
and does not perform any type coercion.
👩💻 infinite number
There is no specific data type for infinite numbers in JavaScript. Instead, it uses the Infinity
global property to represent positive infinity and the -Infinity
global property to represent negative infinity. Here are some examples:
javascript Copy code
console.log(Infinity); // Output: Infinity
console.log(-Infinity); // Output: -Infinity
console.log(1 / 0); // Output: Infinity
console.log(-1 / 0); // Output: -Infinity
You can perform arithmetic operations with infinity values, such as adding or subtracting them from other numbers. However, certain operations like dividing Infinity
by Infinity
will result in NaN
.
👩💻 Difference between == and ===
==
and ===
are both comparison operators in JavaScript used to compare two values.
==
is the equality operator and compares two values for equality. It performs type coercion, which means that if the types of the two values being compared are not the same, JavaScript will try to convert one or both of them to the same type before making the comparison. For example:
javascript Copy code
console.log(5 == '5'); // Output: true
console.log(0 == false); // Output: true
console.log('' == false); // Output: true
===
is the strict equality operator and compares two values for both type and value equality. It does not perform type coercion, which means that if the types of the two values being compared are not the same, the operator will return false
. For example:
javascript Copy code
console.log(5 === '5'); // Output: false
console.log(0 === false); // Output: false
console.log('' === false); // Output: false
It's generally recommended to use ===
instead of ==
whenever possible, since it can help prevent bugs caused by unexpected type coercion. However, there are cases when ==
may be useful, such as when checking for null or undefined values.
👩💻 Explain this console.log('' == false); // Output: true
In JavaScript, the ==
operator performs type coercion, which means that if the types of the two values being compared are different, JavaScript will try to convert one or both of them to the same type before making the comparison.
In this case, ''
is an empty string, which is a falsy value in JavaScript. false
is also a falsy value. Since the types of the two values are different, JavaScript will try to convert them to the same type before comparison.
In this case, the empty string ''
is coerced into a number, which gives us 0
. The false
value is also coerced into a number, which also gives us 0
. Since 0 == 0
, the comparison evaluates to true
.
It's worth noting that using the ===
operator instead of ==
would give us a different result, since ===
does not perform type coercion. In this case, '' === false
would evaluate to false
, since ''
is a string and false
is a boolean, and they are not the same type.
👩💻 falsy value
In JavaScript, a falsy value is a value that is considered false when encountered in a Boolean context. The following values are falsy in JavaScript:
false
0
(numeric zero)''
or ""
(empty string)null
undefined
NaN
Here's an example that demonstrates how falsy values work in JavaScript:
javascript Copy code
if (false) {
console.log('This code will not run.');
}
if ('') {
console.log('This code will not run.');
}
if (0) {
console.log('This code will not run.');
}
if (null) {
console.log('This code will not run.');
}
if (undefined) {
console.log('This code will not run.');
}
if (NaN) {
console.log('This code will not run.');
}
In each case, the condition in the if
statement evaluates to false, so the code block within the if
statement is not executed.
👩💻 modulo (%) operator
The modulo operator (%
) in JavaScript is a mathematical operator that returns the remainder of dividing two numbers. If the dividend is not evenly divisible by the divisor, the remainder will be a positive number less than the divisor. If the dividend is evenly divisible by the divisor, the remainder will be zero.
Here are some examples:
javascript Copy code
console.log(5 % 2); // Output: 1 (since 5 divided by 2 has a remainder of 1)
console.log(10 % 5); // Output: 0 (since 10 divided by 5 has no remainder)
console.log(8 % 3); // Output: 2 (since 8 divided by 3 has a remainder of 2)
The modulo operator is often used in programming to perform operations that depend on whether a number is even or odd (by checking if the remainder when dividing by 2 is 0 or 1, respectively) or to perform an action every n iterations of a loop (by checking if the remainder when dividing the current iteration count by n is 0).
👩💻 console.log(3**3);
The **
operator in JavaScript is the exponentiation operator. It is used to raise a number to a given exponent. The syntax is base ** exponent
. Here's an example:
javascript Copy code
console.log(3 ** 3); // Output: 27 (since 3 raised to the power of 3 equals 27)
In this case, 3
is the base and 3
is the exponent, so 3 ** 3
returns 27
.
👩💻 postfixing and prefixing
Postfix and prefix are terms used to describe the position of an operator in relation to its operands.
Prefix: When an operator is placed before its operand(s), it is said to be a prefix operator. For example, in the expression -3
, the -
operator is placed in front of the operand 3
and is therefore a prefix operator. Another example of a prefix operator is the typeof
operator, which returns a string indicating the type of its operand.
Postfix: When an operator is placed after its operand(s), it is said to be a postfix operator. For example, in the expression x++
, the ++
operator is placed after the operand x
and is therefore a postfix operator. Another example of a postfix operator is the array access operator []
, which is used to access an element of an array.
The main difference between postfix and prefix operators is when they are evaluated. Prefix operators are evaluated before their operands, while postfix operators are evaluated after their operands.
It's worth noting that some operators, such as +
and -
, can be used as both prefix and postfix operators in some contexts. For example, in the expression ++x
, the ++
operator is a prefix operator, while in the expression x++
, the ++
operator is a postfix operator.
can I use a function to change the value of a global variable in javascript?
Yes, you can use a function to change the value of a global variable in JavaScript. Here's an example:
javascript Copy code
let myGlobal = "Hello, World!"; // global variable
function changeGlobal(newVal) {
myGlobal = newVal; // updating the value of the global variable
}
console.log(myGlobal); // Output: "Hello, World!"
changeGlobal("Hi, Everyone!"); // changing the value of the global variable
console.log(myGlobal); // Output: "Hi, Everyone!"
In this example, we declare a global variable myGlobal
with the initial value of "Hello, World!". We then define a function changeGlobal
that takes a parameter newVal
and updates the value of the global variable myGlobal
. Finally, we call the changeGlobal
function with a new value of "Hi, Everyone!" and log the value of the global variable again to confirm that it's been updated.
👩💻 could i call the changeGlobal("Hi,Everyone!") from inside another function with a click event listener?
Yes, you can call the changeGlobal
function from inside another function with a click event listener in JavaScript. Here's an example:
javascript Copy code
let myGlobal = "Hello, World!"; // global variable
function changeGlobal(newVal) {
myGlobal = newVal; // updating the value of the global variable
}
function handleClick() {
changeGlobal("Hi, Everyone!"); // calling the changeGlobal function
}
document.getElementById("myButton").addEventListener("click", handleClick); // adding a click event listener to a button with id "myButton"
In this example, we have a global variable myGlobal
and a function changeGlobal
that updates the value of this variable. We also define a function handleClick
that calls the changeGlobal
function with a new value of "Hi, Everyone!". Finally, we use the addEventListener
method to add a click event listener to a button with the id "myButton". When the button is clicked, the handleClick
function is called, which in turn calls the changeGlobal
function and updates the value of the global variable myGlobal
.
If you have any other questions, you can easily reach out to us here
AI stands for Artificial Intelligence. AI bots are able to learn from conversations with users and expand their knowledge this way.
SheCodes Athena will help you with technical questions about your code using artificial intelligence to find the answer. Imagine a super powerful human who has memorized everything on the internet and can access that knowledge in a matter of seconds. 🤯
SheCodes Athena can answer most coding-related questions, even complicated ones! It can even find bugs in your code and tell you how to fix them in just a few seconds. Impressive, right?
Just remember we're still in testing mode so the AI may return strange or incorrect replies. Feel free to message us if this happens!
SheCodes Athena can only reply to coding-related technical questions. The same type of questions you would ask in the channels on Slack.
For questions that are not coding-related, write us here 😃
You should treat Athena like a SheCodes team member, so always be polite! 😊 Ask your questions as detailed as possible, just like you would do on Slack.
Here are some examples:
- Prettier isn't working on my VS Code. How do I fix this?
- How do I make bullet points with different colors using the list element?
- My code in Codesandbox is having some issues. Can you please tell me what the issue is? [Include the link to your Codesandbox]
For now, SheCodes Athena is limited to 5 questions per day for each student.
In that case, you can either ask SheCodes Athena a follow-up question, or you can post on the designated weekly channel on Slack!
Our technical assistants are still available on Slack and are always happy to help! 😍💪
Remember, questions are limited to 1000 characters.
- If you're working with an HTML file: Post a snippet of your code related to the issue you're having (just copy the code and paste it into the question box).
- If you're working with Codesandbox: Good news, you can just post the link to your Codesandbox and the AI Assistant will be able to view your code.
- If you have a longer question that would require an entire HTML file or more than 1000 characters, post it in the designated weekly channels on Slack! 😃
Athena was the Greek goddess of wisdom, among other elements. She received her name from the city of Athens, which she is known for protecting.
Much like the goddess Athena, SheCodes Athena is also incredibly wise and can solve complicated coding puzzles in a matter of seconds! 😍
Not likely. AI can automate tasks and make developers' jobs more efficient but it can't fully replace the human ability to deal with complex software. And AI will still require human developers to supervise and improve it further.
So developers may see their tasks change but they won't be replaced by AI. 👩💻🤝💻