I’m new to programming in c. Currently trying to implement a licensing program on a machine and intend to use an atmega128 with a watch crystal to set the date such that it cannot be changed
#include <avr/io.h>
#include <avr/interrupt.h>
// Define RTC parameters
#define RTC_PRESCALER_VALUE 1024
#define RTC_OVERFLOW_COUNT 244 // Adjust this based on your RTC module's overflow count
volatile uint32_t system_time = 0; // System time in milliseconds
volatile uint8_t seconds = 0;
volatile uint8_t minutes = 0;
volatile uint8_t hours = 0;
volatile uint8_t day = 1;
volatile uint8_t month = 1;
volatile uint16_t year = 2024; // Initial year
// Initialize RTC
void rtc_init() {
// Configure RTC prescaler
TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024
TIMSK |= (1 << TOIE1); // Enable overflow interrupt
TCNT1 = 0; // Initialize timer counter
}
// RTC overflow interrupt handler
ISR(TIMER1_OVF_vect) {
system_time += RTC_OVERFLOW_COUNT * (1000 / (F_CPU / RTC_PRESCALER_VALUE)); // Update system time
// time
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
day++;
if (day > 30) { // Assuming all months have 30 days for simplicity
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
}
}
}
int main() {}
I tried using the atmel avr atmega128 datasheet to understand how the counter works but to no avail
New contributor
user24648164 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.