0% found this document useful (0 votes)
38 views

Microcontroller Programming: How To Make Something Almost Do Something Else

This document provides an overview of microcontroller programming using PIC microcontrollers. It discusses the differences between microcontrollers and more powerful processors like Pentium chips. It then describes the features of the Microchip PIC16F876A microcontroller including its memory, instructions, and I/O pins. The document provides examples of assembly language code for basic tasks like adding numbers, counting down with loops, simple input and output, and using the USART serial port. It compares assembly to higher-level languages like C and discusses tools for microcontroller development.

Uploaded by

ECE COMP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Microcontroller Programming: How To Make Something Almost Do Something Else

This document provides an overview of microcontroller programming using PIC microcontrollers. It discusses the differences between microcontrollers and more powerful processors like Pentium chips. It then describes the features of the Microchip PIC16F876A microcontroller including its memory, instructions, and I/O pins. The document provides examples of assembly language code for basic tasks like adding numbers, counting down with loops, simple input and output, and using the USART serial port. It compares assembly to higher-level languages like C and discusses tools for microcontroller development.

Uploaded by

ECE COMP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Microcontroller Programming

How to make something almost do something


else
Raffi Krikorian
MAS.863
3 November 2003
What’s wrong with a P4?
Pentiums Microcontrollers
• 50 million transistors • < 150,000 transistors
• $200 • $0.50 - $5.00
• Watts @ idle • 0.01s Watts while
• Complicated active
instruction set and • “Simple”
usage model programming model
PIC16F876A
What is it?
• 8-bit processor that can be clocked from 50 kHz -
20 MHz
• 8K Flash program memory and 368 bytes SRAM
• 22 I/O pins (5 of which could be ADCs)
• 35 Instructions
• Hardware USART
• 2 Comparators
Memory
• Flash memory is
where your “program”
is stored
• SRAM is general
purpose memory
• Registers can be
memory mapped
Instructions
• Processors work with
instructions
– Move, Add, Jump, etc.
• Programs are just a
series of instructions
that the processor
“steps” through
Adding two numbers
• Numbers are defined in // NUMBER3 =
locations in memory // NUMBER1 + NUMBER2
• Move NUMBER1 to the
W registers (working NUMBER1 EQU 0x20
register) NUMBER2 EQU 0x21
NUMBER3 EQU 0x22
• Add NUMBER2 to W and
store the result back in W
MOVF NUMBER1, W
• Move the value in W to ADDWF NUMBER2, W
the NUMBER3’s memory MOVWF NUMBER3
location
Counting down v1.0
• W <- 10 COUNT EQU 0x20
• COUNT <- W
MOVLW d’10’
• Do some stuff MOVWF COUNT
• If the Z bit is set in do_loop:
STATUS (the last // do stuff
operation == 0), then skip DECF COUNT, F
the next line BTFSS STATUS, Z
• If the GOTO is not GOTO do_loop
skipped, then jump back
to the do_loop
Counting down v2.0
• There are optimizations COUNT EQU 0x20
for common operations
MOVLW d’10’
• DECFSZ decrements the MOVWF COUNT
value in COUNT, stores it do_loop:
into COUNT, and if // do stuff
COUNT == 0 (if the Z bit DECFSZ COUNT, F
is set), it skips the next GOTO do_loop
instruction
Labels
• Labels allow you to mark
a place in the code to
GOTO or CALL
• GOTO jumps to a label
• CALL saves the current
position, then jumps to a
label
– Allows for a RETURN to
the current position
Simple Output
• Setup PORTC pin 0
(RC0) to be an output
• Turn PORTC pin 0 on
• Turn PORTC pin 0 off
BSF STATUS, RP0
BCF TRISC, 0
BCF STATUS, RP0

BSF PORTC, 0
BCF PORTC, 0
Simple Input
• Setup PORTB pin 0 (RB0)
to be an input
• If RB0 is “low” (reads 0),
then skip
– this is the button press
BCF STATUS, RP0
• If RB0 is “high”, then do
BSF TRISB, 0
next instruction BSF STATUS, RP0
– Keeps us looping until the
button_test:
button press
BTFSC PORTB, 0
GOTO button_test
// button pressed
Using the USART
• USART RX on RC7, TX on RC6
– Make sure that RC7 is an input, and RC6 is an output in
your code
• Load baud rate into SPBRG
• Receiver enable with CREN bit in RCSTA,
transmitter enable with TXEN bit in TXSTA
• Put value you want to transmit into TXREG
• Loop on PIR1 bit RCIF to wait for bytes
• See sample code!
Assembler is fast! But…
• Large programs are hard to manage
• Allocating memory locations in your head
is a pain
• Remembering the nuances of all the
instructions can get annoying
• “Porting” your code to a different processor
is almost impossible
Higher level languages
• C, Basic, Java, Lisp
• All “abstract” out the processor and let you focus
on code
– The compiler handles the conversion from the high
level language to the assembly instructions
• There is a penalty, however…
– Size of code
– Execution speed
C vs. Assembler
Assembler C
MOVLW d’10’ count = 10;
MOVWF COUNT while( count-- > 0 ) {
flash: port_c = 1;
BSF PORTC, 0 port_c = 0;
BCF PORTC, 0
DECFSZ COUNT, F }
GOTO flash
Raffi vs. CCS compiled
Raffi-written ASM CCS generated ASM
MOVLW d’10’ MOVLW d’10’
MOVWF COUNT
MOVWF COUNT
flash:
flash:
MOVF COUNT, W
BSF PORTC, 0 DECF COUNT, F
BCF PORTC, 0 XORLW d’0’
DECFSZ COUNT, F BTFSC STATUS, Z
GOTO flash GOTO flash_done
MOVLW d’1’
MOVWF PORTC
CLRF PORTC
GOTO flash
flash_done:
Getting the job done
Software
• MPLAB IDE : Microchip’s integrated
development environment
• PICC : CCS C compiler for PICs
– Integrates into MPLAB
• gpasm : open source assembler
Hardware
• PICSTART Plus or
equivalent programmer
• Project ideas
– Program a “bootloader”
into the software and then
load code over the serial
port
– Build a PIC programmer
(you can easily do it with
another PIC and some
simple circuitry)
Attaching your board
• Pin 1 goes to 15V when
programming, pins 28 and
27 bidirectionally talk to
programmer
• Attach a header and
connect that to the
programmer
• Also connect power (5V)
and ground
Compiling your code (MPLAB)
Getting ready to program
(MPLAB)
Burn baby, burn (MPLAB)
MSP430F1232
What is it?
• 16-bit processor that can be clocked from 30 kHz -
8 MHz
• 8K Flash program memory and 256 bytes RAM
• 22 I/O pins (8 of which could be ADCs)
• Hardware USART
Why would you want to use it?
• This is where we’re going
• GCC as the compiler/toolchain
• JTAG programming/debugging port
• 350 uA max current draw (PIC on avg.
draws 6 mA)
• Easy to bridge into much more powerful
micros

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy