experiment no

31
Experiment No. 1 : 4-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. The picture below shows these connections. Software: Here is the C program written for mikroC for PIC 2009 compiler. /* * Project name: 4-bit Counter * Copyright: (c) Rajendra Bhatt, 2009. * Description: This is a simple 4-bit counter. The output will be displayed on 4 LEDs connected to PORT RB.0-RB.3 * Test configuration: MCU: PIC16F628A Dev.Board: PIC16F628A Board Oscillator: XT, 04.0000 MHz */ void main() { Page 1 of 31

Upload: ishimaru-thor

Post on 06-Nov-2015

221 views

Category:

Documents


0 download

DESCRIPTION

Experiment No

TRANSCRIPT

Experiment No. 1 : 4-Bit Binary CounterExperimental 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. The picture below shows these connections.

Software:Here is the C program written for mikroC for PIC 2009 compiler./** Project name: 4-bit Counter* Copyright: (c) Rajendra Bhatt, 2009.* Description: This is a simple 4-bit counter. The output will be displayed on 4 LEDs connected to PORT RB.0-RB.3* Test configuration: MCU: PIC16F628A Dev.Board: PIC16F628A Board Oscillator: XT, 04.0000 MHz

*/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}

Experiment No. 2 : Push Button and Seven Segment Display InterfaceIn 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 (HS-5461AS2 fromwww.futurlec.com).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:We will use in-built 'Button Library' to detect push button press.

Here is the complete C program written for mikroC for PIC 2009./** Project name: UP/DOWN Decimal Counter with Push Button and 7-Segment Interface* Copyright: (c) Rajendra Bhatt, 2009.* 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; 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}

Experiment No. 3: LCD Interface in 4-bit ModeThe 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 -> RB7RS -> RA0, and EN -> RA1Software:Note:Never forget to disable the comparator functions onPORTA.0, 1, 2, 3pins if you are going to use those pins as digital I/O./** Project name: Test LCD in 4-bit mode* Copyright: (c) Rajendra Bhatt, 2009.* Description: This code demonstrates how to display test message on a LCD which is connected to PIC16F628A through PORTB. D4-D7 pins of LCD are connected to RB4-RB7, whereas RS and EN pins connected to RA0 and RA1 respectively. MCU: PIC16F628A Oscillator: XT, 4.0 MHz*/// 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[] = "Testing LCD";char message2[] = "using PIC16F628A";char message3[] = "Test Successful!";char message4[] = "2009/09/18";void main() { CMCON |= 7; // Disable Comparators Lcd_Init(); // Initialize LCD do { 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(2,1,message2); // Write message1 in 2nd row Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Out(1,1,message3); // Write message3 in 1st row Lcd_Out(2,1,message4); Delay_ms(2000); } while(1);}

Experiment No. 4 : Reading Temperature Values from DS1820 using 1-Wire ProtocolIn 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* Copyright: (c) Rajendra Bhatt, 2009.* 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 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 // 0.5 Centigrade step ) temp_whole = temp2write >> RES_SHIFT ;

// convert temp_whole to charactersif (!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 > RES_SHIFT ;

// convert temp_whole to characters 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