/* * Test-horloge-1.c * Version 1.1 * Author : Thierry LEQUEU * Device : ATmega8535 * Created: 24/12/2023 * Modified: 25/12/2023 * with the help of : https://exploreembedded.com/wiki/AVR_External_Interrupts */ #ifndef F_CPU #define F_CPU 16000000UL // 16 MHz clock speed. #endif // Pin declaration for the 16x4 LCD display in 4-bits mode: #define D4 eS_PORTC4 #define D5 eS_PORTC5 #define D6 eS_PORTC6 #define D7 eS_PORTC7 #define RS eS_PORTC0 #define RW eS_PORTC1 // Pin R/W is Read or Write pin which will determine whether the data is to be written or it is to be read from the LCD display. // HIGH value of this pin will indicate the data is read from the display and LOW value indicates writing information to the display. // Normally we need only writing values to the display, so we usually tie RW to GND. #define EN eS_PORTC2 #include #include #include // In order to get sprintf function. #include // In order to get sei(). #include "K:\USERS\2023-Microchip-Studio\Librairies\lcd-TL.h" // Declare your global variables here unsigned char temps, // from 0 to 10. secondes=00, // from 0 to 59. minutes=20, // from 0 to 59. heures=10; // from 0 to 23. char tampon[20]; // Only char, not unsigned char... // With a 16-character display, you need a little more than 16... //*************************************************************************** // From : https://mansfield-devine.com/speculatrix/2017/04/avr-basics-reading-and-writing-gpio-pins/ //*************************************************************************** void SetBit (volatile uint8_t * registre, uint8_t pin) { *registre |= (1 << pin); }; void ClearBit (volatile uint8_t * registre, uint8_t pin) //PORTD &= ~(1<=40) // 40 x 25 ms = 1 second. //if (temps>=20) // 20 x 50 ms = 1 second. if (temps>=10) // 10 x 100 ms = 1 second. { temps=0; secondes++; if (secondes>59) { secondes=0; minutes++; if (minutes>59) { minutes=0; heures++; if (heures>23) { heures=0; }; }; }; // if (secondes>=59). }; // if (temps>=10). }; int main(void) { // Declare your local variables here: // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA = 0x00; DDRA = 0x00; // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB = 0x00; DDRB = 0x00; // Port C initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC = 0x00; DDRC = 0xFF; // Set Port C to OUTPUT for the LCD display. // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD = 0x00; DDRD = 0x01; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=0xFF // OC0 output: Disconnected TCCR0 = 0x00; TCNT0 = 0x00; OCR0 = 0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 250,000 kHz // Mode: CTC top=OCR1A // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: On // Compare B Match Interrupt: Off TCCR1A = 0x00; TCCR1B = 0x0B; TCNT1H = 0x00; TCNT1L = 0x00; ICR1H = 0x00; ICR1L = 0x00; //OCR1AH=0x30; // Base frequency = 250 kHz, so a period of 4 us. //OCR1AL=0xD4; // Interrupt when 2500 is reached = 0x09C4 i.e. 10 ms. //OCR1AH=0x30; // Base frequency = 250 kHz, so a period of 4 us. //OCR1AL=0xD4; // Interrupt when 12500 is reached = 0x30D4 i.e. 50 ms. OCR1AH = 0x61; // Base frequency = 250 kHz, so a period of 4 us. OCR1AL = 0xA8; // Interrupt when 25000 is reached = 0x61A8 i.e. 100 ms. OCR1BH = 0x00; OCR1BL = 0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR = 0x00; TCCR2 = 0x00; TCNT2 = 0x00; OCR2 = 0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off MCUCR = 0x00; MCUCSR = 0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK = 0x10; // USART initialization // USART disabled UCSRB = 0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR = 0x80; SFIOR = 0x00; // ADC initialization // ADC disabled ADCSRA = 0x00; // SPI initialization // SPI disabled SPCR = 0x00; // TWI initialization // TWI disabled TWCR = 0x00; // Alphanumeric LCD initialization // RS - PORTC Bit 0 // RD - PORTC Bit 1 // EN - PORTC Bit 2 // D4 - PORTC Bit 4 // D5 - PORTC Bit 5 // D6 - PORTC Bit 6 // D7 - PORTC Bit 7 // Characters/line: 16/4 Lcd4_Init(); Lcd4_Clear(); // Global enable interrupts sei(); // Enable global interrupts by setting global interrupt enable bit in SREG while(1) { // Place your code here // PORTD &= ~(1<23) { heures=0; }; _delay_ms(100); } if bit_is_clear(PIND,PD6) // Test of the input PORTD.6 to increase the value "minutes". // if ((PIND & 0x40)==0 ) // Test of the input PORTD.6 to increase the value "minutes". { minutes++; if (minutes>59) { minutes=0; }; _delay_ms(100); } Lcd4_Set_Cursor(0,0); // "1234567890123456" Lcd4_Write_String("Projet Horloge 1"); sprintf(tampon,"%02u h %02u min %02u s",heures,minutes,secondes); Lcd4_Set_Cursor(0,2); Lcd4_Write_String(tampon); // The contents of the buffer variable "tampon" are displayed. //PORTD |= (1<