/* Calculate sum of numbers 1,2,3,4,6,7,8,9,10 */
Sum = 0;
i = 1;
for(i = 1; i <= 10; i++) {
if (i == 5) continue; // Skip number 5
Sum = Sum + i;
}
Similarly, a break statement can be used to terminate a loop from inside the loop. In the following example, the sum of numbers from 1 to 5 is calculated even though the loop parameters are set to iterate 10 times:
/* Calculate sum of numbers 1,2,3,4,5 */
Sum = 0;
i = 1;
for(i = 1; i <= 10; i++) {
if (i > 5) break; // Stop loop if i > 5
Sum = Sum + i;
}
3.1.20 Mixing mikroC with Assembly Language Statements
It sometimes becomes necessary to mix PIC microcontroller assembly language statements with the mikroC language. For example, very accurate program delays can be generated by using assembly language statements. The topic of assembly language is beyond the scope of this book, but techniques for including assembly language instructions in mikroC programs are discussed in this section for readers who are familiar with the PIC microcontroller assembly languages.
Assembly language instructions can be included in a mikroC program by using the keyword asm (or _asm, or __asm). A group of assembly instructions or a single such instruction can be included within a pair of curly brackets. The syntax is:
asm {
assembly instructions
}
Assembly language style comments (a line starting with a semicolon character) are not allowed, but mikroC does allow both types of C style comments to be used with assembly language programs:
asm {
/* This assembly code introduces delay to the program*/
MOVLW 6 // Load W with 6
................
................
}
User-declared C variables can be used in assembly language routines, but they must be declared and initialized before use. For example, C variable Temp can be initialized and then loaded to the W register as:
unsigned char Temp = 10;
asm {
MOVLW Temp // W = Temp = 10
..................
..................
}
Global symbols such as predefined port names and register names can be used in assembly language routines without having to initialize them:
asm {
MOVWF PORTB
.....................
.....................
}
3.2 PIC Microcontroller Input-Output Port Programming
Depending on the type of microcontroller used, PIC microcontroller input-output ports are named as PORTA, PORTB, PORTC, and so on. Port pins can be in analog or digital mode. In analog mode, ports are input only and a built-in analog-to-digital converter and multiplexer circuits are used. In digital mode, a port pin can be configured as either input or output. The TRIS registers control the port directions, and there are TRIS registers for each port, named as TRISA, TRISB, TRISC, and so on. Clearing a TRIS register bit to 0 sets the corresponding port bit to output mode. Similarly, setting a TRIS register bit to 1 sets the corresponding port bit to input mode.
Ports can be accessed as a single 8-bit register, or individual bits of a port can be accessed. In the following example, PORTB is configured as an output port and all its bits are set to a 1:
TRISB = 0; // Set PORTB as output
PORTB = 0xFF; // Set PORTB bits to 1
Similarly, the following example shows how the 4 upper bits of PORTC can be set as input and the 4 lower bits of PORTC can be set as output:
TRISC = 0xF0;
Bits of an input-output port can be accessed by specifying the required bit number. In the following example, variable P2 is loaded with bit 2 of PORTB:
P2 = PORTB.2;
All the bits of a port can be complemented by the statement:
PORTB = ~PORTB;
3.3 Programming Examples
In this section, some simple programming examples are given to familiarize the reader with programming in C.
Write a program to set all eight port pins of PORTB to logic 1.
PORTB is configured as an output port, and then all port pins are set to logic 1 by sending hexadecimal number 0xFF:
void main() {
TRISB = 0; // Configure PORTB as output
PORTB = 0xFF; // Set all port pins to logic a
}
Write a program to set the odd-numbered PORTB pins (bits 1, 3, 5, and 7) to logic 1.
Odd-numbered port pins can be set to logic 1 by sending the bit pattern 10101010 to the port. This bit pattern is the hexadecimal number 0xAA and the required program is:
void main() {
TRISB = 0; // Configure PORTB as output
PORTB = 0xAA; // Turn on odd numbered port pins
}
Write a program to continuously count up in binary and send this data to PORTB. Thus PORTB requires the binary data:
00000000
00000001
00000010
00000011
........
........
11111110
11111111
00000000
........
A for loop can be used to create an endless loop, and inside this loop the value of a variable can be incremented and then sent to PORTB:
void main() {
unsigned char Cnt = 0;
for(;;) // Endless loop
{
PORTB = Cnt; // Send Cnt to PORTB
Cnt++; // Increment Cnt
}
}
Write a program to set all bits of PORTB to logic 1 and then to logic 0, and to repeat this process ten times.
A for statement can be used to create a loop to repeat the required operation ten times:
void main() {
unsigned char j;
for(j = 0; j < 10; j++) // Repeat 10 times
{
PORTB = 0xFF; // Set PORTB pins to 1
PORTB = 0; // Clear PORTB pins
}
}
The radius and height of a cylinder are 2.5cm and 10cm respectively. Write a program to calculate the volume of this cylinder.
The required program is: