Microprocessors A practical approach.
Digital input/output
Digital pin configuration and on/off void DS1(void) { PORTC=0; //reset PORTC latches TRISC=0b ; //RC0-RC3 //output else High impedance PORTC.0=1; //RC0 High }
Hardware description
Delays Do,for or while loops Timers, no – interrupts Timers interrupt based.
Delay : Simple loops while (z<1270) {z++; }
Delay : Simple loops(2) Advantage : easy to program Disadvantage: Difficult to make a exact time delay Disadvantage: processor is busy in delay loop.
Delays : Timer Seperate piece hardware. Easy to calculate
Timer 0 8-bit timer/counter Readable and writable 8-bit software programmable prescaler Internal or external clock select Interrupt on overflow from FFh to 00h Edge select for external clock
Timer 0 (2)
Timer 0 (3)
Timer 0 (3) // Delays a multiple of 1 milliseconds at 4 MHz // using the TMR0 timer void Delay_ms( uns16 millisec) { char next = 0; OPTION = 2; // prescaler divide TMR0 rate by 8 TMR0 = 1; // deduct 1*8 fixed instruction cycles delay do { next += 125; while (TMR0 != next) // 125 * 8 *1 us = 1000 us (= 1 ms) ; } while ( -- millisec != 0);}
Timer 0 (3)
Assignment 2a Make 2 programs with 2 variations of a running light. The first program with a simple delay based on a variable. The second program based on TIMER 0
Microprocessoren lesson 3 Digital input and counters
Contents lesson 3 Configuration input pin Switch connecting Bouncing Debouncing Schmitt –trigger input Counting
Program example simple input void init(void) { PORTA=0; //reset PORTA latches TRISA = 0b ; //al pins are input TRISC = 0b ; // al pins output for led ANSEL = 0b ; //All pins as digital I/O } void main( void) { uns8 x; //count value x=0; // set initial value to 0; init(); PORTA=0; while (1) { if (PORTA.2==1) {PORTC.0=1;} else {PORTC.0=0;} //if switch = 5v, //wait until x=0 again to wait for falling edge, wait 10 ms for debouncing }
Program Example bouncing void init(void) { PORTA=0; // reset PORTA latches TRISA = 0b ; // all pins are input TRISC = 0b ; // all pins output for led ANSEL = 0b ; //All pins as digital I/O } void main( void) { uns8 x; //count value x=0; // set initial value to 0; init(); while (1) { if (PORTA.2==1) {x++;while(PORTA.2==1);} //if switch = 5v increment x //,wait until x=0 again to wait for falling edge, wait 10 ms for debouncing PORTC=x; //show x value on led }
Schmitt trigger input
software debounce 1
timer 0 External clock
timer 0 External clock OPTION_REG
Timer 0 configuration for counting with TIMER0 OPTION_REG=0b ; x=TMR0;
Assignment 4 1.Write a program for a that LED shows if the switch (on RA2) is pressed or not. 2.Write a program which shows the bouncing of the switch. 3.Write a program that makes clear that the software debounced the switch. 4.Write a program that counts the switch on a falling edge.(no use of timer0) 5. Write a program that counts the switch on a falling edge. (use :timer0)