void init_port(void);
void init_timer(void);
void setup() {
// put your setup code here, to run once:
volatile long i = 0;
volatile long j = 0;
init_port();
init_timer();
Serial.begin(9600);
while(1){
for(i = 0; i < 500000; i++);
// Serial.println(j, OCT);
// Serial.print(j);
Serial.println(j);
// NOTE:
// For the Serial.println(); function, if we havn't mentioned the print"ln", it will
// just print the character continously. To print in new line, we have to mention,
// println.
// Also, in the arguments of println(); we can see that, we have mentioned HEX, which
// means, in the Serial monitor, we will be having the output in "Hexadecimal" format.
// We can also have our output in octal form by mentioning "OCT" as the argument after "j".
// If we havn't mentioned any extra argument, it means we are taking decimal, but we
// can explicitly mention "DEC" for decimal.
j++;
}
}
void init_port(void){
volatile char *dirf = nullptr;
dirf = 0x30;
*dirf = 0x01;
}
void init_timer(void){
volatile short *Timer1_TCNT1 = 0x84; // This is a 16 - bit register.
// Here, we have gave the address
// of the Lower Byte.
// TCNT1H AND TCNT1L ARE, "0x85" AND "0x84".
volatile char *Timer1_TCCR1A = 0x80;
volatile char *Timer1_TCCR1B = 0x81;
volatile short *Timer1_OCR1A = 0x88; // This is a 16 - bit register.
// Here, we have gave the address
// of the Lower Byte.
// OCR1AH AND OCR1AL ARE, "0x89" AND "0x88".
volatile char *Timer1_TIMSK1 = 0x6f;
*Timer1_TCNT1 = 0;
*Timer1_TCCR1A = 0;
*Timer1_TCCR1B = 0;
// NOTE:
// Here, we have used TCCR1B register instead of TCCR1A register,
// Because, we need to set up the CTC (mode of operation) and 256 - prescaler (clock selection).
// In TCCR1A register, we can perform certain operations such as, set / clear / toggle on the
// certain port pin based on the direction of the port.
*Timer1_OCR1A = 50000; // TCNT1 register value increases till 50000 and
// after reaching this, the compare register will be cleared.
// We can see in the below, we have set the timer1 in CTC mode.
*Timer1_TCCR1B = 0x0c; // CTC mode and 256 - prescaler
*Timer1_TIMSK1 = 0x02; // This register is used to mask the
// output compare register that we need
// to use for comparison purpose to enable
// the corresponding interrupt
}
ISR(TIMER1_COMPA_vect){
volatile char *outf = nullptr;
outf = 0x31;
*outf ^= 0x01;
}
void loop() {
// put your main code here, to run repeatedly:
}