You are on page 1of 16

C:\Documents and Settings\Dejan Krga\Desktop\proba.

Monday, May 02, 2011 12:23 AM

/*
* Project name:
ADC_on LEDs (Display the result of ADC on Lcd display)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20080930:
- initial release;
* Description:
A simple example of using the ADC library.
ADC results are displayed on PORTB and PORTC.
* Test configuration:
MCU:
PIC18F4520
http://ww1.microchip.com/downloads/en/DeviceDoc/39762d.pdf
Dev.Board:
EasyPIC5
http://www.mikroe.com/en/tools/lv18fj/
Oscillator:
HS, 8.0000 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Turn on PORTC and PORTB LEDs.
- Select ADC module reference by placing jumper J5 (or J6) in proper position.
- To simulate analog input on ADC channel 2, use on-board potentiometer P1 (or P2)
http://www.mikroe.com/pdf/lv18fj_manual.pdf#page25
by connecting jumper J5 (or J5) to MCU pin corresponding to ADC channel 2 input.
*/
unsigned int temp_res;
void main() {
ADCON1 = 0b00001100;
TRISA = 0xFF;
TRISB = 0;
TRISC = 0;
do {
temp_res = ADC_Read(2);
PORTC = temp_res;
PORTB = temp_res >> 8;
Delay_ms (500);
} while(1);

//
//
//
//

configure RA2 pin as analog


PORTA is input
PORTB is output
PORTC is output

// Get results of AD conversion


// Send lower 8 bits to PORTC
// Send 2 most significant bits to RB1, RB0
// endless loop

-1-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
Button (Demonstration of using Button Library)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20080110:
- initial release;
* Description:
This program demonstrates usage on-board button as PORTB input.
On every RB0 one-to-zero transition PORTC is inverted.
* Test configuration:
MCU:
PIC18F4520
http://ww1.microchip.com/downloads/en/DeviceDoc/39762d.pdf
Dev.Board:
EasyPIC5
http://www.mikroe.com/en/tools/easypic/
Oscillator:
HS, 8.0000 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Turn ON the PORTC LEDs.
- Put button jumper (J1) into VCC position and pull-down PORTB. (board specific)
*/
bit oldstate;

// Old state flag

void main() {
ADCON1 |= 0x0F;
CMCON |= 7;

// Configure AN pins as digital


// Disable comparators

TRISB0_bit = 1;

// Set pin as input

TRISC = 0x00;
PORTC = 0xAA;
oldstate = 0;

// Configure PORTC as output


// Initial PORTC value

do {
if (Button(&PORTB, 0, 1, 1)) { // Detect logical one
oldstate = 1;
// Update flag
}
if (oldstate && Button(&PORTB, 0, 1, 0)) { // Detect one-to-zero transition
PORTC = ~PORTC;
// Invert PORTC
oldstate = 0;
// Update flag
}
} while(1);
// Endless loop
}

-2-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
LED_Blinking (Simple 'Hello World' project)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20081218:
- initial release;
* Description:
This is a simple 'Hello World' project. It turns on/off LEDs connected to
PORTA, PORTB, PORTC, PORTD, PORTE, PORTF, PORTG, PORTH and PORTJ.
* Test configuration:
MCU:
P18F8520
http://ww1.microchip.com/downloads/en/DeviceDoc/39609b.pdf
Dev.Board:
BIGPIC5
http://www.mikroe.com/en/tools/bigpic5/
Oscillator:
HS, 10.0 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Turn ON the PORT LEDs at SW5. (board specific)
*/
void main() {
ADCON1 |= 0x0F;
CMCON |= 7;

// Configure AN pins as digital


// Disable comparators

TRISB = 0;
TRISC = 0;
TRISD = 0;

// set direction to be output


// set direction to be output
// set direction to be output

do {
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
Delay_ms(1000);

//
//
//
//

PORTB = 0xFF;
PORTC = 0xFF;
PORTD = 0xFF;
Delay_ms(1000);
} while(1);

Turn OFF
Turn OFF
Turn OFF
1 second

LEDs on PORTB
LEDs on PORTC
LEDs on PORTD
delay

// Turn ON LEDs on PORTB


// Turn ON LEDs on PORTC
// Turn ON LEDs on PORTD
// 1 second delay
// Endless loop

-3-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
LED_Blinking (Simple 'Hello World' project)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20081218:
- initial release;
* Description:
This is a simple 'Hello World' project. It turns on/off LEDs connected to
PORTB, PORTC and PORTD.
* Test configuration:
MCU:
P18F8520
http://ww1.microchip.com/downloads/en/DeviceDoc/39609b.pdf
Dev.Board:
BIGPIC5
http://www.mikroe.com/en/tools/bigpic5/
Oscillator:
HS, 10.0 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Turn ON the PORT LEDs at SW5. (board specific)
*/
char counter;
void wait() {
Delay_ms(100);
}
void main() {
ADCON1 |= 0x0F;
CMCON |= 7;

// Configure AN pins as digital


// Disable comparators

TRISB
TRISC
TRISD
PORTB
PORTC
PORTD

//
//
//
//
//
//

=
=
=
=
=
=

0x00;
0x00;
0x00;
0x00;
0x00;
0x00;

while (1) {
for (counter=0;
PORTB |= 1 <<
PORTC |= 1 <<
PORTD |= 1 <<

set direction to be output


set direction to be output
set direction to be output
turn OFF the PORTB leds
turn OFF the PORTC leds
turn OFF the PORTD leds

counter<8; counter++){
counter;
counter;
counter;

wait();
}
counter = 0;
while (counter<8)
PORTB &= ~(1 <<
PORTC &= ~(1 <<
PORTD &= ~(1 <<

{
counter);
counter);
counter);

wait();
-4-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

counter++;
}
}
}

-5-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* this example smoothly blinks LEDs on RC1 and RC2 alternatvely
* using PIC CCP module configured as PWM output
*
* source code example for mikroC
* feel free to use this code at your own risks
*
* target : PIC16F877A, 8 Mhz crystal
* HS clock, no watchdog.
*
* easyPIC4 settings :
*
LEDs on PORTC enabled
*
* Author : Bruno Gavand, September 2007
* see more details on http://www.micro-examples.com/
*
*******************************************************************************
*/
void main()
{
unsigned char
TRISC = 0 ;
PORTC = 0 ;

dc ;
// set PORTC as output
// clear PORTC

/*
* configure CCP module as 4000 Hz PWM output
*/
PR2 = 0b01111100 ;
T2CON = 0b00000101 ;
CCP1CON = 0b00001100 ;
CCP2CON = 0b00111100 ;
for(;;)

// forever
{
/*
* PWM resolution is 10 bits
* don't use last 2 less significant bits CCPxCON,
* so only CCPRxL have to be touched to change duty cycle
*/
for(dc = 0 ; dc < 128 ; dc++)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
Delay_ms(10) ;
}
for(dc = 127 ; dc > 0 ; dc--)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
Delay_ms(10) ;
}
}

-6-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
PWM (PWM library Demonstration)
* Copyright:
(c) MikroElektronika, 2005-2008
* Description:
This is a simple demonstration of PWM library, which is being used for
control of the PIC's CCP module. The module is initialized and started,
after which the PWM4 ans PWM5 Duty Ratios can be adjusted by means of 4 buttons
connected to pins RA0, RA1, RA2 and RA3. The changes can be monitored on the CCP
output pins (RG3 and RG4).
* Test configuration:
MCU:
P18F8520
http://ww1.microchip.com/downloads/en/DeviceDoc/39609b.pdf
Dev.Board:
BIGPIC5
http://www.mikroe.com/en/tools/bigpic5/
Oscillator:
HS, 10.0 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Place jumper J11 to upper position, connect buttons to VCC . (board specific)
- Turn on LEDs on PORTG.6 (board specific)
*/
unsigned short current_duty, old_duty, current_duty1, old_duty1;
void InitMain() {
ADCON1 |= 0x0F;
CMCON |= 7;
PORTA = 0;
TRISA = 255;
PORTC = 0;
TRISC = 0;
PWM1_Init(5000);
PWM2_Init(5000);

// Configure AN pins as digital


// Disable comparators

//
//
//
//
//

configure PORTA
set PORTG to 0
designate PORTG
Initialize PWM4
Initialize PWM5

pins as input
pins as output
module at 5KHz
module at 5KHz

}
void main() {
InitMain();
current_duty = 16;
current_duty1 = 16;

// initial value for current_duty


// initial value for current_duty1

PWM1_Start();
PWM2_Start();
PWM1_Set_Duty(current_duty);
PWM2_Set_Duty(current_duty1);

//
//
//
//

while (1) {
if (RA0_bit) {
Delay_ms(40);
current_duty++;
PWM1_Set_Duty(current_duty);
}

// endless loop
// button on RA0 pressed

if (RA1_bit) {

start PWM4
start PWM5
Set current duty for PWM4
Set current duty for PWM5

// increment current_duty

// button on RA1 pressed


-7-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Delay_ms(40);
current_duty--;
PWM1_Set_Duty(current_duty);
}
if (RA2_bit) {
Delay_ms(40);
current_duty1++;
PWM2_Set_Duty(current_duty1);
}

Monday, May 02, 2011 12:23 AM

// decrement current_duty

// button on RA2 pressed


// increment current_duty1

if (RA3_bit) {
Delay_ms(40);
current_duty1--;
PWM2_Set_Duty(current_duty1);
}

// button on RA3 pressed


// decrement current_duty1

Delay_ms(5);

// slow down change pace a little

}
}

-8-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
PWM (PWM library Demonstration)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20081218:
- initial release;
* Description:
This is a simple demonstration of PWM library, which is being used for
control of the MCU's PWM module. The module is initialized and started,
after which the PWM1 ans PWM2 Duty Ratios can be adjusted by means of 4 buttons
connected to pins RA0, RA1, RA2 and RA3. The changes can be monitored
on the PWM output pins.
* Test configuration:
MCU:
PIC18F87J60
http://ww1.microchip.com/downloads/en/DeviceDoc/39762d.pdf
Dev.Board:
LV18FJ
http://www.mikroe.com/en/tools/lv18fj/
Oscillator:
HS, 25.0000 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Connect button jumper (J1) to Vcc
and pull-down PORTA (place jumper J15 in lower position). (board specific)
- Turn on LEDs on PORTC switch SW2.3. (board specific)
*/
unsigned short current_duty1, old_duty1, current_duty2, old_duty2;
void InitMain() {
ADCON1 |= 0x0F;
CMCON |= 7;

// Configure AN pins as digital


// Disable comparators

PORTA = 0;
TRISA = 255;
PWM1_Init(5000);
PWM2_Init(5000);

// configure PORTA pins as input


// Initialize PWM1 module at 5KHz
// Initialize PWM2 module at 5KHz

}
void main() {
InitMain();
current_duty1 = 127;
current_duty2 = 127;

// initial value for current_duty1


// initial value for current_duty2

PWM1_Start();
PWM2_Start();
PWM1_Set_Duty(current_duty1);
PWM2_Set_Duty(current_duty2);

//
//
//
//

start PWM1
start PWM2
Set current duty for PWM1
Set current duty for PWM2

while (1) {
if (RA0_bit) {
current_duty1--;
PWM1_Set_Duty(current_duty1);
}

// endless loop
// button on RA0 pressed
// decrement current_duty1

-9-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

if (RA1_bit) {
current_duty1++;
PWM1_Set_Duty(current_duty1);
}

// button on RA1 pressed


// increment current_duty1

if (RA2_bit) {
current_duty2++;
PWM2_Set_Duty(current_duty2);
}

// button on RA2 pressed


// decrement current_duty2

if (RA3_bit) {
current_duty2--;
PWM2_Set_Duty(current_duty2);
}

// button on RA3 pressed


// increment current_duty2

Delay_ms(5);

// slow down change pace a little

}
}

-10-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
Display7seg (Advanced 7Seg. display example)
* Copyright:
(c) MikroElektronika, 2005-2008.
* Description:
This code demonstrates displaying number on four 7-segment display (common
cathode), http://www.mikroe.com/pdf/easypic5/easypic5_manual.pdf#page22
in multiplex mode. All 7-segment displays are connected to PORTD
(RD0..RD7, segment A to RD0, segment B to RD1, etc) with refresh via pins
RA0..RA3 on PORTA. Number is on for 1 second.
* Test configuration:
MCU:
PIC16F887
http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
Dev.Board:
EasyPIC5
http://www.mikroe.com/en/tools/easypic5/
Oscillator:
HS, 08.0000 MHz
Ext. Modules:
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Turn on 7seg displays on (SW6.5, SW6.6, SW6.7, SW6.8 ) (board specific)
*/
#include "Display_utils.h"
unsigned short shifter, portd_index;
unsigned int
digit, number;
unsigned short portd_array[4];
void interrupt() {
PORTA = 0;
PORTD = portd_array[portd_index];
PORTA = shifter;

// Turn off all 7seg displays


// bring appropriate value to PORTD
// turn on appropriate 7seg. display

// move shifter to next digit


shifter <<= 1;
if (shifter > 8u)
shifter = 1;
// increment portd_index
portd_index++ ;
if (portd_index > 3u)
portd_index = 0;
TMR0L
=
0;
TMR0H
=
0;
INTCON = 0x20;

// turn on 1st, turn off 2nd 7seg.


// reset TIMER0 value
// Clear T0IF

}
void main() {
ADCON1 |= 0x0F;
CMCON |= 0xFF;
T0CON
INTCON2
digit

= 0xC0;
= 0x84;
=
0;

// Set AN pins to Digital I/O


// Disable comparators
// Timer0 settings

-11-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

portd_index
shifter
TMR0L
TMR0H
INTCON
PORTA
TRISA
PORTD
TRISD

=
0;
=
1;
=
0;
=
0;
= 0xA0;
=
0;
=
0;
=
0;
=
0;

number
=
6789;
do {
digit = number / 1000u ;
portd_array[3] = mask(digit);
digit = (number / 100u) % 10u;
portd_array[2] = mask(digit);
digit = (number / 10u) % 10u;
portd_array[1] = mask(digit);
digit = number % 10u;
portd_array[0] = mask(digit);

Monday, May 02, 2011 12:23 AM

// Enable GIE, T0IE


// Set PORTA as output
// Set PORTD as output
// some initial value
//
//
//
//
//
//
//
//

extract thousands digit


and store it to PORTD array
extract hundreds digit
and store it to PORTD array
extract tens digit
and store it to PORTD array
extract ones digit
and store it to PORTD array

Delay_ms(1000);

// one second delay

number++ ;
if (number > 9999u)
number = 0;

// increment number

} while(1);

// endless loop

-12-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

/*
* Project name:
OneWire (Interfacing the DS1820 temperature sensor - all versions)
* Copyright:
(c) Mikroelektronika, 2008.
* Revision History:
20080930:
- initial release;
* Description:
This code demonstrates one-wire communication with temperature sensor
DS18x20 connected to RA5 or RE2 pin.
MCU reads temperature from the sensor and prints it on the LCD.
The display format of the temperature is 'xxx.xxxxC'. To obtain correct
results, the 18x20's temperature resolution has to be adjusted (constant
TEMP_RESOLUTION).
* Test configuration:
MCU:
PIC18F87J60
http://ww1.microchip.com/downloads/en/DeviceDoc/39762d.pdf
Dev.Board:
LV18FJ
http://www.mikroe.com/en/tools/lv18fj/
Oscillator:
HS, 25.0000 MHz
Ext. Modules:
DS18x20, LCD 2x16
http://www.mikroe.com/pdf/lv18fj_manual.pdf#page24
SW:
mikroC PRO for PIC
http://www.mikroe.com/en/compilers/mikroc/pro/pic/
* NOTES:
- Place DS1820 jumper (J2) in left position to use RD0 as OW pin (board specific).
- Turn on LCD backlight switch SW3.7 (board specific).
- Pull up (place jumper j18 in upper position) (board specific)
and turning off diode on pin (SW2.4) (board specific)
used for one wire bus may be required.
*/
// LCD module connections:
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;
char *text = "000.0000";
-13-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

unsigned temp;
void Display_Temperature(unsigned int temp2write) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;
// check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
}
// extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;
// convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
text[0] = '0';
text[1] = (temp_whole/10)%10 + 48;
text[2] = temp_whole%10
+ 48;

// Extract tens digit


// Extract ones digit

// extract temp_fraction and convert it to unsigned int


temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// convert temp_fraction to characters
text[4] = temp_fraction/1000
+ 48;
text[5] = (temp_fraction/100)%10 + 48;
text[6] = (temp_fraction/10)%10 + 48;
text[7] = temp_fraction%10
+ 48;

//
//
//
//

Extract
Extract
Extract
Extract

thousands digit
hundreds digit
tens digit
ones digit

// print temperature on LCD


Lcd_Out(2, 5, text);
}
void main() {
TRISB = 0;
ADCON1 |= 0x0F;
CMCON |= 7;
PORTB = 0x00;

// Configure AN pins as digital


// Disable comparators

Lcd_Init();
// Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
// Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF);
// Turn cursor off
Lcd_Out(1, 1, " Temperature:
");
// Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223); // different LCD displays have different char code for degree
// if you see greek alpha letter try typing 178 instead of 223
Lcd_Chr(2,14,'C');
//--- main loop
-14-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

do {
//--- perform temperature reading
Ow_Reset(&PORTA, 5);
Ow_Write(&PORTA, 5, 0xCC);
Ow_Write(&PORTA, 5, 0x44);
Delay_us(120);

// Onewire reset signal


// Issue command SKIP_ROM
// Issue command CONVERT_T

Ow_Reset(&PORTA, 5);
Ow_Write(&PORTA, 5, 0xCC);
Ow_Write(&PORTA, 5, 0xBE);

// Issue command SKIP_ROM


// Issue command READ_SCRATCHPAD

temp = Ow_Read(&PORTA, 5);


temp = (Ow_Read(&PORTA, 5) << 8) + temp;
//--- Format and display result on Lcd
Display_Temperature(temp);
Delay_ms(2000);
} while (1);
}

-15-

C:\Documents and Settings\Dejan Krga\Desktop\proba.c

Monday, May 02, 2011 12:23 AM

// Ispis na LCD
//
sbit
sbit
sbit
sbit
sbit
sbit

Lcd pinout settings


LCD_RS at RB4_bit;
LCD_EN at RB5_bit;
LCD_D4 at RB0_bit;
LCD_D5 at RB1_bit;
LCD_D6 at RB2_bit;
LCD_D7 at RB3_bit;

// Pin direction
sbit LCD_RS_Direction
sbit LCD_EN_Direction
sbit LCD_D4_Direction
sbit LCD_D5_Direction
sbit LCD_D6_Direction
sbit LCD_D7_Direction
char
char
char
char

txt1[]
txt2[]
txt3[]
txt4[]

int

i;

=
=
=
=

at
at
at
at
at
at

TRISB4_bit;
TRISB5_bit;
TRISB0_bit;
TRISB1_bit;
TRISB2_bit;
TRISB3_bit;

"Hvala Bogu!!!";
"nakon mnogo muke";
"Dejan Krga";
"upalio displej";

void main()
{
TRISB = 0;
ADCON1 |= 0x0F;
CMCON |= 0xFF;
PORTB = 0x00;

// Configure AN pins as digital


// Disable comparators

Lcd_Init();
for(i=1;;)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,3,txt3);
Lcd_Out(2,1,txt2);
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,txt4);
Lcd_Out(2,4,txt1);
Delay_ms(2000);
}
}

-16-