2 Basics of C Flashcards
The structure that both ASM and C share?
- Configuration bits
- Variable storage
- Entry Point
- Ininite loop
STRUC: What are the configuration bits?
These configure the basic setup of the PIC including things like the oscillator type and the presence of the watch dog timer
STRUC: What is variable storage setup?
Definitions of variables, including where in memory to store them
STRUC: Entry point
An indicator of where in the code the program counter will start
STRUC: Infinite loop?
An infinite loop is commonly used to keep repeating a set of operations ad infinitum
Config(Role/Default): FOSC?
The type of oscillator to use (external/internal/crystal)
Default: XT(external crystal)
Config(Role/Defult): WDTE?
Enable/Disable the watch dog timer
OFF(no wdt)
Config(Role/Defult): PWRITE?
Enable/Disable the power-up timer (72 ms)
ON(power-up timer)
Config(Role/Defult): CP?
Enable/Disable code protection
OFF(code protection)
Configuration bits for LED flash?
pragma config FOSC = XT // XT oscillator
// Setup configuration bits
Configure oscillator frequency for flashing LED?
define _XTAL_FREQ 4000000
// Oscillator Frequency (4 MHz)
Flashing LED: Code to import library functions?
include
// Include C library functions for PIC
Flashing LED: Code to write to ports?
PORTBbits.RB0 = 1; // Set RB0 high __delay_ms(500); // Wait 0.5 seconds PORTBbits.RB0 = 0; // Set RB0 low __delay_ms(500); // Wait 0.5 seconds
Flashing LED: Port Setup?
// Set all port B bits to be output
TRISB = 0x0;
Flashing LED: Main loop for the program?
A statement?
A statement is the smallest standalone element of a program, normally a single line of code
In C a statement must be terminated with a ….
semicolon
C: Blocks?
Statements can be grouped together into blocks, for example blocks of code that form part of a loop
C: Example of a block?
Advantages of C in terms of I/O?
We can read or write to individual pins as well as to the entire PORT register (this is a bit more powerful than ASM)
Example in C of writing to a port?
C program to Toggle an LED based on a user keypress
Differences between ASM and C in variable naming?
- In assembler we had to specify exactly where in memory a variable would live, this was tedious and does not port well to other PICs
- In C life is much better, and all we need to do is give our variables a name and a type, for our small PIC we will only use integers
Creating variables in C?