View
224
Download
1
Category
Preview:
Citation preview
7/22/2019 Db-dp113 Sample Code
1/68
PICDEM 2 PLUS
SAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver1.0
7/22/2019 Db-dp113 Sample Code
2/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver1.0_Page1
Sample 1. Light LEDs Associated With PORTB ................................................. 2
Sample 2. Make the Buzzer Beep ........................................................................ 3
Sample 3. Read Temperature f rom Buil t-in IIC Temperature Sensor............... 4Sample 4. How to Read and Write to the Data EEPROM Memory .................... 7
Sample 5. Transmit Data through the USART of PIC18F4520 ........................ 10
Sample 6. How to Display Information on 7-segment LEDs ............................11
Sample 7. How to Display Information on HD44780 LCD Module .................. 13
Sample 8. LED Mode Code................................................................................. 30
Sample 9. Display Information on the LCD Module......................................... 40
7/22/2019 Db-dp113 Sample Code
3/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page2
Sample 1. Light LEDs Associated With PORTB
This Sample will show you how to illuminate LED associated with PORTB of PIC18F4520
microcontroller. LEDs are connected to PORTB1-3, when those pins are set to low,
corresponding LED would be illuminated.
#include #include
#pragma config OSC = HSPLL // High-Speed Crystal/Resonator
//with PLL enabled
#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
// Initializtionvoid init(void)
{CMCON=0b00000111; // Close Comparator
TRISA=0b00010000;TRISB=0b00000001;TRISC=0b00000000;TRISD=0b00000000;TRISE=0b00001000;ADCON1=0b00001111; // Configure Digital Channel
}// Main Programvoid main( void )
{init(); // Initialize Microchipwhile(1){
PORTBbits.RB1=0;Delay1KTCYx(255);PORTBbits.RB1=1;Delay1KTCYx(255);
PORTBbits.RB2=0;Delay1KTCYx(255);PORTBbits.RB2=1;Delay1KTCYx(255);
7/22/2019 Db-dp113 Sample Code
4/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page3
PORTBbits.RB3=0;Delay1KTCYx(255);
PORTBbits.RB3=1;Delay1KTCYx(255);
}}
Sample 2. Make the Buzzer Beep
In this Sample, we will show you how to make the speaker that connected to PORTC2
(CCP1) pin buzz.
Before starting this test, you should first connect a passive speaker to J 2. The speaker is
connected to the collector of a NPN transistor built in ULN2003 chip. Base of the ULN2003
chip is driven by PORTC2 (CCP1 Pin). When a PWM wave is applied on the PORTC2 pin,
the speaker will start buzzing.
#include #include
#pragma config OSC = HS#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
void init(void);void PWM(unsigned char i);
// Initializtionvoid init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000;TRISB=0b00000001;TRISC=0b00000000;TRISD=0b00000000;TRISE=0b00001000;ADCON1=0b00001111; // Configure Digital Channel
}// Set PWM Modevoid PWM(unsigned char i)
7/22/2019 Db-dp113 Sample Code
5/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page4
{CCP1CON=0b00001100;
T2CONbits.TMR2ON = 0;T2CONbits.T2OUTPS3 = 1;
T2CONbits.T2OUTPS2 = 1;T2CONbits.T2OUTPS1 = 1;T2CONbits.T2OUTPS0 = 1;T2CONbits.T2CKPS1 = 1;T2CONbits.T2CKPS1 = 1;PR2 = 255;
TRISCbits.TRISC2=0;T2CONbits.TMR2ON = 1;CCPR1L = 25*i;
}// Main Programmer
void main( void ){
init(); // Initializtionwhile(1){
PWM(4); //Buzzer BeepDelay1KTCYx(255);PWM(0);Delay1KTCYx(255);
}
}
Sample 3. Read Temperature from Built-in IIC Temperature Sensor
This Sample will show you how to read temperature value from LM75A temperature sensor
via IIC interface of PIC18F4520 control microchip. In this demo code, only IIC operation of
PIC18F4520 control microchip is shown. For hardware IIC chip, you could change sw_i2c.h
to hw_i2c.h and modify this program.
LM75A had been connected to PORTC3 and PORTC4. When correct time sequence has
been applied on those 2 pins, temperature can be obtained from LM75A temperature
sensor.
#include
#include
#pragma config OSC = HS#pragma config PWRT = OFF
#pragma config BOREN = OFF
7/22/2019 Db-dp113 Sample Code
6/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page5
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF#pragma config LVP = OFF
unsigned char i2c_var;
unsigned int cvalue,fvalue; // Memory Centigrade and Fahrenheit value
unsigned char cent_buf[5],fahr_buf[5]; // Centigrade and Fahrenheit value array
// Initializtion
void init(void)
{
CMCON=0b00000111; // Close Comparator
TRISA=0b00010000;
TRISB=0b00000001;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00001000;
ADCON1=0b00001111; // Configure Digital Channel
}
void LM75_init(void) // Temperature Sensor Initializtion
{
SWStartI2C();
i2c_var = SWPutcI2C(0x90); // Control Byte
SWAckI2C();
i2c_var = SWPutcI2C(0x01); // Configure Register
SWAckI2C();
i2c_var = SWPutcI2C(0x18); // Configure ByteSWAckI2C();
SWStopI2C();
}
void LM75_temperature(void)
{
unsigned char tptr[2];
unsigned int temp_H,temp_L;
SWStartI2C();
7/22/2019 Db-dp113 Sample Code
7/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page6
i2c_var = SWPutcI2C(0x90); // Control Byte
SWAckI2C();
i2c_var = SWPutcI2C(0x00); // Data Address
SWAckI2C();SWRestartI2C();
i2c_var = SWPutcI2C(0x91); // Control Byte
SWAckI2C();
i2c_var = SWGetsI2C(tptr, 2); // Read Temperature Value
SWStopI2C();
temp_H=tptr[0]; // High Bits
temp_L=tptr[1]; // Low Bits
// Compute Centigrade
cvalue=(temp_H5;
cvalue=cvalue * 1.25;
cent_buf[1]=cvalue/100+48;
cent_buf[2]=(cvalue/10)%10+48;
cent_buf[3]='.';
cent_buf[4]=cvalue%10+48;
cent_buf[5]='\0';
// Compute Fahrenheit
fvalue=((cvalue*9)/5)+32;
fahr_buf[0]=' ';
if(fvalue&0x80==1)
{
fvalue=~fvalue+1; //Calculate Base Complement
fahr_buf[0]='-';
}
fahr_buf[1]=fvalue/100+48;fahr_buf[2]=(fvalue/10)%10+48;
fahr_buf[3]='.';
7/22/2019 Db-dp113 Sample Code
8/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page7
fahr_buf[4]=fvalue%10+48;
fahr_buf[5]='\0';
}
// Main Programmer
void main( void )
{
init(); // Initialize Control Microchip
LM75_init(); // Temperature Sensor Initializtion
while(1)
{
LM75_temperature(); // Read Temperature Value
}
}
Sample 4. How to Read and Write to the Data EEPROM Memory
This demo code will show you how to read and write data EEPROM memory microchip
24C04 via PIC18F4520 control microchip.
24C04 EEPROM memory microchip is connected to PORTC3 and PORTC4 of
PIC18F4520 microchip, when correct time sequence is applied on 24C04, it could be read
or written to.
#include #include #include
#pragma config OSC = HS#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
unsigned char i2c_var;unsigned char wr_data[]={0x0a,0x0b,0x0c,0x0d};unsigned char rd_data[4];
// Initializtion
7/22/2019 Db-dp113 Sample Code
9/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page8
void init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000; // RA4 is for Switch SW1 Input
TRISB=0b00000001; // RB0 is for Switch SW2 InputTRISC=0b00000000;TRISD=0b00000000;TRISE=0b00001000; // RE3 is for Switch SW3 InputADCON1=0b00001111; // Configure Digital Channel
}// Write datavoid byte_write(unsigned char adr,unsigned char data){
SWStartI2C();
i2c_var = SWPutcI2C(0xA0); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word addressSWAckI2C();i2c_var = SWPutcI2C(data); // Write DataSWAckI2C();SWStopI2C();
}// Read data
void byte_read(unsigned char adr){SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C( 0xA1 ); // Control ByteSWAckI2C();i2c_var = SWGetcI2C(); // Get Data
SWStopI2C();}// Write stringvoid page_write(unsigned char adr,unsigned char wdata[]){
SWStartI2C();i2c_var = SWPutcI2C(0xA0); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();
i2c_var = SWPutsI2C(wdata); // Get DataSWStopI2C();
}
7/22/2019 Db-dp113 Sample Code
10/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page9
// Read stringvoid sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len)
{SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C( 0xA1 ); // Control ByteSWAckI2C();i2c_var = SWGetsI2C(rdata,len); // Get DataSWStopI2C();
}// Inquiries confirmedvoid ack_poll( void ){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control Bytewhile( SWAckI2C() ){
SWRestartI2C();
i2c_var = SWPutcI2C(0xA0); // Write Data}SWStopI2C();
}// Main Programmervoid main( void ){
init(); // Initialize Control Microchip
while(1)
{ack_poll();page_write(0x02,wr_data);ack_poll();Nop();sequential_read(0x02,rd_data,4);Nop();
}
}
7/22/2019 Db-dp113 Sample Code
11/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page10
Sample 5. Transmit Data through the USART of PIC18F4520
This Sample will show you how to transmit data through USART of PIC18F4520
microcontroller. User must configure Hyper Terminal or other terminal software in PC
correctly, and then connect it to this development board.
A single-chip USB to UART Bridge CP2102 is connected to PORTC6/TX, PORTC 7/ RX of
PIC18F4520 microcontroller.
#include #include #include
#include
#pragma config OSC = HS#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
// Initializtionvoid init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000; // RA4 is for Switch SW1 InputTRISB=0b00000001; // RB0 is for Switch SW2 InputTRISC=0b00000000;TRISD=0b00000000;TRISE=0b00001000; // RE3 is for Switch SW3 InputADCON1=0b00001111; // Configure Digital Channel
SPBRG=38; // Baud Rate 4800bpsBAUDCONbits.BRG16=0; // Choose 8-bit Baud Rate GeneratorTXSTAbits.BRGH=0; // High Baud RateTXSTAbits.SYNC=0; // Asynchronous ModeRCSTAbits.SPEN=1; // Enable Serial Port
TXSTAbits.TX9=0; // Transmit 8-bit dataTXSTAbits.TXEN=1; // Enable Transmission
}// Main Programmervoid main( void )
{union USART USART_Status;char wr_data[]={"world"};
7/22/2019 Db-dp113 Sample Code
12/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page11
char rd_data[7];
init(); // Initializtion Program
WriteUSART('Q'); // Transmit Q
putsUSART(wr_data); // Transmit Data
while(!PIR1bits.TXIF)continue;
}
Sample 6. How to Display Information on 7-segment LEDs
This demonstration board shows how to display information on 7-segment LEDs through
GPIOs of PIC18F4520 microchip.
PORTD is connected to a-h pins of 7segment LEDs, and PORTB4-7 is connected to
ULN2003 chip, that drive 4 cathodes of those LEDs.
Remove the LCD panel above the LED segments.Transmit data via PORTD and enable one of the cathodes, then the LED segments will
display a digit letter. If switch of cathode is quick enough, it will display 4 digits and looks
seamlessly.
#include #include
#pragma config OSC = HSPLL#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
#define LED0 PORTDbits.RD0#define LED1 PORTDbits.RD1#define LED2 PORTDbits.RD2#define LED3 PORTDbits.RD3
#define LED4 PORTDbits.RD4#define LED5 PORTDbits.RD5#define LED6 PORTDbits.RD6
7/22/2019 Db-dp113 Sample Code
13/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page12
#define LED7 PORTDbits.RD7
#define LEDbuf0 PORTBbits.RB4#define LEDbuf1 PORTBbits.RB5
#define LEDbuf2 PORTBbits.RB6#define LEDbuf3 PORTBbits.RB7
// Initializtionvoid init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000; // RA4 is for Switch SW1 InputTRISB=0b00000001; // RB0 is for Switch SW2 InputTRISC=0b00000000;TRISD=0b00000000;
TRISE=0b00001000; // RE3 is for Switch SW3 InputADCON1=0b00001111; // Configure Digital Channel
}// Main Programmervoid main( void ){
init();while(1){
LEDbuf0=0;LEDbuf1=0;LEDbuf2=0;LEDbuf3=1;LED0=0;LED1=1;LED2=1;LED3=0;LED4=0;LED5=0;LED6=0;
LED7=0;Delay100TCYx(2);
LEDbuf0=0;LEDbuf1=0;LEDbuf2=1;LEDbuf3=0;LED0=1;LED1=1;LED2=0;LED3=1;
LED4=1;LED5=0;LED6=1;
7/22/2019 Db-dp113 Sample Code
14/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page13
LED7=0;Delay100TCYx(2);
LEDbuf0=0;LEDbuf1=1;LEDbuf2=0;LEDbuf3=0;LED0=1;LED1=1;LED2=1;LED3=1;LED4=0;LED5=0;LED6=1;LED7=0;Delay100TCYx(2);
LEDbuf0=1;LEDbuf1=0;LEDbuf2=0;LEDbuf3=0;LED0=0;LED1=1;LED2=1;
LED3=0;LED4=0;LED5=1;LED6=1;LED7=0;Delay100TCYx(2);
}}
Sample 7. How to Display Information on HD44780 LCD Module
This Sample will show you how to display information on HD44780 compatible LCD Module.
The LCD module is connected to PORTD (as data port), PORTA1 (E signal of LCD module),
PORTA2 (RW signal of LCD module), and PORTA3 (RS signal of LCD module). Before
using this function, you should install the LCD panel with screws. Adjust R28 to change the
contrast of the LCD panel.
#include "p18f4520.h"#include
7/22/2019 Db-dp113 Sample Code
15/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page14
#include #include
#pragma config OSC = HS
#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
/**************************************************************************/
/* 8-bit or 4-bit interface type* For 8-bit operation uncomment the #define BIT8
*/#define BIT8
/* When in 4-bit interface define if the data is in the upper* or lower nibble. For lower nibble, comment the #define UPPER*/
/* #define UPPER */
/* DATA_PORT defines the port to which the LCD data lines are connected */#define DATA_PORT PORTD
#define TRIS_DATA_PORT TRISD
/* CTRL_PORT defines the port which the control signals are connected to.Following codes are just for references. They may be amended to match yourapplication.
*/#define RW_PIN PORTAbits.RA2 /* PORT for RW */#define TRIS_RW DDRAbits.RA2 /* TRIS for RW */#define RS_PIN PORTAbits.RA3 /* PORT for RS */#define TRIS_RS DDRAbits.RA3 /* TRIS for RS */#define E_PIN PORTAbits.RA1 /* PORT for E */
#define TRIS_E DDRAbits.RA1 /* TRIS for E */
/* Display ON/OFF Control defines */#define DON 0b00001111 /* Display on */#define DOFF 0b00001011 /* Display off */#define CURSOR_ON 0b00001111 /* Cursor on */#define CURSOR_OFF 0b00001101 /* Cursor off */#define BLINK_ON 0b00001111 /* Cursor Blink */#define BLINK_OFF 0b00001110 /* Cursor No Blink */
/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */#define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */#define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */
7/22/2019 Db-dp113 Sample Code
16/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page15
#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */
/* Function Set defines */
#define FOUR_BIT 0b00101111 /* 4-bit Interface */#define EIGHT_BIT 0b00111111 /* 8-bit Interface */#define LINE_5X7 0b00110011 /* 5x7 characters, single line */#define LINE_5X10 0b00110111 /* 5x10 characters */#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
#define PARAM_SCLASS auto#define MEM_MODEL far /* Change this to near for small memory model */
/* OpenXLCD* Configures I/O pins for external LCD*/
void OpenXLCD(PARAM_SCLASS unsigned char);
/* SetCGRamAddr* Sets the character generator address*/
void SetCGRamAddr(PARAM_SCLASS unsigned char);
/* SetDDRamAddr* Sets the display data address
*/void SetDDRamAddr(PARAM_SCLASS unsigned char);
/* BusyXLCD* Returns the busy status of the LCD*/
unsigned char BusyXLCD(void);
/* ReadAddrXLCD* Reads the current address*/
unsigned char ReadAddrXLCD(void);
/* ReadDataXLCD* Reads a byte of data*/
char ReadDataXLCD(void);
/* WriteCmdXLCD* Writes a command to the LCD*/
void WriteCmdXLCD(PARAM_SCLASS unsigned char);
/* WriteDataXLCD* Writes a data byte to the LCD
7/22/2019 Db-dp113 Sample Code
17/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page16
*/void WriteDataXLCD(PARAM_SCLASS char);
/* putcXLCD
* A putc is a write*/
#define putcXLCD WriteDataXLCD
/* putsXLCD* Writes a string of characters to the LCD*/
void putsXLCD(PARAM_SCLASS char *);
/* putrsXLCD* Writes a string of characters in ROM to the LCD
*/void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *);
/* User defines these routines according to the oscillator frequency */extern void DelayFor18TCY(void);extern void DelayPORXLCD(void);extern void DelayXLCD(void);
/********************************************************************
* Function Name: BusyXLCD ** Return Value: char: busy status of LCD controller ** Parameters: void ** Description: This routine reads the busy status of the ** Hitachi HD44780 LCD controller. *********************************************************************/unsigned char BusyXLCD(void){
RW_PIN = 1; // Set the control bits for readRS_PIN = 0;DelayFor18TCY();
E_PIN = 1; // Clock in the commandDelayFor18TCY();
#ifdef BIT8 // 8-bit interfaceif(DATA_PORT&0x80) // Read bit 7 (busy bit){ // If high
E_PIN = 0; // Reset clock lineRW_PIN = 0; // Reset control linereturn 1; // Return TRUE
}else // Bit 7 low{
E_PIN = 0; // Reset clock lineRW_PIN = 0; // Reset control linereturn 0; // Return FALSE
7/22/2019 Db-dp113 Sample Code
18/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page17
}#else // 4-bit interface#ifdef UPPER // Upper nibble interface
if(DATA_PORT&0x80)#else // Lower nibble interface
if(DATA_PORT&0x08)#endif
{E_PIN = 0; // Reset clock lineDelayFor18TCY();E_PIN = 1; // Clock out other nibbleDelayFor18TCY();E_PIN = 0;RW_PIN = 0; // Reset control linereturn 1; // Return TRUE
}else // Busy bit is low{
E_PIN = 0; // Reset clock lineDelayFor18TCY();E_PIN = 1; // Clock out other nibbleDelayFor18TCY();E_PIN = 0;RW_PIN = 0; // Reset control line
return 0; // Return FALSE}#endif}/******************************************************************************************** Function Name: OpenXLCD ** Return Value: void ** Parameters: lcdtype: set the type of LCD (lines) ** Description: This routine configures the LCD. Based on ** the Hitachi HD44780 LCD controller. The *
* routine will configure the I/O pins of the ** microcontroller, setup the LCD for 4- or ** 8-bit mode and clear the display. The user ** must provide three delay routines: ** DelayFor18TCY() provides a 18 Tcy delay ** DelayPORXLCD() provides at least 15ms delay ** DelayXLCD() provides at least 5ms delay ******************************************************************************************/void OpenXLCD(unsigned char lcdtype){
// The data bits must be from either a 8-bit port or the upper and// lower 4-bit port. These pins should be set to input
#ifdef BIT8 // 8-bit mode, use whole portDATA_PORT &= 0;
7/22/2019 Db-dp113 Sample Code
19/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page18
TRIS_DATA_PORT |= 0xff;#else // 4-bit mode#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;
TRIS_DATA_PORT |= 0xf0;#else // Lower 4-bits of the port
DATA_PORT &= 0xf0;TRIS_DATA_PORT |= 0x0f;
#endif#endif
TRIS_RW = 0; // All control signals made outputsTRIS_RS = 0;TRIS_E = 0;RW_PIN = 0; // R/W pin made lowRS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low
// Delay for 15ms to allow for LCD Power on resetDelayPORXLCD();
// Setup interface to LCD#ifdef BIT8 // 8-bit mode interface
TRIS_DATA_PORT &= 0; // Data port outputDATA_PORT &= 0;DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit mode interface#ifdef UPPER // Upper nibble interfaceTRIS_DATA_PORT &= 0x0f;DATA_PORT &= 0x0f;DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface)
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0;DATA_PORT &= 0xf0;DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface)
#endif#endif
E_PIN = 1; // Clock the cmd inDelayFor18TCY();E_PIN = 0;
// Delay for at least 4.1msDelayXLCD();
// Setup interface to LCD#ifdef BIT8 // 8-bit interface
DATA_PORT &= 0;DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
7/22/2019 Db-dp113 Sample Code
20/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page19
DATA_PORT |= 0b00100000;#else // Lower nibble interface
DATA_PORT &= 0xf0; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00000010;#endif#endif
E_PIN = 1; // Clock the cmd inDelayFor18TCY();E_PIN = 0;
// Delay for at least 100usDelayXLCD();
// Setup interface to LCD#ifdef BIT8 // 8-bit interface
DATA_PORT &= 0;DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)DATA_PORT |= 0b00100000;
#else // Lower nibble interfaceDATA_PORT &= 0xf0; // Function set cmd(4-bit interface)DATA_PORT |= 0b00000010;
#endif#endifE_PIN = 1; // Clock cmd inDelayFor18TCY();E_PIN = 0;
#ifdef BIT8 // 8-bit interfaceTRIS_DATA_PORT |= 0xff; // Make data port input
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make data nibble input
#else // Lower nibble interfaceTRIS_DATA_PORT |= 0x0f; // Make data nibble input
#endif#endif
// Set data interface width, #lines, fontwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(lcdtype); // Function set cmd
// Turn the display on then offwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(DOFF); // Display OFF/Blink OFFwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(DON); // Display ON/Blink ON
7/22/2019 Db-dp113 Sample Code
21/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page20
while(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(BLINK_OFF&CURSOR_OFF); // Display ON/Blink
ON
// Clear displaywhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(0x01); // Clear display
// Set entry mode inc, no shiftwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode
// Set DD Ram address to 0while(BusyXLCD()); // Wait if LCD busy
SetDDRamAddr(0); // Set Display data ram address to 0
return;}
/********************************************************************* Function Name: putrsXLCD* Return Value: void
* Parameters: buffer: pointer to string* Description: This routine writes a string of bytes to the* Hitachi HD44780 LCD controller. The user* must check to see if the LCD controller is* busy before calling this routine. The data* is written to the character generator RAM or* the display data RAM depending on what the* previous SetxxRamAddr routine was called.********************************************************************//*void putrsXLCD(const rom char *buffer)
{while(*buffer) // Write data to LCD up to null{
while(BusyXLCD()); // Wait while LCD is busyWriteDataXLCD(*buffer); // Write character to LCDbuffer++; // Increment buffer
}return;
}*/
/********************************************************************* Function Name: putsXLCD* Return Value: void
7/22/2019 Db-dp113 Sample Code
22/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page21
* Parameters: buffer: pointer to string* Description: This routine writes a string of bytes to the* Hitachi HD44780 LCD controller. The user
* must check to see if the LCD controller is* busy before calling this routine. The data* is written to the character generator RAM or* the display data RAM depending on what the* previous SetxxRamAddr routine was called.********************************************************************/void putsXLCD(char *buffer){
while(*buffer) // Write data to LCD up to null{
while(BusyXLCD()); // Wait while LCD is busyWriteDataXLCD(*buffer); // Write character to LCDbuffer++; // Increment buffer
}return;
}
/********************************************************************** Function Name: ReadAddrXLCD ** Return Value: char: address from LCD controller *
* Parameters: void ** Description: This routine reads an address byte from the ** Hitachi HD44780 LCD controller. The user ** must check to see if the LCD controller is ** busy before calling this routine. The address** is read from the character generator RAM or ** the display data RAM depending on what the ** previous SetxxRamAddr routine was called. **********************************************************************/unsigned char ReadAddrXLCD(void){
char data; // Holds the data retrieved from the LCD
#ifdef BIT8 // 8-bit interfaceRW_PIN = 1; // Set control bits for the readRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock data out of the LCD controllerDelayFor18TCY();data = DATA_PORT; // Save the data in the registerE_PIN = 0;RW_PIN = 0; // Reset the control bits
#else // 4-bit interfaceRW_PIN = 1; // Set control bits for the readRS_PIN = 0;
7/22/2019 Db-dp113 Sample Code
23/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page22
DelayFor18TCY();E_PIN = 1; // Clock data out of the LCD controllerDelayFor18TCY();
#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibble ofdata#else // Lower nibble interface
data = (DATA_PORT4)&0x0f; // Read the nibble into the lower nibble ofdata#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble ofdata#endif
E_PIN = 0;RW_PIN = 0; // Reset the control lines
#endifreturn (data&0x7f); // Return the address, Mask off the busy bit
}
/********************************************************************* Function Name: ReadDataXLCD ** Return Value: char: data byte from LCD controller ** Parameters: void ** Description: This routine reads a data byte from the ** Hitachi HD44780 LCD controller. The user ** must check to see if the LCD controller is ** busy before calling this routine. The data *
* is read from the character generator RAM or ** the display data RAM depending on what the ** previous SetxxRamAddr routine was called. *********************************************************************/char ReadDataXLCD(void){
char data;
#ifdef BIT8 // 8-bit interfaceRS_PIN = 1; // Set the control bitsRW_PIN = 1;
DelayFor18TCY();E_PIN = 1; // Clock the data out of the LCDDelayFor18TCY();
7/22/2019 Db-dp113 Sample Code
24/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page23
data = DATA_PORT; // Read the dataE_PIN = 0;RS_PIN = 0; // Reset the control bits
RW_PIN = 0;#else // 4-bit interface
RW_PIN = 1;RS_PIN = 1;DelayFor18TCY();E_PIN = 1; // Clock the data out of the LCDDelayFor18TCY();
#ifdef UPPER // Upper nibble interfacedata = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interfacedata = (DATA_PORT4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interfacedata |= DATA_PORT&0x0f; // Read the lower nibble of data
#endif
E_PIN = 0;RS_PIN = 0; // Reset the control bitsRW_PIN = 0;
#endifreturn(data); // Return the data byte
}
/********************************************************************* Function Name: SetCGRamAddr ** Return Value: void *
* Parameters: CGaddr: character generator ram address ** Description: This routine sets the character generator ** address of the Hitachi HD44780 LCD ** controller. The user must check to see if ** the LCD controller is busy before calling ** this routine. *********************************************************************/void SetCGRamAddr(unsigned char CGaddr){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouputDATA_PORT = CGaddr | 0b01000000; // Write cmd and address to portRW_PIN = 0; // Set control signalsRS_PIN = 0;
7/22/2019 Db-dp113 Sample Code
25/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page24
DelayFor18TCY();E_PIN = 1; // Clock cmd and address inDelayFor18TCY();E_PIN = 0;
DelayFor18TCY();TRIS_DATA_PORT = 0xff; // Make data port inputs
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble inputDATA_PORT &= 0x0f; // and write upper nibbleDATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0; // Make nibble inputDATA_PORT &= 0xf0; // and write upper nibbleDATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endifRW_PIN = 0; // Set control signalsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock cmd and address inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f; // Write lower nibbleDATA_PORT |= ((CGaddr
7/22/2019 Db-dp113 Sample Code
26/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page25
* is busy before calling this routine. *********************************************************************/void SetDDRamAddr(unsigned char DDaddr)
{#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port outputDATA_PORT = DDaddr | 0b10000000; // Write cmd and address to portRW_PIN = 0; // Set the control bitsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock the cmd and address inDelayFor18TCY();E_PIN = 0;DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port outputDATA_PORT &= 0x0f; // and write upper nibbleDATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0; // Make port outputDATA_PORT &= 0xf0; // and write upper nibbleDATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endif RW_PIN = 0; // Set control bitsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock the cmd and address inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f; // Write lower nibbleDATA_PORT |= ((DDaddr
7/22/2019 Db-dp113 Sample Code
27/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page26
}
/********************************************************************
* Function Name: WriteCmdXLCD ** Return Value: void ** Parameters: cmd: command to send to LCD ** Description: This routine writes a command to the Hitachi** HD44780 LCD controller. The user must check ** to see if the LCD controller is busy before ** calling this routine. *********************************************************************/void WriteCmdXLCD(unsigned char cmd){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT &= 0; // Data port outputDATA_PORT &= 0;DATA_PORT |= cmd; // Write command to data portRW_PIN = 0; // Set the control signalsRS_PIN = 0; // for sending a commandDelayFor18TCY();E_PIN = 1; // Clock the command inDelayFor18TCY();E_PIN = 0;DelayFor18TCY();
TRIS_DATA_PORT |= 0xff; // Data port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;DATA_PORT &= 0x0f;DATA_PORT |= cmd&0xf0;
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0;DATA_PORT &= 0xf0;DATA_PORT |= (cmd>>4)&0x0f;
#endif
RW_PIN = 0; // Set control signals for commandRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock command inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f;DATA_PORT |= (cmd
7/22/2019 Db-dp113 Sample Code
28/68
7/22/2019 Db-dp113 Sample Code
29/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page28
RW_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock nibble into LCDDelayFor18TCY();
E_PIN = 0;#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;DATA_PORT |= ((data
7/22/2019 Db-dp113 Sample Code
30/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page29
Delay1KTCYx(25); // Delay of 5ms// Cycles = (TimeDelay * Fosc) / 4// Cycles = (5ms * 20MHz) / 4
// Cycles = 25,000return;
}// Initializtionvoid init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000;TRISB=0b00000001;TRISC=0b00000000;TRISD=0b00000000;TRISE=0b00001000;ADCON1=0b00001111; // Configure Digital Channel
}// Main Programmervoid main( void ){
unsigned char i;char display_name[]="Sure Electronics";
char display_Ver[]="Ver 2.1";
init();
// Configure external LCDOpenXLCD( EIGHT_BIT&LINES_5X7 );
// Write to LCDwhile(BusyXLCD()); // Wait if LCD busyputsXLCD(display_name);
// Write to LCDwhile(BusyXLCD()); // Wait if LCD busySetDDRamAddr(0x40);putsXLCD(display_Ver);
while(1){};}
7/22/2019 Db-dp113 Sample Code
31/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page30
Sample 8. LED Mode Code
This demonstration shows how to display message on the 7segment LEDs through GPIOs
of PIC18F4520 microcontroller.
The 8 bits data output of PORTD is connected to a-h of 7segment LEDs, and PORTB4-7 is
connected to base of ULN2003, then drive 4 cathode of those LEDs. Remove the LCD
panel on the LED segments. Send data from PORTD and enable one of the cathodes. The
LED segments will display a digit. If switch-on and switch-off of the four cathodes in turn
quickly enough, it will display 4 digits just like simultaneously. That looks seamlessly.
#include
#include #include #include #include
#pragma config OSC = HSPLL // High-Speed Crystal/Resonator
// with PLL enabled
#pragma config PWRT = OFF#pragma config BOREN = OFF#pragma config WDT = OFF#pragma config MCLRE = ON#pragma config PBADEN = OFF#pragma config LVP = OFF
#define leddata_port PORTD#define ledcom_port0 PORTBbits.RB4#define ledcom_port1 PORTBbits.RB5#define ledcom_port2 PORTBbits.RB6#define ledcom_port3 PORTBbits.RB7#define Func_key PORTBbits.RB0 // Function key#define change_key PORTAbits.RA4 // Switch key
unsigned char i2c_var;unsigned int cvalue,fvalue; // Memory Centigrade and Fahrenheit valuechar cent_buf[6],fahr_buf[6]; // Centigrade and Fahrenheit value array
unsigned char reset_key_pressed,last_reset_key_pressed;unsigned char Func_key_pressed,last_Func_key_pressed;unsigned char change_key_pressed,last_change_key_pressed;unsigned char LED_thousand,LED_hundred,LED_ten,LED_one;unsigned char value_thousand,value_hundred,value_ten,value_one;unsigned char temperature_flag;
unsigned char cnt_fuckey;unsigned char key_flag=0;char pointer=0;
7/22/2019 Db-dp113 Sample Code
32/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page31
// a total of cathodicconst unsigned char Digital_TAB[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x6d,0x1c,0x50,0x79,0x86,0x06}; // 0123456789 SurE 1.1
void timer_isr (void); // Interruptvoid init(void); // Initialize Control Microchipchar Funckey(void); // Get State of Function Switchvoid changekey(void); // Get State of Change Display Switchvoid LED_init(void); // Set LED Initial Displayvoid PWM(unsigned char i); // PWM Modevoid LED_data(unsigned int data); // Data for LED Displayvoid LED_display(unsigned char i); // LED Displayvoid sendUSART(char *sendbuf); // Send Data to PC
/**************************************************************************/void init(void){
CMCON=0b00000111; // Close ComparatorTRISA=0b00010000; // RA4 is for Switch SW1 InputTRISB=0b00000001; // RB0 is for Switch SW2 InputTRISC=0b11000000;
TRISD=0b00000000;TRISE=0b00001000; // RE3 is for Switch SW3 InputADCON1=0b00001111; // Digital Channel Allocation
SPBRG=155; // Baud Rate 4800bpsBAUDCONbits.BRG16=0; // Choose 8-bit Baud Rate Generator
TXSTAbits.BRGH=0; // High Baud RateTXSTAbits.SYNC=0; // Asynchronous ModeRCSTAbits.SPEN=1; // Enable Serial
TXSTAbits.TX9=0; // 8-bit TransmissionTXSTAbits.TXEN=1; // Enable Transmission
}/**************************************************************************/void PWM(unsigned char i){
CCP1CON=0b00001100;T2CONbits.TMR2ON = 0;T2CONbits.T2OUTPS3 = 1;T2CONbits.T2OUTPS2 = 1;T2CONbits.T2OUTPS1 = 1;T2CONbits.T2OUTPS0 = 1;T2CONbits.T2CKPS1 = 1;T2CONbits.T2CKPS1 = 1;PR2 = 255;
TRISCbits.TRISC2=0;
7/22/2019 Db-dp113 Sample Code
33/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page32
T2CONbits.TMR2ON = 1;CCPR1L = 25*i;
}/**************************************************************************/
char Funckey(void){
unsigned char temp;Func_key_pressed=Func_key;if((Func_key_pressed==0)&(last_Func_key_pressed==1)){
Delay100TCYx(1); //Delay to avoid unexpected buffetingif(Func_key_pressed==0)cnt_fuckey++;
}last_Func_key_pressed=Func_key_pressed;
temp=cnt_fuckey%4;return temp;
}void changekey(void){
change_key_pressed=change_key;if((change_key_pressed==0)&(last_change_key_pressed==1)){
Delay100TCYx(1); //Delay to avoid unexpected buffeting
if(change_key_pressed==0)return;}last_change_key_pressed=change_key_pressed;
}/**************************************************************************/// Write datavoid byte_write(unsigned char adr,unsigned char data){
SWStartI2C();i2c_var = SWPutcI2C(0xA0); // Control Byte
SWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();i2c_var = SWPutcI2C(data); // Write DataSWAckI2C();SWStopI2C();
}// Read datavoid byte_read(unsigned char adr){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control ByteSWAckI2C();
7/22/2019 Db-dp113 Sample Code
34/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page33
i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();SWRestartI2C();
i2c_var = SWPutcI2C( 0xA1 ); // Control ByteSWAckI2C();i2c_var = SWGetcI2C();//dataSWStopI2C();
}// Write stringvoid page_write(unsigned char adr,unsigned char wdata[]){
SWStartI2C();i2c_var = SWPutcI2C(0xA0); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();i2c_var = SWPutsI2C(wdata); // Write DataSWStopI2C();
}// Read stringvoid sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control ByteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word AddressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C( 0xA1 ); // Control ByteSWAckI2C();i2c_var = SWGetsI2C(rdata,len); // Get DataSWStopI2C();
}
// Inquiries confirmedvoid ack_poll( void ){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control Bytewhile( SWAckI2C() ){
SWRestartI2C();i2c_var = SWPutcI2C(0xA0); // Write Data
}SWStopI2C();
}
7/22/2019 Db-dp113 Sample Code
35/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page34
/**************************************************************************/void LM75_init(void) //Temperature Sensor Initializtion{
SWStartI2C();
i2c_var = SWPutcI2C(0x90); //Control ByteSWAckI2C();i2c_var = SWPutcI2C(0x01); //Configure RegisterSWAckI2C();i2c_var = SWPutcI2C(0x18); //Configure ByteSWAckI2C();SWStopI2C();
}void LM75_temperature(void){
unsigned char tptr[2];unsigned int temp_H,temp_L;SWStartI2C();i2c_var = SWPutcI2C(0x90); //control byteSWAckI2C();i2c_var = SWPutcI2C(0x00); //Data AddressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C(0x91); //Control ByteSWAckI2C();
i2c_var = SWGetsI2C(tptr, 2); //Read TemperatureSWStopI2C();
temp_H=tptr[0]; //High bitstemp_L=tptr[1]; //Low bits
//Compute Centigradecvalue=(temp_H5;cvalue=cvalue * 1.25;cent_buf[1]=cvalue/100+48;cent_buf[2]=(cvalue/10)%10+48;cent_buf[3]='.';cent_buf[4]=cvalue%10+48;cent_buf[5]='\0';
//compute Fahrenheitfvalue=((cvalue*9)/5)+32;fahr_buf[0]=' ';
7/22/2019 Db-dp113 Sample Code
36/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page35
if(fvalue&0x80==1){
fvalue=~fvalue+1; //Calculate Base Complement
fahr_buf[0]='-';}fahr_buf[1]=fvalue/100+48;fahr_buf[2]=(fvalue/10)%10+48;fahr_buf[3]='.';fahr_buf[4]=fvalue%10+48;fahr_buf[5]='\0';
}/**************************************************************************/void LED_init(void){
unsigned char i,j;unsigned char temp;while(1){
i++;j=i%4;LED_display(j); //Display Characters
if(i==1)
{ temp++;if(temp==2) //Display "Sure" when Power on or after Resetting{
value_thousand=0x6d;value_hundred=0x1c;value_ten=0x50;value_one=0x79;
}if(temp==120) //Then Display "1.1" at some Intervals{
value_thousand=0x00;value_hundred=0x00;value_ten=0x86;value_one=0x06;
}}if(temp==255)break;
}}void LED_display(unsigned char i){
ledcom_port0=0;ledcom_port1=0;
7/22/2019 Db-dp113 Sample Code
37/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page36
ledcom_port2=0;ledcom_port3=0;
switch(i)
{case(0):{
ledcom_port3=1;leddata_port=value_thousand; //Display Top Digit
}break;case(1):{
ledcom_port2=1;leddata_port=value_hundred; //Display Second-order Digit
}break;
case(2):{
ledcom_port1=1;leddata_port=value_ten; //Display Third-order Digit
}break;case(3):{
ledcom_port0=1;leddata_port=value_one; //Display Least Significant Digit
}break;
}}void LED_data(unsigned int data){
unsigned int i;LED_thousand=0;LED_hundred=0;LED_ten=0;LED_one=0;
for(i=0;i=10){LED_one=0;LED_ten++;}if(LED_ten>=10){LED_ten=0;LED_hundred++;}if(LED_hundred>=10){LED_hundred=0;LED_thousand++;}
}for(i=0;i
7/22/2019 Db-dp113 Sample Code
38/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page37
if(temperature_flag==1){
value_ten=value_ten|0x80; //Temperature value should have// a decimal point
temperature_flag=0;}
}/**************************************************************************/#pragma code low_vector=0x18void low_interrupt (void){
_asm GOTO timer_isr _endasm}#pragma code#pragma interruptlow timer_isrvoid timer_isr (void){
unsigned char i,j,k;unsigned char temp,send;char Cent[]="Centigrade";char Fahr[]="Fahrenheit";char Cont[]="Contrast";
char Current[]="Current Temperatrue";
TMR0H=0X80;TMR0L=0X00;
i++;j=i%4; //Not more than 4-digit dataLED_display(j); //Display Characters
if(i==0){
LM75_temperature(); //Read Temperature Value}
if(key_flag==0) //Display one piece of information at a time{
k=Funckey();switch(k){
case(1): //Display "Sure"{
value_thousand=0x6d;
value_hundred=0x1c;value_ten=0x50;value_one=0x79;
7/22/2019 Db-dp113 Sample Code
39/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page38
}break;case(2): //Display "1.1"{
value_thousand=0x00;
value_hundred=0x00;value_ten=0x86;value_one=0x06;
}break;case(3): //Display Centigrade Value{
temperature_flag=1;LED_data(cvalue);
}break;case(0): //Display Fahrenheit Value{
temperature_flag=1;LED_data(fvalue);
}break;}if(change_key==0)key_flag=1;
}
if(key_flag==1) //Display information in turn{
if(i%8==0)
{ temp++;if(temp%64==15) //Display "Sure"{
value_thousand=0x6d;value_hundred=0x1c;value_ten=0x50;value_one=0x79;
}if(temp%64==31) //Display "1.1"{
value_thousand=0x00;value_hundred=0x00;value_ten=0x86;value_one=0x06;
}if(temp%64==47) //Display Centigrade Value{
temperature_flag=1;LED_data(cvalue);
}if(temp%64==0) //Display Fahrenheit Value
{temperature_flag=1;LED_data(fvalue);
7/22/2019 Db-dp113 Sample Code
40/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page39
}}if(Func_key==0)key_flag=0;
}
if(i%8==0){
send++;if((send>0)&(send28)&(send36)&(send56)&(send64)&(send
7/22/2019 Db-dp113 Sample Code
41/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page40
}if(send==80){
pointer=0;
TXREG=13;TXREG=10;
}}INTCONbits.TMR0IF = 0;
}void sendUSART(char *sendbuf){
if(*(sendbuf+pointer)!=0){
if(BusyUSART()==0){
TXREG=*(sendbuf+pointer);pointer++;return;
}else{
return;}
}}/**************************************************************************/void main (void){
init(); //Initialize Control MicrochipLM75_init(); //Temperatrue Sensor InitializtionLED_init(); //Open LED to display "SurE" and "1.1"
OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT);INTCONbits.GIE = 1;
while(1){};}
Sample 9. Display Information on the LCD Module.
This Sample shows how to display information on the HD44780 compatible LCD Module.
The LCD module is connected to PORTD (as data port), PORTA1 (E signal of LCD module),
PORTA2 (RW signal of LCD module), and PORTA3 (RS signal of LCD module), PORTC1
(CCP2 as PWM controlled DC voltage generator).
7/22/2019 Db-dp113 Sample Code
42/68
7/22/2019 Db-dp113 Sample Code
43/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page42
/**************************************************************************/void init(void){
CMCON=0b00000111; // Close Comparator
TRISA=0b00010000;TRISB=0b00000001;TRISC=0b11000000;TRISD=0b00000000;TRISE=0b00001000;ADCON1=0b00001111; // Digital Channel Allocation
SPBRG=38; // Baud Rate 4800bpsBAUDCONbits.BRG16=0; // Choose 8-bit Baud Rate Generator
TXSTAbits.BRGH=0; // High Baud RateTXSTAbits.SYNC=0; // Asynchronous Mode
RCSTAbits.SPEN=1; // Enable SerialTXSTAbits.TX9=0; // 8-bit TransmissionTXSTAbits.TXEN=1; // Enable Transmission
CCP1CON=0b00001100; // Set PWM ModeCCP2CON=0b00001100;
T2CONbits.TMR2ON = 0;T2CONbits.T2OUTPS3 = 1;T2CONbits.T2OUTPS2 = 1;T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS0 = 1;T2CONbits.T2CKPS1 = 1;T2CONbits.T2CKPS1 = 1;PR2 = 255;
TRISCbits.TRISC2=0;TRISCbits.TRISC1=0;T2CONbits.TMR2ON = 1;CCPR1L = 100;CCPR2L = 0;
}
/**************************************************************************/void PWM(unsigned char i){
CCP1CON=0b00001100;T2CONbits.TMR2ON = 0;T2CONbits.T2OUTPS3 = 1;T2CONbits.T2OUTPS2 = 1;T2CONbits.T2OUTPS1 = 1;T2CONbits.T2OUTPS0 = 1;T2CONbits.T2CKPS1 = 1;T2CONbits.T2CKPS1 = 1;
PR2 = 255;TRISCbits.TRISC2=0;T2CONbits.TMR2ON = 1;
7/22/2019 Db-dp113 Sample Code
44/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page43
CCPR1L = 25*i;}
/**************************************************************************/char Funckey(void){
unsigned char temp;Func_key_pressed=Func_key;if((Func_key_pressed==0)&(last_Func_key_pressed==1)){
Delay100TCYx(1); //Delay to avoid buffetingif(Func_key_pressed==0)cnt_fuckey++;
}last_Func_key_pressed=Func_key_pressed;
temp=cnt_fuckey%3;return temp;
}char changekey(void){
unsigned char temp;change_key_pressed=change_key;if((change_key_pressed==0)&(last_change_key_pressed==1))
{ Delay100TCYx(1); //Delay to avoid buffetingif(change_key_pressed==0)cnt_chgkey++;
}last_change_key_pressed=change_key_pressed;
temp=cnt_chgkey%11;return temp;
}/**************************************************************************/
// Write datavoid byte_write(unsigned char adr,unsigned char data){
SWStartI2C();i2c_var = SWPutcI2C(0xA0); // Control byteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word addressSWAckI2C();i2c_var = SWPutcI2C(data); // DataSWAckI2C();SWStopI2C();
}// Read data
7/22/2019 Db-dp113 Sample Code
45/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page44
void byte_read(unsigned char adr){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control byte
SWAckI2C();i2c_var = SWPutcI2C(adr); // Word addressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C( 0xA1 ); // Control byteSWAckI2C();i2c_var = SWGetcI2C();//dataSWStopI2C();
}// Write string
void page_write(unsigned char adr,unsigned char wdata[]){
SWStartI2C();i2c_var = SWPutcI2C(0xA0); // Control byteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word addressSWAckI2C();i2c_var = SWPutsI2C(wdata); // DataSWStopI2C();
}
// Read stringvoid sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control byteSWAckI2C();i2c_var = SWPutcI2C(adr); // Word addressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C( 0xA1 ); // Control byte
SWAckI2C();i2c_var = SWGetsI2C(rdata,len); // DataSWStopI2C();
}// Inquiries confirmedvoid ack_poll( void ){
SWStartI2C();i2c_var = SWPutcI2C( 0xA0 ); // Control bytewhile( SWAckI2C() )
{SWRestartI2C();i2c_var = SWPutcI2C(0xA0); // Data
7/22/2019 Db-dp113 Sample Code
46/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page45
}SWStopI2C();
}
/**************************************************************************/void LM75_init(void) // Temperature Sensor Initializtion{
SWStartI2C();i2c_var = SWPutcI2C(0x90); // Control byteSWAckI2C();i2c_var = SWPutcI2C(0x01); // Configure registerSWAckI2C();i2c_var = SWPutcI2C(0x18); // Configure byteSWAckI2C();SWStopI2C();
}void LM75_temperature(void){
unsigned char tptr[2];unsigned int temp_H,temp_L;
SWStartI2C();i2c_var = SWPutcI2C(0x90); // Control byteSWAckI2C();
i2c_var = SWPutcI2C(0x00); // Data AddressSWAckI2C();SWRestartI2C();i2c_var = SWPutcI2C(0x91); // Control byteSWAckI2C();i2c_var = SWGetsI2C(tptr, 2); // Read TemperatureSWStopI2C();
temp_H=tptr[0]; // High bitstemp_L=tptr[1]; // Low bits
// Compute Centigradecvalue=(temp_H5;cvalue=cvalue * 1.25;cent_buf[1]=cvalue/100+48;cent_buf[2]=(cvalue/10)%10+48;cent_buf[3]='.';cent_buf[4]=cvalue%10+48;
7/22/2019 Db-dp113 Sample Code
47/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page46
cent_buf[5]='\0';
// Compute Fahrenheitfvalue=((cvalue*9)/5)+32;
fahr_buf[0]=' ';if(fvalue&0x80==1){
fvalue=~fvalue+1; // Calculate Base Complementfahr_buf[0]='-';
}fahr_buf[1]=fvalue/100+48;fahr_buf[2]=(fvalue/10)%10+48;fahr_buf[3]='.';fahr_buf[4]=fvalue%10+48;fahr_buf[5]='\0';
}/**************************************************************************/
/* 8-bit or 4-bit interface type* For 8-bit operation uncomment the #define BIT8*/
#define BIT8
/* When in 4-bit interface define if the data is in the upper or lower nibble.For lower nibble, comment the #define UPPER
*//* #define UPPER */
/* DATA_PORT defines the port which the LCD data lines are connected to */#define DATA_PORT PORTD#define TRIS_DATA_PORT TRISD
/* CTRL_PORT defines the port where the control lines are connected.* These are just samples, change to match your application.*/
#define RW_PIN PORTAbits.RA2 /* PORT for RW */
#define TRIS_RW DDRAbits.RA2 /* TRIS for RW */#define RS_PIN PORTAbits.RA3 /* PORT for RS */#define TRIS_RS DDRAbits.RA3 /* TRIS for RS */#define E_PIN PORTAbits.RA1 /* PORT for E */#define TRIS_E DDRAbits.RA1 /* TRIS for E */
/* Display ON/OFF Control defines */#define DON 0b00001111 /* Display on */#define DOFF 0b00001011 /* Display off */#define CURSOR_ON 0b00001111 /* Cursor on */#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */#define BLINK_OFF 0b00001110 /* Cursor No Blink */
7/22/2019 Db-dp113 Sample Code
48/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page47
/* Cursor or Display Shift defines */#define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */#define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */
/* Function Set defines */#define FOUR_BIT 0b00101111 /* 4-bit Interface */#define EIGHT_BIT 0b00111111 /* 8-bit Interface */#define LINE_5X7 0b00110011 /* 5x7 characters, single line */#define LINE_5X10 0b00110111 /* 5x10 characters */#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
#define PARAM_SCLASS auto#define MEM_MODEL far /* Change this to near for small memory model */
/* OpenXLCD* Configures I/O pins for external LCD*/
void OpenXLCD(PARAM_SCLASS unsigned char);
/* SetCGRamAddr* Sets the character generator address*/
void SetCGRamAddr(PARAM_SCLASS unsigned char);
/* SetDDRamAddr* Sets the display data address*/
void SetDDRamAddr(PARAM_SCLASS unsigned char);
/* BusyXLCD* Returns the busy status of the LCD*/
unsigned char BusyXLCD(void);
/* ReadAddrXLCD* Reads the current address*/
unsigned char ReadAddrXLCD(void);
/* ReadDataXLCD* Reads a byte of data*/
char ReadDataXLCD(void);
/* WriteCmdXLCD* Writes a command to the LCD*/
7/22/2019 Db-dp113 Sample Code
49/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page48
void WriteCmdXLCD(PARAM_SCLASS unsigned char);
/* WriteDataXLCD* Writes a data byte to the LCD
*/void WriteDataXLCD(PARAM_SCLASS char);
/* putcXLCD* A putc is a write*/
#define putcXLCD WriteDataXLCD
/* putsXLCD* Writes a string of characters to the LCD*/
void putsXLCD(PARAM_SCLASS char *);
/* putrsXLCD* Writes a string of characters in ROM to the LCD*/
void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *);
/* User defines these routines according to the oscillator frequency */extern void DelayFor18TCY(void);extern void DelayPORXLCD(void);
extern void DelayXLCD(void);
/********************************************************************* Function Name: BusyXLCD ** Return Value: char: busy status of LCD controller ** Parameters: void ** Description: This routine reads the busy status of the ** Hitachi HD44780 LCD controller. *********************************************************************/unsigned char BusyXLCD(void)
{RW_PIN = 1; // Set the control bits for readRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock in the commandDelayFor18TCY();
#ifdef BIT8 // 8-bit interfaceif(DATA_PORT&0x80) // Read bit 7 (busy bit){ // If high
E_PIN = 0; // Reset clock lineRW_PIN = 0; // Reset control line
return 1; // Return TRUE}else // Bit 7 low
7/22/2019 Db-dp113 Sample Code
50/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page49
{E_PIN = 0; // Reset clock lineRW_PIN = 0; // Reset control line
return 0; // Return FALSE}
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
if(DATA_PORT&0x80)#else // Lower nibble interface
if(DATA_PORT&0x08)#endif
{E_PIN = 0; // Reset clock lineDelayFor18TCY();E_PIN = 1; // Clock out other nibbleDelayFor18TCY();E_PIN = 0;RW_PIN = 0; // Reset control linereturn 1; // Return TRUE
}else // Busy bit is low{
E_PIN = 0; // Reset clock lineDelayFor18TCY();
E_PIN = 1; // Clock out other nibbleDelayFor18TCY();E_PIN = 0;RW_PIN = 0; // Reset control linereturn 0; // Return FALSE
}#endif}/********************************************************************* Function Name: OpenXLCD *
* Return Value: void ** Parameters: lcdtype: sets the type of LCD (lines) ** Description: This routine configures the LCD. Based on ** the Hitachi HD44780 LCD controller. The ** routine will configure the I/O pins of the ** microcontroller, setup the LCD for 4-bit or ** 8-bit mode and clear screen. The user ** must provide three delay routines: ** DelayFor18TCY() provides a 18 Tcy delay ** DelayPORXLCD() provides at least 15ms delay ** DelayXLCD() provides at least 5ms delay *********************************************************************/void OpenXLCD(unsigned char lcdtype){
7/22/2019 Db-dp113 Sample Code
51/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page50
// The data bits must be either a 8-bit port or the upper or// lower 4-bits of a port. These pins are made into inputs
#ifdef BIT8 // 8-bit mode, use whole portDATA_PORT &= 0;
TRIS_DATA_PORT |= 0xff;#else // 4-bit mode#ifdef UPPER // Upper 4-bits of the port
DATA_PORT &= 0x0f;TRIS_DATA_PORT |= 0xf0;
#else // Lower 4-bits of the portDATA_PORT &= 0xf0;
TRIS_DATA_PORT |= 0x0f;#endif#endif
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;TRIS_E = 0;RW_PIN = 0; // R/W pin made lowRS_PIN = 0; // Register select pin made lowE_PIN = 0; // Clock pin made low
// Delay for 15ms to allow for LCD Power on resetDelayPORXLCD();
// Setup interface to LCD
#ifdef BIT8 // 8-bit mode interfaceTRIS_DATA_PORT &= 0; // Data port outputDATA_PORT &= 0;DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit mode interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;DATA_PORT &= 0x0f;DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface)
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0;
DATA_PORT &= 0xf0;DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface)
#endif#endif
E_PIN = 1; // Clock the cmd inDelayFor18TCY();E_PIN = 0;
// Delay for at least 4.1msDelayXLCD();
// Setup interface to LCD#ifdef BIT8 // 8-bit interface
DATA_PORT &= 0;
7/22/2019 Db-dp113 Sample Code
52/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page51
DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)#else // 4-bit interface#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)DATA_PORT |= 0b00100000;
#else // Lower nibble interfaceDATA_PORT &= 0xf0; // Function set cmd(4-bit interface)DATA_PORT |= 0b00000010;
#endif#endif
E_PIN = 1; // Clock the cmd inDelayFor18TCY();E_PIN = 0;
// Delay for at least 100usDelayXLCD();
// Setup interface to LCD#ifdef BIT8 // 8-bit interface
DATA_PORT &= 0;DATA_PORT |= 0b00110000; // Function set cmd(8-bit interface)
#else // 4-bit interface#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f; // Function set cmd(4-bit interface)
DATA_PORT |= 0b00100000;#else // Lower nibble interfaceDATA_PORT &= 0xf0; // Function set cmd(4-bit interface)DATA_PORT |= 0b00000010;
#endif#endif
E_PIN = 1; // Clock cmd inDelayFor18TCY();E_PIN = 0;
#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT |= 0xff; // Make data port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT |= 0xf0; // Make data nibble input#else // Lower nibble interface
TRIS_DATA_PORT |= 0x0f; // Make data nibble input#endif#endif
// Set data interface width, #lines, fontwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(lcdtype); // Function set cmd
// Turn the display on then off
7/22/2019 Db-dp113 Sample Code
53/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page52
while(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(DOFF); // Display OFF/Blink OFFwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(DON); // Display ON/Blink ON
while(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(BLINK_OFF&CURSOR_OFF); // Display ON/Blink OFF
// Clear displaywhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(0x01); // Clear display
// Set entry mode inc, no shiftwhile(BusyXLCD()); // Wait if LCD busyWriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode
// Set DD Ram address to 0while(BusyXLCD()); // Wait if LCD busySetDDRamAddr(0); // Set Display data ram address to 0
return;}
/********************************************************************
* Function Name: putrsXLCD* Return Value: void* Parameters: buffer: pointer to string* Description: This routine writes a string of bytes to the* Hitachi HD44780 LCD controller. The user* must check to see if the LCD controller is* busy before calling this routine. The data* is written to the character generator RAM or* the display data RAM depending on what the* previous SetxxRamAddr routine was called.********************************************************************/
/*void putrsXLCD(const rom char *buffer){
while(*buffer) // Write data to LCD up to null{
while(BusyXLCD()); // Wait while LCD is busyWriteDataXLCD(*buffer); // Write character to LCDbuffer++; // Increment buffer
}return;
}
*//********************************************************************
7/22/2019 Db-dp113 Sample Code
54/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page53
* Function Name: putsXLCD* Return Value: void* Parameters: buffer: pointer to string
* Description: This routine writes a string of bytes to the* Hitachi HD44780 LCD controller. The user* must check to see if the LCD controller is* busy before calling this routine. The data* is written to the character generator RAM or* the display data RAM depending on what the* previous SetxxRamAddr routine was called.********************************************************************/void putsXLCD(char *buffer){
while(*buffer) // Write data to LCD up to null{
while(BusyXLCD()); // Wait while LCD is busyWriteDataXLCD(*buffer); // Write character to LCDbuffer++; // Increment buffer
}return;
}
/*********************************************************************
* Function Name: ReadAddrXLCD ** Return Value: char: address from LCD controller ** Parameters: void ** Description: This routine reads an address byte from the ** Hitachi HD44780 LCD controller. The user ** must check to see if the LCD controller is ** busy before calling this routine. The address** is read from the character generator RAM or ** the display data RAM depending on what the ** previous SetxxRamAddr routine was called. **********************************************************************/
unsigned char ReadAddrXLCD(void){
char data; // Holds the data retrieved from the LCD
#ifdef BIT8 // 8-bit interfaceRW_PIN = 1; // Set control bits for the readRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock data out of the LCD controllerDelayFor18TCY();data = DATA_PORT; // Save the data in the registerE_PIN = 0;RW_PIN = 0; // Reset the control bits
#else // 4-bit interface
7/22/2019 Db-dp113 Sample Code
55/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page54
RW_PIN = 1; // Set control bits for the readRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock data out of the LCD controller
DelayFor18TCY();#ifdef UPPER // Upper nibble interface
data = DATA_PORT&0xf0; // Read the nibble into the upper nibbleof data#else // Lower nibble interface
data = (DATA_PORT4)&0x0f; // Read the nibble into the lower nibbleof data#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the nibble into the lower nibbleof data#endif
E_PIN = 0;RW_PIN = 0; // Reset the control lines
#endif return (data&0x7f); // Return the address, Mask off the busy bit}
/********************************************************************* Function Name: ReadDataXLCD ** Return Value: char: data byte from LCD controller ** Parameters: void ** Description: This routine reads a data byte from the ** Hitachi HD44780 LCD controller. The user *
* must check to see if the LCD controller is ** busy before calling this routine. The data ** is read from the character generator RAM or ** the display data RAM depending on what the ** previous SetxxRamAddr routine was called. *********************************************************************/char ReadDataXLCD(void){
char data;
#ifdef BIT8 // 8-bit interface
RS_PIN = 1; // Set the control bitsRW_PIN = 1;DelayFor18TCY();
7/22/2019 Db-dp113 Sample Code
56/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page55
E_PIN = 1; // Clock the data out of the LCDDelayFor18TCY();data = DATA_PORT; // Read the data
E_PIN = 0;RS_PIN = 0; // Reset the control bitsRW_PIN = 0;
#else // 4-bit interfaceRW_PIN = 1;RS_PIN = 1;DelayFor18TCY();E_PIN = 1; // Clock the data out of the LCDDelayFor18TCY();
#ifdef UPPER // Upper nibble interfacedata = DATA_PORT&0xf0; // Read the upper nibble of data
#else // Lower nibble interfacedata = (DATA_PORT4)&0x0f; // Read the lower nibble of data
#else // Lower nibble interface
data |= DATA_PORT&0x0f; // Read the lower nibble of data#endifE_PIN = 0;RS_PIN = 0; // Reset the control bitsRW_PIN = 0;
#endifreturn(data); // Return the data byte
}
/********************************************************************
* Function Name: SetCGRamAddr ** Return Value: void ** Parameters: CGaddr: character generator ram address ** Description: This routine sets the character generator ** address of the Hitachi HD44780 LCD ** controller. The user must check to see if ** the LCD controller is busy before calling ** this routine. *********************************************************************/void SetCGRamAddr(unsigned char CGaddr){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make data port ouputDATA_PORT = CGaddr | 0b01000000; // Write cmd and address to
7/22/2019 Db-dp113 Sample Code
57/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page56
portRW_PIN = 0; // Set control signalsRS_PIN = 0;DelayFor18TCY();
E_PIN = 1; // Clock cmd and address inDelayFor18TCY();E_PIN = 0;DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make data port inputs#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make nibble inputDATA_PORT &= 0x0f; // and write upper nibbleDATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0; // Make nibble inputDATA_PORT &= 0xf0; // and write upper nibbleDATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
#endifRW_PIN = 0; // Set control signalsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock cmd and address inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f; // Write lower nibbleDATA_PORT |= ((CGaddr
7/22/2019 Db-dp113 Sample Code
58/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page57
* Description: This routine sets the display data address ** of the Hitachi HD44780 LCD controller. The ** user must check to see if the LCD controller*
* is busy before calling this routine. *********************************************************************/void SetDDRamAddr(unsigned char DDaddr){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port outputDATA_PORT = DDaddr | 0b10000000; // Write cmd and address to
portRW_PIN = 0; // Set the control bitsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock the cmd and address inDelayFor18TCY();E_PIN = 0;DelayFor18TCY();
TRIS_DATA_PORT = 0xff; // Make port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f; // Make port outputDATA_PORT &= 0x0f; // and write upper nibbleDATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0; // Make port outputDATA_PORT &= 0xf0; // and write upper nibbleDATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
#endifRW_PIN = 0; // Set control bitsRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock the cmd and address inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f; // Write lower nibbleDATA_PORT |= ((DDaddr
7/22/2019 Db-dp113 Sample Code
59/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page58
TRIS_DATA_PORT |= 0x0f; // Make port input#endif#endif
return;
}
/********************************************************************* Function Name: WriteCmdXLCD ** Return Value: void ** Parameters: cmd: command to send to LCD ** Description: This routine writes a command to the Hitachi** HD44780 LCD controller. The user must check ** to see if the LCD controller is busy before ** calling this routine. *
********************************************************************/void WriteCmdXLCD(unsigned char cmd){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT &= 0; // Data port outputDATA_PORT &= 0;DATA_PORT |= cmd; // Write command to data portRW_PIN = 0; // Set the control signalsRS_PIN = 0; // for sending a commandDelayFor18TCY();
E_PIN = 1; // Clock the command inDelayFor18TCY();E_PIN = 0;DelayFor18TCY();
TRIS_DATA_PORT |= 0xff; // Data port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;DATA_PORT &= 0x0f;DATA_PORT |= cmd&0xf0;
#else // Lower nibble interface
TRIS_DATA_PORT &= 0xf0;DATA_PORT &= 0xf0;DATA_PORT |= (cmd>>4)&0x0f;
#endifRW_PIN = 0; // Set control signals for commandRS_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock command inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interface
DATA_PORT &= 0x0f;DATA_PORT |= (cmd
7/22/2019 Db-dp113 Sample Code
60/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page59
DATA_PORT &= 0xf0;DATA_PORT |= cmd&0x0f;
#endif
DelayFor18TCY();E_PIN = 1; // Clock command inDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Make data nibble inputTRIS_DATA_PORT |= 0xf0;
#elseTRIS_DATA_PORT |= 0x0f;
#endif#endif
return;}
/********************************************************************* Function Name: WriteDataXLCD ** Return Value: void ** Parameters: data: data byte to be written to LCD ** Description: This routine writes a data byte to the ** Hitachi HD44780 LCD controller. The user ** must check to see if the LCD controller is *
* busy before calling this routine. The data ** is written to the character generator RAM or** the display data RAM depending on what the ** previous SetxxRamAddr routine was called. *********************************************************************/void WriteDataXLCD(char data){#ifdef BIT8 // 8-bit interface
TRIS_DATA_PORT = 0; // Make port outputDATA_PORT = data; // Write data to portRS_PIN = 1; // Set control bits
RW_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock data into LCDDelayFor18TCY();E_PIN = 0;RS_PIN = 0; // Reset control bits
TRIS_DATA_PORT = 0xff; // Make port input#else // 4-bit interface#ifdef UPPER // Upper nibble interface
TRIS_DATA_PORT &= 0x0f;DATA_PORT &= 0x0f;DATA_PORT |= data&0xf0;
#else // Lower nibble interfaceTRIS_DATA_PORT &= 0xf0;
7/22/2019 Db-dp113 Sample Code
61/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page60
DATA_PORT &= 0xf0;DATA_PORT |= ((data>>4)&0x0f);
#endifRS_PIN = 1; // Set control bits
RW_PIN = 0;DelayFor18TCY();E_PIN = 1; // Clock nibble into LCDDelayFor18TCY();E_PIN = 0;
#ifdef UPPER // Upper nibble interfaceDATA_PORT &= 0x0f;DATA_PORT |= ((data
7/22/2019 Db-dp113 Sample Code
62/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page61
return;}void DelayXLCD (void)
{Delay1KTCYx(25); // Delay of 5ms// Cycles = (TimeDelay * Fosc) / 4// Cycles = (5ms * 20MHz) / 4// Cycles = 25,000return;
}void delayinit(void){
//Delay of 4sDelay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);Delay10KTCYx(200);
}
void LCD_init(void){OpenXLCD( EIGHT_BIT&LINES_5X7 );// Configure External LCD
while(BusyXLCD()); // Wait if LCD is busyputsXLCD(LCD_name); // write to LCD
while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x40); // Set Display data RAM address to 0x40putsXLCD(LCD_Ver); // write to LCD
delayinit();}void LCD_display(char *i){
// Set DD Ram address to 0while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x40); // Set Display data RAM address
// write to LCDwhile(BusyXLCD()); // Wait if LCD is busyputsXLCD(i);
}
7/22/2019 Db-dp113 Sample Code
63/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page62
/**************************************************************************/#pragma code low_vector=0x18void low_interrupt (void){
_asm GOTO timer_isr _endasm}#pragma code#pragma interruptlow timer_isrvoid timer_isr (void){
unsigned int i,m,n;unsigned char send;char Cent[]="Centigrade";char Fahr[]="Fahrenheit";
char Cont[]="Contrast ";char Current[]="Current Temperature";
TMR0H=0X80;TMR0L=0X00;
i++;
LM75_temperature(); //Read Temperature
if(key_flag==0){m=Funckey();switch(m){
case(0): //Display "Centigrade"{
LCD_display(Cent);putsXLCD(cent_buf);PWM_data=m;
}break;
case(1): //Display "Fahrenheit"{
LCD_display(Fahr);putsXLCD(fahr_buf);PWM_data=m;
}break;case(2): //Display Contrast information{
LCD_display(Cont);PWM_data=m;
}break;
}if(change_key==0)key_flag=1;
}
7/22/2019 Db-dp113 Sample Code
64/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page63
if(key_flag==1){
n=changekey(); //Get state of change function key
if((PWM_data==2)|(PWM_data==3)){
PWM(n);switch(n){
case(0):{
PWMbuf[0]='0';PWMbuf[1]='%';PWMbuf[2]=' ';PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(1):{
PWMbuf[0]='1';
PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(2):{
PWMbuf[0]='2';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(3):{
PWMbuf[0]='3';PWMbuf[1]='0';
7/22/2019 Db-dp113 Sample Code
65/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page64
PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';
while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(4):{
PWMbuf[0]='4';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';while(BusyXLCD()); // Wait if LCD is busy
SetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(5):{
PWMbuf[0]='5';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';
PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(6):{
PWMbuf[0]='6';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';
PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data ram addressputsXLCD(PWMbuf);
}break;case(7):{
PWMbuf[0]='7';PWMbuf[1]='0';PWMbuf[2]='%';
PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';
7/22/2019 Db-dp113 Sample Code
66/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page65
while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(8):{
PWMbuf[0]='8';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);PWM(8);
}break;case(9):{
PWMbuf[0]='9';PWMbuf[1]='0';PWMbuf[2]='%';PWMbuf[3]=' ';PWMbuf[4]=' ';
PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;case(10):{
PWMbuf[0]='1';PWMbuf[1]='0';PWMbuf[2]='0';PWMbuf[3]='%';
PWMbuf[4]=' ';PWMbuf[5]=' ';while(BusyXLCD()); // Wait if LCD is busySetDDRamAddr(0x4b); // Set Display data RAM addressputsXLCD(PWMbuf);
}break;}
}if(Func_key==0)key_flag=0;
}
if(i%8==0){
send++;
7/22/2019 Db-dp113 Sample Code
67/68
PICDEM 2 PLUSSAMPLE CODE
2004 -2008 Sure Electronics Inc.DB-DP113_Ver2.0_Page66
if((send>0)&(send28)&(send36)&(send56)&(send64)&(send
7/22/2019 Db-dp113 Sample Code
68/68
PICDEM 2 PLUSSAMPLE CODE
void sendUSART(char *sendbuf){
if(*(sendbuf+pointer)!=0)
{if(BusyUSART()==0){
TXREG=*(sendbuf+pointer);pointer++;return;
}else{
return;}
}}/**************************************************************************/void main (void){
init(); // Initialize Control MicrochipLM75_init(); // Temperature Sensor InitializtionLM75_temperature(); // Read TemperatureLCD_init(); // Open LCD and display "Sure Electronics" and "Ver 2.1"
OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT);INTCONbits.GIE = 1;
while(1){};}
Recommended