« ikea, long island | Main | Switches, using body parts »

September 20, 2005

Digital Input and output using the microcontroller

First PIC program

Second week of pcomp and we've moved on from the basic electronics.
Half way through Luck's explanation of Programming the PIC, the PicBasic Pro, we had a fire drill at TSOA... My first at NYU. And it was a drill... we didn't see any smoke~

We covered the following

Microcontrollers : what they are, different types, levels.
Intro to PIC and PIC programming
Digital Input and Output

For programming the chip, you need the board set up with your Pic Chip and the EPIC. hardware programmer. The programmer will talk to the chip and program the chip.

IMG_0002.JPG
This is what the EPIC programmer looks like.

Basically, after you set up your board, there are two parts to programming. You first write all the programming on the MCS and then using this code, the Epic compiler will send the code into the chip.



I got my 18F452 way in advance and ordered extras from microchip.com. Free Pic microcontrollers! ^^ Order early in advance. People say it takes about 3-5 business days.


With programming, we can now make our own codes, use variables (similar to Lingo to execute certain loops)and to control the outputs on the board (the LEDs). I also found out that the best way to keep track of your codes is to forward them to your gmail account and keep them as separate codes.

So here goes my try... the lab assignment for this week



LED blinking

The code:

Define OSC 4 'clock speed at 4mHz

output portd.1 'output at pin, portd.1 electricity out

main:

low portd.1 'set pin RD1 low
pause 500
high portd.1 'set RD1 high
pause 500

goto main

First, since this is not a BX, with a built in clock, we need to define the crystal clock on the board (OSC). It's speed is at 4Mhz.
So, basically, whatever happens between "main:" and "goto main" loops in the program. This keeps the LED blinking.


Next step
connecting a switch into the board to control the LED.

\

The layout of the board.
IMG_0014.JPG

IMG_0016.JPG

Between the switch and the microchip and 10k resistor is needed to prevent a short circuit. The switch becomes the input in this system since it will either allow or prevent the flow of electricity into the board.

Code:


Define OSC 4 'clock speed at 4mHz

input portb.0 'this is the switch
output portd.1 'output at pin, portd.0 electricity out

main:

IF portb.0=1 then 'if the switch is closed on pin RB0
low portd.1 'set pin RD1 low
else
high portd.1 'set RD1 high

endif

goto main


Using the IF statments, we are giving conditions to the program. In the condition that the swich is closed, it will either light up or turn off the LED in portd.1

I googled a few Picbasic Pro codes and saw some interesting ones... This makes a computer-like sound from a buzzer or speaker.

' SOUND Command
'
' Make random computer-like noises. More refinement might make sound effects
' realistic enough to convince your boss you're working when you're really
' just playing Doom!!!


Define OSC 4

output portb.7

Include "bs1defs.bas" ' Include BS1 variables

SND CON 7 ' Define speaker pin

loop: 'Random W0 ' Randomize W0
B2 = (B0 & 31) + 100 ' Generate notes [64..95]
If B2 >= 50 Then beep ' Make [64..68] silence
B2 = 40
beep: Sound SND,[B2,1] ' Generate sound
Goto loop ' Forever


' SLEEP Command
'
' Slowly Blink LED Using Low Power Mode Delay

define OSC 4

output portb.2 ' LED Pin

loop: Toggle portb.2 ' Toggle LED
Sleep 10 ' Sleep for 10 Seconds (or so)
Goto loop ' Forever


LEDs lighting up and down.


I went onto creating a variable in the code and creating a sequence of lights turning on and off, up and down the line of LEDs.
The code:

'using a switch, lights flickering up and down repeatedly

Define OSC 4

'this is my switch
Input portd.1

TRISB = %00000000 'all the pins on portb OUTPUT

pauseTime VAR WORD
pauseTime = 40


main:

if portd.1 = 0 then 'if the switch is closed on this pin


HIGH portb.2
pause pauseTime
low portb.2


HIGH portb.3
pause pauseTime
low portb.3

HIGH portb.4
pause pauseTime
low portb.4

HIGH portb.5
pause pauseTime
low portb.5

HIGH portb.6
pause pauseTime
low portb.6

HIGH portb.7
pause pauseTime
low portb.7

HIGH portb.6
pause pauseTime
low portb.6

HIGH portb.5
pause pauseTime
low portb.5

HIGH portb.4
pause pauseTime
low portb.4

HIGH portb.3
pause pauseTime
low portb.3

HIGH portb.2
pause pauseTime
low portb.2




endif


goto main


Here, I've used a variable and declared it before the main:.
pauseTime VAR WORD
pauseTime = 40


TRISB = %00000000 refers to all outputs on portb.

Posted by min at September 20, 2005 10:12 PM

Comments