Programming PIC 18F in C with C18 compiler and C18 library |
This document is a memo and a compilation of code for C18 microchip C compiler and the C18 library ReferencesC compiler library ref: http://ww1.microchip.com/downloads/en/devicedoc/51297c.pdf
C compiler Getting start: http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Getting_Started_51295d.pdf BasicsSetting configuration bitsIt's possible to set configuration bit in the interface or in the program code. Some very typical settings in program: #pragma config OSC = XT /* Sets the oscillator mode to XT for cristal up to 4MHz or HS=>4MHz */ #pragma config WDT = OFF /* Turns the watchdog timer off */ #pragma config LVP = OFF /* Turns low voltage programming off */ #pragma config DEBUG = OFF /* Compiles without extra debug code */ Basic ports manipulationI/O pin direction
set TRIS register to 0 (remember 0 lookslike O for Output) TRISX=0x00; where X is a port letter (A, B, C..)
set TRIS register to 1 (remember 1 lookslike I for Input) TRISX=0xFF; where X is a port letter (A, B, C..)
TRISXbits.TRISXn = 1 (or 0); where X is a port lettre and n is the pin number in the port exemple:TRISBbits.TRISB4 = 1; Read/Write pin
PORTX =0;
PORTX=0xFF;
PORTXbits.RXn = 0; Exemple: PORTCbits.RC1 = 0; Delais
Nop(); // Wait for 1 CPU cycle
More delays are defined in <delays.h> Delay10TCYx (unsigned char N); // Delay in multiples of 10 instruction cycles. Delay100TCYx (unsigned char N); // Delay in multiples of 100 instruction cycles. Delay1KTCYx (unsigned char N); // Delay in multiples of 1000 instruction cycles. Delay10TCYx (unsigned char N); // Delay in multiples of 10000 instruction cycles. USARTPrototypes are in <usart.h>
CloseUSART(void) Disable the USART. exemple (1200baud, 10MHz cristal):
#include <p18cxxx.h>
#include <usart.h>
#include <delay.h>
void main()
{
TRISC = 0x00;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 129);
putrsUSART( "Hello World!" );
closeUSART();
}
How to set config and spbrg in openUSART to choose de baud rate ?
With USART_BRGH_LOW: FOSC is the oscillator frequency
With USART_BRGH_HIGH: Error must be <5%, an error <1% is better ADCFonction prototypes are in <adc.h>
BusyADC Is A/D converter currently performing a conversion? Exemple: Read 10 bit value on ADC_CH0 (RA0)
#include <adc.h>
#include <delays.h>
void main(){
int temp;
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_0_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
0b1011);
SetChanADC(ADC_CH0);
Delay10TCYx( 50 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for ADC conversion
temp = ReadADC(); // Read result and put in temp
CloseADC(); // Disable A/D converter
}
PWMPWM (Pulse-width modulation) is a signal involves the modulation of its duty cycle. It's an easy way to modulate for example the brightness of a LED or the speed of an electric motor. Many PIC includes PWM generation. In C18 you must include in your code <pwm.h> First configure the channel with OpenPWM1 or OpenPWM2
void OpenEPWM1( char period ); Then set the duty cycle with SetDCPWM1
void SetDCPWM1( unsigned int dutycycle ); You can close the channel with ClosePWM1(2)
CAPTURETODO TIMERSTODO LCDThe library includes functions for Hitachi HD44780 LCD. The main difficulty is to rebuild the library for your own schematic. Example of use (timings for 4MHz cristal) :
#include <delays.h>
#include <p18C452.h>
#include <xlcd.h>
void DelayFor18TCY( void )
{
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop(); Nop(); Nop();
Nop(); Nop();
}
void DelayPORXLCD( void )
{
Delay1KTCYx(15); //Delay of 15ms
return;
}
void DelayXLCD( void )
{
Delay1KTCYx(5); //Delay of 5ms
return;
}
void main( void )
{
char data='a';
// configure external LCD
OpenXLCD( EIGHT_BIT & LINES_5X7 );
putsXLCD(data);
putrsXLCD("test");
}
If your pin assignment is same like that:
PORTBbits.RB4 Pin used for the E line. This example should work, in other case you have to recompile the library! How to rebuild the library
To use this library, then you can: Open you main project
USBTODO
|
mXcomment 1.0.5 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved






