Micro Controller Notes
DAY
1:
LED
(Light Emitting Diodes)
Light
Emitting Diodes (LED)
is the most commonly used components, usually for displaying pins digital
states. Typical uses of LEDs include alarm devices, timers and
confirmation of user input such as a mouse click or keystroke.
Interfacing
LED
Fig.
1 shows how to interface the LED to microcontroller. As you can see
the Anode is connected through a resistor to GND . the Cathode is connected to
the Microcontroller pin. So when the Port Pin is HIGH the LED is
OFF. when the Port Pin is LOW the LED is turned ON.
Interfacing
LED with PIC16F887
We
now want to flash a LED in PIC16F/18F Evaluation board. It works by turning ON
a LED & then turning it OFF & then looping back to
START. However the operating speed of microcontroller is very high.so the
flashing frequency will also be very fast to be detected by human eye.
The PIC16F/18F Evaluation board has eight numbers of
point LEDs. That 8 pins connected with I/O Port lines (PORTx.0 –
PORTx.7) to make port pins high.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface LED with PIC16F887
Source
Code
#include<pic.h>
#define led PORTB
void delay(int x)
{
while(x--);
}
void
main()
{
TRISB=0X00;
PORTB=0X00;
ANSEL=0X00;
ANSELH=0X00;
while(1)
{
led=0xAA;
delay(20000);
led=0x55;
delay(30000);
}
}
The Interfacing LED
with PIC16F887 program is very simple and straight
forward, that uses a delay procedure loop based software delay. In C programs
you cannot be sure of delay, because it depends on compiler how it optimizes
the loops as soon as you make changes in the options the delay changes.
C
Program to switch ON and OFF LED using PIC16F
To
compile the above C code you must need the Mplab software & Hi-Tech C
Compiler. They must be properly set up and a project with correct settings must
be created in order to compile the code. To compile the above code, the C file
must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without PIC16F/18F Evaluation board.
The
PICKIT2 software is used to download the hex file into your microcontroller
IC PIC16F887 through USB port.
Testing
the LED with PIC16F887
Give
+12V power supply to PIC16F/18F Evaluation board; the LED is
connected with PIC16F/18F Evaluation board. When the program is
downloading into PIC16F887 in Evaluation Board, the LED output
is working that the LED is ON some time period and the LED is
OFF some other time period.
If
you are not reading any output from LED, then you just check the
jumper connections & check the LED is working. Otherwise
you just check it with debugging mode in Mplab.
DAY 2:
LCD
(Liquid Crystal Display)
Liquid
Crystal Display also
called as LCD is very helpful in providing user interface as
well as for debugging purpose. A liquid crystal display (LCD)
is a flat panel display that uses the light modulating properties of liquid
crystals (LCs). LCD Modules can present textual information to
user.
Interfacing
LCD
Fig.
1 shows how to interface the LCD to microcontroller. The 2×16
character LCD interface card with supports both modes 4-bit and 8-bit
interface, and also facility to adjust contrast through trim pot. In 8-bit
interface 11 lines needed to create 8-bit interface; 8 data bits (D0 – D7),
three control lines, address bit (RS), read/write bit (R/W) and control signal
(E).
Fig.
1 Interfacing LCD to Microcontroller
Interfacing
LCD with PIC16F887
We
now want to display a text in PIC16F/18F Development Board by using LCD
module. In PIC16F/18F Development Board contains the LCD connections
in a single header.
The PIC16F/18F Development Board has eleven numbers
of LCD connections, connected with I/O Port lines (PORTE.0 –
PORTE.3 && PORTD.0 – PORTD.7) to make LCD display.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface LCD with PIC16F887
Source
Code
The Interfacing
LCD with PIC16F887 program is very simple and straight
forward, which display a text in 2 X 16 LCD modules. Some delay is
occurring when a single command / data is executed.
C Program to display a text in LCD using PIC16F887
include
//Define
PIC Registers
#include
//Define
I/O Functions __CONFIG(0x3f72);
//Select
HS oscillator, Enable (PWRTE,BOREN),
//Disable
(CPD,CP,WDTEN,In-circuit Debugger)
#define
RS RE0
//LCD
Register Select
#define
RW RE1
//LCD
Read/Write Pin
#define
EN RE2
//LCD
Enable Pin
#define
DATA PORTD
//LCD
Data Port
#define
DATADIR TRISD
//LCD
Data Port Direction Register
#define
CNTRLDIR TRISE //RS,RW,EN Direction Register void lcdinit(void);
//LCD
initialization Function void lcdclr(void);
//LCD
Clear Function void lcdcomd(unsigned char);
//LCD
Command Declaring Fucntion void lcddata(unsigned char);
//LCD
Data Display Fucntion void DelayMs(unsigned int);
void
main()
{
int
i;
unsigned
char First[]={" PIC DEV. BOARD "};
unsigned
char Secnd[]={"LCD DEMO PROGRAM"};
DelayMs(500);
lcdinit();
DelayMs(500);
while(1)
{
lcdclr();
lcdcomd(0x80);
for(i=0;i<16;i++)
//Display
the Message
{
lcddata(First[i]);
DelayMs(50);
}
lcdcomd(0xc0);
for(i=0;i<16;i++)
//Display
the Message
{
lcddata(Secnd[i]);
DelayMs(50);
}
DelayMs(500);
}
}
void
lcdinit(void)
{
int
i;
unsigned
char command[]={0x38,0x0c,0x06,0x01};
//LCD
Command set for 8 bit Interface, 2 Lines, 5x7 Dots ADCON1 = 0x07;
//Make
PORTE Pin as Digital CNTRLDIR = 0x00;
//Make
LCD control port (PORTE) as output DATADIR = 0x00; DelayMs(50);
//Make
LCD Data Port (PORTD) as output port for(i=0;i<4;i++)
{
lcdcomd(command[i]);
//Send
the Initialisation Commands DelayMs(5);
}
DelayMs(500);
}
void
lcdclr(void)
//Send
LCD clear command
{
lcdcomd(0x01);
DelayMs(2);
}
void
lcdcomd(unsigned char cmd)
{
RS=0;
RW=0;
//Select
Command Register, R/W--write enabled EN=1;
//Enable
LCD to accept commands DATA=cmd;
EN=0;
DelayMs(5);
//Give
a Pulse via Enable pin
}
void
lcddata(unsigned char byte)
{
RS=1;
RW=0;
//Select
Data register, R/W--write enabled EN=1;
DATA=byte;
//Give
a Pulse via Enable pin EN=0;
DelayMs(5);
}
void
DelayMs(unsigned int Ms)
{
int
delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst
= 0;
delay_cnst
<220;delay_cnst++);
}
}
To compile the above C code
you need the Mplab software & Hi-Tech C Compiler. They must be properly set
up and a project with correct settings must be created in order to compile the
code. To compile the above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without microcontroller Board.
The
PICKIT2 software is used to download the hex file into your microcontroller
through USB port.
Testing
the LCD Module with PIC16F877A
Give
+12V power supply to PIC16F/18F Development Board; the LCD is
connected with microcontroller PIC16F/18F Development Board. When the program is
downloading into PIC16F877A in Development Board, the screen should
show the text messages.
If
you are not reading any text from LCD, then you just check the
jumper connections & adjust the trim pot level. Otherwise you just check it
with debugging mode in Mplab.
DAY 3:
GLCD
(Graphical Liquid Crystal Display)
The Graphics
LCD as the name suggests is a type of LCD which can
display graphics. The graphical representation of any data presents good
understanding than just characters. More user friendly applications can be
designed by using the graphical LCDs.
Interfacing
GLCD
Fig.
1 shows how to interface the GLCD to microcontroller. The 128X64
Graphical LCD interfaces to adjust contrast through trim pot.
The GLCD needed to create 8-bit interface; 8 data
bits (D0 – D7), three control lines, address bit (RS), read/write bit (R/W) and
control signal (E), Page Select (CS).
Fig.
1 Interfacing GLCD to Microcontroller
Interfacing
GLCD with PIC16F887
We
now want to display a text in PIC16F/18F Advanced Development Board by using GLCD
module. The PIC16F/18F Advanced Development Board has numbers of GLCD connections,
connected with I/O Port lines (PORTE.0 – PORTE.2 & PORTD.0 – PORTD.7) to
make GLCD display.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface GLCD with PIC16F
Source
Code
The Interfacing
GLCD with PIC16F887 program is very simple and straight
forward, which display a text in 128 X 64 GLCD modules. Some delay
is occurring when a single command / data is executed. In C programs you cannot
be sure of delay, because it depends on compiler how it optimizes the loops as
soon as you make changes in the options the delay changes.
C
Program to display a text in GLCD using PIC16F
#include
__CONFIG(0x3f72);
#define CS1 RA4
#define CS2 RA5
#define RS RA1
#define RW RA2
#define E RA3
#define DATA PORTD void
DelayMs(unsigned int);
void GLCD_Init(void);
void GLCD_Comd(unsigned
char);
void GLCD_Data(unsigned
char);
void GLCD_PutPicture(const
unsigned char *);
void Select_page(unsigned
char);
void GLCD_ClrScr(void);
unsigned int Page=0,i=0;
unsigned int Column=0;
const unsigned char
carsun_bmp[1024] ={ 0, 0, 0, 0, 4, 10, 58, 62,122,210,208,208,208,208,208, 48,
188, 60,172,172,246,247,255,110,254,190,126, 126, 92,152, 96, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 128,128,128,128,0,129,129,141,143,136, 192, 80,208,200, 8, 8, 140, 12, 58,
58, 71,130,135, 0,161,208,112,240,160,166,166,153, 139,142,141,143, 15, 27, 23,
23, 27, 15,15, 143,190,187,191,159,204,140,143,246,
246,246,230,189,255,177,189,238,195, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 28,222,
126,123, 57, 63,125,120, 15, 15, 3, 3, 65,193,225,193,192,192, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 98, 99, 29, 153,192, 66,199,132,132,133,135, 0, 0,128, 80,120,
81,113,118,126, 63, 31, 22,227,253, 248,224, 192,193,194,194,194,195,129, 0,
1,131,131, 191,191,230,234,234,188,248,241,195, 66, 71,
129,133,133,174,255,255,247,255,255,254, 254,255,251,253,210,208, 0, 0, 0, 0,
0, 0, 32, 60, 52,247,254,241,245,239,255,188,182,254,
52,230,243,233,247,151,153,255,247,243,223, 221,252,120,120,248,
104,120,248,112,112, 48, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 31, 31, 31, 31, 17, 17, 17,
17, 17, 17, 17, 18, 18, 25, 25, 44, 15, 31, 63, 63,111,127, 58,
63,119,119,255,191, 53, 22, 31, 11, 31, 31, 47, 63, 63,255,207,255,255, 47, 37,
0, 0, 0, 0, 0, 0, 0, 32,251,255,255,255,255,255, 131, 129,193, 227,227, 51,
55,229,199, 7, 15, 11, 27, 31, 28, 56,240, 96, 96, 97, 97, 97, 97, 96, 96, 96,
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96,224, 96, 96, 96, 96,
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96,
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96,
96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 97, 99, 98,102,108,248,240, 96,
96, 0,255,255,255,255, 0, 0, 0, 0, 0, 0, 0, 2, 23, 29, 33,
33,255,255,255,255,167,167, 58, 24,120,248,232,121,123,122,158,144,
0,252,158,159,127, 96,128, 14, 10, 10, 10, 10, 10, 10, 14, 0, 0, 0, 14, 10, 10,
10, 10, 10, 14, 14, 0, 0,128,255,248,232,192,192,254, 254,254,34, 34, 34, 34,
34, 34,254, 34, 34, 34, 34, 34, 34, 34,254,254,254, 50, 0,0, 0,254,130, 186,
58, 58, 98, 98, 98, 98, 2,254, 0, 0, 0,254,254,254,254, 34, 34, 34, 34,254,254,
34, 34, 34, 34, 34,254,254,254,224,127,255,194,222, 126,255,255,255,255, 0, 0,
0, 0, 0, 0, 0, 0,128,128,128, 96,191,255,127,255,255,225,195, 207,140,137, 8,
8, 8, 8, 25, 63, 59, 11, 79, 79, 73,233,254, 168,160,160, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 63, 39, 47, 63, 63,125, 73,
9,225, 64, 72, 120,120,120,121, 73, 65, 77, 81, 25,121, 73,121, 49,113, 73,
73,200,200,120,127, 80, 81, 64, 64, 64, 80, 80,240,224,127, 24,152,
144,201,233, 249,249, 56,136, 8,144,241,121,120,160,248,240, 112, 9,121, 63,127,120,104,
77, 76,124,255,255, 255,255,248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3, 3, 3, 6, 6,
12, 12, 24, 24, 16, 48, 32, 96, 96, 192,192,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 2, 2, 6, 6, 0, 0, 4, 0, 0, 2, 2, 34, 98, 98,208,209, 145, 145, 16, 16,
16, 8, 8, 8, 4, 6, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 128,128,192,192,224,224,248,231,239,255,119,67, 0, 0, 0, 0, 0,
0, 0, 0, 0,128,128,0,192, 192,192,192,192,192, 192,192,192,192,192,192,
192,192,192,192,192,192,192, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 96, 64, 64, 65, 65, 99, 99, 98,102,102,108,
108,120,120,120,112,112, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,1, 1,
3, 2, 6, 6, 12, 12, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 16, 16, 0,
0, 0, 0, 0, 0};
void main(void)
{
DelayMs(100);
ADCON1= 7;
TRISA = 0;
TRISD = 0x00;
DelayMs(50);
GLCD_Init();
DelayMs(100);
while(1)
{
GLCD_ClrScr();
DelayMs(1000);
GLCD_PutPicture(carsun_bmp);
DelayMs(1000);
}
}
void GLCD_Init()
{
unsigned char
Comd[5]={0xc0,0xb8,0x40,0x3f};
Select_page(1);
for(i=0;i<4;i++)
GLCD_Comd(Comd[i]);
Select_page(0);
for(i=0;i<4;i++)
GLCD_Comd(Comd[i]);
}
void Select_page(unsigned
char Page)
{
if(Page==1)
{
CS1=0; CS2=1;
}
else
{
CS1=1;
CS2=0;
}
}
void GLCD_Comd(unsigned
char command)
{
RS=0;
RW=0;
E=1;
DATA=command;
E=0;
}
void GLCD_Data(unsigned
char data)
{
RS=1;
RW=0;
E=1;
DATA=data;
E=0;
}
void GLCD_PutPicture(const
unsigned char *ip)
//Change here for method
1,2 and 3
{
for (Page = 0; Page < 8;
Page++)
{
Select_page(1);
GLCD_Comd(0xb8 | Page);
GLCD_Comd(0x40);
for (Column = 0;
Column < 128; Column++)
{
if (Column == 64)
{
Select_page(0);
GLCD_Comd(0xb8 | Page);
GLCD_Comd(0x40);
}
GLCD_Data(*ip++);
//This is Method 3
}
}
}
void GLCD_ClrScr(void)
{
for (Page = 0; Page < 8;
Page++)
{
Select_page(1);
GLCD_Comd(0xb8 | Page);
GLCD_Comd(0x40);
for (Column = 0;
Column < 128; Column++)
{
if (Column == 64)
{
Select_page(0);
GLCD_Comd(0xb8 | Page);
GLCD_Comd(0x40);
}
GLCD_Data(0x00);
//This is Method 3
}
}
}
void DelayMs(unsigned int
Ms)
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst =
0;delay_cnst <220;delay_cnst++);
}
}
To compile the above C code
you need the Mplab software & Hi-Tech C Compiler. They must be properly set
up and a project with correct settings must be created in order to compile the
code. To compile the above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without microcontroller Advanced Development Board .
The
PICKIT2 software is used to download the hex file into your microcontroller
through USB.
Testing
the Graphical LCD Module with PIC16F887
Give
+12V power supply to PIC16F/18F Advanced Development Board, the Graphical LCD is
connected with microcontroller PIC16F/18F Board. When the program is
downloading into PIC16F887 in Advanced Development Board, the screen should show
the image output.
If
you are not getting any output from Graphical LCD, then you just
check the jumper connections & adjust the trim pot level. Otherwise you
just check it with debugging mode in Mplab.
DAY 4:
Keypad
A keypad is
a set of buttons arranged in a block or “pad” which usually bear digits,
symbols and usually a complete set of alphabetical letters. If it mostly
contains numbers then it can also be called a numeric keypad. Here
we are using 4 X 4 matrix keypad.
Interfacing
keypad
Fig.
1 shows how to interface the 4 X 4 matrix keypad to two ports
in microcontroller. The rows are connected to an output port and the columns
are connected to an input port.
To
detect a pressed key, the microcontroller grounds all rows by providing 0 to
the output latch, and then it reads the columns. If the data read from the
columns is D3-D0=1111, no key has been pressed and the process continues until
a key press is detected. However, if one of the column bits has a zero, this
means that a key press has occurred. For example, if D3-D0=1101, this means
that a key in the D1 column has been pressed.
After
a key press is detected, the microcontroller will go through the process of
identifying the key. Starting with the top row, the microcontroller grounds it
by providing a low to row D0 only; then it reads the columns.
If
the data read is all 1s, no key in that row is activated and the process is
moved to the next row. It grounds the next row, reads the columns, and checks
for any zero. This process continues until the row is identified. After
identification of the row in which the key has been pressed, the next task is
to find out which column the pressed key belongs to.
Interfacing
keypad with PIC16F887
We
now want to scan a keypad in PIC16F/18F Advanced Development .In case of 4X4
matrix Keypad both the ends of switches are connected to the port pin i.e. four
rows and four columns. So in all sixteen switches have been interfaced using
just eight lines.
1Keypads
arranged by matrix format, each row and column section pulled by high or low by
selection J15, all row lines(PORTB.0 – PORTB.3) and column lines(PORTB.4 to
PORTB.7) connected directly by the port pins.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface keypad with PIC16F887
Source
Code
The Interfacing
keypad with PIC16F877A program is very simple and straight
forward, that scan a keypad rows and columns. When the rows and columns are
detected then it will display in PC through UART0. The C programs are developed
in Mplab software with Hi-Tech Compiler.
C Program to 4 X 4 matrix keypad using PIC16F887
#include
// Define PIC Registers
#include
// Define I/O functions __CONFIG(0x3f72);
// Select HS oscillator, Enable (PWRTE,BOREN),
// Disable (CPD,CP,WDTEN,In-circuit Debugger)
#define FOSC 10000
#define BAUD_RATE 9.6
//9600 Baudrate #define BAUD_VAL (char)(FOSC/ (16 *
BAUD_RATE )) - 1;
//Calculation For 9600 Baudrate @10Mhz void
SerialInit(void);
//Serial port Initialization Function void
ScanCol(void);
//Column Scan Function void ScanRow(void);
//Row Scan Function void DelayMs(unsigned int);
unsigned char KeyArray[4][4]= { '1','2','3','4',
'5','6','7','8', '9','A','B','C', 'D','E','F','0'};
//Keypad value Initialization Function unsigned char
Count[4][4]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int Col=0,Row=0,count=0,i,j;
void main()
{
DelayMs(50);
SerialInit();
nRBPU=0;
//Enable PORTB Pullup values while(1)
{
TRISB=0x0f;
// Enable the 4 LSB as I/P & 4 MSB as O/P
PORTB=0;
while(PORTB==0x0f);
// Get the ROW value ScanRow(); TRISB=0xf0;
// Enable the 4 LSB as O/P & 4 MSB as I/P
PORTB=0; while(PORTB==0xf0);
// Get the Column value ScanCol(); DelayMs(150);
Count[Row][Col]++;
// Count the Pressed key printf("[%c]
Pressed:[%d] Times\n\r", KeyArray[Row][Col],Count[Row][Col]);
DelayMs(200);
}
}
void ScanRow()
// Row Scan Function
{
switch(PORTB)
{
case 0x07: Row=3;
// 4th Row break;
case 0x0b: Row=2;
// 3rd Row break;
case 0x0d: Row=1;
// 2nd Row break;
case 0x0e: Row=0;
// 1st Row break;
}
}
void ScanCol()
// Column Scan Function {
switch(PORTB)
{
case 0x70: Col=3;
// 4th Column break;
case 0xb0: Col=2;
// 3rd Column break;
case 0xd0: Col=1;
// 2nd Column break; case 0xe0: Col=0;
// 1st Column break;
}
}
void SerialInit()
{
TRISC=0xc0;
// RC7,RC6 set to usart mode(INPUT) TXSTA=0x24;
//Enable Serial Transmission, Asynchronous mode,
High Speed mode SPBRG=BAUD_VAL;
// 9600 Baud rate selection RCSTA=0x90;
// Enable Serial Port & Continuous Receive
printf("Press Anyone Key:\n\r");
}
void putch(unsigned char character)
{
while(!TXIF);
// Wait for the TXREG register to be empty
TXREG=character;
// Display the Character
}
void DelayMs(unsigned int Ms)
{
int delay_cnst; while(Ms>0)
{
Ms--;
for(delay_cnst = 0;
delay_cnst <220;
delay_cnst++);
}
To compile the above C code
you need the Mplab software & Hi-Tech Compiler. They must be properly set
up and a project with correct settings must be created in order to compile the
code. To compile the above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without PIC16F/18F Advanced Development.The PICKIT2 software is
used to download the hex file into your microcontroller IC PIC16F887 through USB port.
Testing
the Keypad with PIC16F887
Give
+12V power supply to PIC16F/18F Advanced Development.The serial cable is
connected between the PIC16F/18F Advanced Development and PC.Open
the Hyper Terminal screen, select which port you are using and set the default
settings. Now the screen should show some text messages & it display which
key is pressed in keypad.
DAY 5:
Relay
Relays are devices which
allow low power circuits to switch a relatively high Current/Voltage ON/OFF. A
relay circuit is typically a smaller switch or device which drives
(opens/closes) an electric switch that is capable of carrying much larger
current amounts.
Interfacing
Relays
Fig.
1 shows how to interface the Relay to microcontroller. There are 2
input channels. Each input is connected to the triggering coil of the
respective relay. There are 2 output channels that each correspond
to an input. When the input is energized, the relay turns on
and the ‘+’ output is connected to +12v. When the relay is off, the ‘+’ output
is connected to Ground. The ‘-‘ output is permanently wired to Ground.
Interfacing
Relay with PIC16F887
We
now want to control the relay operations by using PIC16f/18FAdvanced Development Board.Here we are using two
Relays. The relay consists of a coil and a switch. When the coil is energized,
the switch closes, connecting the two contacts together. ULN2803 is
used as a driver for port I/O lines, drivers output connected to relay modules.
Connector provided for external power supply if needed.
Relay
Module: Port
B pins (Relay1 – PORTB.0) and Relay2-PORTB.1) for relay module,
make port pins to high, relay will activated.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface Relay with PIC16F887
Source
Code
The
Interfacing Relay with PIC16F887 program is very simple and straight
forward, which control the relays in PIC16f/18F Advanced Development Board.The relay is
working that uses a delay procedure loop based software delay. The C programs
are developed in Mplab software with Hi-Tech Compiler.
C
Program to control Relay in PIC16F887
#include
//Define PIC Registers
#include
//Define I/O Function
#define Relay1 RB0
//RB1 interfaced to Relay1
#define Relay2 RB1
//RB2 interfaced to Relay2
__CONFIG(0x3f72);
//Select HS oscillator,
Enable (PWRTE,BOREN),
//Disable
(CPD,CP,WDTEN,In-circuit Debugger)
#define FOSC 10000
//10Mhz==>10000Khz
#define BAUD_RATE 9.6
//9600 Baudrate
#define BAUD_VAL
((char)(FOSC/ (16 * BAUD_RATE )) – 1)
//Calculation For 9600
Baudrate @10Mhz void Serial_init(void);
//Serial Communication
Initialization void DelayMs(unsigned int);
void main()
{
unsigned char ReceivChar;
TRISB = 0x00;
PORTB = 0x04;
//PORTB configured as O/P
Serial_init();
printf("\033[2J");
DelayMs(20);
printf("\n\r\t3:
Relay1 ON \n\r\t4: Relay1 OFF");
printf("\n\r\t5:
Relay2 ON \n\r\t6: Relay2 OFF\n\r\n");
while(1)
{
while(RCIF==0);
//Wait until the reception
is over RCIF=0;
//Clear the flag for next
reception ReceivChar=RCREG;
//Store the received char
to a variable printf(" %c",ReceivChar);
//Dislpay the character
received switch(ReceivChar)
{
case '3': //If '3' is
received Relay1 turned ON Relay1=1;
break;
case '4': //If '4' is
received Relay1 turned OFF Relay1=0;
break; case '5': //If '5'
is received Relay2 turned ON Relay2=1;
break; case '6': //If '6'
is received Relay1 turned OFF Relay2=0;
break;
}
}
}
void Serial_init()
{
TRISC=0xc0;
//RC7,RC6 set to usart
mode(INPUT) TXSTA=0x24;
//Enable(Serial
TXn,//Asynchronous, High Speed) SPBRG=BAUD_VAL;
//9600 baud at 10Mhz
RCSTA=0x90;
//Usart Enable, Continuous
receive enable TXIF=1;
//Start Transmit
}
void putch(unsigned char
data)
{
while(TXIF==0);
TXREG=data;
//Transmit the data
serially
}
void DelayMs(unsigned int
Ms)
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst =
0;delay_cnst <220;
delay_cnst++);
}
}
To compile the above C code
you need the Mplab software & Hi-Tech C Compiler. They must be properly set
up and a project with correct settings must be created in order to compile the
code. To compile the above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without PIC16f/18F Advanced Development Board.
The
PICKIT2 software is used to download the hex file into your microcontroller
IC PIC16F887 through USB port.
Testing
the Relay with PIC16F887
Give
+12V power supply to PIC16f/18F Advanced Development Board .The Relay module is
connected with PIC16f/18F Advanced Development Board. When the program is
downloading into PIC16F887 in Advanced Development Board , the Relay output is
working that the Relay is ON some time period and the Relay is OFF some other
time of period.
If
you are not getting any output from Relay, then you just check the
jumper connections & check the Relay is connected properly. Otherwise you
just check it with debugging mode in Mplab.
DAY 6,7,8:
I2C
(Inter Integrated Circuit)
The I2C (Inter-IC)
bus is a bi-directional two-wire serial bus that provides a communication link
between integrated circuits (ICs).I2C is a synchronous protocol
that allows a master device to initiate communication with a slave device. Data
is exchanged between these devices.
RTC
(Real Time Clock)
The DS1307
Serial Real-Time Clock is a low-power; full binary-coded decimal (BCD)
clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred
serially via a 2-wire, bi-directional bus. The clock/calendar provides seconds,
minutes, hours, day, date, month, and year information. The end of the month
date is automatically adjusted for months with fewer than 31 days, including
corrections for leap year. The clock operates in either the 24-hour or 12-hour
format with AM/PM indicator.
Interfacing
I2C – RTC
Fig.
1 shows how to interface the EEPROM with microcontroller through I2C. I2C is
a Master-Slave protocol. I2C has a clock pulse along with the
data. The master device controls the clock line, SCL. This line dictates the
timing of all transfers on the I2C bus. No data will be
transferred unless the clock is manipulated.
I2c bus supports many
devices, each device is recognized by a unique address—whether it’s a
micro-controller, LCD Driver, memory or keyboard interface and can operate as
transmitter or receiver based on the functioning of the device. The controller
designed controls the RTC ds1307 device through I2C protocol.
The I2C Controller here acts as a master device and
controls RTC ds1307 which acts as a slave. The read operation
is accomplished by sending a set of control signals including the address
and/or data bits. The control signals must be accompanied with proper clock
signals.
Interfacing
I2C – RTC with PIC16F887
We
now want to read date & time by using I2C – RTC in PIC16F/18F Development Board. Wiring up an I2C
based RTC to the I2C port is relatively simple. The
RTC also makes the software easier as it takes care of all calendar functions;
accounting for leap years etc. The DS1307 (RTC) Real Time Clock IC (an I2C
real time clock) is an 8 pin device using an I2C interface.
In PIC16F/18F Development Kit 2 nos. of RTC lines
are controlled by I2C Enabled drivers. I2C Lines
serial clock of CLK (RC3), serial data of DATA
(RC4) connected to the I2C based serial RTC ds1307 IC. The
date & times are read in PIC16F/18F Development Kit by using these DATA &
CLK I2C lines.
Pin
Assignment with PIC16F887
Circuit
Diagram to Interface I2C–RTC with PIC16F
Source
Code
The Interfacing
I2C – RTC with PIC16F887 program is very simple and straight
forward that read date & time in RTC by using I2C &
the value is displayed in serial port. A delay is occurring in every single
data read from RTC. The delay depends on compiler how it
optimizes the loops as soon as you make changes in the options the delay
changes.
C
Program to interface I2C – RTC with PIC16F
#include
//Define PIC Registers
#include
__CONFIG(0x3f72);
//Select HS oscillator,
Enable (PWRTE,BOREN),
//Disable
(CPD,CP,WDTEN,In-circuit Debugger)
#define LC01CTRLIN 0xd0
#define LC01CTRLOUT 0xd1
#define I2C_FREG 100
#define FOSC 10000
#define BAUD_RATE 9.6
// 9600 Baud rate
#define BAUD_VAL
(char)(FOSC/ (16 * BAUD_RATE )) - 1;
//Calculation For 9600
Baudrate @10Mhz unsigned char sec,min,hour,day,date,month,year; unsigned char
data[7]={0x45,0x59,0x71,0x04,0x05,0x10,0x06};
int i;
void DS1307Write(unsigned
char,unsigned char);
void WaitMSSP();
unsigned char
DS1307Read(unsigned char);
void i2c_init(void); void
ds1307_init(void);
void serial_init(void);
void DelayMs(unsigned int);
void main()
{
int count=0;
DelayMs(20);
ds1307_init();
serial_init();
for(i=0;i<7;i++)
DS1307Write(i,data[i]);
printf("\033[2J");
DelayMs(20);
while(1)
{
sec=DS1307Read(0);
// Read second
min=DS1307Read(1);
// Read minute
hour=DS1307Read(2);
// Read hour
day=DS1307Read(3);
// Read day date=DS1307Read(4);
// Read date
month=DS1307Read(5);
// Read month
year=DS1307Read(6);
// Read year
printf("Time: %x : %x : %x ",(hour&0x1f),min,sec);
//Display the Hours,
Minutes, Seconds(hours is taken from 5 LSB bits printf("Date: %x / %x / %x
\r",date,month,year);
//Display the Date, Month,
Year DelayMs(150);
}
}
void DS1307Write(unsigned
char addr, unsigned char data)
{
SEN=1;
//Initiate Start condition
on SDA & SCL pins WaitMSSP();
SSPBUF=LC01CTRLIN;
// Slave address + Write
command WaitMSSP();
SSPBUF=addr;
// Write the location
WaitMSSP();
SSPBUF=data;
// Write the Data
WaitMSSP();
PEN=1;
// Enable the Stop bit
WaitMSSP();
}
//Write the location
(memory address of Hour, minute, etc... WtMSSP ();
RSEN=1;
// Enable the repeated
Start Condition WaitMSSP ();
SSPBUF=LC01CTRLOUT;
// Slave address + Read
command WaitMSSP ();
RCEN=1;
// Enable to receive data
WaitMSSP (); ACKDT=1;
// Acknowledge the
operation (Send NACK) ACKEN=1;
// Acknowledge sequence on
SDA & SCL pins PEN=1;
// Enable the Stop bit
WaitMSSP (); x=SSPBUF;
// Store the Receive value
in a variable return (x);
}
void WaitMSSP()
{
while(!SSPIF);
// SSPIF is zero while
TXion is progress SSPIF=0;
}
void ds1307_init()
{
TRISC3=1;
// RC3,RC4 set to I2C
Mode(Input) TRISC4=1;
SSPCON=0x28;
// Enable the SDA,SCL &
I2C Master Mode SSPADD=(FOSC / (4 * I2C_FREG)) - 1;
// SSP baud rate 100Khz
SSPSTAT=0x80;
// Disable slew Rate
control PORTC=0x18;
DS1307Write(0,0x00);
}
void serial_init()
{
TRISC6=1;
// RC7, RC6 set to USART
Mode TRISC7=1;
TXSTA=0x24;
// Enable Transmission,
Asynchronous mode, High Speed mode SPBRG=BAUD_VAL;
// 9600 Baud rate selection
RCSTA=0x90;
// Enable Serial Port &
Continuous Reception TXIF=1;
// Enable Transmission
}
void putch(unsigned char
byte)
//Required for printf
statement
{
while(!TXIF);
// Wait for the Transmit
Buffer to be empty TXREG = byte;
// Transmit the Data } void
DelayMs(unsigned int Ms)
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst =
0;delay_cnst <220;delay_cnst++);
}
}
To compile the above C code
you need the Mplab software & Hi-Tech Compiler. They must be properly set
up and a project with correct settings must be created in order to compile the
code. To compile the above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without PIC16F/18F Development Board
The
PICKIT2 software is used to download the hex file into your microcontroller
IC PIC16F887 through USB port.
Testing
the I2C – RTC with PIC16F/18F
Give
+12V power supply to PIC16F/18F Development Board; the RTC Battery device is
connected with the PIC16F/18F Development Board. First check the entire
Battery device fixed properly. A serial cable is connected between the microcontroller
and PC.
In
PC, open the Hyper Terminal for displaying the values from RTC.
Now,
the Hyper Terminal shows the received data from RTC Battery through I2C.
If
the Hyper Terminal is working but it is not reading any value from PIC16F/18F Development Board, then you just check the
jumper connections. Change the Battery & ds1307 device.
If
you are not receiving any data in Hyper Terminal, then you just check the
serial cable is working or not. Otherwise you just check the code with
debugging mode in Mplab.
DAY 9,10:
UART
UART (Universal
Asynchronous Receiver Transmitter) are one of the basic interfaces which
provide a cost effective simple and reliable communication between one
controller to another controller or between a controller and PC.
RS-232 Level Converter
Usually
all the digital ICs work on TTL or CMOS voltage levels which cannot be used to
communicate over RS-232 protocol. So a voltage or level converter is needed
which can convert TTL to RS232 and RS232 to TTL voltage levels. The most
commonly used RS-232 level converter is MAX232.
This
IC includes charge pump which can generate RS232 voltage levels (-10V and +10V)
from 5V power supply. It also includes two receiver and two transmitters and is
capable of full-duplex UART/USART communication.
RS-232
communication enables point-to-point data transfer. It is commonly used in data
acquisition applications, for the transfer of data between the microcontroller
and a PC.
The
voltage levels of a microcontroller and PC are not directly compatible with
those of RS-232, a level transition buffer such as MAX232 be used.
Interfacing UART
Fig.
1 shows how to interface the UART to microcontroller. To communicate
over UART or USART, we just need three basic signals which are
namely, RXD (receive), TXD (transmit), GND (common ground). So to interface
UART with LPC2148, we just need the basic signals.
Interfacing UART with PIC16F/18F Advanced Development Board
We
now want to display a text in PC from by using UART module. It contains two
serial interfaces that are UART & UART1. Here we are using UART. The Transmitter
pins send the data into PC and the receiver pin receives the data from PC. The
PC and microcontroller speed are denoted by using baud rate. When the baud
rates of both PC and Microcontroller are same, then only the data transmit and
receive correctly otherwise not.
Pin Assignment with PIC16F887
Circuit Diagram to Interface UART with PIC16F887
Source Code
The Interfacing
UART with PIC16F887 program is very simple and straight
forward, which display a text in PC from PIC16F887 Advanced Development Board Board through UART. Some
delay is occurring when a single data is sent to PC. The C programs are written
in Mplab software with Hi-Tech C Compiler. The baud rate of microcontroller is
9600.
C Program to display a text in PC from PIC16F887
#include __CONFIG(0x3f72);
#define FOSC 10000
//10Mhz==>10000Khz
#define BAUD_RATE 9.6
//9600 Baudrate
#define BAUD_VAL (char)(FOSC/ (16 * BAUD_RATE )) - 1;
//Calculation For 9600 Baudrate @10Mhz void main()
{
unsigned char ReceiveChar;
TRISC=0xc0;
//RC7,RC6 set to usart mode(INPUT) TXSTA=0x24;
//Transmit Enable SPBRG=BAUD_VAL;
//9600 baud at 10Mhz RCSTA=0x90;
//Usart Enable, Continus receive enable TXREG='0';
while(1)
{
if (RCIF==1)
//char received? Send 'A' back to Terminal
{
ReceiveChar=RCREG;
if(TXIF==1)
TXREG=ReceiveChar;
}
}
}
To compile the above C code you need the Mplab software
& Hi-Tech C Compiler. They must be properly set up and a project with
correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In
Mplab, you want to develop or debug the project without any hardware setup. You
must compile the code for generating HEX file. In debugging Mode, you want to
check the port output without PIC16F/18F Advanced Development Board.
Testing the UART with PIC16F887
Give
+12V power supply to PIC16F/18F Advanced Development Board.The serial cable is
connected between the PIC16F/18F Advanced Development Board and PC. Open the
Hyper Terminal screen, select which port you are using and set the default
settings. Now the screen should show some text messages. If you are not reading
any text from UART, then you just check the jumper connections & just check
the serial cable is working. Otherwise you just check the code with debugging
mode in Mplab. If you want to see more details about debugging just see the
videos in below link.
How to Interface ds1820 with PIC16F887 pic advanced
development board
Spread
the love
Microcontroller IC PIC16F887 is specifically designed to help students to master
the required skills in the area of embedded systems. The kit is designed in such
way that all the possible features of the microcontroller will
be easily used by the students. The kit supports in system programming (ISP)
which is done through USB port.
Microchip’s
PIC (PIC16F877A)PIC16F/18F Advanced Development Board is proposed to smooth the
progress of developing and debugging of various designs encompassing of High
speed 8-bit Microcontrollers
DAY
11:
Temperature Sensor
DS1820 is a
temperature sensor which is small sensor. The output of sensor converted to
digital that easy connecting with microcontroller.
Interfacing ds1820
Fig. 1 shows
how to interface the ds1820 to microcontroller. As you
can see the first pin is connected to GND, the third pin is connected to VCC
& the second pin is connected to the Microcontroller.
So when the temperature is sensing, it give the sensor reading to controller.
Interfacing ds1820
with PIC16F887
We now want to
read the temperature in PIC16F/18F Advanced Development Board from temperature
sensor ds1820. The PIC16F/18F Advanced Development Board uses the ADC pin
for reading temperature from temperature sensor ds1820.
The reading output is displayed into PC through UART.
The 10 bit ADC
used to read temperature. Basic clocking for the A/D converters is provided by
the VPB clock. A programmable divider is included in each converter, to scale
this clock to the 4.5 MHz (max) clock needed by the successive approximation
process. A fully accurate conversion requires 11 of these clocks.
Pin Assignment
with PIC16F887
Circuit Diagram to
Interface ds1820 with PIC16F887
Source Code
The Interfacing
ds1820 with PIC16F887 program is very simple and straight forward, that reading
temperature from temperature sensor ds1820 and it
display into PC through serial port.
C Program to read
temperature using PIC16F887
#include
#include
"delay.c"
__CONFIG(0x3f72);
#define
DQ RC0
#define
DQ_DIR TRISC0
#define
FOSC 10000
#define
BAUD_RATE 9.6
//
9600 Baudrate
#define
BAUD_VAL (char)(FOSC/ (16 * BAUD_RATE )) - 1;
//
Calculation For 9600 Baudrate @10Mhz unsigned char i=0;
float
Temerature=0;
void
ds1820_init();
void
Reset(void);
void
write(unsigned char);
unsigned
char Read(void);
void
Serial_init();
void
main()
{
unsigned
char Temp[9];
DelayMs(100);
Serial_init();
ds1820_init();
DelayMs(100);
printf("\033[2J");
//
Clear the Hyper terminal;
while(1)
{
Reset();
write(0xcc);
write(0x44);
DQ_DIR
= 1;
DelayUs(10);
while(!DQ);
//
this will be raised after finishing conversion Reset();
write(0xcc);
write(0xbe);
for(i=0;i<9;i++)
Temp[i] = Read();
//
Read 9 bytes for(i=0;i<9;i++) printf("%d ",Temp[i]);
printf("Temperature:%3.1f%cC",(float)Temp[0]/2,0xf8);
printf("\r");
DelayMs(250);
DelayMs(250);
DelayMs(250);
DelayMs(250);
}
}
void
ds1820_init()
{
DQ
= 1;
DQ_DIR
= 1;
//
pull up } void Reset(void)
{
DQ_DIR
= 0;
DQ
= 0;
DelayMs(1);
DelayUs(250);
//
500us DQ_DIR = 1;
DelayUs(100);
//
40us while(DQ==1);
//
wait until presence pulse DelayMs(1);
DelayUs(250);
//
500us
}
void
write(unsigned char cmd)
{
for(i=0;i<8;i++)
{
DQ_DIR
= 0;
pull
down DQ = 0;
DelayUs(25);
//
10us DQ = (cmd & 0x01)?1:0;
//
Send bit cmd = cmd >> 1;
DelayUs(120);
// >45us DQ_DIR = 1;
//
release
}
}
unsigned
char Read(void)
{
unsigned
char temp=0,RecDat=0; for(i=0;i<8;i++)
{
DQ_DIR
= 0; DQ = 0;
//
pull down DelayUs(25);
//
10us DQ_DIR = 1;
//
release DelayUs(25);
//
10us temp = DQ;
//
read bit temp = temp<<i; RecDat |= temp;
DelayUs(70);
//
30us
}
return
RecDat;
}
void
Serial_init()
{
TRISC=0xc0;
//
RC7,RC6 set to usart mode(INPUT) TXSTA=0x24;
//
Transmit Enable SPBRG=BAUD_VAL;
//
9600 baud at 10Mhz RCSTA=0x90;
//
USART Enable, Continuous receive enable TXIF=1;
//
Start Transmit
}
void putch(unsigned char Data)
//
data TX required for printf
{
while(TXIF==0);
TXREG
= Data;
}
To
compile the above C code you need the Mplab software & Hi-Tech Compiler.
They must be properly set up and a project with correct settings must be
created in order to compile the code. To compile the above code, the C file
must be added to the project.
In Mplab, you
want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check
the port output without Advanced Development Board.
The PICKIT2
software is used to download the hex file into your PIC16F/18F Advanced Development Board through USB port line.
Testing the ds1820
with PIC16F887
Give +12V
power supply to PIC16F/18F Advanced Development Board.The serial cable is connected between
the controller and PC. Open the Hyper Terminal screen, select which port you
are using and set the default settings. Now the screen should show the current
temperature readings.
Bring a Hot
soldering iron tip near the ds1820’s pins, don’t touch it keep it 1 or 2mm
away. The screen should update with the rising temperature. Now finally touch
the pins of ds1820 with the tip of iron, the temperature should rise quickly.
Keep it there until temperature rise to 80 degrees, and then remove the iron.
If any data is
not coming in Hyper Terminal, then you just check the serial cable is working
or not. Otherwise you just check the code with debugging mode in Mplab.
DAY 12,13:
Generating PWM with PIC16F877A PIC Advanced Development
Board
PIC16F/18F Advanced Development Board is specifically designed to help
students to master the required skills in the area of embedded systems. The kit
is designed in such way that all the possible features of the microcontroller will
be easily used by the students. The kit supports in system programming (ISP)
which is done through USB port.
Microchip’s
PIC(PIC16F887)PIC16F/18F
Advanced Development Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 8-bit
Microcontrollers.
PWM (Pulse Width
Modulation)
Pulse
width modulation (PWM)
is a powerful technique for controlling analog circuits with a processor’s
digital outputs. PWM is employed in a wide variety
of applications, ranging from measurement and communications to power control
and conversion.
Interfacing PWM
Figure 1 shows
four different PWM signals. One is PWM output
at a 25% duty cycle. That is, the signal is on for 25% of the period and off
the other 75%. Next shows PWM output at 50%, 75% and
100% duty cycles, respectively. These three PWM outputs encode three different
analog signal values, at 10%, 50%, and 90% of the full strength.
Fig. 1 PWM Outputs
Interfacing PWM
with PIC16F887
We now want to
generate a PWM in PIC16F/18F Advanced Development Board at a particular frequency. Pulse
Width Modulation is a technique for getting analog results with
digital means.
Digital
control is used to create a square wave, a signal switched between on and off.
This on-off pattern can simulate voltages in between full on (5 Volts) and off
(0 Volts) by changing the portion of the time the signal spends on versus the
time that the signal spends off. The duration of “on time” is called the pulse
width. To get varying analog values, you change, or modulate, that pulse width.
Pin Assignment
with PIC16F887
Circuit Diagram to
Interface PWM with PIC16F887
Source Code
The Interfacing
PWM with PIC16F887 program is very simple and straight forward, which generates a
pulse pattern in a particular frequency. An ADC signal is used to varying the
duty cycle of PWM signal. The C program is written in Mplab software & it
executed with Hi-Tech C compiler.
C Program to generate
PWM in PIC16F887
#include
//Define
PIC registers __CONFIG(0x3f72);
//Select
HS oscillator, Enable (PWRTE,BOREN),
//Disable
(CPD,CP,WDTEN,In-circuit Debugger)
#define
XTAL 10000 //10Mhz=10000Khz
#define
PWM_Freq 1 //1Khz PWM frequency
#define
TMR2_PRE 16 //Timer2 Prescale
#define
PR2_Val ((char)((XTAL/(4*TMR2_PRE*PWM_Freq))-1))
//Calculation
for Period register PR2 (1Khz)
#define
Duty_Cyc PR2_Val*2 unsigned int i;
void
PWM_init(void);
void
PWM_change(unsigned int);
void
DelayMs(unsigned int);
void
main(void)
{
PWM_init();
while(1)
{
i=0;
PWM_change(i);
DelayMs(10);
while(i<PR2_Val)
{
i=i+1;
PWM_change(i);
DelayMs(200);
}
}
}
void
PWM_init(void)
{
TRISC2=0;
//PWM
channel 1 and 2 configured as output TRISC1=0;
PORTC
= 0x00; CCP1CON=0x0c;
//CCP1
and CCP2 are configured for PWM CCP2CON=0x0c;
PR2=PR2_Val;
//Move
the PR2 value T2CON=0x03;
//Timer2
Prescale is 16 TMR2=0x00;
TMR2ON=1;
//Turn
ON timer2
}
void
PWM_change(unsigned int DTY)
//Duty
cycle change routine
{
CCPR1L=DTY;
//Value
is between 0 to 255 CCPR2L=DTY;
}
void
DelayMs(unsigned int Ms)
//Delay
Routine
{
int
delay_cnst; while(Ms>0)
{
Ms--;
for(delay_cnst
= 0;delay_cnst <220;delay_cnst++);
//delay
constant for 1Ms @10Mhz
}
}
To
compile the above C code you need the Mplab software & Hi-Tech C Compiler.
They must be properly set up and a project with correct settings must be
created in order to compile the code. To compile the above code, the C file
must be added to the project.
In Mplab, you
want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check
the port output without PIC16F/18F Advanced Development Board.
The PICKIT2
software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.
Testing the PWM
with PIC16F887
Give +12V
power supply to PIC16F/18F Advanced Development Board. The PWM port
line is connected in PIC16F/18F Advanced Development Board. When the program is downloading
into PIC16F887 in Advanced Development Board, the PWM output is generating at a
particular frequency.
If you are not
reading any PWM output, then you just check the
jumper connections. Otherwise you just check it with debugging mode in Mplab.
If you want to see more details about debugging just see the videos in below
link.
DAY 14,15:
How to Interface SPI-EEPROM with PIC16F – PIC Development
Board
The PIC16F/18F Development Board is specifically designed to help students to master
the required skills in the area of embedded systems. The kit is designed in
such way that all the possible features of the microcontroller will
be easily used by the students. The kit supports in system programming (ISP)
which is done through USB port.
Microchip’s
PIC ( PIC16F887), PIC16F/18F Development Kit is proposed to smooth the
progress of developing and debugging of various designs encompassing of High
speed 8-bit Microcontrollers.
SPI (Serial
Peripheral Interface)
Serial
Peripheral Interface (SPI) is a synchronous serial data protocol used
by microcontrollers for communicating with one or more peripheral devices
quickly over short distances. It can also be used for communication between
two microcontrollers.
EEPROM
EEPROM (electrically erasable
programmable read-only memory) is user-modifiable read-only memory
(ROM). That can be erased and reprogrammed (written to) repeatedly through the
application of higher than normal electrical voltage. It is a type of
non-volatile memory.That can be used in computers and other electronic devices
to store small amounts of data that must be saved when power is removed.For
instance, calibration tables or device configuration.
Interfacing SPI –
EEPROM
Fig. 1 shows
how to interface the SPI-DAC to microcontroller. With
an SPI connection there is always one master device
(usually a microcontroller) which controls the peripheral devices. Typically
there are three lines common to all the devices,
☞Master In Slave Out (MISO) – The Slave
line for sending data to the master,
☞Master Out Slave In (MOSI) – The
Master line for sending data to the peripherals,
☞Serial Clock (SCK) – The clock pulses
which synchronize data transmission generated by the master, and
☞Slave Select pin – the pin on each
device that the master can use to enable and disable specific devices. When a
device’s Slave Select pin is low, it communicates with the master. When it’s
high, it ignores the master.
These allow
you to have multiple SPI devices sharing the same
MISO, MOSI, and CLK lines.
The controller
designed controls the EEPROM device through SPI
protocol. The SPI Controller here acts as a master
device and controls EEPROM which acts as a slave.
The read-write operations are accomplished by sending a set of control signals
including the address and/or data bits. The control signals must be accompanied
with proper clock signals.
Interfacing SPI –
EEPROM with PIC16F887
We now want to
Read, write and Erase EEPROM by using SPI
in PIC16F/18F Development Board. Wiring up an SPI based EEPROM to the
SPI port is relatively simple. The basic operation of the SPI based EEPROM’s is
to send a command, such as WRITE, followed by an address and the data. In WRITE
operation, the EEPROM to store the data.
In SPI,
the clock signal is controlled by the master device PIC16F/18F Development Board, All data is clocked in and out using this pin.
These lines
need to be connected to the relevant pins on the PIC16F/18F Development Board. Any unused GIO pin can be used for CS, instead pull
this pin high.
In PIC16F/18F Development Kit four nos. of EEPROM lines
are controlled by SPI Enabled drivers. The SPI Lines Chip Select of CS
(PORTC.0), serial clock of CLK (PORTC.3), serial
input data of MISO (PORTC.4) and serial output data
of MOSI (PORTC.5) connected to the SPI based serial
EEPROM IC. The EEPROM read & write operations are done in PIC16F/18F Development Kit by using these CS, CLK, MOSI, MISO SPI lines.
Pin Assignment
with PIC16F877A
Circuit Diagram to
Interface SPI–EEPROM with PIC16F
Source Code
The
Interfacing SPI – EEPROM with PIC16F887 program is very simple and straight forward . Read, write and
erase operations in EEPROM by using SPI & the value is displayed in serial
port. A delay is occurring in every single data read or write in EEPROM. The
delay depends on compiler how it optimizes the loops as soon as you make
changes in the options the delay changes.
C Program with SPI – EEPROM using PIC16F887
#include
__CONFIG(0x3f72);
//HS
oscillator,BODEN,PWRT and disable others
#define FOSC
10000
//10Mhz==>10000Khz
#define
BAUD_RATE 9.6
//9600
Baudrate
#define
BAUD_VAL (char)(FOSC/ (16 * BAUD_RATE )) - 1;
//Calculation
For 9600 Baudrate @10Mhz
//SPI lines
#define CS
RC0 //Chip select ON RC2
#define SI
RC5 //Master Out Slave In
#define SO
RC4 //Master in slave out
#define SCK
RC3 //Clock /*SPI_COMMANDS*/
#define READ
0x03
#define
WRITE 0x02
#define WRDI
0x04
#define WREN
0x06
#define RDSR
0x05
#define WRSR
0x01 unsigned char i,a,j;
unsigned
char Msg[]="SPI TEST Program";
void
Serial_init(void);
void
SPi_init(void);
void
SPi_WRITE(unsigned char);
unsigned
char SPi_RDSR(void);
unsigned
char SPi_READ(unsigned char);
void
DelayMs(unsigned int);
void main()
{
unsigned
char x;
TRISC=0xd0;
//Enable
RX,TX pin and Set MISO as input TRISD=0;
//and set
the remaining pins as output Serial_init();
//Setup the
serial port SPi_init();
DelayMs(10);
while(!SPi_RDSR());
//SPI ready?
SPi_WRITE(0x00);
//Send
initialisation Command DelayMs(10);
while(1)
{
x=0;
while(x<16)
{
TXREG=PORTD=SPi_READ(x);
//Read byte
from 25c040 and send via Usart ++x;
DelayMs(50);
}
}
}
void
SPi_init()
{
CS=1;
//Make CS
pin high SI=0;
//Clear
input pin SCK=0;
//Clock low
}
unsigned
char SPi_RDSR()
{
unsigned
char Data=0x05
CS=0;
//Initiate
transmission by pulling CS pin low for(i=0;i<8;i++)
{
SI=(Data
& 0x80)?1:0;
//Send Read
Status Register Command bit by bit(MSB) first SCK=1;
Data=Data<<1;
SCK=0;
}
for(i=0;
i<8; i++)
//wait for
0x00--device not busy
{
SCK=1;
Data|=((SO
& 1)?1:0);
Data=Data<<1;
SCK=0;
}
CS=1;
//Pull up
return !Data;
}
void
SPi_WRITE(unsigned char Addr)
{
unsigned
char Data=WREN;
int
AH=WRITE;
AH=(AH<<8)+Addr;
CS=0;
for(i=0;i<8;i++)
//Send Write
Enable
{
SI=(Data
& 0x80)?1:0;
SCK=1;
Data=Data<<1;
SCK=0;
}
CS=1;
//Rise CS
and pull down again CS=0;
for(i=0;i<16;i++)
//Send WRITE
command and Addr
{
SI=(AH &
0x8000)?1:0;
SCK=1;
AH=AH<<1; SCK=0;
}
for(i=0;i<16;i++)
//Send
Data's
{
Data=Msg[i];
for(j=0;j<8;j++)
{
SI=(Data
& 0x80)?1:0;
SCK=1;
Data=Data<<1;
SCK=0;
}
}
CS=1;
}
unsigned
char SPi_READ(unsigned char Addr)
{
int
Data=READ; unsigned char RData=0;
Data=(Data<<8)|Addr;
while(!SPi_RDSR());
//Device
Ready?Proceed to next statement else wait here CS=0;
//Pull down
CS for(i=0;i<16;i++)
//Send READ
command and Addr
{
SI=(Data
& 0x8000)?1:0;
SCK=1;
Data=Data<<1;
SCK=0;
}
for(i=0;
i<8; i++)
//Read a
Byte
{
RData=RData<<1;
SCK=1;
RData|=((SO
& 1)?1:0);
SCK=0;
}
CS=1;
return
RData;
}
void
Serial_init()
{
TXSTA=0x24;
//Transmit
Enable SPBRG=BAUD_VAL;
//9600 baud
at 10Mhz RCSTA=0x90;
//Usart
Enable, Continus receive enable TXREG=0x00;
//Dummy
transmission printf("\033[2J");
//Clear the
Hypherterminal;
}
void
putch(unsigned char character)
{
while(!TXIF);
//Wait for
the TXREG register to be empty TXREG=character;
//Display
the Character
}
void
DelayMs(unsigned int Ms)
{
int
delay_cnst; while(Ms>0)
{
Ms--;
for(delay_cnst
= 0;delay_cnst <220;delay_cnst++);
}
To
compile the above C code you need the Mplab software & Hi-Tech Compiler.
They must be properly set up and a project with correct settings must be
created in order to compile the code. To compile the above code, the C file
must be added to the project.
In Mplab, you
want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check
the port output without PIC16F/18F Development Board.
The PICKIT2
software is used to download the hex file into your microcontroller
IC PIC16F887 through USB port.
Testing the SPI –
EEPROM with PIC16F887
Give +12V
power supply to PIC16F/18F Development Board; the EEPROM device is
connected with the PIC16F/18F Development Board. First check the entire EEPROM device
fixed properly. A serial cable is connected between the microcontroller and PC.
In PC, open the Hyper Terminal for displaying the values from EEPROM through
SPI.
The Read &
Write operations are performed in EEPROM with EEPROM address.
When the EEPROM address is correct, then only you
can write, read, and erase data’s correctly in EEPROM.
If any data is
not coming in Hyper Terminal, then you just check the serial cable is working
or not. Otherwise you just check the code with debugging mode in Mplab. If you
want to see more details about debugging just see the videos in below link.
MP LAB IDE & MP LAB X IDE SOFTWARE LINK
Archives link :
http://microchipdeveloper.com/ http://www.microchip.com/development-...
Comments
Post a Comment