user manua1

42
USER MANUAL FOR PIC16 DEVELOPMENT BOARD

Upload: ishimaru-thor

Post on 25-Dec-2015

213 views

Category:

Documents


0 download

DESCRIPTION

PIC16F628A DEVELOPMENT BOARD USER MANUAL

TRANSCRIPT

Page 1: User Manua1

USER MANUAL

FOR PIC16 DEVELOPMENT BOARD

Page 2: User Manua1

I. Getting to know the PIC16 Development board

PIC16F628A has an 18-pin with two (2) ports, PORT A and PORT B.In each code you will make, the first choice you would be thinking is what PORT are you going to use for input and output. PORTA = 0x00; // turn all outputs offPORTB = 0x00; // turn all outputs off

From the code above, it just shows that PORT A is closed for all pins same as port B.

2 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 3: User Manua1

II. Programming using K150 Pic Programmer

Since for a PIC to work with your desired output, you need to program it, so here is the step by step procedure using MikroC and K150 PIC Programmer.

a. Open MikroC and click File, under New and New Project (Ctrl+Shift+N), then a new pop-up window will open llike this:

b. Click Next then Create a New Project Name, Select Project Folder (recommended is to create a new folder for every project), Choose the correct Device Name (PIC16F628A),and Device Clock as 8.0MHz (PIC16F628A has already 4MHZ internal and the system has additional 4 MHz crystal for accuracy).

3 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 4: User Manua1

c. Click NEXT

d. Make sure Include All is SELECED

4 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 5: User Manua1

e. CHECK configuration bits

f. Under Oscillator Selection, choose XT oscillator (although PIC16F628A has an internal oscillator of 4MHz, an additional 4MHz crystal resonator has been added for the accuracy). Make sure Power-Up Timer, RA5, and Brown-Out are ENABLED. Click Ok.

g. This time, the command window will open, Since we will be doing the LED_Blinking, here is the C program code we had used,

void main() //MAIN;

{

TRISB = 0x00; //SET PORTB TO BE OUTPUT;

5 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 6: User Manua1

PORTB = 0x00; //TURN OFF LEDs ON PORTB;

while(1){ //INFINITE LOOP;

PORTB = ~PORTB; //INVERT STATE ON PORTB;

Delay_ms(1000); //WAIT 1S;

}

} //END;

After inputting the code to the command window, go to Build Tab, then select Build. (Ctrl +F9).

h. You can open the folder you have created a while ago, and the .hex file is already there.

i. Connect the ICSP on the K150 PIC Programmer, if you are not sure follow this image (you can look at the back of the K150 and the NC- there is the Not Connect, then follow this image respectively.)

j. Go to device manager and look for Com Port

k. On the K150 programmer, go to File then Port, type

the number shown in your device manager, mine is

4. Hit Ok! From that it will show that your K150

board is connected.

6 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 7: User Manua1

l. Erase any file first, tap Blank, Erase Chip then

Ok.

m. For programming, (make sure Chip Selector is in

PIC16F628A), then go to Options -> ICSP Mode.

n. Click LOAD and look for you .hex file then tap

FUSES, hit OK, then PROGRAM and AFTER that VERIFY.

Then the PROGRAMMING IS COMPLETE.

7 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 8: User Manua1

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. Button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode the microcontroller. ICSP connector provides the connection between the programmer and microcontroller (PIC16F628A).

Software:

Here is the C program written for MikroC PRO for PIC.void main() //MAIN;

{ TRISB = 0x00; //SET PORTB TO BE OUTPUT; PORTB = 0x00; //TURN OFF LEDs ON PORTB; while(1){ //INFINITE LOOP; PORTB = ~PORTB; //INVERT STATE ON PORTB; Delay_ms(1000); //WAIT 1S; }} //END;

8 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 9: User Manua1

EXAMPLE NO. 2

FOUR-BIT BINARY COUNTER

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}

9 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 10: User Manua1

EXAMPLE NO.3

Push Button and Seven Segment Display Interface

In this experiment, we will program the PIC16F628A as an UP/DOWN Decade Counter. The count value will be displayed on a Seven-Segment Display and will be incremented/decremented by two push buttons on the board.

Experimental Setup:

The board has built in interface for a multiplexed 4-digit seven segment display. We will select only one digit by connecting a Digit Select pin to Vcc, as shown in figure below. A black jumper wire is used for this purpose. The seven segments will be driven through PORTB (already wired on the board). Connect Push Buttons (PB3 and PB4) to RA1 and RA0 female headers using jumper wires.

Software:

10 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 11: User Manua1

We will use in-built 'Button Library' to detect push button press.

Here is the complete C program

/** Project name: UP/DOWN Decimal Counter with Push Button and 7-Segment Interface * Description: This code is an example of Seven Segment Display and Push Button interface. A decimal counter value will be displayed on the seven segment display. The value of the counter can be incremented or decremented through push buttons. * Test configuration: MCU: PIC16F628A The two push buttons are connected to RA0(Increment) and RA1(Decrement) and the seven segment display connected to PORTB (Segment a to PB.0, Segment b to PB.1 and so on)*///-------------- Function to Return mask for common cathode 7-seg. displayunsigned short mask(unsigned short num) { switch (num) { case 0 : return 0x3F;

11 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 12: User Manua1

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 Valueunsigned short number; // To Hold Equivalent Seven Segment Value

void main() { CMCON |= 7; // Disable Comparators TRISB = 0x00; // Set PORTB direction to be output PORTB = 0x00; // Turn OFF LEDs on PORTB TRISA0_bit = 1; // PA.0 Input for Increment TRISA1_bit = 1; // PA.1 Input for Decrement

digit = 0; // Initial Value of Counter number = mask(digit) ; PORTB = number; do { if (Button(&PORTA, 0, 1, 0)) { // Detect logical one to zero Delay_ms(300); digit ++; // Increase Counter number = mask(digit) ; PORTB = number; } if (Button(&PORTA, 1, 1, 0)) { // Detect logical one to zero Delay_ms(300) ; digit = digit-1; // Decrease Counter number = mask(digit) ; PORTB = number; // Update flag } } while(1); // endless loop}

12 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 13: User Manua1

EXAMPLE NO. 4

Alphanumeric LCD and Push Button

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.' 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:

13 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 14: User Manua1

' 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 connectionssbit 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; // LCD_RS assigned to TRIS B4;sbit LCD_EN_Direction at TRISB5_bit; // LCD_EN assigned to TRIS B5;sbit LCD_D4_Direction at TRISB0_bit; // LCD_D4 assigned to TRIS B0;sbit LCD_D5_Direction at TRISB1_bit; // LCD_D5 assigned to TRIS B1;sbit LCD_D6_Direction at TRISB2_bit; // LCD_D6 assigned to TRIS B2;sbit LCD_D7_Direction at TRISB3_bit; // LCD_D7 assigned to TRIS B3;// End LCD module connections

char Message1[]="LCD and Button"; // Message for line1;unsigned int number = 0;

char *digit = "0000";

void Display_init() // define display_init;{ digit[0] = number/1000 + 48; // thousands digit; digit[1] = (number/100)%10 +48; // hundreds digit; digit[2] = (number/10)%10 + 48; // tens digit; digit[3] = number%10 +48; // unit digit; Lcd_Out(2,7,digit); // display on LCD from column 2, character 7;}

void main() // main;{ CMCON |= 7; // turn off analogue comparator and make PORTA to digital I/O; TRISA6_bit = 1; // make PORT RA6 as input for 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 M o d u l e

Page 15: User Manua1

TRISA7_bit = 1; // make PORT RA7 as input for button; PORTA = 0; // Turn ON PORTA; TRISB = 0; // Set PORTB direction to be output; PORTB = 0; // 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.

15 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 16: User Manua1

EXAMPLE NO. 5

LCD Interface in 4-bit Mode

The objective of this experiment is to interface a 16x2 LCD to PIC16F628A in 4-bit mode. This means the data transfer will use only four pins of the microcontroller. There is no additional hardware setup needed for this experiment, as we have a ready-made LCD interface female header. We only need to define the data transfer and control pins in the software. Remember, the LCD interface in our development board uses the following pins of PIC16F628A:

Data Transfer : D4 -> RB4, D5 -> RB5, D6 -> RB6, D7 -> RB7

RS -> RA0, and EN -> RA1

,ii

16 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 17: User Manua1

EXAMPLE NO. 6

Multiplexed 7Segment as counter mode

In this experiment, we are going to learn how to interface more than one 7-segment LED display to a PIC Port using multiplexing technique. We are going to interface a 4-digit common cathode seven segment display to our PIC board. The multiplexing circuit is already built up in the board using 4 transistors and few resistors. The basic idea of multiplexing is that all seven segment displays are connected to the microcontroller in parallel and the microcontroller alternately prints ones, tens, hundreds, and thousands digits, selecting one at a time. The switching among the digits is so fast that it gives an impression of simultaneous light emission.

Experimental Setup:Connect RA0 through RA3 to 7-Segment Digit Select headers DG1, DG2, DG3, and DG4 using jumper wires.

Insert 7FR5641AS 4-Digit Seven Segment module in to its place on the board.Software:/*

17 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 18: User Manua1

* Project name: 4-Digit UP Counter with Four 7-Segment Display Multiplexing * Description: This code is an example of multiplexing 4 Seven Segment Displays.

* 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. displayunsigned 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 = 0; // Initial Value of Counter

do { DD0 = NUM%10; // Extract Ones Digit DD0 = mask(DD0); DD1 = (NUM/10)%10; // Extract Tens Digit DD1 = mask(DD1); DD2 = (NUM/100)%10; // Extract Hundreds Digit DD2 = mask(DD2); DD3 = (NUM/1000); // Extract Thousands Digit DD3 = mask(DD3);

18 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 19: User Manua1

for (i = 0; i<=50; i++) { PORTB = DD0; RA0_bit = 1; // Select Ones Digit RA1_bit = 0; RA2_bit = 0; RA3_bit = 0; delay_ms(5); PORTB = DD1; RA0_bit = 0; RA1_bit = 1; // Select Tens Digit RA2_bit = 0; RA3_bit = 0; delay_ms(5); PORTB = DD2; RA0_bit = 0; RA1_bit = 0; RA2_bit = 1; // Select Hundreds Digit RA3_bit = 0; delay_ms(5); PORTB = DD3; RA0_bit = 0; RA1_bit = 0; RA2_bit = 0; RA3_bit = 1; // Select Thousands Digit delay_ms(5); } NUM = NUM + 1 ; } while(1); // endless loop}

19 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 20: User Manua1

EXAMPLE NO. 7

DS18B20 using 1-Wire Protocol & 7segment

In this Lesson, we will make a digital temperature meter using DS18B20.The connection between temperature sensor and microcontroller will be done through a single wire. This is advantage of the temperature sensor model. The temperature value will be displayed on 4 digits-with 7 segment, in multiplexed mod of course. 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.Pull-up resistor (with a value of 4.7 k), is required to perform communication between the sensor and microcontroller.

/*' Digital thermometer with DS18B20 and 7- Segments.' Description:' 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

20 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 21: User Manua1

' 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 valueunsigned 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){ for (i = 0; i<=4; i++) { PORTB = DD3; RA0_bit = 1; // Select C Digit; RA1_bit = 0; RA2_bit = 0; RA3_bit = 0; delay_ms(2); PORTB = DD0; RA0_bit = 0; RA1_bit = 1; // Select Ones Digit; RA2_bit = 0; RA3_bit = 0;

21 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 22: User Manua1

delay_ms(2); PORTB = DD1; RA0_bit = 0; RA1_bit = 0; RA2_bit = 1; // Select Tens Digit; RA3_bit = 0; delay_ms(2); PORTB = DD2; RA0_bit = 0; RA1_bit = 0; RA2_bit = 0 ; RA3_bit = 1; // Select +/- Digit; delay_ms(2); }return;}

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 if (temp_value & 0x8000) { temp_value = ~temp_value + 1; N_Flag = 1; // Temp is -ive } if (temp_value & 0x0001) temp_value += 1; // 0.5 round to 1 temp_value = temp_value >> 4 ; //<<< // 1 for DS1820 and // 4 for DS18B20; }

void main(){ CMCON |= 7; // Disable Comparators TRISB = 0; // Set PORTB direction to be output PORTB = 0; // Turn OFF LEDs on PORTB PORTA = 0; TRISA0_bit = 0; // RA.0 to RA3 Output

22 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 23: User Manua1

TRISA1_bit = 0; TRISA2_bit = 0; TRISA3_bit = 0; 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); }

23 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 24: User Manua1

EXAMPLE NO. 8

Reading Temperature Values from DS1820 using 1-Wire 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.

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

24 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 25: User Manua1

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 definitionssbit 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 connections definitions

// String array to store temperature value to displaychar *temp = "000.00";

// Temperature Resolution : No. of bits in temp value = 9const unsigned short TEMP_RES = 9;

// Variable to store temperature register valueunsigned temp_value;

void Display_Temperature(unsigned int temp2write) { const unsigned short RES_SHIFT = TEMP_RES - 8;

// Variable to store Integer value of temperature char temp_whole;

// Variable to store Fraction value of temperature unsigned int temp_fraction; unsigned short isNegative = 0x00;

// check if temperature is negative if (temp2write & 0x8000) { temp[0] = '-'; // Negative temp values are stored in 2's complement form temp2write = ~temp2write + 1; isNegative = 1; // Temp is -ive }

// Get temp_whole by dividing by 2 (DS1820 9-bit resolution with

25 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 26: User Manua1

// 0.5 Centigrade step ) temp_whole = temp2write >> RES_SHIFT ;

// convert temp_whole to characters if (!isNegative) { if (temp_whole/100) // 48 is the decimal character code value for displaying 0 on LCD temp[0] = temp_whole/100 + 48; else temp[0] = '0'; }

temp[1] = (temp_whole/10)%10 + 48; // Extract tens digit temp[2] = temp_whole%10 + 48; // 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 temp[4] = temp_fraction/1000 + 48; // Extract tens digit temp[5] = (temp_fraction/100)%10 + 48; // Extract ones digit

// print temperature on LCD Lcd_Out(2, 5, temp);}

void main() { CMCON |= 7; // Disable Comparators Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear LCD Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off Lcd_Out(1, 3, "Temperature: "); // Print degree character, 'C' for Centigrades Lcd_Chr(2,11,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,12,'C');

//--- main loop do { //--- perform temperature reading Ow_Reset(&PORTB, 0); // Onewire reset signal Ow_Write(&PORTB, 0, 0xCC); // Issue command SKIP_ROM Ow_Write(&PORTB, 0, 0x44); // Issue command CONVERT_T

26 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 27: User Manua1

Delay_ms(600); // If this delay is less than 500ms, you will see the first reading on LCD //85C which is (if you remember from my article on DS1820) //a power-on-reset value. Ow_Reset(&PORTB, 0); Ow_Write(&PORTB, 0, 0xCC); // Issue command SKIP_ROM Ow_Write(&PORTB, 0, 0xBE); // Issue command READ_SCRATCHPAD

// Read Byte 0 from Scratchpad temp_value = Ow_Read(&PORTB, 0); // Then read Byte 1 from Scratchpad and shift 8 bit left and add the Byte 0 temp_value = (Ow_Read(&PORTB, 0) << 8) + temp_value;

//--- Format and display result on Lcd Display_Temperature(temp_value);

} while (1);}

27 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 28: User Manua1

EXAMPLE NO. 9

Use of PWM to control the brightness of a LED.

A PIC16F628A has an in-built Capture/Compare/PWM (CCP) module for which the I/O pin is served by RB.3 (Pin No. 9). In this experiment we are going to use the CCP as a PWM to control the power to a LED. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load. The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.

The mikroC has an in-built library functions for PWM hardware module.

Experimental Setup:

In this experiment, we are going to have 11 different intensities (including complete turn OFF) of a LED by varying the duty cycle. We will connect a LED to RB.3, and two Push Buttons to RB.0 and RB.1. The two buttons are for Increment/Decrement the intensity of the LED.

28 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 29: User Manua1

Software:

/*Project Name: Use of Timer 0 and Interrupt* Description:Use of CCP module as a Pulse Width Modulation

* Test configuration:MCU: PIC16F628AOscillator: XT, 4.0 MHz*/

unsigned short new_DC, current_DC;void main() {

PORTB = 0; // Initial state of port BTRISB = 3; // RB0, RB1 input, RB3 (PWM1) outputPWM1_Init(5000); // PWM module initialization (5KHz)new_DC = 0; // Initial value of variable Duty Cyclecurrent_DC = 0;PWM1_Start(); // Start PWM1 module with Zero DCPWM1_Set_Duty(current_DC);while (1) {if (Button(&PORTB, 0,1,0)) { // If the button connected to RB0 is pressedif (new_DC < 250) // Don't go above 250new_DC = new_DC + 25 ; // increment Duty Cycle by 25}if (Button(&PORTB, 1,1,0)) { // If the button connected to RB1 is pressedif (new_DC !=0) // Don't go below 0new_DC= new_DC - 25 ; // decrement Duty Cycle by 25}

if (current_DC != new_DC) { current_DC = new_DC ;PWM1_Set_Duty(current_DC); // Change the current DC to new value}Delay_ms(150);

}}

29 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 30: User Manua1

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 Oscillator: XT, 4.0 MHz

30 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 31: User Manua1

*/

// LCD module connectionssbit 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 Messageschar 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 ComparatorsTRISB = 0x0F;PORTB = 0x00;Lcd_Init();start:Lcd_Cmd(_LCD_CLEAR); // Clear displayLcd_Cmd(_LCD_CURSOR_OFF); // Cursor offLcd_Out(1,1,message1); // Write message1 in 1st rowLcd_Out(1,8,message2);Lcd_Out(2,1,message3);do {// Read Operationif (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;

31 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e

Page 32: User Manua1

NUM = EEPROM_Read(temp); digits[i] = NUM+48; } Lcd_Out(2,1,digits); delay_ms(3000); goto start; }// Write Operationif (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 Operationif (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);}

32 | P I C D e v e l o p m e n t B o a r d w i t h M o d u l e