Академический Документы
Профессиональный Документы
Культура Документы
MANUA
L
FOR PIC16 DEVELOPMENT BOARD
I.
II.
c. Click NEXT
f.
//MAIN;
{
TRISB = 0x00;
PORTB = 0x00;
while(1){
PORTB = ~PORTB;
Delay_ms(1000);
//WAIT 1S;
}
}
//END;
EXAMPLE NO. 1
Blinking LED
Experimental Setup:
In this experiment we will show how to set MCU to
make a LED blinking. PORTB will be used to show that.
8 | PIC Development Board with Module
EXAMPLE NO. 2
Experimental Setup:
The first experiment that we are going to do with
our PIC16F628A board is a 4-bit binary counter that
counts from 0(00h) to 15(0Fh) with 1sec delay between
each count. The output will be at RB.0 through RB.3 and
will be displayed on 4 LEDs. Use four jumper wires to
connect RB.0 through RB.3 to LEDs.
Software:
Here is the C program
void main() {
TRISB = 0x00;
// set direction to be output
PORTB = 0x00;
// Turn OFF LEDs on PORTB
do {
Delay_ms(1000);
// 1 second delay
PORTB ++ ;
} while(PORTB < 0x0F);
// Till PORTB < 15
}
10 | P I C D e v e l o p m e n t B o a r d w i t h
Module
EXAMPLE NO.3
Software:
We will use in-built 'Button Library' to detect push button
press.
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
unsigned int digit;
// To Hold Decimal Value
unsigned short number;
// To Hold Equivalent Seven
Segment Value
void main() {
CMCON |= 7;
TRISB = 0x00;
PORTB = 0x00;
TRISA0_bit = 1;
TRISA1_bit = 1;
// Disable Comparators
// Set PORTB direction to be output
// Turn OFF LEDs on PORTB
// PA.0 Input for Increment
// PA.1 Input for Decrement
PORTB = number;
}
} while(1);
}
EXAMPLE NO. 4
// Update flag
// endless loop
Hardware setup:
`
In this experiment we will work with alphanumeric
LCD and push button. Communication with LCD will be
performed through 4-bits and connections is made as
follows: D4 with RB0, D5 with RB1, D6 with RB2, D7 with
RB3, RS with RB4 and EN with RB5. The Push button is
connected to PORT RA4 (for increment) and PORT RA6 (for
decrement). Of course both button have pull-up resistors
(4k7).To make this project more interesting , you can
reach from 0000 to 9999 by pressing the button.
Software:
Here is the C program
/********************************************************************
' Description:
'
In this experiment we will work with alphanumeric LCD and push
button.
14 | P I C D e v e l o p m e n t B o a r d w i t h
Module
'
Communication with LCD will be performed through 4-bits and
connections
'
is made as follows: D4 with RB0, D5 with RB1, D6 with RB2, D7
with RB3;
'
RS with RB4 and EN with RB5.
'
The Push button is connected to PORT RA4 (for increment) and
PORT RA6
'
(for decrement). Of course both button have pull-up resistors
(4k7).
'
To make this project more interesting , you can reach from 0000
to 9999
'
by pressing the button.
' Test configuration:
' MCU:
PIC16F628A
' Configuration Word
' Oscillator:
INTOSC:I/O on RA.6, I/O on RA.7
' Watchdog Timer:
OFF
' Power up Timer:
Disabled
' Master Clear Enable:
Enabled
' Browun Out Detect:
Enabled
' Low Voltage Program:
Disabled
' Data EE Read Protect:
Disabled
' Code Protect:
OFF
'***********************************************************************
********
*/
// LCD module connections
sbit LCD_RS at RB4_bit;
// LCD_RS assigned to PORT RB4;
sbit LCD_EN at RB5_bit;
// LCD_EN assigned to PORT RB5;
sbit LCD_D4 at RB0_bit;
// LCD_D4 assigned to PORT RB0;
sbit LCD_D5 at RB1_bit;
// LCD_D5 assigned to PORT RB1;
sbit LCD_D6 at RB2_bit;
// LCD_D6 assigned to PORT RB2;
sbit LCD_D7 at RB3_bit;
// LCD_D7 assigned to PORT RB3;
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
15 | P I C D e v e l o p m e n t B o a r d w i t h
Module
// main;
// turn off analogue comparator and
// make PORT RA6 as input for button;
// make PORT RA7 as input for button;
// Turn ON PORTA;
// Set PORTB direction to be output;
// Turn ON PORTB;
Lcd_init();
// LCD Initialization;
Lcd_cmd(_LCD_CLEAR);
// Clear LCD;
Lcd_cmd(_LCD_CURSOR_OFF);
// Cursor mode, off;
Lcd_out(1,2,Message1);
// display message1 from column 1,
character 3;
do{
if(Button(&PORTA,4,1,0)){
Delay_ms(200); // If button is pressed, delay 0,2s
and increment "number" with 1;
number = number +1;
}
if(Button(&PORTA,6,1,0)){
Delay_ms(200); // If button is pressed, delay 0,2s
and decrement "number" with 1;
number = number -1;
}
if (number > 9999u)
// if it's more than 9999 go to 0;
number = 0;
display_init();
// call display_init();
} while(1);
// infinite loop;
}
// end.
16 | P I C D e v e l o p m e n t B o a r d w i t h
Module
EXAMPLE NO. 5
EXAMPLE NO. 6
18 | P I C D e v e l o p m e n t B o a r d w i t h
Module
19 | P I C D e v e l o p m e n t B o a r d w i t h
Module
* Test configuration:
MCU:
PIC16F628A
The common cathode of four seven segment dispalys are
connected to RA0, RA1, RA2 and RA3
*/
//-------------- Function to Return mask for common cathode 7-seg.
display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
unsigned short i, DD0, DD1, DD2, DD3, NUM ;
void main() {
CMCON |= 7;
// Disable Comparators
TRISB = 0x00; // Set PORTB direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA0_bit = 0; // RA.0 to RA3 Output
TRISA1_bit = 0;
TRISA2_bit = 0;
TRISA3_bit = 0;
TRISA4_bit = 1;
// RA.4 is Input only
NUM =
do {
DD0 =
DD0 =
DD1 =
DD1 =
DD2 =
DD2 =
DD3 =
DD3 =
20 | P I C D e v e l o p m e n t B o a r d w i t h
Module
RA2_bit = 0;
RA3_bit = 0;
delay_ms(5);
PORTB = DD1;
RA0_bit = 0;
RA1_bit = 1;
RA2_bit = 0;
RA3_bit = 0;
delay_ms(5);
PORTB = DD2;
RA0_bit = 0;
RA1_bit = 0;
RA2_bit = 1;
RA3_bit = 0;
delay_ms(5);
PORTB = DD3;
RA0_bit = 0;
RA1_bit = 0;
RA2_bit = 0;
RA3_bit = 1;
delay_ms(5);
}
NUM = NUM + 1
} while(1);
}
21 | P I C D e v e l o p m e n t B o a r d w i t h
Module
EXAMPLE NO. 7
DS18B20 using 1-Wire Protocol & 7segment
/*
'
Digital thermometer with DS18B20 and 7- Segments.
' Description:
22 | P I C D e v e l o p m e n t B o a r d w i t h
Module
'
In this experiment we will work with one-wire communication.
'
The thermal sensor used is "DS18B20" and measured value is
displayed
'
on the 7-segment digits. PORTB will be used for character
'
(RB0=a,RB1=b...RB6=g,RB7=dp)and for digits
RA0=digit1...RA3=digit4.
'
Data wire from DS18B20 is conected to RA4.
' Test configuration:
' MCU:
PIC16F628A
' Test.Board:
WB-106 Breadboard 2420 dots
' SW:
MikroC PRO for PIC 2010 (version v4.15)
' Configuration Word
' Oscillator:
INTOSC:I/O on RA.6, I/O on RA.7
' Watchdog Timer:
OFF
' Power up Timer:
Disabled
' Master Clear Enable:
Enabled
' Browun Out Detect:
Enabled
' Low Voltage Program:
Disabled
' Data EE Read Protect:
Disabled
' Code Protect:
OFF
'***********************************************************************
********
*/
unsigned short i, DD0=0x40, DD1=0x40,DD2=0x40, DD3 =0x61,
N_Flag;
unsigned temp_value=0;
// Variable to store
temperature register value
unsigned short mask(unsigned short num)
// Mask for 7 segment
common cathode;
{
switch (num)
{
case 0 : return 0x3F;
// 0;
case 1 : return 0x06;
// 1;
case 2 : return 0x5B;
// 2;
case 3 : return 0x4F;
// 3;
case 4 : return 0x66;
// 4;
case 5 : return 0x6D;
// 5;
case 6 : return 0x7D;
// 6;
case 7 : return 0x07;
// 7;
case 8 : return 0x7F;
// 8;
case 9 : return 0x6F;
// 9;
case 10 : return 0x40;
// Symbol '-'
case 11 : return 0x61;
// Symbol C
case 12 : return 0x00;
// Blank
} //case end
}
void display_temp(short DD0, short DD1, short DD2, short DD3)
23 | P I C D e v e l o p m e n t B o a r d w i t h
Module
{
for (i = 0; i<=4; i++)
{
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 0;
RA2_bit = 0;
RA3_bit = 0;
delay_ms(2);
PORTB = DD0;
RA0_bit = 0;
RA1_bit = 1;
RA2_bit = 0;
RA3_bit = 0;
delay_ms(2);
PORTB = DD1;
RA0_bit = 0;
RA1_bit = 0;
RA2_bit = 1;
RA3_bit = 0;
delay_ms(2);
PORTB = DD2;
RA0_bit = 0;
RA1_bit = 0;
RA2_bit = 0 ;
RA3_bit = 1;
delay_ms(2);
}return;
}
// Select C Digit;
void DS18B20()
//Perform temperature reading
{
Display_temp(DD0, DD1, DD2, DD3);
Ow_Reset(&PORTA, 4);
// Onewire reset signal
Ow_Write(&PORTA, 4, 0xCC);
// Issue command SKIP_ROM
Ow_Write(&PORTA, 4, 0x44);
// Issue command CONVERT_T
Display_temp(DD0, DD1, DD2, DD3);
Ow_Reset(&PORTA, 4);
Ow_Write(&PORTA, 4, 0xCC);
// Issue command SKIP_ROM
Ow_Write(&PORTA, 4, 0xBE);
// Issue command
READ_SCRATCHPAD
Display_temp(DD0, DD1, DD2, DD3);
// Next Read Temperature
temp_value = Ow_Read(&PORTA, 4);
// Read Byte 0 from
Scratchpad
temp_value = (Ow_Read(&PORTA, 4) << 8) + temp_value;
// Then
read Byte 1 from
// Scratchpad and shift
// 8 bit left and add the Byte 0
24 | P I C D e v e l o p m e n t B o a r d w i t h
Module
// Disable Comparators
// Set PORTB direction to be output
// Turn OFF LEDs on PORTB
// RA.0 to RA3 Output
do {
//--- main loop
N_Flag = 0;
// Reset Temp Flag
DS18B20();
DD0 = temp_value%10;
// Extract Ones Digit
DD0 = mask(DD0);
DD1 = (temp_value/10)%10;
// Extract Tens Digit
DD1 = mask(DD1);
DD2 = temp_value/100;
// Extract Hundred digit
if (N_Flag == 1) DD2=0x0A;
// DD2 10 ??
else if (DD2 == 0) DD2 = 0x0D;
// DD2 13 ??
DD2 = mask(DD2);
display_temp(DD0, DD1, DD2, DD3);
// Infinite loop;
} while (1);
}
25 | P I C D e v e l o p m e n t B o a r d w i t h
Module
EXAMPLE NO. 8
Reading Temperature Values from DS1820 using 1Wire Protocol
In this experiment, we are going to build a digital temperature
meter using DS1820 connected to our PIC16F628A
development board. The temperature value will be displayed on
the LCD display. I have modified the sample program that
comes with the compiler according to our PIC board
requirements. Also I have elaborated comments in the program
so that every step will be more clear to the readers.
Experimental Setup:
The experimental setup is very straight-forward. Place DS1820
device on the three-pin female header that we recently added
to our board. And also connect the data pin of DS1820 to RB.0
pin of PIC16F628A using a jumper wire.
26 | P I C D e v e l o p m e n t B o a r d w i t h
Module
Software:
Here is the program written in microC that reads temperature values
from DS1820 device using OneWire Library.
/* Project name:
One Wire Communication Test between PIC16F628A and DS1820
* Description:
This code demonstrates how to use One Wire Communication
Protocol
between PIC16F628A and a 1-wire peripheral device. The peripheral
device used here is DS1820, digital temperature sensor.
MCU:
PIC16F628A
Oscillator:
XT, 4.0 MHz
*/
// LCD connections definitions
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
27 | P I C D e v e l o p m e n t B o a r d w i t h
Module
28 | P I C D e v e l o p m e n t B o a r d w i t h
Module
29 | P I C D e v e l o p m e n t B o a r d w i t h
Module
} while (1);
}
EXAMPLE NO. 9
31 | P I C D e v e l o p m e n t B o a r d w i t h
Module
32 | P I C D e v e l o p m e n t B o a r d w i t h
Module
EXAMPLE NO. 10
Read/Write Internal EEPROM Memory
An EEPROM (Electrically-Erasable Programmable
ROM) data memory is one of the important features of
flash-based PIC microcontrollers. It is called non-volatile to
indicate that it retains the data even when the power is
down. Practically speaking, if you want to design a digital
lock system, then the password to unlock the system can
be saved into the EEPROM, so that when the power is
down, the password will still be saved. And other good
thing is that the data can be easily modified or
overwritten with software control. In this experiment, we
are going to show you how to read and write in to the
internal EEPROM memory of PIC16F628A using mikroC
EEPROM library functions.
Here is what we are going to do:
We will write 0s to 10 EEPROM locations. We will
read them first, then write 0-9 to these locations, and turn
the power off. We will turn the power on, and read the
data in those locations and see. We have created a simple
menu on LCD with Read, Write and Delete functions.
Experimental Setup:
Connect the three push buttons on the board to
RB.0, RB.1, and RB.2, and plug-in the LCD module.
Software:
/*
Project Name: Read/Write Internal EEPROM
* Description:
This code is an example of accessing internal EEPROM.
* Test configuration:
MCU:
PIC16F628A
33 | P I C D e v e l o p m e n t B o a r d w i t h
Module
Oscillator:
*/
// LCD module connections
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
// Define Messages
char message1[] = "1.READ";
char message2[] = "2.WRITE";
char message3[] = "3.Delete";
char message4[] = "WRITE COMPLETED";
char message5[] = "Read Data";
char message6[] = "Data Deleted";
char digits[] = "0000000000";
unsigned short i, NUM ;
unsigned int ADD = 0x00, temp; // Start EEPROM Location
void main() {
CMCON |= 7;
// Disable Comparators
TRISB = 0x0F;
PORTB = 0x00;
Lcd_Init();
start:
Lcd_Cmd(_LCD_CLEAR);
// Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
// Cursor off
Lcd_Out(1,1,message1);
// Write message1 in 1st row
Lcd_Out(1,8,message2);
Lcd_Out(2,1,message3);
do {
// Read Operation
if (Button(&PORTB, 0, 1, 0)) { // Detect logical one to zero
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message5);
for (i=0; i<=9; i++) {
temp = ADD+i;
34 | P I C D e v e l o p m e n t B o a r d w i t h
Module
NUM = EEPROM_Read(temp);
digits[i] = NUM+48;
}
Lcd_Out(2,1,digits);
delay_ms(3000);
goto start;
}
// Write Operation
if (Button(&PORTB, 1, 1, 0)) { // Detect logical one to zero
Delay_ms(300);
for (i=0; i<10; i++) {
temp = ADD + i;
EEPROM_Write(temp,i);
}
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message4);
delay_ms(2000);
goto start;
}
// Delete Operation
if (Button(&PORTB, 2, 1, 0)) { // Detect logical one to zero
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,message6);
for (i=0; i<=9; i++) {
temp = ADD+i;
EEPROM_Write(temp, 0);
}
delay_ms(2000) ;
goto start;
}
} while(1);
}
35 | P I C D e v e l o p m e n t B o a r d w i t h
Module