Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Multiple LEDs and using and input button Please anwswer parts a and b for proble

ID: 3743472 • Letter: M

Question

Multiple LEDs and using and input button

Please anwswer parts a and b for problem 1 below.

1. Modify the instructor’s C program:

a. Instead of using the multiply and divide functions to change pins/LED’s, use the << and >> functions. References: Deitel and Deitel “C, How to Program and https://en.wikipedia.org/wiki/Operators_in_C_and_C

b. Change the clock frequency in the program to 1 MHz and make the on/off time of each LED .1 seconds. This should make the rotation visibly faster. (Remember to change the _XTAL_FREQ value since this is used for the __delay_ms() function built into XC8)

Devices:

Low Pin Count board (16F1829 on board) and 44-Pin Demo Board are both on same backboard. (You only use the 16F1829 for this lab.)

PICKIT 3 programmer with USB cable

MPLAB X (I used v3.00 but a different version may be on lab computers))

Microchip XC8 C Compiler User Manual

PIC16F1829 Data Sheet

PICkit 3 User’s Guide

Low Pin Count Board User Guide

“C How to Program” Deitel, Pearson/Prentice-Hall (Any edition)

Internet Browser Search Engine for research (Google, Bing, etc)

/*   
LEDs on for approximately 0.5 sec.
PIC: 16F1829 Enhanced Mid-Level
Compiler: XC8 v1.34
IDE: MPLABX v3.00         */
#include //Not required but this is the reference used by "C" for names and location on uC
#include                  //refers on HiTech C, Microchip purchased HiTech
#define _XTAL_FREQ 4000000        //Used by the XC8 delay_ms(x) macro
#define switch PORTAbits.RA2       // Can use RA2 instead of PORTAbit.RA2 to define pin attached to switch
                   //instead of saying PORTAbits.RA2 each time
//config bits for the PIC16F1829
#pragma config FOSC=INTOSC, WDTE=OFF, PWRTE=OFF, MCLRE=OFF, CP=OFF, CPD=OFF, BOREN=ON, CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF
#pragma config WRT=OFF, PLLEN=OFF, STVREN=OFF, LVP=OFF
//Initialization subroutine
void initialize(void) {
ANSELC=0;            //All pins of Port C are digital I/O
ANSA2=0;          //switch pin, RA2, is digital IO
TRISA2 = 1;           //switch is an input
TRISC = 0;             //all pins of Port C are outputs
OSCCON = 0b01101000;        // 4 MHz
           }
unsigned char i1; //only need 4 bits to count to 16. unsigned character variable is 8 bits long
   // Here is main(). There are many ways to do this 4-pin (LED) sequence
void main(void)
{
    initialize();
   i1=1;   //Start the main program with the variable =1. Could have done this during its definition
while (1)   //runs continuously until MCU is shut off
        {           if (switch==1) //Button not pressed pin at 5V
                     {         i1=1;            }
            while (switch==1) //Button not pressed
                {
                   PORTC=i1;       //Note that writing to PORTC writes to LATC
                   __delay_ms(500);
                    i1=i1*2;
                  if (i1==16)
           {      i1=1;       }
                 }

        if (switch==0) //Button pressed pin at ground
             {       i1=8;             }
            while (switch==0) //Button pressed
               {
                   PORTC=i1;
                   __delay_ms(500);
            i1=i1/2;
                  if (i1==0)
                    {          i1=8;              }
               }
         }
}

Explanation / Answer

------------------------------------------------CODE----------------------------------------------------

/*   
LEDs on for approximately 0.5 sec.
PIC: 16F1829 Enhanced Mid-Level
Compiler: XC8 v1.34
IDE: MPLABX v3.00         */
#include //Not required but this is the reference used by "C" for names and location on uC
#include                  //refers on HiTech C, Microchip purchased HiTech
#define _XTAL_FREQ 1000000        //Used by the XC8 delay_ms(x) macro
#define switch PORTAbits.RA2       // Can use RA2 instead of PORTAbit.RA2 to define pin attached to switch
                   //instead of saying PORTAbits.RA2 each time
//config bits for the PIC16F1829
#pragma config FOSC=INTOSC, WDTE=OFF, PWRTE=OFF, MCLRE=OFF, CP=OFF, CPD=OFF, BOREN=ON, CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF
#pragma config WRT=OFF, PLLEN=OFF, STVREN=OFF, LVP=OFF
//Initialization subroutine
void initialize(void) {
ANSELC=0;            //All pins of Port C are digital I/O
ANSA2=0;          //switch pin, RA2, is digital IO
TRISA2 = 1;           //switch is an input
TRISC = 0;             //all pins of Port C are outputs
OSCCON = 0b01001000;        // 1 MHz
           }
unsigned char i1; //only need 4 bits to count to 16. unsigned character variable is 8 bits long
   // Here is main(). There are many ways to do this 4-pin (LED) sequence
void main(void)
{
    initialize();
   i1=1;   //Start the main program with the variable =1. Could have done this during its definition
while (1)   //runs continuously until MCU is shut off
        {           if (switch==1) //Button not pressed pin at 5V
                     {         i1=1;            }
            while (switch==1) //Button not pressed
                {
                   PORTC=i1;       //Note that writing to PORTC writes to LATC
                   __delay_ms(500);
                    i1=i1<<2;
                  if (i1==16)
           {      i1=1;       }
                 }

        if (switch==0) //Button pressed pin at ground
             {       i1=8;             }
            while (switch==0) //Button pressed
               {
                   PORTC=i1;
                   __delay_ms(500);
            i1=i1>>2;
                  if (i1==0)
                    {          i1=8;              }
               }
         }
}

--------------------------------------------CODE ENDS--------------------------------------------------

Good Luck! Thanks for the question!