Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS140 - PIC Microcontroller Programming: Writing Random Number Programs, Slides of Computer Architecture and Organization

In this cs140 lab, students will learn how to write a program for the pic microcontroller development board to generate and display the logical and, or, and xor of two random numbers. An overview of the lab, functional specification, designing the first piece of code, and programming hints.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

baijayanthi
baijayanthi 🇮🇳

4.5

(13)

171 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS140 - PIC 1
Computer Organization
CS 140
Programming The PIC
Micro Controller
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS140 - PIC Microcontroller Programming: Writing Random Number Programs and more Slides Computer Architecture and Organization in PDF only on Docsity!

CS140 - PIC 1

Computer Organization

CS 140

Programming The PIC

Micro Controller

CS140 - PIC 2

In Lab01, you learned how to program the PIC Microcontroller running on the Development Board. You could write a bit of new code, and you could explain what various Assembly instructions do. You also had a general idea of how to read the hardware specification.

In this lab, you will have one very specific task.

Write a program with the following characteristics:

  1. Get a random number
  2. Get a second random number
  3. Perform and display the logical AND of the two numbers
  4. Perform and display the logical OR of the two numbers
  5. Perform and display the logical XOR of the two numbers

The Lab - Overview

CS140 - PIC 4

Designing Your First Piece Of Code

This program is pretty simple really. You could write it in java in about 5 minutes. But it will take MUCH MORE code in PIC Assembler, and will have lots of new learning involved. It’s frequently difficult to do numerous tasks at the same time and that is the case here; you’re coping with a new language, a new piece of hardware and a new IDE.

Your design can be on paper or electronic. It should include boxes that represent methods/subroutines that will be performed by your code. Each box should have the name of the routine, as well as the tasks to be performed by that routine.

I’ve started a design on the next slide to show what I mean.

I’m REALLY emphasizing using separate methods/subroutines for various actions as a way of organizing your thinking.

CS140 - PIC 5

Writing Your First Piece Of Code

Start

  1. Initialize switch
    1. Initialize LEDs

MainLoop

Smile Frown Be Happy MegaDelay

Delay

This says: Start calls MainLoop: MainLoop in turn calls these various routines – perhaps not every time around the loop, but calls to all these other routines might occur at some time.

The box contains a list of the actions the routine performs.

CS140 - PIC 7

Programming Hints

Random Number Generation: The easiest way to do this is have the program running in a loop. Every time it goes thru the loop it increments a counter. It also checks if the switch has been pushed. When it sees a push, then the counter at that time is the random number to use. This actually is quite random since it depends on a human (who knows nothing about how fast the counter is going) pushing a button at an arbitrary moment.

Code Formatting and Commenting: Formatting is mostly about indentation. I don’t care what you do as long as you’re consistent. Also be consistent about using upper or lower case for mnemonics. Commenting should give the “big picture” of what’s happening. A comment like “moving from accumulator to file” is NOT very useful – that’s what the instruction already tells us. But if you say “Moving accumulator to PORTC to display on lights”, that tells why the instruction is there.

Timers: The spec asks that you keep values in the LEDs for 5 seconds. You will need to develop a timer to do this. I’d recommend that you use the timer already developed in Lab01, and call it repeatedly many times in order to achieve the total time desired.

CS140 - PIC 8

Show and Tell Evaluation Sheet

Lab02: Your Name:______________________

Design

  • There IS a design written out electronically or on paper – show me.

Examination of the code.

  • Code is formatted correctly.
  • Code has comments and is legible.

Code execution

  • Program is assembled from source and can be loaded onto the Board.
  • Five Second Delay works
  • Random Numbers generated correctly.
  • Logic performed correctly.
  • Program behaves according to the specification given in this write-up.