C Programming, LCD Displays. Due Monday, September 30th 1 ...

5
ECE 376 - Homework #4 C Programming, LCD Displays. Due Monday, September 30th 1) Modify the following code so that it waits 100ms when called as Wait(100) #include <pic18.h> void Wait(unsigned int DATA) { unsigned int i, j; for (i=0; i<DATA; i++) { for (j=0; j<623; j++); } } // Main Routine void main(void){ TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; TRISE = 0; ADCON1 = 0x0F; while(1) { PORTC = PORTC + 1; Wait(100); } }

Transcript of C Programming, LCD Displays. Due Monday, September 30th 1 ...

Page 1: C Programming, LCD Displays. Due Monday, September 30th 1 ...

ECE 376 - Homework #4C Programming, LCD Displays. Due Monday, September 30th

1) Modify the following code so that it waits 100ms when called as Wait(100)

#include <pic18.h>

void Wait(unsigned int DATA){ unsigned int i, j; for (i=0; i<DATA; i++) { for (j=0; j<623; j++); } }

// Main Routine

void main(void){ TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; TRISE = 0; ADCON1 = 0x0F;

while(1) { PORTC = PORTC + 1; Wait(100); } }

Page 2: C Programming, LCD Displays. Due Monday, September 30th 1 ...

2) Modify the following code so that it plays note F4 on RC0 every time you press RB0

#include <pic18.h>

void main(void) {

unsigned int i;

ADCON1 = 15; TRISB = 0xFF; TRISC = 0;

while(1) { while(RB0) { RC0 = !RC0; for(i=0; i<894; i++); } } }

Page 3: C Programming, LCD Displays. Due Monday, September 30th 1 ...

Problem 3-6)

Stopwatch

Problem3) Requirements:Inputs: RB0 = Stop, RB1 = Start, RB2 = ClearOutput: PORTCRelationship: Display the time on PORTC as time at 100ms per count

- When you press RB0, the time stops- When you press RB1, time starts- When you press RB2, time is cleared

Tolerance: 10%

Problem 4) Calculations

From problem #2, 1 ms wait loop was creaed by counting to 623.

To determine how long the LCD driver routine took, the following code was used...

while(1) { TIME = TIME + 1; LCD_Move(1,0); LCD_Out(TIME,1); RC0 = 1; Wait_ms(100); RC0 = 0; } }

From this, it was found thatThe 100ms wait loop took 100ms (RC0 was high)The rest of the code took 17ms to execute (RC0 was low)

To fix the timing, the wait loop was reduced to 83ms (100ms total)

Page 4: C Programming, LCD Displays. Due Monday, September 30th 1 ...

Problem 5) C-Code and Flow Chart

C-Code:

// Global Variablesconst unsigned char MSG[16] = "Stopwatch ";

// Subroutine Declarations#include <pic18.h>

// Main Routine

void main(void){ unsigned int i; unsigned int TIME; unsigned int RUN;

TRISA = 0; TRISB = 0xFF; TRISC = 0; TRISD = 0; ADCON1 = 0x0F;

TIME = 0; RUN = 0;

LCD_Init(); LCD_Move(0,0); for(i=0; i<16; i++) LCD_Write(MSG[i]);

while(1) { if (RB0) RUN = 0; if (RB1) RUN = 1; if (RB2) TIME = 0;

if (RUN) TIME = TIME + 1; LCD_Move(1,0); LCD_Out(TIME,1); Wait_ms(83);

} }

Memory Summary: Program space used 54Eh ( 1358) of 10000h bytes ( 2.1%) Data space used 18h ( 24) of F80h bytes ( 0.6%) EEPROM space used 0h ( 0) of 400h bytes ( 0.0%) ID Location space used 0h ( 0) of 8h nibbles ( 0.0%) Configuration bits used 0h ( 0) of 7h words ( 0.0%)

The C-code compiled into 679 lines of assembler (1358 / 2)

Start

PORTC = Output

PORTB = Input

ButtonPressed?

RB0

RB1

RB2

Stop Start Clear Time

Running? yes

Display Time

Inc TIMEno

Wait 100ms

Page 5: C Programming, LCD Displays. Due Monday, September 30th 1 ...

Problem 6) Validation

To test the duration of the main loop, pin RC0 was measured using an oscilloscope:

RC0 toggles every 99.8ms, which is within 10% of 100ms (meets the requirements)

PIC Board running Stopwatch program. The LCD time increments every 100ms

Problem 7) Demo