void main() {
OSCCONbits.IRCF = 0b111; // Set internal oscillator frequency to 8MHz
TRISCbits.TRISC0 = 0; // Set STEP pin as output
TRISCbits.TRISC1 = 0; // Set DIR pin as output
TRISCbits.TRISC2 = 0; // Set EN pin as output
TRISCbits.TRISC3 = 0; Set LCD_RS pin as output
TRISCbits.TRISC4 = 0; // Set LCD_EN pin as output
TRISCbits.TRISC5 = 0; // Set LCD_D4 pin as output
TRISCbits.TRISC6 = 0; // Set LCD_D5 pin as output
TRISCbits.TRISC7 = 0; // Set LCD_D6 pin as output
TRISAbits.TRISA4 = 1; // Set LIGHT_SENSOR pin as input
TRISAbits.TRISA5 = 1; // Set BUTTON pin as input
ANSELA = 0b00000000; // Set all PORTA pins as digital
ANSELC = 0b00000000; // Set all PORTC pins as digital
// Initialize RTC
rtc_init();
// Initialize stepper motor driver
stepper_motor_init();
// Initialize LCD
lcd_init();
// Initialize light sensor
light_sensor_init();
while(1) {
// Get current time from RTC
rtc_time_t time = rtc_get_time();
// Check if it is time to ring the alarm
if(time.hour == 7 && time.minute == 0 && time.second == 0) {
// Ring the alarm by rotating the stepper motor
for(int i = 0; i < 100; i++) {
stepper_motor_step(1);
__delay_ms(10);
}
}
// Check if it is time to open the curtain
if(time.hour == 7 && time.minute == 30 && time.second == 0) {
// Check if it is dark outside
if(light_sensor_read() < 500) {
// Open the curtain by rotating the stepper motor
for(int i = 0; i < 1000; i++) {
stepper_motor_step(1);
__delay_ms(10);
}
}
}
// Display current time on LCD
lcd_clear();
lcd_print("Time: %02d:%02d:%02d", time.hour, time.minute, time.second);
// Check if button is pressed to set the time
if(!PORTAbits.RA5) {
// Wait for button to be released
while(!PORTAbits.RA5);
// Set current time on RTC
rtc_set_time(8, 0, 0);
}