Professional Documents
Culture Documents
/*
* 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);
//
//
//
//
-1-
/*
* 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;
void main() {
ADCON1 |= 0x0F;
CMCON |= 7;
TRISB0_bit = 1;
TRISC = 0x00;
PORTC = 0xAA;
oldstate = 0;
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-
/*
* 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;
TRISB = 0;
TRISC = 0;
TRISD = 0;
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
-3-
/*
* 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;
TRISB
TRISC
TRISD
PORTB
PORTC
PORTD
//
//
//
//
//
//
=
=
=
=
=
=
0x00;
0x00;
0x00;
0x00;
0x00;
0x00;
while (1) {
for (counter=0;
PORTB |= 1 <<
PORTC |= 1 <<
PORTD |= 1 <<
counter<8; counter++){
counter;
counter;
counter;
wait();
}
counter = 0;
while (counter<8)
PORTB &= ~(1 <<
PORTC &= ~(1 <<
PORTD &= ~(1 <<
{
counter);
counter);
counter);
wait();
-4-
counter++;
}
}
}
-5-
/*
* 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-
/*
* 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 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;
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
Delay_ms(40);
current_duty--;
PWM1_Set_Duty(current_duty);
}
if (RA2_bit) {
Delay_ms(40);
current_duty1++;
PWM2_Set_Duty(current_duty1);
}
// decrement current_duty
if (RA3_bit) {
Delay_ms(40);
current_duty1--;
PWM2_Set_Duty(current_duty1);
}
Delay_ms(5);
}
}
-8-
/*
* 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;
PORTA = 0;
TRISA = 255;
PWM1_Init(5000);
PWM2_Init(5000);
}
void main() {
InitMain();
current_duty1 = 127;
current_duty2 = 127;
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-
if (RA1_bit) {
current_duty1++;
PWM1_Set_Duty(current_duty1);
}
if (RA2_bit) {
current_duty2++;
PWM2_Set_Duty(current_duty2);
}
if (RA3_bit) {
current_duty2--;
PWM2_Set_Duty(current_duty2);
}
Delay_ms(5);
}
}
-10-
/*
* 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;
}
void main() {
ADCON1 |= 0x0F;
CMCON |= 0xFF;
T0CON
INTCON2
digit
= 0xC0;
= 0x84;
=
0;
-11-
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);
Delay_ms(1000);
number++ ;
if (number > 9999u)
number = 0;
// increment number
} while(1);
// endless loop
-12-
/*
* 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-
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
Extract
Extract
Extract
thousands digit
hundreds digit
tens digit
ones digit
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-
do {
//--- perform temperature reading
Ow_Reset(&PORTA, 5);
Ow_Write(&PORTA, 5, 0xCC);
Ow_Write(&PORTA, 5, 0x44);
Delay_us(120);
Ow_Reset(&PORTA, 5);
Ow_Write(&PORTA, 5, 0xCC);
Ow_Write(&PORTA, 5, 0xBE);
-15-
// Ispis na LCD
//
sbit
sbit
sbit
sbit
sbit
sbit
// 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;
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-