Chapter5-Pic Programming in C
Chapter5-Pic Programming in C
The compiler convert high level programming language to machine instruction of the target processor. Mixed language programming using C-language with assembly language is supported by the C18 compiler. Assembly blocks are surrounded with at _asm and a _endasm directives to the C18 compiler. Assembly code can also be located in a separate asm file
Write a C 18 program to get a byte of data from Port B, wait 112 second, and then send it to Port C.
A door sensor is connected to the RB I pin, and a buzzer is connected to RC7. Write a CI8 program to monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz frequency to it.
#include <P18F4550 . inc > void MSDelay (unsigned int) ; //function prototype #define Dsensor PORTBbits.RB1 #define buzzer PORTCbits . RC7 void main (void) { TRISBbits.TRISB1 = 1; //PORTB.1 as an input TRISCbits . TRISC7 = 0; //make PORTC . 7 an output while (Dsensor ==1) { buzzer = 0; //here to make square wave for buzzer output with 200ms interval MSDelay (5) ; // Hz,f = 1/t = 1/10m = 100Hz buzzer = 1; //cara untuk memberikan square wave kepada buzzer dengan 200ms interval MSDelay (5) ; // } while (1) ; //stay here forever } void MSDelay(unsigned int itime) { unsigned int i; unsigned char j; for(i=O;i <itime;i++ ) for (j=0;j<l65;j++) ; }
example
The data pins of an LCD are connected to Port B. The information is latched into the LCD whenever its Enable pin goes from HIGH to LOW. Write a CI8 program to send "The Earth is but One Country" to this LCD.
LOGIC OPERATIONS IN C
One of the most important and powerful features of the C language is its ability to perform bit manipulation. While every C programmer is familiar with the logical operators AND (&&), OR (II), and NOT (!), many C programmers are less familiar with the bitwise operators AND (&), OR (I), EX-OR (^), inverter (~), shift right (>>), and shift left(<<)). These bit-wise operators are widely used in software engineering for embedded systems and control; consequently, their understanding and mastery are critical in microprocessor-based system design and interfacing.
ASCII numbers
On ASCII keyboards, when the key "0" is activated, "0 II 0000" (30H) is provided to the computer. Similarly, 31H (0110001) is provided for the key "1", and so on.
Every timer needs a clock pulse to tick. The clock source can be internal or external. If we use the internal clock source, then 1/4th of the frequency of the crystal oscillator on the OSCI and OSC2 pins (Fosc/4) is fed into the timer.
Therefore, it is used for time delay generation and for that reason is called a timer. By choosing the external clock option, we feed pulses through one of the PICI8s pins: this is called a counter.
4. After the timer reaches its limit and rolls over, in order to repeat the process, the registers TMR0H and TMR0L must be reloaded with the original value, and the TMR0IF flag must be reset to 0 for the next round.
1.
2.
3. 4. 5. 6. 7.
Write a CI8 program to generate a frequency of 2 Hz only on pin PORTB.5. Use Timer0, 8-bit mode to create the delay.
COUNTER PROGRAMMING
We used the timers of the PICI8 to generate time delays. These timers can also be used as counters to count events happening outside the PIC 18. When it is used as a counter, however, it is a pulse outside the PIC 18 that increments the TH, TL registers. In counter mode, notice that registers such as T0CON, TMR0H, and TMR0L are the same as for the timer discussed in the last section; they even have the same names.
Example - counter
Assume that a 1Hz external clock is being fed into pin T0CKI (RA4). Write a CI8 program for Counter0 in 8-bit mode to count up and display the state of the TMR0L count on PORTB. Start the count at 0H.
example
Assume that a 1Hz external clock is being fed into pin T0CKI (RA4). Write a C program for Counter 0 in mode 1 (16-bit) to count the pulses and display the TMR0H and TMR0L registers on PORTD and PORTB, respectively.
example
Assume that a 60Hz external clock is being fed into pin T0CKI (RA4). Write a C program for Counter 0 in 8-bit mode to display the seconds and minutes on PORTB and PORTD, respectively.