A A A

Electronicfr.com

Home arrow Microcontrollers circuits arrow Microcontrollers programming arrow Programming PIC 18F in C with C18 compiler and C18 library 20.08.2008

Main Menu

Home
Robotic
Microcontrollers and Ethernet
Microcontrollers and USB
Embedded computing
Submit a circuit

Search

Google
Web
electronicfr


Programming PIC 18F in C with C18 compiler and C18 library

Written by Administrator, on 15-01-2008 22:32
Views 9540    

This document is a memo and a compilation of code for C18 microchip C compiler and the C18 library

References

C 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
 

Basics

Setting configuration bits

It'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 manipulation

I/O pin direction 

  • Set port in output

set TRIS register to 0 (remember 0 lookslike O for Output)

TRISX=0x00;   where X is a port  letter (A, B, C..) 

  •  Set port in input

set TRIS register to 1 (remember 1 lookslike I for Input)

 TRISX=0xFF;   where X is a port  letter (A, B, C..)

  • Set a pin for input or output

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 

  • Set Port Low (0)

PORTX =0;

  • Set Port to High (1)

PORTX=0xFF;

  • Set pin to 0 or 1

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. 

USART

Prototypes are in <usart.h>

CloseUSART(void)  Disable the USART.
getsUSART( char * buffer, unsigned char len ); Read a string from the USART.
OpenUSARTunsigned char config, unsigned int spbrg); Configure the USART.
putsUSART( char *data ); Write a string from data memory to the USART.
putrsUSART( const rom char *data ); Write a string from program memory to the USART.
ReadUSART(void); Read a byte from the USART.
WriteUSART(char data); Write a byte to 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:
baud rate= FOSC / (64 * (spbrg + 1))

FOSC is the oscillator frequency 

With  USART_BRGH_HIGH:
baud rate = FOSC / (16 * (spbrg + 1))

 Error must be <5%, an error <1% is better

ADC

Fonction prototypes are in <adc.h>

BusyADC Is A/D converter currently performing a conversion?
CloseADC Disable the A/D converter.
ConvertADC Start an A/D conversion.
OpenADC Configure the A/D convertor.
ReadADC Read the results of an A/D conversion.
SetChanADC Select A/D channel to be used.

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
}

PWM

PWM (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 );
"period" can be any value from 0x00 to 0xff. This value determines the PWM
frequency by using the following formula:
PWM period =[(period ) + 1] x 4 x TOSC x TMR2 prescaler

Then set the duty cycle with SetDCPWM1

void SetDCPWM1( unsigned int dutycycle );
void SetDCPWM2( unsigned int dutycycle );
"dutycycle" is the value of dutycycle,  a 10-bit number.

You can close the channel with ClosePWM1(2) 

 

CAPTURE 

TODO 

TIMERS

TODO 

LCD

The 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.
PORTBbits.RB5 Pin used for the RS line.
PORTBbits.RB6 Pin used for the RW line.
Data Lines PORTB

This example should work, in other case you have to recompile the library!

How to rebuild  the library

  • - Create a folder where you while build the C18 library and into copy the header (\mcc18\h\xlcd.h) and any source files (from \mcc18\src\traditional\pmc\XLCD\*)
  • - Start MPLAB with a new  project
  • - Choose a appropriate device
  • - Onto menu "Project->Build Options...->Project", select Build library target radio button in MPASM/C17/C18/Suite tab
  • - Add all the source code files and  the header file to the project. 
  • - Modify the pin assignment in the xlcd.h file and then build the project
  • - Copy the p18fxxx.lib file from \mcc18\lib folder to the project folder (where xxx is the device type)
  • - Run this command in a 'cmd' terminal window:  for %%i in (*.o) do mplib /r .\p18fxxx.lib %%i  . The modified functions will be changed in the library 

To use this library, then you can:

Open you main project
  • Copy the modifyed header file xlcd.h and the library p18fxxx.lib  into the project folder
  • Use only local reference of xlcd.h :
    #include ".\xlcd.h"
  • Remove the reference to the library in the Linker Scripts
  • Add the local copy of p18fxxx.lib to the project

 

USB 

TODO

Quote this article in website Print Related articles Save this to del.icio.us

Users' Comments (4) RSS feed comment
Comment language: English (4), French (0)
Posted by Mike, on 06-03-2008 20:17, IP 216.9.243.104
1. Pic18F programing
This article is great. A simplified, easy to use version. 
 
Question, how can we send ASCII characters? I tried something like WriteUSART("22"); but it didn't work. I am trying to interface to Parallax's Serial LCD screen, but all I get is garbage. :S
 
» Report this comment to administrator
» Reply to this comment...
» See all 2 replie(s)

Posted by edi, on 17-04-2008 07:58, IP 210.187.51.62
2. programming PIC 18Fin c
i like this article..easy tu use..great..the best guides..can to use for beginner programmer
 
» Report this comment to administrator
» Reply to this comment...

Posted by Zeushomer, on 03-05-2008 17:09, IP 82.249.67.183
3. Thks
Thanks you to debug me !!!!!
 
» Report this comment to administrator
» Reply to this comment...

Posted by rodwell ( Zimbabwe), on 06-05-2008 23:41, IP 146.141.15.34
4. you rock my world administrator
i have been stuck on my project for hours trying to figure out some instruction sets for the c compiler librariess and u just gave me all i needed. thanks and God bless u.
 
» Report this comment to administrator
» Reply to this comment...

Add your comment



mXcomment 1.0.5 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
 
 
TOP