C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem1\problem1Cunningham.c / FileName: problem1cunningham.c Processor: PIC18F4520 Compiler: MPLAB C18 v.3.06 This file does the following... Creation and Revisions: Author Date Comments Patrick Cunningham 10/21/2011 / / Header Files / #include <p18f4520.h> #include <adc.h> / Configuration Bits / #pragma config OSC = EC // EC = External 4MHz Crystal for PICDEM board only #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF / Define Constants Here / #define SAMPLE 100 / Local Function Prototypes / void samplefunction(void); / Global Variables / int samplevariable = 0; int RA0result; / Function: void main(void) / #pragma code void main (void) ADCON1 = 0x0E; // Set RA0 to Analog, not necessary here TRISB = 0x00; // Set PortB to outputs PORTB = 0x00; // Initalize PortB so LEDs are off // Configure the A/D convertor OpenADC( ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, 0x0E ); while (1) 1
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem1\problem1Cunningham.c SetChanADC(ADC_CH0); // Not necessary here ConvertADC(); while( BusyADC() ); RA0result = ReadADC(); if( RA0result <= 250 ) PORTB = 0b00001000; else if ( (RA0result > 250) && (RA0result <= 550) ) PORTB = 0b00001100; else if ( (RA0result > 550) && (RA0result <= 800) ) PORTB = 0b00001110; else PORTB = 0b00001111; / Additional Helper Functions / / Function: void sample(void) Input Variables: none Output Return: none Overview: Use a comment block like this before functions / void samplefunction() // Some function that does a specific task 2
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem2\problem2Cunningham.c / FileName: (change filename of template).c Processor: PIC18F4520 Compiler: MPLAB C18 v.3.06 This file does the following... Author Date Comment ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/ // (Your name here) / Header Files / #include <p18f4520.h> #include <portb.h> #include <timers.h> #include <pwm.h> #include <delays.h> / Configuration Bits / #pragma config OSC = EC // EC = External 4MHz Crystal for PICDEM board only #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF / Define Constants Here / #define SAMPLE 100 / Local Function Prototypes / void low_isr(void); void high_isr(void); void samplefunction(void); / Declare Interrupt Vector Sections / #pragma code high_vector=0x08 void interrupt_at_high_vector(void) _asm goto high_isr _endasm #pragma code low_vector=0x18 void interrupt_at_low_vector(void) _asm goto low_isr _endasm / Global Variables / int samplevariable = 0; unsigned char PWMperiod[] = 141, 119, 105, 94, 79, 70 ; char idx = 0; 1
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem2\problem2Cunningham.c / Function: void main(void) / #pragma code void main (void) ADCON1 = 0x0F; // set all pins to digital TRISB = 0x01; // set RB0-RB3 to inputs RCONbits.IPEN = 0; // interrupts in compatibility mode // Configure RB0 Interrupts OpenRB0INT( PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF ); INTCONbits.GIE = 1; // enable global interrupts // Configure Timer 2 for PWM functionality OpenTimer2( TIMER_INT_OFF & T2_PS_1_16 ); while (1) // This area loops forever / Additional Helper Functions / / Function: void sample(void) Input Variables: none Output Return: none Overview: Use a comment block like this before functions / void samplefunction() // Some function that does a specific task / Function: void high_isr(void) Possible sources of interrupt - none Overview: / #pragma interrupt high_isr void high_isr(void) if( INTCONbits.INT0IF ) INTCONbits.INT0IF = 0; OpenPWM1(PWMperiod[idx]); // 440 Hz A4 2
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem2\problem2Cunningham.c // SetDCPWM1(102); // 10% Duty Cycle // Delay10KTCYx(100); // Wait 1 second // SetDCPWM1(0); // Turn Buzzer on while RB0 is pressed while( PORTBbits.RB0 == 0 ) SetDCPWM1(102); // 10% Duty Cycle SetDCPWM1(0); // Turn Buzzer off idx++; if( idx == 6 ) idx = 0; / Function: void low_isr(void) Possible sources of interrupt - none Overview: / #pragma interruptlow low_isr void low_isr(void) // Add code here for the low priority Interrupt Service Routine (ISR) 3
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem3\problem3Cunningham.c / FileName: (change filename of template).c Processor: PIC18F4520 Compiler: MPLAB C18 v.3.06 This file does the following... Author Date Comment ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/ // (Your name here) / Header Files / #include <p18f4520.h> #include <stdio.h> #include <timers.h> #include "LCD Module.h" / Configuration Bits / #pragma config OSC = EC // EC = External 4MHz Crystal for PICDEM board only #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF / Define Constants Here / #define SAMPLE 100 / Local Function Prototypes / void low_isr(void); void high_isr(void); void samplefunction(void); void updatelcd(void); / Declare Interrupt Vector Sections / #pragma code high_vector=0x08 void interrupt_at_high_vector(void) _asm goto high_isr _endasm #pragma code low_vector=0x18 void interrupt_at_low_vector(void) _asm goto low_isr _endasm / Global Variables / int samplevariable = 0; char LCD_str[17]; unsigned int idx = 1; 1
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem3\problem3Cunningham.c unsigned int Fcurr; unsigned int tmrstart = 34285; / Function: void main(void) / #pragma code void main (void) TRISD = 0x00; // Make all D Channels Outputs (for LCD) XLCDInit(); XLCDClear(); // Initialize & Clear the LCD Display RCONbits.IPEN = 0; // Interrupts in Compatibility Mode // Configure Timer0 OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16 ); INTCONbits.GIE = 1; // Enable Global Interrupts WriteTimer0(tmrStart); // while (1) // This area loops forever / Additional Helper Functions / / Function: void sample(void) Input Variables: none Output Return: none Overview: Use a comment block like this before functions / void samplefunction() // Some function that does a specific task / Function: void updatelcd(void) Input Variables: Output return: Overview: When called, updates the LCD with the latest age global variables. / void updatelcd(void) // Go to the start of line 1 2
C:\Users\cunningh\StaysOnPC\ME430 Downloads & Projects\exam2_problem3\problem3Cunningham.c // write line 1 XLCDL1home(); XLCDPutRamString(LCD_str); / Function: void high_isr(void) Possible sources of interrupt - none Overview: / #pragma interrupt high_isr void high_isr(void) if( INTCONbits.TMR0IF ) INTCONbits.TMR0IF = 0; // Reset Interrupt Flag Fcurr = idxidxidx; // Compute n^3 sprintf(lcd_str,"f( %-u) = %-u", idx, Fcurr); updatelcd(); idx++; // Increment Counter WriteTimer0(tmrStart); / Function: void low_isr(void) Possible sources of interrupt - none Overview: / #pragma interruptlow low_isr void low_isr(void) // Add code here for the low priority Interrupt Service Routine (ISR) 3