Tuesday, 20 October 2015
AT89C51 ADC
#include<reg51.h>
#include<stdio.h>
sbit RS=P0^0;
sbit RW=P0^1;
sbit EN=P0^2;
sbit cs=P2^0;
sbit rd=P2^1;
sbit wr=P2^2;
sbit intr=P2^3;
#define ldata P1
void lcd_init();
void cmd(unsigned char c);
void dat(unsigned char c);
void delay(unsigned int i);
void digit(unsigned int i);
int adc()
{
unsigned int x;
cs=0;
wr=0;
delay(1);
wr=1;
rd=1;
while(intr==1);
rd=0;
x=P3;
return x;
}
void main()
{
unsigned char z[20],i=0,s[]="WWW.SUMMITBHARDWAJ.BLOGSPOT.IN";
unsigned int v,a,t;
lcd_init();
cmd(0x80);
while(s[i]!='\0')
{
dat(s[i]);
i++;
}
while(1)
{
a=adc();
v=((a*5)/255);
t=((a*5)/2.55);
sprintf(z,"ADC=%3d volt=%2dV temp=%3d'c",a,v,t);
cmd(0xc0);
i=0;
while(z[i]!='\0')
{
dat(z[i]);
i++;
}
}
}
void digit(unsigned int i)
{
unsigned char a,c=0x85;
do
{
cmd(c--);
a=i%10;
dat(48+a);
cmd(0x80);
delay(10);
i=i/10;
}
while(i>0);
}
void lcd_init()
{
cmd(0X01);
delay(10);
cmd(0X08);
delay(10);
cmd(0X0c);
delay(10);
cmd(0X38);
delay(10);
}
void cmd(unsigned char c)
{
int i;
ldata=c;
RS=0;
RW=0;
EN=1;
for(i=0;i<10;i++);
EN=0;
return;
}
void dat(unsigned char c)
{
int i;
ldata=c;
RS=1;
RW=0;
EN=1;
for(i=0;i<=10;i++);
EN=0;
return;
}
void delay(unsigned int b)
{
unsigned int i,j;
for(i=0;i<b;i++)
for(j=0;j<3000;j++);
}
PIC16F1518 SERIAL COMMUNICATION
#include<pic.h>
#include<htc.h>
#define __PIC16F1518_H
#define _XTAL_FREQ 16000000
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & WRT_OFF );
void TX(unsigned char Byte);
unsigned int a1;
void interrupt ISR(void)
{
if(RCIF) // If UART Rx Interrupt
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}
TX(RCREG); // Echo back received char
}
}
void UART(void)
{
TRISC6 = 0; // TX Pin
TRISC7 = 1; // RX Pin
SPBRG = 103;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
void TX(unsigned char Byte) // Writes a character to the serial port
{
while(!TXIF); // wait for previous transmission to finish
TXREG = Byte;
}
unsigned char RX(void) // Reads a character from the serial port
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}
while(!RCIF); // Wait for transmission to receive
a1 = RCREG;
return a1;
}
void String(unsigned char*p)
{
while(*p)
TX(*p++);
}
void main(void)
{
UART(); // Intialize UART
String("EMBEDDED_LAB,WWW.SUMMITBHARDWAJ.BLOGSPOT.IN"); // Send string on UART
GIE = 1; // Enable global interrupts
PEIE = 1; // Enable Peripheral Interrupts
while(1)
{
}
}
PIC16F1518 RTC BY BIT BANGING
#include<pic.h>
#include<htc.h>
#define __PIC16F1518_H
#define _XTAL_FREQ 16000000
__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_ON & CP_OFF & MCLRE_ON & BOREN_ON & LVP_OFF & WRT_OFF );
unsigned char RTCArray[4];
unsigned char Temp;
#define SDA_ADC ANSA3
#define SCK_ADC ANSA5
// Function Declarations
void cmd(unsigned char);
void dat(char);
void InitLCD(void);
void string(unsigned char *s);
void clrscr(void);
void DisplayTimeToLCD(unsigned char*) ;
void DisplayDateOnLCD( unsigned char*);
// Define i2c pins
#define SDA RA3 // Data pin for i2c
#define SCK RA5 // Clock pin for i2c
#define SDA_DIR TRISA3 // Data pin direction
#define SCK_DIR TRISA5 // Clock pin direction
// Define macros
#define Set_SDA_Low SDA_DIR = 0
#define Set_SDA_High SDA_DIR = 1
#define Set_SCK_Low SCK_DIR = 0
#define Set_SCK_High SCK_DIR = 1
//Function Declarations
void InitI2C(void);
void I2C_Start(void);
void I2C_ReStart(void);
void I2C_Stop(void);
void I2C_Send_ACK(void);
void I2C_Send_NACK(void);
bit I2C_Write_Byte(unsigned char);
unsigned char I2C_Read_Byte(void);
// Define DS1307 i2c device address
#define RTC_ADDRESS 0xD0
// Define Time Modes
#define AM_Time 0
#define PM_Time 1
#define TwentyFourHoursMode 2
// Define days
#define Mon 1
#define Tue 2
#define Wed 3
#define Thu 4
#define Fri 5
#define Sat 6
#define Sun 7
// Function Declarations
void Write_Byte_To_DS1307_RTC(unsigned char, unsigned char);
unsigned char Read_Byte_From_DS1307_RTC(unsigned char);
void Write_Bytes_To_DS1307_RTC(unsigned char,unsigned char*,unsigned char);
void Read_Bytes_From_DS1307_RTC(unsigned char,unsigned char*,unsigned int);
void Set_DS1307_RTC_Time(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Time(void);
void Set_DS1307_RTC_Date(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Date(void);
void delay(unsigned int i)
{
while(i--);
}
void cmd(unsigned char c)
{
unsigned int l,h;
l = c &0X0F;
h = c >>4;
LATC5 = 0;
LATC = h;
LATC4 = 1;
delay(5);
LATC4 = 0;
LATC = l;
LATC4 = 1;
delay(5);
LATC4 = 0;
}
void dat(unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c >>4;
LATC = h;
LATC4 = 1;
LATC5 = 1;
delay(5);
LATC4 = 0;
LATC = l;
LATC4 = 1;
LATC5 = 1;
delay(5);
LATC4 = 0;
}
void InitLCD(void)
{
cmd(0x02);
delay(1);
cmd(0x28);
delay(1);
cmd(0x01);
delay(1);
cmd(0x80);
delay(1);
cmd(0x0e);
delay(1);
}
void string(unsigned char *s)
{
while(*s!='\0')
{
dat(*s);
s++;
}
}
void clrscr(void) // Clear the Screen and return cursor to zero position
{
cmd(0x01); // Clear the screen
delay(160); // Delay for cursor to return at zero position
}
void DisplayTimeToLCD( unsigned char* Time ) // Displays time in HH:MM:SS AM/PM format
{
clrscr(); // Move cursor to zero location and clear screen
// Display Hour
dat ((Time[2]/10)+0x30);
dat ((Time[2]%10)+0x30);
//Display ':'
dat(':');
//Display Minutes
dat ((Time[1]/10)+0x30);
dat ((Time[1]%10)+0x30);
//Display ':'
dat(':');
//Display Seconds
dat ((Time[0]/10)+0x30);
dat ((Time[0]%10)+0x30);
//Display Space
dat (' ');
// Display mode
switch(Time[3])
{
case AM_Time: string("AM"); break;
case PM_Time: string("PM"); break;
default: dat('H'); break;
}
}
void DisplayDateOnLCD( unsigned char* Date ) // Displays Date in DD:MM:YY @ Day format
{
cmd(0xc0); // Move cursor to second line
// Display Date
dat ((Date[1]/10)+0x30);
dat ((Date[1]%10)+0x30);
//Display '/'
dat('/');
//Display Month
dat ((Date[2]/10)+0x30);
dat ((Date[2]%10)+0x30);
//Display '/'
dat('/');
//Display Year
dat ((Date[3]/10)+0x30);
dat ((Date[3]%10)+0x30);
//Display Space
dat(' ');
// Display Day
switch(Date[0])
{
case Mon: string("MONDAY"); break;
case Tue: string("TUEDAY"); break;
case Wed: string("WEDNESDAY"); break;
case Thu: string("THURSDAY"); break;
case Fri: string("FRIDAY"); break;
case Sat: string("SATURDAY"); break;
case Sun: string("SUNDAY"); break;
default: string("???"); break;
}
}
// Function Purpose: Set initial values of SCK and SDA pins
void InitI2C(void)
{
SDA_ADC = 1; // Make analog output
SCK_ADC = 1; // Make analog output
// Make SDA and SCK pins input initially
SDA_DIR = 1;
SCK_DIR = 1;
// Write zero in output register of SDA and SCK pin
SDA = 0;
SCK = 0;
}
// Function Purpose: I2C_Start sends start bit sequence
void I2C_Start(void)
{
SDA = 0; // Write zero in output register
SCK = 0; // of SDA and SCK pin
Set_SCK_High; // Make SCK pin high
Set_SDA_High; // Make SDA pin High
delay(2); // Half bit delay
Set_SDA_Low; // Make SDA Low
delay(2); // Half bit delay
}
// Function Purpose: I2C_ReStart sends start bit sequence
void I2C_ReStart(void)
{
Set_SCK_Low; // Make SCK pin low
delay(1); // Data pin should change it's value,
// when it is confirm that SCK is low
Set_SDA_High; // Make SDA pin High
delay(1); // 1/4 bit delay
Set_SCK_High; // Make SCK pin high
delay(1); // 1/4 bit delay
Set_SDA_Low; // Make SDA Low
delay(1); // 1/4 bit delay
}
//Function : I2C_Stop sends stop bit sequence
void I2C_Stop(void)
{
Set_SCK_Low; // Make SCK pin low
delay(1); // Data pin should change it's value,
// when it is confirm that SCK is low
Set_SDA_Low; // Make SDA pin low
delay(1); // 1/4 bit delay
Set_SCK_High; // Make SCK pin high
delay(1); // 1/4 bit delay
Set_SDA_High; // Make SDA high
delay(1); // 1/4 bit delay
}
//Function : I2C_Send_ACK sends ACK bit sequence
void I2C_Send_ACK(void)
{
Set_SCK_Low; // Make SCK pin low
delay(1); // Data pin should change it's value,
// when it is confirm that SCK is low
Set_SDA_Low; // Make SDA Low
delay(1); // 1/4 bit delay
Set_SCK_High; // Make SCK pin high
delay(2); // Half bit delay
}
//Function : I2C_Send_NACK sends NACK bit sequence
void I2C_Send_NACK(void)
{
Set_SCK_Low; // Make SCK pin low
delay(1); // Data pin should change it's value,
// when it is confirm that SCK is low
Set_SDA_High; // Make SDA high
delay(1); // 1/4 bit delay
Set_SCK_High; // Make SCK pin high
delay(2); // Half bit delay
}
// Function Purpose: I2C_Write_Byte transfers one byte
bit I2C_Write_Byte(unsigned char Byte)
{
static bit ACK = 0;
unsigned char i; // Variable to be used in for loop
for(i=0;i<8;i++) // Repeat for every bit
{
Set_SCK_Low; // Make SCK pin low
delay(1); // Data pin should change it's value,
// when it is confirm that SCK is low
if((Byte<<i)&0x80) // Place data bit value on SDA pin
Set_SDA_High; // If bit is high, make SDA high
else // Data is transferred MSB first
Set_SDA_Low; // If bit is low, make SDA low
delay(1); // Toggle SCK pin
Set_SCK_High; // So that slave can
delay(2); // latch data bit
}
// Get ACK from slave
Set_SCK_Low;
Set_SDA_High;
delay(2);
Set_SCK_High;
delay(2);
SDA_ADC = 0; // Make digital input
ACK = SDA; // Read data pin status
SDA_ADC = 1; // Make analog output
return ACK;
}
// Function Purpose: I2C_Read_Byte reads one byte
unsigned char I2C_Read_Byte(void)
{
unsigned char i, RxData = 0;
for(i=0;i<8;i++)
{
Set_SCK_Low; // Make SCK pin low
Set_SDA_High; // Don't drive SDA
delay(2); // Half bit delay
Set_SCK_High; // Make SCK pin high
delay(1); // 1/4 bit delay
SDA_ADC = 0; // Make digital input
RxData = RxData|(SDA<<(7-i)); // Captured received bit
SDA_ADC = 1; // Make analog output
delay(1); // 1/4 bit delay
}
return RxData; // Return received byte
}
// Function Purpose: Write_Byte_To_DS1307_RTC writes a single byte on given address
// Address can have any value fromm 0 to 0xFF, and DataByte can have a value of 0 to 0xFF.
void Write_Byte_To_DS1307_RTC(unsigned char Address, unsigned char DataByte)
{
I2C_Start(); // Start i2c communication
// Send i2c address of DS1307 with write command
while(I2C_Write_Byte(RTC_ADDRESS + 0) == 1)// Wait until device is free
{ I2C_Start(); }
I2C_Write_Byte(Address); // Write Address byte
I2C_Write_Byte(DataByte); // Write data byte
I2C_Stop(); // Stop i2c communication
}
// Function Purpose: Read_Byte_From_DS1307_RTC reads a single byte from given address
// Address can have any value fromm 0 to 0xFF.
unsigned char Read_Byte_From_DS1307_RTC(unsigned char Address)
{
unsigned char Byte = 0; // Variable to store Received byte
I2C_Start(); // Start i2c communication
// Send i2c address of DS1307 with write command
while(I2C_Write_Byte(RTC_ADDRESS + 0) == 1)// Wait until device is free
{ I2C_Start(); }
I2C_Write_Byte(Address); // Write Address byte
I2C_ReStart(); // Restart i2c
// Send i2c address of DS1307 RTC with read command
I2C_Write_Byte(RTC_ADDRESS + 1);
Byte = I2C_Read_Byte(); // Read byte from EEPROM
I2C_Send_NACK(); // Give NACK to stop reading
I2C_Stop(); // Stop i2c communication
// Send start bit and then stop bit to stop transmission
Set_SDA_Low; // Make SDA Low
delay(1); // Half bit delay
Set_SDA_High; // Make SDA high
delay(1); // Half bit delay
return Byte; // Return the byte received from 24LC64 EEPROM
}
// Function Purpose: Write_Bytes_To_DS1307_RTC writes mulitple bytes from given starting address.
// Address can have any value fromm 0 to 0xFF and pData is pointer to the array
// containing NoOfBytes bytes in it. NoOfBytes is the number of bytes to write.
void Write_Bytes_To_DS1307_RTC(unsigned char Address,unsigned char* pData,unsigned char NoOfBytes)
{
unsigned int i;
I2C_Start(); // Start i2c communication
// Send i2c address of DS1307 with write command
while(I2C_Write_Byte(RTC_ADDRESS + 0) == 1)// Wait until device is free
{ I2C_Start(); }
I2C_Write_Byte(Address); // Write Address byte
for(i=0;i<NoOfBytes;i++) // Write NoOfBytes
I2C_Write_Byte(pData[i]); // Write data byte
I2C_Stop(); // Stop i2c communication
}
// Function Purpose: Read_Bytes_From_DS1307_RTC reads a NoOfBytes bytes from given starting address.
// Address can have any value fromm 0 to 0xFF. NoOfBytes is the number of bytes to write.
// Read bytes are returned in pData array.
void Read_Bytes_From_DS1307_RTC(unsigned char Address, unsigned char* pData, unsigned int NoOfBytes)
{
unsigned int i;
I2C_Start(); // Start i2c communication
// Send i2c address of DS1307 with write command
while(I2C_Write_Byte(RTC_ADDRESS + 0) == 1)// Wait until device is free
{ I2C_Start(); }
I2C_Write_Byte(Address); // Write Address byte
I2C_ReStart(); // Restart i2c
// Send i2c address of DS1307 RTC with read command
I2C_Write_Byte(RTC_ADDRESS + 1);
pData[0] = I2C_Read_Byte(); // Read First byte from EEPROM
for(i=1;i<NoOfBytes;i++) // Read NoOfBytes
{
I2C_Send_ACK(); // Give Ack to slave to start receiving next byte
pData[i] = I2C_Read_Byte(); // Read next byte from EEPROM
}
I2C_Send_NACK(); // Give NACK to stop reading
// Send start bit and then stop bit to stop transmission
Set_SDA_Low; // Make SDA Low
delay(1); // Half bit delay
Set_SDA_High; // Make SDA high
delay(1); // Half bit delay // Stop i2c communication
}
// Function Purpose: Set_DS1307_RTC_Time sets given time in RTC registers.
// Mode can have a value AM_Time or PM_Time or TwentyFourHoursMode only.
// Hours can have value from 0 to 23 only.
// Mins can have value from 0 to 59 only.
// Secs can have value from 0 to 59 only.
void Set_DS1307_RTC_Time(unsigned char Mode, unsigned char Hours, unsigned char Mins, unsigned char Secs)
{
// Convert Hours, Mins, Secs into BCD
RTCArray[0] = (((unsigned char)(Secs/10))<<4)|((unsigned char)(Secs%10));
RTCArray[1] = (((unsigned char)(Mins/10))<<4)|((unsigned char)(Mins%10));
RTCArray[2] = (((unsigned char)(Hours/10))<<4)|((unsigned char)(Hours%10));
switch(Mode) // Set mode bits
{
case AM_Time: RTCArray[2] |= 0x40; break;
case PM_Time: RTCArray[2] |= 0x60; break;
default: break; // do nothing for 24HoursMode
}
// WriteRTCArray to DS1307
Write_Bytes_To_DS1307_RTC(0x00, RTCArray, 3);
}
// Function Purpose: Get_DS1307_RTC_Time returns current time from RTC registers.
// Pointer to RTCArray is returned, in this array
// RTCArray[3] can have a value AM_Time or PM_Time or TwentyFourHoursMode only.
// RTCArray[2] (Hours byte) can have value from 0 to 23 only.
// RTCArray[1] (Mins byte) can have value from 0 to 59 only.
// RTCArray[0] (Secs byte) can have value from 0 to 59 only.
unsigned char* Get_DS1307_RTC_Time(void)
{
// Read Hours, Mins, Secs register from RTC
Read_Bytes_From_DS1307_RTC(0x00, RTCArray, 3);
// Convert Secs back from BCD into number
Temp = RTCArray[0];
RTCArray[0] = ((Temp&0x7F)>>4)*10 + (Temp&0x0F);
// Convert Mins back from BCD into number
Temp = RTCArray[1];
RTCArray[1] = (Temp>>4)*10 + (Temp&0x0F);
// Convert Hours back from BCD into number
if(RTCArray[2]&0x40) // if 12 hours mode
{
if(RTCArray[2]&0x20) // if PM Time
RTCArray[3] = PM_Time;
else // if AM time
RTCArray[3] = AM_Time;
Temp = RTCArray[2];
RTCArray[2] = ((Temp&0x1F)>>4)*10 + (Temp&0x0F);
}
else // if 24 hours mode
{
Temp = RTCArray[2];
RTCArray[2] = (Temp>>4)*10 + (Temp&0x0F);
RTCArray[3] = TwentyFourHoursMode;
}
return RTCArray;
}
// Function Purpose: Set_DS1307_RTC_Date sets given date in RTC registers.
// Year can have a value from 0 to 99 only.
// Month can have value from 1 to 12 only.
// Date can have value from 1 to 31 only.
// Day can have value from 1 to 7 only. Where 1 means Monday, 2 means Tuesday etc.
void Set_DS1307_RTC_Date(unsigned char Date, unsigned char Month, unsigned char Year, unsigned char Day)
{
// Convert Year, Month, Date, Day into BCD
RTCArray[0] = (((unsigned char)(Day/10))<<4)|((unsigned char)(Day%10));
RTCArray[1] = (((unsigned char)(Date/10))<<4)|((unsigned char)(Date%10));
RTCArray[2] = (((unsigned char)(Month/10))<<4)|((unsigned char)(Month%10));
RTCArray[3] = (((unsigned char)(Year/10))<<4)|((unsigned char)(Year%10));
// WriteRTCArray to DS1307
Write_Bytes_To_DS1307_RTC(0x03, RTCArray, 4);
}
// Function Purpose: Get_DS1307_RTC_Date returns current date from RTC registers.
// Pointer to RTCArray is returned, in this array
// RTCArray[3] (Year byte) can have value from 0 to 99 only.
// RTCArray[2] (Month byte) can have value from 1 to 12 only.
// RTCArray[1] (Date byte) can have value from 1 to 31 only.
// RTCArray[0] (Day byte) can have value from 1 to 7 only.
unsigned char* Get_DS1307_RTC_Date(void)
{
// Read Hours, Mins, Secs register from RTC
Read_Bytes_From_DS1307_RTC(0x03, RTCArray, 4);
// Convert Date back from BCD into number
Temp = RTCArray[1];
RTCArray[1] = (Temp>>4)*10 + (Temp&0x0F);
// Convert Month back from BCD into number
Temp = RTCArray[2];
RTCArray[2] = (Temp>>4)*10 + (Temp&0x0F);
// Convert Year back from BCD into number
Temp = RTCArray[3];
RTCArray[3] = (Temp>>4)*10 + (Temp&0x0F);
return RTCArray;
}
// Main function
void main()
{
TRISC = 0X00;
InitLCD(); // Initialize LCD
InitI2C(); // Initialize i2c pins
// Set initial time
Set_DS1307_RTC_Time(PM_Time, 11, 59, 50); // Set time 12:40:59 AM
// Set initial date
Set_DS1307_RTC_Date(11, 11, 11, Fri); // Set 31-03-2014 @ Monday
while(1)
{
// Display RTC time on first line of LCD
DisplayTimeToLCD(Get_DS1307_RTC_Time());
// Display RTC date on second line of LCD
DisplayDateOnLCD(Get_DS1307_RTC_Date());
delay(5000); // 1 second delay
}
}
PIC16F877 GRAPHIC LCD INTERFACE
#include<pic.h>
#include<htc.h>
#include "Picture.h"
#define __PIC16F877_H
// LCD Port and Connection Configurations
#define Output 0
#define ConfigureLCDPINsDirection(Value) (TRISD = Value, \
TRISCbits.TRISC3 = Value, \
TRISCbits.TRISC4 = Value, \
TRISCbits.TRISC5 = Value, \
TRISCbits.TRISC6 = Value, \
TRISCbits.TRISC7 = Value)
#define DataBusLCD PORTD
#define RegisterSelect PORTCbits.RC3
#define EnableLCD PORTCbits.RC4
#define ChipSelect1 PORTCbits.RC5
#define ChipSelect2 PORTCbits.RC6
#define ResetLCD PORTCbits.RC7
#define Enable 1
#define Disable 0
#define Command 0
#define Data 1
#define DisplayON 63
#define DisplayStartLine 192
#define LeftChip 1
#define RightChip 2
#define BothChip 3
// LCD Index addresses
#define Index0 0x40
#define Index1 0x50
#define Index2 0x60
#define Index3 0x70
#define Index4 0x80
#define Index5 0x90
#define Index6 0xA0
#define Index7 0xB0
// LCD Pages address
#define Page0 0xB8
#define Page1 0xB9
#define Page2 0xBA
#define Page3 0xBB
#define Page4 0xBC
#define Page5 0xBD
#define Page6 0xBE
#define Page7 0xBF
#define ColourON 1
#define ColourOFF 0
void delay(unsigned int i)
{
while(i--);
}
void InitializeGraphicalLCD(void);
void ChipSelection(unsigned char Chip);
void ToggleEnablePin(void);
void WriteCommandToLCD(unsigned char CommandToLCD, unsigned char SelectChip);
void WriteDataToLCD(unsigned char DataToLCD, unsigned char SelectChip);
void WriteAddressToLCD(unsigned char AddressX, unsigned char AddressY, unsigned char SelectChip);
void ClearScreen(void);
void FillScreen(void);
void DisplayPicture(const unsigned char *PtrArray);
void InitializeGraphicalLCD(void)
{
ConfigureLCDPINsDirection(Output);
RegisterSelect = Command;
EnableLCD = Enable;
DataBusLCD = Disable;
ChipSelect1 = Disable;
ChipSelect2 = Disable;
ResetLCD = Enable;
delay(1);
ResetLCD = Disable; // Normal Operation
WriteCommandToLCD(DisplayON, BothChip); // Sending 63 to both chip CS1 and CS2
WriteCommandToLCD(DisplayStartLine, BothChip); // Sending 192 to both chip CS1 and CS2
ClearScreen();
}
void ChipSelection(unsigned char Chip)
{
if (Chip == 1)
{
ChipSelect1 = Enable;
ChipSelect2 = Disable;
}
else if (Chip == 2)
{
ChipSelect1 = Disable;
ChipSelect2 = Enable;
}
else if (Chip == 3)
{
ChipSelect1 = Enable;
ChipSelect2 = Enable;
}
}
void ToggleEnablePin(void)
{
delay(1);
EnableLCD = Enable;
delay(1);
EnableLCD = Disable;
}
void WriteCommandToLCD(unsigned char CommandToLCD, unsigned char SelectChip)
{
// sending data to LCD.
ChipSelection(SelectChip);
RegisterSelect = Command;
DataBusLCD = CommandToLCD;
ToggleEnablePin();
}
void WriteDataToLCD(unsigned char DataToLCD, unsigned char SelectChip)
{
// sending data to LCD.
ChipSelection(SelectChip);
RegisterSelect = Data;
DataBusLCD = DataToLCD;
ToggleEnablePin();
}
void WriteAddressToLCD(unsigned char AddressX, unsigned char AddressY, unsigned char SelectChip)
{
// sending X address.
WriteCommandToLCD(AddressX, SelectChip);
// sending Y address.
WriteCommandToLCD(AddressY, SelectChip);
}
void ClearScreen(void)
{
unsigned char X = 0, Y = 0, Page = 0;
Page = Page0;
for (X = 0; X < 8; X++)
{
WriteAddressToLCD(Page++, Index0, BothChip);
for (Y = 0; Y < 64; Y++)
WriteDataToLCD(0x00, BothChip);
}
}
void FillScreen(void)
{
unsigned char X = 0, Y = 0, Page = 0;
Page = Page0;
for (X = 0; X < 8; X++)
{
WriteAddressToLCD(Page++, Index0, BothChip);
for (Y = 0; Y < 64; Y++)
WriteDataToLCD(0xFF, BothChip);
}
}
void DisplayPicture(const unsigned char *PtrArray)
{
unsigned char X = 0, Y = 0, Page = 0;
Page = Page0;
for (X = 0; X < 8; X++)
{
WriteAddressToLCD(Page, Index0, LeftChip);
for (Y = 0; Y < 64; Y++)
WriteDataToLCD(*PtrArray++, LeftChip);
WriteAddressToLCD(Page++, Index0, RightChip);
for (Y = 0; Y < 64; Y++)
WriteDataToLCD(*PtrArray++, RightChip);
}
}
int main() {
InitializeGraphicalLCD();
//FillScreen();
ClearScreen();
DisplayPicture(Picture1);
while(1)
{
}
return 0;
}
//**************************************PICTURE.H******************//
#ifndef PICTURE_H
#define PICTURE_H
#ifdef __cplusplus
extern "C" {
#endif
const unsigned char Picture1 [] ={ /* 0X22,0X01,0X80,0X00,0X40,0X00, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XF8,0XFC,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0XC0,0XF8,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0XE0,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XC0,0XF8,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0XE0,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XF8,0XFF,
0XFF,0X3F,0X1F,0X8F,0XCF,0XE7,0XF3,0XF1,0XF9,0XF9,0XF9,0XF8,0XF9,0XF1,0XF3,0X87,
0X1F,0XF8,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XF8,0X3F,0X0F,0XC3,0XF1,
0XF8,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X3F,0X3F,0X3F,0X7F,0X7F,
0X00,0XFF,0XFF,0XFF,0XF8,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0XF8,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0XF0,0XC0,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X7F,0X7F,0X7F,0X7F,
0X7F,0X7F,0X7F,0X7F,0X7E,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,
0X00,0XFF,0X7F,0X7F,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X7F,0X7F,
0X7F,0X7F,0X00,0X00,0X00,0X80,0XF0,0XFC,0XFE,0XFF,0XFF,0XFF,0XFF,0XFE,0XFC,0XE0,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0XF8,0X3F,0X07,0XF0,0XFC,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X03,0X00,0X00,0X00,0X00,0X80,0XE0,0XFC,0X1F,
0X03,0X1F,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XF8,0XE0,0X00,
0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,
0XF0,0XF0,0XF0,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,
0X00,0X00,0XC0,0XF0,0XFE,0XFF,0XFF,0XFF,0XFF,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X00,0X00,0X00,0XC0,0XF0,0XFE,0XFF,0XFF,0X00,0X3F,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X78,0X20,0XB0,0XB8,0X9E,0X87,0X81,0XC0,0XE0,
0XF8,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XC0,0X00,0X00,0X00,0X00,
0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X03,0X0F,0X7F,0XFF,0XFF,0XFF,0XFF,
0XFC,0XF0,0X80,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X07,0X07,0X07,0X07,0X07,0X07,
0X07,0X07,0X07,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0XE0,
0XFC,0XFF,0XFF,0XFF,0XFF,0X7F,0X1F,0X07,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X80,0XF0,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0XF1,0XF3,0XE7,0XC7,0XCF,
0XC7,0XE7,0XE7,0XF3,0XF1,0XF9,0XFC,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF8,0XC0,0X00,
0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X00,0XFE,0XFE,0XFE,0XFF,0XFF,0XFF,0XFF,
0XFF,0X7F,0X3F,0X02,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,
0XFE,0XFE,0XFE,0XFE,0X7C,0X00,0X3F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFE,0XFE,0XFE,
0XFE,0XFE,0XFE,0XFE,0XFE,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X1F,0X7F,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFE,0XFE,0X00,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,
};
#ifdef __cplusplus
}
#endif
#endif /* PICTURE_H */
//*********************************************************************//
PIC16F1518 LCD INTERFACE BY TRANSISTOR LOGIC
#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define __PIC16F1518_H
#define _XTAL_FREQ 16000000
#define RS LATB2
#define EN LATB4
void cmd(unsigned char c);
__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF);
void msdelay(unsigned int time)
{
unsigned int i=0,j=0;
for(i=0;i<=time;i++)
for(j=0;j<=3000;j++);
}
/* function of simple delay*/
void delay(unsigned int i)
{
while(i)
i--;
}
/*to blink cursor*/
void lcd_cursorblink(void)
{
cmd(0x0D);
}
/*to off cursorblinking*/
void lcd_cursorOff(void)
{
cmd(0x0C);
}
void lcd_init()
{
cmd(0x03); //lcd reset cmds
msdelay(1);
cmd(0x02); //lcd reset cmds
msdelay(1);
cmd(0x28); //2 lines 5x7 matrix
msdelay(1);
cmd(0x0C); //display on,cursor off,blink off
msdelay(1);
cmd(0x01); //clear display
msdelay(1);
cmd(0x06); //entry mode, set increment
msdelay(1);
}
void dat (unsigned char data1)
{
data1=~data1;
unsigned char l,h;
h = data1 & 0xF0;
l = data1 << 4;
delay(4);
RS=0;
delay(4);
PORTC = 0XF0;
delay(4);
PORTC = h; // Write Upper nibble of data
delay(4);
EN=0;
delay(4000);
EN=1;
delay(4);
PORTC = 0XF0; // Make Data pins zero
delay(4);
PORTC = l; // Write Lower nibble of data
delay(4);
EN=0;
delay(4000);
EN=1;
}
void cmd(unsigned char command)
{
command =~ command;
unsigned char l,h;
delay(4);
RS=1;
delay(4);
h = command&0xF0;
l=command<<4;
PORTC=0xf0;
delay(4);
PORTC=h;
delay(4);
EN=0;
delay(4000);
EN=1;
delay(4);
PORTC=0xf0;
delay(4);
PORTC=l;
delay(4);
EN=0;
delay(4000);
EN=1;
}
void string(unsigned char *p)
{
while(*p!='\0')
{
//delay(20000);
dat(*p);
p++;
}
}
int main()
{
TRISB = 0x00;
TRISC = 0x00;
lcd_init();
string ("EMBEDDED_LAB");
while(1)
{
LATB6 = 1;
delay(4);
LATB6 = 0;
delay(4);
}
}
PIC16F877A PWM
#include<pic.h>
#include<htc.h>
#define __PIC16F877AH
#define _XTAL_FREQ 2000000
__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);
unsigned int a=0;
void InitPWM(void);
void SetPWMDutyCycle(unsigned int l);
void delay(unsigned int i)
{
while(i--);
}
void InitPWM(void)
{
TRISC2 = 0; // Make CCP1 pin as output
CCP1CON = 0x0C; // Configure CCP1 module in PWM mode
PR2 = 0xFF; // Configure the Timer2 period
T2CON = 0x01; // Set Prescaler to be 4, hence PWM frequency is set to 4.88KHz.
SetPWMDutyCycle(0); // Intialize the PWM to 0 duty cycle
T2CON |= 0x04; // Enable the Timer2, hence enable the PWM.
}
void SetPWMDutyCycle(unsigned int l) // Give a value in between 0 and 1024 for DutyCycle
{
CCPR1L = l>>2; // Put MSB 8 bits in CCPR1L
CCP1CON &= 0xCF; // Make bit4 and 5 zero
CCP1CON |= (0x30&(l<<4)); // Assign Last 2 LSBs to CCP1CON
}
void main(void)
{
InitPWM();
while(1)
{
SetPWMDutyCycle(a); //50% duty cycle
a++;
if(a>1000)
a=0;
}
}
PIC16F1518 4ADC VALUE ON LCD PROJECT
#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define __PIC16F1518_H
#define _XTAL_FREQ 16000000
__CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON & LVP_OFF );
unsigned char z[40],z1[40],z2[40],z3[40];
unsigned int v,t,w,w1,w2,w3;
void delay(unsigned int i)
{
while(i--);
}
void cmd(unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;
PORTB=h|0X40;
delay(20);
PORTB=h|0X00;
PORTB=l|0X40;
delay(20);
PORTB=l|0X00;
}
void dat (unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;
PORTB=h|0X50;
delay(20);
PORTB=h|0X10;
PORTB=l|0X50;
delay(20);
PORTB=l|0X10;
}
void lcd_init()
{
cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);
cmd(0x0c);
delay(100);
}
void string(unsigned char *p)
{
while(*p!='\0')
{
dat(*p);
p++;
}
}
int adc(unsigned char ch)
{
unsigned int x;
TRISA=0XFF;
TRISB=0X00;
TRISC=0XFC;
ANSELA=0x0F;
ADCON0=(0x01)|ch;
ADCON1=0XF0;
delay(100);
GO_nDONE=1;
while(GO_nDONE);
x=(ADRESL+(ADRESH*255));
return x;
}
int main()
{
//LATB = 0x00;
int i=0;
TRISB=0x00;
lcd_init();
while(1)
{
w=adc(0x00);
delay(100);
w1=adc(0x04);
delay(100);
w2=adc(0x08);
delay(100);
w3=adc(0x0C);
v=((w*5)/1023);
t=((w*5)/10.24);
sprintf(z,"a1=%3d v=%2dV t=%3d'c|",w,v,t);
cmd(0x80);
i=0;
while(z[i]!='\0')
{
dat(z[i]);
i++;
}
v=((w1*5)/1023);
t=((w1*5)/10.24);
sprintf(z1,"a2=%3d v=%2dV t=%3d'c|",w1,v,t);
cmd(0xc0);
i=0;
while(z1[i]!='\0')
{
dat(z1[i]);
i++;
}
v=((w2*5)/1023);
t=((w2*5)/10.24);
sprintf(z2,"a3=%3d v=%2dV t=%3d'c",w2,v,t);
cmd(0x95);
i=0;
while(z2[i]!='\0')
{
dat(z2[i]);
i++;
}
v=((w3*5)/1023);
t=((w3*5)/10.24);
sprintf(z3,"a4=%3d v=%2dV t=%3d'c",w3,v,t);
cmd(0xd5);
i=0;
while(z3[i]!='\0')
{
dat(z3[i]);
i++;
}
}
}
PIC16F877A ENVIRONMENT CONTROL UNIT PROJECT
#include<pic.h>
#include<htc.h>
#include<stdio.h>
#include"piclcd.h"
#define _PIC16F877A_H
//__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);
unsigned int volt,adcSmpl,time;
void fan();
int adc()
{
unsigned int x;
GO=1;
while((ADCON0&0x04)==0x04);
x=(ADRESL+(ADRESH*255));
return x;
}
int main()
{
unsigned char z[40];
int i=0;
TRISD=0x00;
TRISC=0x00;
ADCON0=0xc1;
ADCON1=0Xc0;
lcd_init();
cmd(0x80);
string("EMBEDDED_LAB, SUMMITBHARDWAJ.BLOGSPOT.IN");
while(1)
{
adcSmpl=adc();
volt=((adcSmpl*5)/1023);
time=((adcSmpl*5)/10.24);
sprintf(z,"adc=%3d volt=%2dV temp=%3d'c",adcSmpl,volt,time);
cmd(0xc0);
//PORTC=0x02;
fan();
i=0;
while(z[i]!='\0')
{
dat(z[i]);
i++;
}
}
}
void fan()
{
if(adcSmpl <= 62)
{
PORTC=0x01;
}
else if(adcSmpl <= 123)
{
PORTC=0x02;
}
else if(adcSmpl <= 185)
{
PORTC=0x04;
}
else
{
PORTC=0x07;
}
}
//************************************LCD.H******************************//
#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define _PIC16F877A_H
__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);
void delay(unsigned int i)
{
while(i--);
}
void cmd(unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;
PORTD=h|0X40;
delay(20);
PORTD=h|0X00;
PORTD=l|0X40;
delay(20);
PORTD=l|0X00;
}
void dat (unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;
PORTD=h|0X50;
delay(20);
PORTD=h|0X10;
PORTD=l|0X50;
delay(20);
PORTD=l|0X10;
}
void lcd_init()
{
cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);
}
void num(unsigned int p)
{
unsigned int k,w,b=1;
k=p;
while(k>=10)
{
b=b*10;
k=k/10;
}
while(b>=1)
{
w=p/b;
p=p%b;
b=b/10;
dat(w+48);
}
}
void string(unsigned char *p)
{
while(*p!='\0')
{
dat(*p);
p++;
}
}
//*****************************************************************//
Subscribe to:
Posts (Atom)