EECE.3170: Microprocessor Systems Design I Summer 2017

Similar documents
EECE.3170: Microprocessor Systems Design I Spring 2016

Chapter 11: Interrupt On Change

Figure 1: Pushbutton without Pull-up.

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

Lecture (04) PIC16F84A (3)

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

D:\PICstuff\PartCounter\PartCounter.asm

Laboratory Exercise 5 - Analog to Digital Conversion

16.317: Microprocessor Systems Design I Fall Exam 3 December 15, Name: ID #:

Assembly Language Instructions

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

PIC 16F84A programming (II)

Embedded Systems Programming and Architectures

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

Flow Charts and Assembler Programs

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

The University of Texas at Arlington Lecture 5

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

Week1. EEE305 Microcontroller Key Points

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

Introduction to PIC Programming

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

Application Note - PIC Source Code v1.1.doc

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

ECE Test #1: Name

Introduction to PIC Programming

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

Introduction to PIC Programming

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

ECE 354 Introduction to Lab 2. February 23 rd, 2003

Performance & Applications

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

CENG-336 Introduction to Embedded Systems Development. Timers

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Embedded System Design

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

EEE111A/B Microprocessors

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

Introduction to PIC Programming

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

ME 6405 Introduction to Mechatronics

16.317: Microprocessor-Based Systems I Spring 2012

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

Laboratory: Introduction to Mechatronics

Experiment 7:The USART

16.317: Microprocessor-Based Systems I Summer 2012

Embedded systems. Exercise session 3. Microcontroller Programming Lab Preparation

Instuction set

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

Section 13. Timer0 HIGHLIGHTS. Timer0. This section of the manual contains the following major topics:

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

DERTS Design Requirements (1): Microcontroller Architecture & Programming

C:\IDE\Brooke-16F88-LED\Brooke-16F88_LED.asm

C and Embedded Systems. So Why Learn Assembly Language? C Compilation. PICC Lite C Compiler. PICC Lite C Optimization Results (Lab #13)

Binary Outputs and Timing

Outline. Micriprocessor vs Microcontroller Introduction to PIC MCU PIC16F877 Hardware:

SOLUTIONS!! DO NOT DISTRIBUTE!!

Chapter 3: Further Microcontrollers

Introduction to PIC Programming

Learning Objectives:

LCD. Configuration and Programming

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

/ 40 Q3: Writing PIC / 40 assembly language TOTAL SCORE / 100 EXTRA CREDIT / 10

Micro II and Embedded Systems

Mechatronics and Measurement. Lecturer:Dung-An Wang Lecture 6

S w e d i s h c r. w e e b l y. c o m j a l i l a h m e l i v e. c o m Page 1

16.317: Microprocessor Systems Design I Fall 2015

ECE Homework #3

Remote Controlled KitchenAid Mixer for the Clients at ADEC. Appendices. Arnaud Bacye Karina Dubé Justin Erman Matthew Martin

Relay. bit4 bit5 bit6 bit7

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

Arithmetic,logic Instruction and Programs

DISCONTINUED. SPI Communication with AMT bit Absolute Encoder

Chapter 5 Sections 1 6 Dr. Iyad Jafar

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

Input/Output Ports and Interfacing

THE UNIVERSITY OF THE WEST INDIES

PIC16F84A 7.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

These 3 registers contain enable, priority,

Laboratory 10. Programming a PIC Microcontroller - Part II

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

PIC Microcontroller Trainer

EXPERIMENT 4: Parallel Input/Output. Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing

PIC Discussion. By Eng. Tamar Jomaa

Transcription:

EECE.3170: Microprocessor Systems Design I Summer 2017 1. What is an interrupt? What is an exception? Lecture 13: Key Questions June 19, 2017 2. For what purposes are interrupts useful? 3. Describe the basic steps in interrupt processing. 1

EECE.3170: Microprocessor Systems Design I Summer 2017 M. Geiger Lecture 13: Key Questions 4. What is an interrupt or exception vector? Describe briefly how an interrupt vector table functions. 5. Explain how interrupts can be set up and managed in the PIC microcontrollers. 6. Explain the operation of the programs used to rotate the LEDs using interrupts (interrupt.asm and interrupt.c). 2

EECE.3170: Microprocessor Systems Design I Summer 2017 M. Geiger Lecture 13: Key Questions 7. Explain how the analog to digital converter module is configured in PIC microcontrollers. 3

EECE.3170: Microprocessor Systems Design I Summer 2017 M. Geiger Lecture 13: Key Questions 8. Explain the operation of the programs used to test the ADC (a2d.asm and a2d.c). 9. Explain the programs that use the ADC result to vary the speed of rotation (vs_rotate.asm and vs_rotate.c). 4

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\assy\10 Interrupt\interrupt.asm Lesson 10 Interrupts and Pull ups This lesson will introduce interrupts and how they are useful. It will also introduce internal weak pull ups that are available on most PICs. It should be noted that this lesson is more efficent than the last one, "Timer0". Notice how the processor is no longer waiting for Timer0 to roll over. Instead, we let the hardware modules do the work, freeing the CPU to do other things in the main loop The switch is no longer continuously polled for a button press. Instead, an interrupt will occur which will automically place the program counter inside of the ISR where we can change directions outisde of normal code execution LEDs rotate at a constant speed and the switch reverses their direction PIC: 16F1829 Assembler: MPASM v5.43 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information 1 #include <p16f1829.inc> CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF) CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF) errorlevel 302 surpress the 'not in bank0' warning #define SWITCH PORTA, 2 pin where SW1 is connected..note: always READ from the PORT and WRITE to the LATCH #define PULL_UPS if this is uncommented, JP5 can be pulled out #define LED_RIGHT 0xFF keep track of LED direction #define LED_LEFT 0x00 cblock 0x70 Direction Delay1 endc shared memory location that is accessible from all banks LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 Org 0x0 Reset Vector starts at 0x0000 bra Start main code execution Org 0x0004 Interrupt Vector starts at address 0x0004 goto ISR Start: Setup main init banksel OSCCON bank1 movlw b'00111000' set cpu clock speed FO 500KHz movwf OSCCON move contents of the working register into OSCCON bsf TRISA, RA2 switch as input banksel ANSELA bank3

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\assy\10 Interrupt\interrupt.asm bcf ANSELA, RA2 digital can reference pins by their position in the PORT (2) or name (RA2) Configure the LEDs banksel TRISC bank1 clrf TRISC make all of PORTC an output banksel LATC bank2 movlw b'00001000' start with DS4 lit Setup Timer0 as the delay banksel OPTION_REG bank1 movlw b'00000111' 1:256 prescaler for a delay of: (insruction cycle 256 counts) prescaler = ((8uS 256)256) =~ 524mS movwf OPTION_REG bsf INTCON, TMR0IE enable the rollover interrupt to occur Setup interrupt on change for the switch bsf INTCON, IOCIE must set this global enable flag to allow any interrupt on change flags to cause an interrupt banksel IOCAN bank7 bsf IOCAN, IOCAN2 when SW1 is pressed, enter the ISR (Note, this is set when a FALLING EDGE is detected) bsf INTCON, GIE must set this global to allow any interrupt to bring the program into the ISR if this is not set, the interrupt flags will still get set, but the ISR will never be entered #ifdef PULL_UPS enter here if this is defined (not commented out) banksel WPUA bank4 bsf WPUA, 2 enable the weak pull up for the switch banksel OPTION_REG bank1 bcf OPTION_REG, NOT_WPUEN enable the global weak pull up bit this bit is active HIGH, meaning it must be cleared for it to be enabled #endif movlw LED_RIGHT start with LEDs shifting to the right movwf Direction 2 Clear the RAM clrf Delay1 MainLoop: bra MainLoop can spend rest of time doing something critical here Debounce: delay for approximatly 5ms movlw d'209' (1/(500KHz/4))2093 = 5.016mS movwf Delay1 DebounceLoop: decfsz Delay1, f 1 instruction to decrement,unless if branching (ie Delay1 = 0) bra DebounceLoop 2 instructions to branch return RotateRight: lsrf LATC, f logical shift right btfsc STATUS,C did the bit rotate into the carry? bsf LATC,3 yes, put it into bit 3. retfie RotateLeft: lslf LATC, f logical shift left btfsc LATC, 4 did it rotate out of the LED display? bsf LATC, 0 yes, put in bit 0 retfie Enter here if an interrupt has occured First, check what caused the interrupt by checking the ISR flags

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\assy\10 Interrupt\interrupt.asm This lesson only has 2 flags to check ISR: banksel IOCAF bank7 btfsc IOCAF, 2 check the interrupt on change flag bra Service_SW1 switch was pressed bra Service_TMR0 Timer0 overflowed Service_SW1: In order to ensure that no detected edge is lost while clearing flags, the following 3 lines mask out only the known changed bits and don 't interfere with the others. A simple clrf would work, but this current method is good practice movlw 0xFF xorwf IOCAF, w andwf IOCAF, f MUST ALWAYS clear this in software or else stuck in the ISR forever clearing this will clear the INTCON, IOCIF bit as well call Debounce delay for 5ms and then check the switch again banksel PORTA bank0 btfsc SWITCH is it still held down? retfie nope, exit the ISR back to the main code movlw 0xFF xorwf Direction, f toggle the direction state and save it back retfie return to main code 3 Service_TMR0: bcf INTCON, T0IF MUST ALWAYS clear this in software or else stuck in the ISR forever banksel LATC change to bank2 movlw LED_RIGHT check what direction currently in subwf Direction, w be sure to save in wreg so as to not corrupt 'Direction' btfsc STATUS, Z bra RotateRight bra RotateLeft end end code generation

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\c\10 Interrupt\interrupt.c / Lesson 10 "Interrupts and Pull ups" This lesson will introduce interrupts and how they are useful. It will also introduce internal weak pull ups that are available on most PICs. It should be noted that this lesson is more efficent than the last one, "Timer0". Notice how the processor is no longer waiting for Timer0 to roll over. Instead, we let the hardware modules do the work, freeing the CPU to do other things in the main loop The switch is no longer continuously polled for a button press. Instead, an interrupt will occur which will automically place the program counter inside of the ISR where we can change directions outisde of normal code execution LEDs rotate at a constant speed and the switch reverses their direction PIC: 16F1829 Compiler: XC8 v1.00 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information / 1 #include <htc.h> #define _XTAL_FREQ 500000 //PIC hardware mapping //Used by the XC8 delay_ms(x) macro #define DOWN 0 #define UP 1 #define SWITCH PORTAbits.RA2 #define LED_RIGHT 1 #define LED_LEFT 0 #define PULL_UPS //if this is uncommented, the trace under JP5 can be cut //with no affect on the output //config bits that are part specific for the PIC16F1829 CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF) CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF) / LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 / unsigned char _direction void main(void) { OSCCON = 0b00111000 TRISC = 0 LATC = 0 LATCbits.LATC3 = 1 _direction = LED_RIGHT //a global variable //general init //500KHz clock speed //all LED pins are outputs //init LEDs in OFF state //DS4 is lit //start with LEDs rotating from right to left //setup switch (SW1)

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\c\10 Interrupt\interrupt.c TRISAbits.TRISA2 = 1 //switch as input ANSELAbits.ANSA2 = 0 //digital switch 2 //by using the internal resistors, you can save cost by eleminating an external pull up/down resistor #ifdef PULL_UPS WPUA2 = 1 //enable the weak pull up for the switch nwpuen = 0 //enable the global weak pull up bit #endif //setup TIMER0 as the delay //1:256 prescaler for a delay of: (insruction cycle 256 counts)prescaler = ((8uS 256)256) =~ 524mS OPTION_REG = 0b00000111 //setup TIMER0 INTCONbits.TMR0IE = 1 //enable the TMR0 rollover interrupt INTCONbits.IOCIE = 1 IOCANbits.IOCAN2 = 1 GIE = 1 //setup interrupt on change for the switch //enable interrupt on change global //when SW1 is pressed, enter the ISR //enable global interupts while (1) { continue //can spend rest of time doing something critical here void interrupt ISR(void) { if (IOCAF) { IOCAF = 0 delay_ms(5) if (SWITCH == DOWN) { _direction ^= 1 //SW1 was just pressed //must clear the flag in software //debounce by waiting and seeing if still held down //change directions if (INTCONbits.T0IF) { INTCONbits.T0IF = 0 if (_direction == LED_RIGHT) { LATC >> = 1 if (STATUSbits.C == 1) LATCbits.LATC3 = 1 else{ LATC << = 1 if (LATCbits.LATC4 == 1) LATCbits.LATC0 = 1 //rotate right //when the last LED is lit, restart the pattern //rotate left //when the last LED is lit, restart the pattern

C:\Users\Michael_Geiger\Documents\courses\16...\PICkit3_Starter_Kit\src\pic16\assy\04 A2D\a2d.asm Lesson 4 "Analog to Digital" This shows how to read the A2D converter and display the High order parts on the 4 bit LED display. The pot on the Low Pin Count Demo board varies the voltage coming in on in A0. The A2D is referenced to the same Vdd as the device, which is nominally is 5V. The A2D returns the ratio of the voltage on Pin RA0 to 5V. The A2D has a resolution of 10 bits, with 1024 representing 5V and 0 representing 0V. The top four MSbs of the ADC are mirrored onto the LEDs. Rotate the potentiometer to change the display. PIC: 16F1829 Assembler: MPASM v5.43 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information 1 #include <p16f1829.inc> CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF) CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF) errorlevel 302 supress the 'not in bank0' warning LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 ORG 0 start of code at address 0x0000 Start: Setup main init banksel OSCCON bank1 movlw b'00111000' set cpu clock speed movwf OSCCON move contents of the working register into OSCCON Configure the ADC/Potentimator already in bank1 bsf TRISA, 4 Potentimator is connected to RA4...set as input movlw b'00001101' select RA4 as source of ADC and enable the module (carefull, this is actually AN3) movwf ADCON0 movlw b'00010000' left justified Fosc/8 speed vref is Vdd movwf ADCON1 banksel ANSELA bank3 bsf ANSELA, 4 analog for ADC Configure the LEDs banksel TRISC bank1 clrf TRISC make all of PORTC an output banksel LATC select the bank where LATC is movlw b'00001000' start the rotation by setting DS1 ON movwf LATC write contents of the working register to the latch

C:\Users\Michael_Geiger\Documents\courses\16...\PICkit3_Starter_Kit\src\pic16\assy\04 A2D\a2d.asm MainLoop: Start the ADC nop requried ADC delay of 8uS => (1/(Fosc/4)) = (1/(500KHz/4)) = 8uS banksel ADCON0 bsf ADCON0, GO start the ADC btfsc ADCON0, GO this bit will be cleared when the conversion is complete goto $ 1 keep checking the above line until GO bit is clear 2 Grab Results and write to the LEDs swapf ADRESH, w Get the top 4 MSbs (remember that the ADC result is LEFT justified!) banksel LATC movwf LATC move into the LEDs bra MainLoop end end code

C:\Users\Michael_Geiger\Documents\courses\16...\docs\PICkit3_Starter_Kit\src\pic16\c\04 A2D\a2d.c / Lesson 4 "Analog to Digital" This shows how to read the A2D converter and display the High order parts on the 4 bit LED display. The pot on the Low Pin Count Demo board varies the voltage coming in on in A0. The A2D is referenced to the same Vdd as the device, which is nominally is 5V. The A2D returns the ratio of the voltage on Pin RA0 to 5V. The A2D has a resolution of 10 bits, with 1023 representing 5V and 0 representing 0V. The top four MSbs of the ADC are mirrored onto the LEDs. Rotate the potentiometer to change the display. PIC: 16F1829 Compiler: XC8 v1.00 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information / 1 #include <htc.h> #define _XTAL_FREQ 500000 //PIC hardware mapping //Used by the XC8 delay_ms(x) macro //config bits that are part specific for the PIC16F1829 CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF) CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF) / LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 / void main(void) { OSCCON = 0b00111000 TRISC = 0 TRISAbits.TRISA4 = 1 ANSELAbits.ANSA4 = 1 ADCON0 = 0b00001101 (AN3) ADCON1 = 0b00010000 while (1) { delay_us(5) GO = 1 while (GO) continue LATC = (ADRESH >> 4) //500KHz clock speed //all LED pins are outputs //setup ADC //Potentiamtor is connected to RA4...set as input //analog //select RA4 as source of ADC and enable the module //left justified FOSC/8 speed Vref is Vdd //wait for ADC charging cap to settle //wait for conversion to be finished //grab the top 4 MSbs

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\assy\05 VS Rotate\vs_rotate.asm Lesson 5 "Variable Speed Rotate" This lesson combines all of the previous lessons to produce a variable speed rotating LED display that is proportional to the ADC value. The ADC value and LED rotate speed are inversely proportional to each other. Rotate the POT counterclockwise to see the LEDs shift faster. PIC: 16F1829 Assembler: MPASM v5.43 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information 1 #include <p16f1829.inc> CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF) CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF) errorlevel 302 cblock 0x70 Delay1 Delay2 endc supress the 'not in bank0' warning shared memory location that is accessible from all banks Define two file registers for the delay loop in shared memory LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 ORG 0 start of code Start: Setup main init banksel OSCCON bank1 movlw b'00111000' set cpu clock speed movwf OSCCON move contents of the working register into OSCCON Configure the ADC/Potentimator already in bank1 bsf TRISA, 4 Potentimator is connected to RA4...set as input movlw b'00001101' select RA4 as source of ADC and enable the module (carefull, this is actually AN3) movwf ADCON0 movlw b'00010000' left justified Fosc/8 speed vref is Vdd movwf ADCON1 banksel ANSELA bank3 bsf ANSELA, 4 analog for ADC Configure the LEDs banksel TRISC bank1 clrf TRISC make all of PORTC an output banksel LATC bank2 movlw b'00001000' start the rotation by setting DS4 ON movwf LATC write contents of the working register to the latch MainLoop: call A2d get the ADC result top 8 MSbs are now in the working register (Wreg) movwf Delay2 move ADC result into the outer delay loop

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\assy\05 VS Rotate\vs_rotate.asm call CheckIfZero if ADC result is zero, load in a value of '1' or else the delay loop will decrement starting at 255 call DelayLoop delay the next LED from turning ON call Rotate rotate the LEDs 2 bra MainLoop do this forever CheckIfZero: movlw d'0' load wreg with '0' xorwf Delay2, w XOR wreg with the ADC result and save in wreg btfss STATUS, Z if the ADC result is NOT '0', then simply return to MainLoop return return to MainLoop movlw d'1' ADC result IS '0'. Load delay routine with a '1' to avoid decrementing a rollover value of 255 movwf Delay2 move it into the delay location in shared memory (RAM) return return to MainLoop A2d: Start the ADC nop requried ADC delay of 8uS => (1/(Fosc/4)) = (1/(500KHz/4)) = 8uS banksel ADCON0 bsf ADCON0, GO start the ADC btfsc ADCON0, GO this bit will be cleared when the conversion is complete goto $ 1 keep checking the above line until GO bit is clear movf ADRESH, w Get the top 8 MSbs (remember that the ADC result is LEFT justified!) return DelayLoop: Delay amount is determined by the value of the ADC decfsz Delay1,f will always be decrementing 255 here goto DelayLoop The Inner loop takes 3 instructions per loop 255 loops (required delay) decfsz Delay2,f The outer loop takes and additional 3 instructions per lap X loops (X = top 8 MSbs from ADC conversion) goto DelayLoop return Rotate: banksel LATC change to Bank2 lsrf LATC logical shift right btfsc STATUS,C did the bit rotate into the carry? bsf LATC,3 yes, put it into bit 3. return end end code

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\c\05 VS Rotate\vs_rotate.c / Lesson 5 "Variable Speed Rotate" This lesson combines all of the previous lessons to produce a variable speed rotating LED display that is proportional to the ADC value. The ADC value and LED rotate speed are inversely proportional to each other. Rotate the POT counterclockwise to see the LEDs shift faster. PIC: 16F1829 Compiler: XC8 v1.00 IDE: MPLABX v1.10 Board: PICkit 3 Low Pin Count Demo Board Date: 6.1.2012 See Low Pin Count Demo Board User's Guide for Lesson Information / 1 #include <htc.h> #define _XTAL_FREQ 500000 //PIC hardware mapping //Used by the XC8 delay_ms(x) macro //config bits that are part specific for the PIC16F1829 CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF) CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF) / LATC Bit#: 7 6 5 4 3 2 1 0 LED: DS4 DS3 DS2 DS1 / unsigned char adc(void) //prototype void main(void) { unsigned char delay OSCCON = 0b00111000 TRISC = 0 LATC = 0 LATCbits.LATC3 = 1 TRISAbits.TRISA4 = 1 ANSELAbits.ANSA4 = 1 ADCON0 = 0b00001101 (AN3) ADCON1 = 0b00010000 while (1) { delay = adc() delay_ms(5) while (delay!= 0) delay_ms(2) each LATC >> = 1 if(statusbits.c) LATCbits.LATC3 = 1 //500KHz clock speed //all LED pins are outputs //start sequence with DS4 lit //setup ADC //Potentiamtor is connected to RA4...set as input //analog //select RA4 as source of ADC and enable the module //left justified FOSC/8 speed Vref is Vdd //grab the top 8 MSbs //delay for AT LEAST 5ms //decrement the 8 MSbs of the ADC and dealy 2ms for //shift to the right by 1 to light up the next LED //when the last LED is lit, restart the pattern

C:\Users\Michael_Geiger\Documents\courses\16...\src\pic16\c\05 VS Rotate\vs_rotate.c 2 unsigned char adc(void) { delay_us(5) GO = 1 while (GO) continue return ADRESH //wait for ADC charging cap to settle //wait for conversion to be finished //grab the top 8 MSbs