Книга: Advanced PIC Microcontroller Projects in C
3.3 Programming Examples
Разделы на этой странице:
3.3 Programming Examples
In this section, some simple programming examples are given to familiarize the reader with programming in C.
Example 3.2
Write a program to set all eight port pins of PORTB to logic 1.
Solution 3.2
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
}
Example 3.3
Write a program to set the odd-numbered PORTB pins (bits 1, 3, 5, and 7) to logic 1.
Solution 3.3
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
}
Example 3.4
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
........
Solution 3.4
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
}
}
Example 3.5
Write a program to set all bits of PORTB to logic 1 and then to logic 0, and to repeat this process ten times.
Solution 3.5
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
}
}
Example 3.6
The radius and height of a cylinder are 2.5cm and 10cm respectively. Write a program to calculate the volume of this cylinder.
Solution 3.6
The required program is:
void main() {
float Radius = 2.5, Height = 10;
float Volume;
Volume = PI * Radius * Radius * Height;
}
Example 3.7
Write a program to find the largest element of an integer array having ten elements.
Solution 3.7
At the beginning, variable m is set to the first element of the array. A loop is then formed and the largest element of the array is found:
void main() {
unsigned char j;
int m, A[10];
m = A[0]; // First element of array
for(j = 1; j < 10; j++) {
if(A[j] > m) m = A[j];
}
}
Example 3.8
Write a program using a while statement to clear all ten elements of an integer array M.
Solution 3.8
As shown in the program that follows, NUM is defined as 10 and variable j is used as the loop counter:
#define NUM 10
void main() {
int M[NUM];
unsigned char j = 0;
while (j < NUM) {
M[j] = 0;
j++;
}
}
Example 3.9
Write a program to convert the temperature from °C to °F starting from 0°C, in steps of 1°C up to and including 100°C, and store the results in an array called F.
Solution 3.9
Given the temperature in °C, the equivalent in °F is calculated using the formula:
F = (C – 32.0)/1.8
A for loop is used to calculate the temperature in °F and store in array F:
void main() {
float F[100];
unsigned char C;
for(C = 0; C <= 100; C++) {
F[C] = (C – 32.0) / 1.8;
}
}
- 1.1 Real Life Examples of Embedded Systems
- CHAPTER 3 C Programming Language
- PART V Programming Linux
- Programming in C
- Using the C Programming Project Management Tools Provided with Fedora Linux
- 14.4.1. Programming Flash Using a JTAG Probe
- 3.2 PIC Microcontroller Input-Output Port Programming
- 9.11 CAN Bus Programming
- Table B-1. Support for Java, C, and C++ programming
- Задание Begin3 из электронного задачника Programming Taskbook
- Chapter 11. Emacs Lisp Programming
- 9. Programming – using machine code and assembly