Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 32000000UL //!< System clock is 32 MHz
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include "clock.h"
- #include "csrc/ucg.h"
- #include "ucglib_xmega_hal.h"
- #define STEP 200
- #define DEBOUNCE_PERIOD_MS 10
- #define LOCK_PERIOD_MS 200
- ucg_t ucg; //!< ucg is a necessary global structure for ucg_lib
- int x, y, w, v;
- int button_pressed(PORT_t * p, uint8_t bit) {
- if (bit_is_clear(p -> IN, bit)) {
- _delay_ms(DEBOUNCE_PERIOD_MS);
- while (bit_is_clear(p -> IN, bit));
- return 1;
- }
- return 0;
- }
- void cijfer1(void) {
- int z = 0;
- while (z == 0) {
- if (button_pressed( & PORTA, PIN0_bp)) {
- x = 1;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN1_bp)) {
- x = 2;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN2_bp)) {
- x = 3;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN3_bp)) {
- x = 4;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN4_bp)) {
- x = 5;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN5_bp)) {
- x = 6;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN6_bp)) {
- x = 7;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN7_bp)) {
- x = 8;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN0_bp)) {
- x = 9;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN1_bp)) {
- x = 0;
- z = 1;
- }
- }
- return;
- }
- //Deze functie is bedoeld om de eerste cijfer van de code een waarde te geven.
- void cijfer2(void) {
- int z = 0;
- while (z == 0) {
- if (button_pressed( & PORTA, PIN0_bp)) {
- y = 1;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN1_bp)) {
- y = 2;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN2_bp)) {
- y = 3;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN3_bp)) {
- y = 4;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN4_bp)) {
- y = 5;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN5_bp)) {
- y = 6;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN6_bp)) {
- y = 7;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN7_bp)) {
- y = 8;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN0_bp)) {
- y = 9;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN1_bp)) {
- y = 0;
- z = 1;
- }
- }
- return;
- }
- //Deze functie is bedoeld om de tweede cijfer van de code een waarde te geven.
- void cijfer3(void) {
- int z = 0;
- while (z == 0) {
- if (button_pressed( & PORTA, PIN0_bp)) {
- w = 1;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN1_bp)) {
- w = 2;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN2_bp)) {
- w = 3;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN3_bp)) {
- w = 4;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN4_bp)) {
- w = 5;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN5_bp)) {
- w = 6;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN6_bp)) {
- w = 7;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN7_bp)) {
- w = 8;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN0_bp)) {
- w = 9;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN1_bp)) {
- w = 0;
- z = 1;
- }
- }
- return;
- }
- //Deze functie is bedoeld om de derde cijfer van de code een waarde te geven.
- void cijfer4(void) {
- int z = 0;
- while (z == 0) {
- if (button_pressed( & PORTA, PIN0_bp)) {
- v = 1;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN1_bp)) {
- v = 2;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN2_bp)) {
- v = 3;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN3_bp)) {
- v = 4;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN4_bp)) {
- v = 5;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN5_bp)) {
- v = 6;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN6_bp)) {
- v = 7;
- z = 1;
- }
- if (button_pressed( & PORTA, PIN7_bp)) {
- v = 8;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN0_bp)) {
- v = 9;
- z = 1;
- }
- if (button_pressed( & PORTB, PIN1_bp)) {
- v = 0;
- z = 1;
- }
- }
- return;
- }
- //Deze functie is bedoeld om de vierde cijfer van de code een waarde te geven.
- void beginscherm(void) {
- ucg_SetColor( & ucg, 0, 255, 0, 255);
- ucg_SetPrintDir( & ucg, 0);
- ucg_SetPrintPos( & ucg, 2, 60);
- ucg_Print( & ucg, "Voer uw ");
- ucg_SetPrintPos( & ucg, 2, 75);
- ucg_Print( & ucg, "toegangscode in:");
- }
- void varwaarde(void) {
- cijfer1();
- ucg_SetPrintPos( & ucg, 58, 90);
- ucg_Print( & ucg, "%d", x);
- _delay_ms(200);
- ucg_SetPrintPos( & ucg, 58, 90);
- ucg_Print( & ucg, "*");
- cijfer2();
- ucg_SetPrintPos( & ucg, 66, 90);
- ucg_Print( & ucg, "%d", y);
- _delay_ms(200);
- ucg_SetPrintPos( & ucg, 66, 90);
- ucg_Print( & ucg, "*");
- cijfer3();
- ucg_SetPrintPos( & ucg, 74, 90);
- ucg_Print( & ucg, "%d", w);
- _delay_ms(200);
- ucg_SetPrintPos( & ucg, 74, 90);
- ucg_Print( & ucg, "*");
- cijfer4();
- ucg_SetPrintPos( & ucg, 82, 90);
- ucg_Print( & ucg, "%d", v);
- _delay_ms(200);
- ucg_SetPrintPos( & ucg, 82, 90);
- ucg_Print( & ucg, "*");
- }
- void kleur_zwart(void) {
- ucg_SetPrintDir( & ucg, 0);
- ucg_SetColor( & ucg, 0, 0, 0, 0);
- }
- ////Deze functie is bedoeld om ruimte te maken voor nieuw tekst door het oude tekst zwart te maken.
- void toegangscode(void) {
- if (x == 9 && y == 9 && w == 9 && v == 9) {
- kleur_zwart(); // Tekst zwart maken om ruimte te maken voor "welkom"
- ucg_SetPrintPos( & ucg, 2, 60);
- ucg_Print( & ucg, "Voer uw ");
- ucg_SetPrintPos( & ucg, 2, 75);
- ucg_Print( & ucg, "toegangscode in:");
- ucg_SetPrintPos( & ucg, 58, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 66, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 74, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 82, 90);
- ucg_Print( & ucg, "*");
- ucg_SetColor( & ucg, 0, 0, 255, 0);
- ucg_SetPrintPos( & ucg, 55, 70);
- ucg_Print( & ucg, "WELKOM ");
- TCD0.CCA <= (1500 - STEP);
- TCD0.CCABUF = TCD0.CCA + STEP; // Servo 90graden linksom
- _delay_ms(4000);
- TCD0.CCA >= (1100 + STEP);
- TCD0.CCABUF = TCD0.CCA - STEP; // Servo 90graden rechtsom
- kleur_zwart();
- ucg_SetPrintPos( & ucg, 55, 70);
- ucg_Print( & ucg, "Welkom ");
- } else {
- kleur_zwart(); // Tekst zwart maken om ruimte te maken voor "welkom"
- ucg_SetPrintPos( & ucg, 2, 60);
- ucg_Print( & ucg, "Voer uw ");
- ucg_SetPrintPos( & ucg, 2, 75);
- ucg_Print( & ucg, "toegangscode in:");
- ucg_SetPrintPos( & ucg, 58, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 66, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 74, 90);
- ucg_Print( & ucg, "*");
- ucg_SetPrintPos( & ucg, 82, 90);
- ucg_Print( & ucg, "*");
- ucg_SetColor( & ucg, 0, 255, 0, 0);
- ucg_SetPrintPos( & ucg, 10, 70);
- ucg_Print( & ucg, "TOEGANG GEWEIGERD");
- _delay_ms(1500);
- kleur_zwart();
- ucg_SetPrintPos( & ucg, 10, 70);
- ucg_Print( & ucg, "TOEGANG GEWEIGERD");
- }
- }
- //Deze functie is bedoeld om onderscheid te maken tussen de juiste en foute toegangscode.
- int main(void) {
- x,y,w,v = 0;
- uint8_t a;
- init_clock();
- ucg_Init( & ucg, ucg_dev_st7735_18x128x160, ucg_ext_st7735_18, (int( * )(struct _ucg_t * , int, unsigned int, unsigned char * )) ucg_comm_xmega);
- ucg_SetFontMode( & ucg, UCG_FONT_MODE_TRANSPARENT);
- ucg_ClearScreen( & ucg);
- ucg_SetFont( & ucg, ucg_font_8x13_tr);
- ucg_SetColor( & ucg, 255, 0, 0, 0);
- ucg_SetRotate90( & ucg);
- ucg_SetFontMode( & ucg, UCG_FONT_MODE_SOLID); // overwrite background
- ucg_SetFont( & ucg, ucg_font_8x13_mr); // proportional font
- ucg_SetPrintPos( & ucg, 2, 80);
- a = 'A';
- PORTA.DIRCLR = PIN0_bm | PIN1_bm | PIN2_bm | PIN3_bm | PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm; // input pin switch
- PORTB.DIRCLR = PIN0_bm | PIN1_bm;
- PORTA.PIN0CTRL = PORT_OPC_PULLUP_gc; // enable pull up
- PORTA.PIN1CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN2CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN3CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN4CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN5CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN6CTRL = PORT_OPC_PULLUP_gc;
- PORTA.PIN7CTRL = PORT_OPC_PULLUP_gc;
- PORTB.PIN0CTRL = PORT_OPC_PULLUP_gc;
- PORTB.PIN1CTRL = PORT_OPC_PULLUP_gc;
- PORTB.PIN2CTRL = PORT_OPC_PULLUP_gc;
- PORTD.OUTSET = PIN0_bm;
- PORTD.DIRSET = PIN0_bm;
- TCD0.CTRLB = TC0_CCAEN_bm | TC_WGMODE_DSBOTH_gc;
- TCD0.CTRLA = TC_CLKSEL_DIV64_gc;
- TCD0.PER = 10000;
- TCD0.CCA = 345;
- while (1) {
- beginscherm();
- varwaarde();
- toegangscode();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement