diff --git a/paraoleg/lab1-BasicIO/main.c b/paraoleg/lab1-BasicIO/main.c index 8ccbcb6..43d840d 100644 --- a/paraoleg/lab1-BasicIO/main.c +++ b/paraoleg/lab1-BasicIO/main.c @@ -1,6 +1,3 @@ -/******************************************************************************/ -/* Files to Include */ -/******************************************************************************/ #ifdef __XC32 #include /* Defines special function registers, CP0 regs */ #endif @@ -11,21 +8,9 @@ #include "system.h" /* System funct/params, like osc/periph config */ #include "user.h" /* User funct/params, such as InitApp */ -/******************************************************************************/ -/* Global Variable Declaration */ -/******************************************************************************/ - -/* i.e. uint32_t ; */ - -/******************************************************************************/ -/* Main Program */ -/******************************************************************************/ - int32_t main(void) { - /* Initialize I/O - * and Peripherals - * for application */ - InitApp(); + + InitApp(); // Initialize I/O and Peripherals for application Scan_LEDs(); } diff --git a/paraoleg/lab1-BasicIO/system.c b/paraoleg/lab1-BasicIO/system.c index 77bb4ec..df00f35 100644 --- a/paraoleg/lab1-BasicIO/system.c +++ b/paraoleg/lab1-BasicIO/system.c @@ -1,7 +1,3 @@ -/******************************************************************************/ -/* Files to Include */ -/******************************************************************************/ - #ifdef __XC32 #include /* Defines special funciton registers, CP0 regs */ #endif @@ -10,15 +6,3 @@ #include /* For uint32_t definition */ #include /* For true/false definition */ #include "system.h" /* variables/params used by system.c */ - -/******************************************************************************/ -/* System Level Functions */ -/* */ -/* Custom oscillator configuration funtions, reset source evaluation */ -/* functions, and other non-peripheral microcontroller initialization */ -/* functions get placed in system.c */ -/* */ -/******************************************************************************/ - -/* */ - diff --git a/paraoleg/lab1-BasicIO/user.c b/paraoleg/lab1-BasicIO/user.c index 0498801..2c1ee37 100644 --- a/paraoleg/lab1-BasicIO/user.c +++ b/paraoleg/lab1-BasicIO/user.c @@ -1,7 +1,3 @@ -/******************************************************************************/ -/* Files to Include */ -/******************************************************************************/ - #ifdef __XC32 #include /* Defines special function registers, CP0 regs */ #endif @@ -10,35 +6,25 @@ #include /* For true/false definition */ #include "user.h" /* variables/params used by user.c */ -/******************************************************************************/ -/* User Functions */ - -/******************************************************************************/ +/* Setup analog functionality and port direction */ void InitApp(void) { - /* Setup analog functionality and port direction */ + // LED output - // Disable analog mode for G15 - ANSELGbits.ANSG15 = 0; - // Set direction to output for G15 - TRISGbits.TRISG15 = 0; + // Disable analog mode for pins + ANSELG &= ~((1 << 6) | (1 << 15)); + ANSELB &= ~(1 << 11); - // Disable analog mode for B11 - ANSELBbits.ANSB11 = 0; - // Set direction to output for B11 - TRISBbits.TRISB11 = 0; + // Set direction to output for pins + TTRISG &= ~((1 << 6) | (1 << 15)); + TRISB &= ~(1 << 11); // Disable analog mode for D4 PORTDbits.RD4 = 0; // For LED2 on PORTD pin 4 is no analog option so ANSELDbits.ANSD4 = 0 will not compile // Set direction to output for D4 - TRISDbits.TRISD4 = 0; - - // Disable analog mode for G6 - ANSELGbits.ANSG6 = 0; - // Set direction to output for G6 - TRISGbits.TRISG6 = 0; - + TRISD &= ~(1 << 4); + // Initialize outputs for other LEDs // Turn on LEDs for testing @@ -61,17 +47,14 @@ void InitApp(void) { // BTN1 input // Disable analog mode - ANSELAbits.ANSA5 = 0; + ANSELA &= ~(1 << 5); // Set directions to input - TRISAbits.TRISA5 = 1; - - // Initialize input for BTN2 - TRISAbits.TRISA5 = 1; + TRISA |= (1 << 5)|(1 << 4); // Test switches while (1) { - LD1_PORT_BIT = BTN1_PORT_BIT; - LD2_PORT_BIT = BTN2_PORT_BIT; + LD1_PORT_BIT = BTN1_PORT_BIT; + LD2_PORT_BIT = BTN2_PORT_BIT; }; } @@ -99,36 +82,38 @@ void Flash_LED(void) { } void Scan_LEDs(void) { -int LED_state = 1; // 1 on (initial value), 0 off -int delay_count=1000000; - -while (1) { -if (BTN1_PORT_BIT == 1) { // ?????? 1 ?????? -delay_count = 300000; -} else { // ?????? 1 ?? ?????? -delay_count = 1000000; -} -if (BTN2_PORT_BIT == 1) { // ?????? 2 ?????? -// ????????? ??? ?????????? -LD1_PORT_BIT = 0; -LD2_PORT_BIT = 0; -LD3_PORT_BIT = 0; -LD4_PORT_BIT = 0; -} else { -LD1_PORT_BIT = LED_state; -Delay(delay_count); -LD2_PORT_BIT = LED_state; -Delay(delay_count); -LD3_PORT_BIT = LED_state; -Delay(delay_count); -LD4_PORT_BIT = LED_state; -Delay(delay_count); - -// next time, set LEDs to opposite state - -LED_state = 1 - LED_state; -} -} + int LED_state = 1; // 1 on (initial value), 0 off + int delay_count=1000000; + + while (1) { + if (BTN1_PORT_BIT == 1) { //first button is pressed + delay_count = 300000; + } + else { + delay_count = 1000000; + } + + if (BTN2_PORT_BIT == 1) { //second button is pressed + LD1_PORT_BIT = 0; + LD2_PORT_BIT = 0; + LD3_PORT_BIT = 0; + LD4_PORT_BIT = 0; + } + else { + LD1_PORT_BIT = LED_state; + Delay(delay_count); + LD2_PORT_BIT = LED_state; + Delay(delay_count); + LD3_PORT_BIT = LED_state; + Delay(delay_count); + LD4_PORT_BIT = LED_state; + Delay(delay_count); + + // next time, set LEDs to opposite state + + LED_state = 1 - LED_state; + } + } }