Microcontroller Programming: How To Make Something Almost Do Something Else
Microcontroller Programming: How To Make Something Almost Do Something Else
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