Showing posts with label C18. Show all posts
Showing posts with label C18. Show all posts

Friday, June 19, 2009

Setting Up FreeRTOS for the PIC18 Using MPLAB C18

FreeRTOS is a really cool real-time operating system (preemptive scheduler) for various architectures. It has been ported to the PIC18 platform (using MPLAB C18 compiler). However, the port is not very good.

Isaac Marino Bavaresco (isaacbavaresco AT yahoo DOT com DOT br) of the PICLIST has greatly improved and optimized the port for PIC18. His PICLIST page is here.

It took me a long time to get everything set up and working properly, so I’m writing this tutorial as an easy quick-start guide on getting FreeRTOS running on the PIC18.

Things you’ll need to get started:

  • A PIC18 device
  • MPLAB & MPLAB C18 compiler
  • FreeRTOS
    • I have made a convenient FreeRTOS modified package available for download (FreeRTOS 5.3.0) for the PIC18 containing Isaac’s improved port. DOWNLOAD
  • My sample project. It’s the fastest way to get started. However, I recommend that you download this sample project just for the source files (don’t actually use the project file) and you set up your own project by reading this tutorial. DOWNLOAD

 

Step 1 – Setting up your directories

Configure your directories in this structure:

  • PIC_Projects
    • FreeRTOS
    • Project_1
    • Project_2
    • Project_3

Step 2 – Download FreeRTOS_mod for PIC18 from the link above.

Unzip in your projects directory and you will get a directory named “FreeRTOS”.

Step 3 – Create a project

  • Open MPLAB and click Project –> New…
    • Create a project with the name of your choice corresponding to the directory structure outlined above.
  • Click Configure –> Select Device… and select your PIC18 device
  • Click Project –> Build Options –> Project…
    • In the Directories tab
      • select Library Search Path from the combo box and add the lib folder from your C18 installation. For me, it was C:\MCC18\lib.
      • Next, select Include Search Path from the same combo box and add the following directories:
        • .
        • ..\FreeRTOS\Source\Portable\PIC18
        • ..\FreeRTOS\Source\include
    • In the MPASM/C17/C18 Suite tab
      • Enable the checkbox for Extended mode.
    • In the MPLAB C18 tab
      • In the General category, add the following macro: MPLAB_PIC18F_PORT
      • In the Memory Model category, select Large code model, Large data model, Multi-bank model
    • Apply the changes.
  • Add the following files to the project Source Files:
    • Everything in the FreeRTOS\Source directory
    • Everything in the FreeRTOS\Source\portable\PIC18 directory
    • My main.c file from my sample project
  • Add the following files to the project Header Files:
    • FreeRTOSConfig.h from my sample project
  • Add FreeRTOS\Source\portable\PIC18\18f2620_e_FreeRTOS.lkr file to the Linker Script section. If you are using a different device, you need to make your own linker script. Use the one provided as a reference and compare it to the original one.
  • You should now be ready to compile your project. Go ahead and click Project –> Build All and watch the magic happen.

Explanation

All that this project does is create two tasks that constantly print “abcd” and “1234” to the UART. It uses a mutex to ensure that the non-thread-safe puts function is only being used in one thread at a time.

The RTOS configuration file is FreeRTOSConfig.h. The only thing non-standard in there is the #define CUSTOM_HEAP macro. I created this little modification to replace the standard malloc()/free() calls in the RTOS source to instead use Isaac’s functions which are far more optimized.

Monday, June 8, 2009

Some Starter C18 Code for the PIC18

I haven’t posted anything in a while, so here is some C18 code for a starting off a project.

#include 
#include 
#include 
#include 

#pragma config OSC = INTIO67	//Internal oscillator
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF		//MCLR is disabled
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config XINST = ON		//extended mode enabled

long int i = 0;

void delay1s() {
	Delay10KTCYx(200);
	Delay10KTCYx(200);
	Delay10KTCYx(200);
	Delay10KTCYx(200);
}

void main() {
	OSCCON = 0b01110000; 	//8 MHz
	OSCTUNE = 0b01011111;	//enable PLL
	TRISC = 0x00;			//PORTC is all output
	stdout = _H_USART; 		//Set to user-defined output stream via _user_putc
	
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51);


	while(1) {
		delay1s();
		printf("ping! %li\n", i);
		i++;
	}	
}

Here’s the project itself: Click to Download

Friday, March 6, 2009

AS1107 LCD Driver Chip Interface Code for C18

I was unable to find any code to interface this chip on the Internet. However, there were a few people with videos demonstrating its use. After contacting them, borrowing a few of their driver ideas, and writing my own driver, I was finally able to get this chip to work. Here’s my driver.

  • Written for Microchip C18 compiler. Can easily be adapted to other compilers.
  • My chip is an 18F2620, however, the code can work with almost any PIC.

The code is pretty  much self-explanatory. The headers are where everything is defined and where the user is able to define pin assignments.

Download: http://www.mediafire.com/?5jjjzz2jitj

Sunday, February 15, 2009

Turbocharge Your PIC with PLL

The PIC18 series have a maximum internal oscillator frequency of 8 MHz, which translates to 2 MIPS. However, the PIC can run at much higher speeds (especially when using an external clock source). Using the PIC's PLL, one is able to multiply the clock source by up to 4 times. This is even software-controllable if using the internal clock source. Using the maximum speed of the internal oscillator, the PIC can run at up to 32 MHZ with PLL enabled. This yields blazing fast performance of 8 MIPS. Best of all, you can turn this feature on or off as you please to control power consumption. Using the PLL is very easy.

We must first ensure that we are using the internal oscillator. So, in C18, declare a configuration option like this:

#pragma config OSC = INTIO67

Next, we must either configure the internal oscillator to run at 4 or 8 MHz. The PLL will not be enabled on any other configuration. Since we're opting for speed here, let's set it at 8 MHz. Put the following as the first line in your main() function:

OSCCON = 0b01110000;

Note that the last two bits of OSCCON must be 00. And finally, to enable the PLL, add the next line right after:

OSCTUNE = 0b01011111;

This enables the PLL and runs the PIC at the maximum possible frequency using only the internal oscillator - 8 MIPS @ 32 MHz. To toggle the PLL on or off, simply set OSCTUNEbits.PLLEN to either 1 or 0, respectively. Refer to the datasheet for information on how to fine-tune the PLL.

Wednesday, February 4, 2009

Delays

Delays are used in code to pause for a specific amount of time, usually to allow humans to read output. But they are also important when interfacing peripherals. For example, when interfacing a microcontroller to an HD44780 LCD, enough time must be given for the LCD module to complete its operation. For this purpose, delays are used to waste time. There are numerous ways to implement delay functions. For example, one may chose to use the built in delay functions in C18, impliment their own using inline assembler for greater timing precision or use the timer module. The easiest way to use delays are to use the built in delay functions in C18. All you have to do is #include to use the delay functions. However, the delay functions are not a function of time - that is, they do not accept units of time as arguments. Instead, they delay for multiples of instructions. Therefore, to create times delays (1 second, 500 milliseconds, 45 microsecodns), it is important to calculate the number of instructions based on your clock speed. In the previous post, we configured the internal clock source to its highest setting for the PIC18F2620 - 8 MHz. PIC18 chips take 4 clock cycles for each instruction. So the frequency of executed instructions is 8 MHz / 4 = 2 MHz. The instructions execute at a frequency of 2 MHz. Taking the inverse of this value, we get the period (the time each instruction takes to execute). The value is 1/2000000 s per instruction. This is equal to 0.5 microseconds per instruction. We now know that each instruction takes 0.5 microseconds (a millionth of a second). We can now easily create our delay functions. By including delays.h, you now have access to the following built-in delay functions: Nop(); //Delay one instruction cycle 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. Delay10KTCYx(unsigned char N); // Delay in multiples of 10000 instruction cycles. The above functions delay in multiples of instruction cycles. If you want to create a function to delay 1 second (1 million microseconds), you need to delay 2 million instruction cycles (since each instruction cycle takes half a microsecond at our specified clock frequency). Here's a function that delays one second: void delay1s() { Delay10KTCYx(200); //Delay 2 million instruction cycles } As you can see, it is trivial to create your own custom delay functions using the delays.h funcions provided.

Basic Port I/O

In this post, we will examine basic IO port control for the PIC18. In C18, it is very straightforward to control I/O pins of your chip.

To set the data direction, use the TRISX register like you normally would. For example, if I want to make my PORTC all output, I would use:

TRISC = 0x00; In hexadecimal notation

or

TRISC = 0b00000000; In binary notation.

Once the tristate registers have been set, you can begin to send data through the I/O pins. You can do this directly by writing to the PORTX register or control the individual bits with PORTXbits.RXn.

For example, to make pin 6 of PORTC a logic "1", you would do:

PORTCbits.RC6 = 1;

Reading the data from the ports follows the same pattern. Remember to set the TRISX variable to the right value in order to read data.

Setting Configuration Bits and Configuring Clock Speed

Pragmas are preprocessor direrectives which are used in C18 to set configuration options for your chip. They are usually set at the top of the main source file, just under the includes.

They are declared like this:

#pragma [option] = [value]

For example, my usual options are:

#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF

Declare them just under your #includes.

Another essential thing to do when developing with your chip is to define a clock source and set the frequency of oscillation. In the above configuration settings, the OSC has been declared to use the internal clock source. This is advantageous because no external circuitry is required.

The OSCCON register controls the clock frequency of the inter clock source. The following code will set it to the maximum frequency of the PIC18F2620 - 8 MHz.

OSCCON = 0b01110010;

The above code should be the first line in your main() function - more on this later.

Tuesday, February 3, 2009

PIC 18 Microcontroller Programming in C- Introduction

Introduction

I believe that the PIC18 series is the best family when diving into the world of microcontrollers. Myself, I have a PIC18F2620. It's a solid, all round, feature-rich chip with lots of flash and ram (64K, 3K). It's really easy to work with, very reliable, comes in a 28 pin DIP package and is compatible with the Microchip C18 compiler.

Assumptions

This tutorial assumes that you know basic electronics and how to wire up a breadboard or solder your components prototyping board. It is also assumed that the reader knows basic C programming and is able to understand and modify basic C code to his/her requirements.

Requirements

To get started, you'll need a PIC18 series microcontroller (such as the PIC18F2620). To program the chip, you'll need an ICSP programmer. I recommend the PICKIT2 as it is Microchip's own development programmer, is compatible with a wide range of chips, can do very easy UART communication between the PIC and PC and can even act as a mini logic analyzer. You'll also need a small development board (a breadboard or prototyping board, doesn't matter). And of course, you'll need a computer to use as a development machine. Your operating system doesn't matter, as long as you know your way around it well enough.

Setting Up The Circuit

To begin, wire up your circuit as follows:

(to do)

Then connect the PICKIT2 and load the "PICKIT2 Operating System" onto the chip.

Writing Your First Program

Will be in the next post...