Template Code for the PIC18F4620

I’m writing a new template program for the PIC18F4620, for use in our RoboSumo module and other projects. The template program basically sets up a few frequently used features – e.g. digital inputs, digital outputs, analog inputs, serial communication, PWM output. The clock speed is set to 8MHz. This template program is a work in progress, but the code below is working.

//
// PIC18F4620 template program
// Written by Ted Burke (ted.burke@dit.ie)
// Last update 11-4-2011
//

#include <p18f4620.h>
#include <usart.h>
#include <delays.h>
#include <stdio.h>

#pragma config OSC = INTIO67
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF

// Function prototypes
void setup(void);
unsigned int read_analog_channel(unsigned int);

void main(void)
{
    // Configure the PIC
    setup();

    // Flash an LED on RD0
    while(1)
    {
        LATDbits.LATD0 = 1; // Set RD0 high
        Delay10KTCYx(20);   // 100ms delay
        LATDbits.LATD0 = 0; // Set RD0 low
        Delay10KTCYx(20);   // 100ms delay
    }
}

void setup(void)
{
    // Set clock frequency (section 2 of the PIC18F4620 Data Sheet)
    // Set Fosc = 8MHz, which gives Tcy = 0.5us
    OSCCON = 0b01110000;

    // Set up ADC (section 19 of the PIC18F4620 Data Sheet)
    // Enable AN0-7 as analog inputs
    ADCON1 = 0b00000111;
    // Left justified result, manual acquisition time,
    // AD clock source = 8 x Tosc
    ADCON2 = 0b00000001;

    // Set up Port B (section 9.2 of the PIC18F4620 Data Sheet)
    // RB0-5 outputs, RB6-7 inputs
    LATB = 0b00000000;
    TRISB = 0b11000000;

    // Set up Port D (section 9.4 of the PIC18F4620 Data Sheet)
    // RD0-3 digital outputs, RD4-7 digital inputs
    LATD = 0b00000000;
    TRISD = 0b11110000;

    // Set up PWM (section 15 of the PIC18F4620 Data Sheet)
    CCP1CON = 0b00001100;   // PWM on CCP1
    CCP2CON = 0b00001100;   // PWM on CCP1
    TRISC = 0b11111001;     // CCP1, CCP2 as outputs
    T2CON = 0b00000110;     // Enable TMR2 with prescale = 16
    PR2 = 255;              // period = (PR2+1) * Tcy * prescale
    CCPR1L = 127;           // Ch 1 duty cycle = CCPR1L / PR2
    CCPR2L = 127;           // Ch 2 duty cycle = CCPR1L / PR2

    // Set up USART (section 18 of the PIC18F4620 Data Sheet)
    // baud_rate = Fosc/(16*(spbrg+1))
    //           = 8000000/(16*(207+1)) = 2403.846
    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF
            & USART_ASYNCH_MODE & USART_EIGHT_BIT
            & USART_CONT_RX & USART_BRGH_HIGH, 207);
}

// Read voltage from the specified channel.
// This function takes approximately 35us.
unsigned int read_analog_channel(unsigned int n)
{
    unsigned int voltage;

    ADCON0 = n << 2;
    ADCON0bits.ADON = 1;
    Delay10TCYx(3); // 15us charging time
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO); // Await result (11us approx)

    // Return the result (a number between 0 and 1023)
    voltage = ADRESH;
    voltage = (voltage << 2) + (ADRESL >> 6);
    return voltage;
}
Advertisements
This entry was posted in PIC and tagged , , , , , , , , , . Bookmark the permalink.

4 Responses to Template Code for the PIC18F4620

  1. Hi Ted, Just downloaded your new template. Looking forward to trying it out. Richard

  2. sharon says:

    hello! can u show an example in using a push button to control the spin of the servo motor? pls reply! thank you! (:

  3. Pingback: Mains power detection circuit |

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s