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

Lecture 3 C Programming For PICs

Uploaded by

Craig Peri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 3 C Programming For PICs

Uploaded by

Craig Peri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

High Level

Programming for
PICs
Mikro C Programming Language
• It is a C programming language modified and
customised to suit development of programs for
Microchip PICs.
• Same syntax
• Also borrows some C++ syntax.

• Additional keywords - code, data, rx, sfr, ldm, at, sbit,


bit and iv.

• Compiling a Mikro C program produces a hex file that


can be downloaded into a PIC.
Mikro C Programming Language
• SFR locations can be accessed using names of the SFRs

• GPR locations can be accessed through declared


variables by programmer

• The working space can be accessed using the names R0


through to R15.

• The C programming language takes care of changing


between banks as necessary.
Mikro C Programming Language
Variables
• char – 8-bit number stored in a file register

• short int – 8-bit signed integer stored in a file register

• unsigned short int – 8-bit unsigned integer stored in a


file register

• int – 16-bit signed integer stored in 2 file registers

• unsigned int – 16-bit unsigned integer stored in 2 file


registers
Mikro C Programming Language
Operations and Assignments of Values to Variables

• Assignment operator: =
TMR0 = 58;

• Operations: +, -, *, /, etc.
WEIGHT = ADRESL/4;

• Comparison operators: ==, > , <, etc.

• Decision Statements: if – else, switch

• Looping Statements: do; do while; etc.


Accessing Bits in File Registers
• Individual bits in a file registers can be accessed.

• The file register containing the bit and the bit position
are specified, separated by a decimal point.
• For example INTCON.GIE, PIR1.B7, mask.b3,
PORTB.B7.

• The bit name is specified using the format NAME_bit,


• For example RB3_bit, RP0_bit.
Accessing Bits in File Registers
Examples to change bit values

• INTCON.GIE = 1;
• PIR1.B7 = 0;
• Mask.b3 = 1;
• RB3_bit = 0;
• RP0_bit = 1;
Accessing Bits in File Registers
Example to check port pins

if (RA2_bit == 0)
{
// put code here
}
else
{
// put alternate code here
}
The sbit Variable
• The sbit data type provides access to SFRs, bits, variables,
etc.

• The sbit variable is declared in such a way that it points to a


specific SFR or bit in an SFR.

• An example declaration is:


• sfr sbit Abit; // definition of Abit

• The register that sbit points to needs to be defined. For


example:
• sbit Abit at PORTB.B0; // full
definition of Abit
The sbit Variable
char MyVar;
sbit LED at MyVar.B0;
extern char PORTAlias;
char PORTAlias at PORTB;

LED = 1 ; set bit LED – which is bit 0


; on the byte MyVar
Inserting Assembly Statements
asm {
movlw 5
movf 0x71,1
movlw 6
movf 0x72,1
}

asm movf 0x43,1


The Flasher Program
Start

LOOP
Set port pins high

Delay 1s

Set port pins low

Delay 1s
void main() {

The Flasher Program


TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
do {
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
delay();
PORTA = 0xFF;
PORTB = 0xFF;
PORTC = 0xFF;
PORTD = 0xFF;
PORTE = 0xFF;
delay();
} while(1);
}
The Flasher Program
THE DELAY FUNCTION
void delay(void) {
unsigned short int j;
for (j=0; j<255; j++)
{
unsigned short int i;
for (i=0; i<255; i++)
{;}
}
}
void delay(void) {

The Flasher Program


unsigned short int j;
for (j=0; j<255; j++)
{
unsigned short int i;
for (i=0; i<255; i++)
{;}
}
}

void main() {
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
do {
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
Built-in Routines
• The Mikro C compiler provides a set of useful built-in
utility functions.

• Of importance to mention to mention:


• delay_us (long unsigned int period) – delay of length in
microseconds equal to the argument, period.

• delay_ms (long unsigned int period) – delay of length in


milliseconds equal to the argument, period.
Built-in Routines
• The Mikro C compiler provides a set of useful built-in
utility functions.

• Of importance to mention to mention:


• delay_us (long unsigned int period) – delay of length in
microseconds equal to the argument, period.

• delay_ms (long unsigned int period) – delay of length in


milliseconds equal to the argument, period.

Redo the flasher program using built-in delays


Exercise 2
Do the following questions from Chapter 3
Tutorial Questions

•Questions 1
•Question 2

You might also like